All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.nebula.config.plugin.DependencyHierarchyWriter.groovy Maven / Gradle / Ivy

Go to download

Pluggable and configurable linter tool for identifying and reporting on patterns of misuse or deprecations in Gradle scripts

There is a newer version: 20.2.2
Show newest version
package com.netflix.nebula.config.plugin;

class DependencyHierarchyWriter {
    String printHierarchy(Map> dependencies) {
        if (dependencies.isEmpty()) return ''

        def roots = dependencies.keySet().findAll { root -> !dependencies.values().flatten().find { it == root } }

        roots.inject('') { acc, root ->
            acc += '\n\n' + printHierarchyRecurse('', root, dependencies, [:])
            acc.trim()
        }
    }

    private String printHierarchyRecurse(String out, String dep, Map> dependencies,
                                         Map path) {
        def markers = []
        path.values().eachWithIndex { hasAnotherChild, i ->
            def lineMarker = (i < path.size() - 1 || hasAnotherChild) ? '|' : '\\'
            markers += hasAnotherChild || i == path.size() - 1 ? lineMarker : ' '
        }

        out += markers.join('  ')
        if (!path.isEmpty()) out += '_ '
        out += dep

        if (path.containsKey(dep))
            return out + ' (cycle)\n'

        out += '\n'

        def children = dependencies.get(dep)
        children.eachWithIndex { child, i ->
            out = printHierarchyRecurse(out, child, dependencies, path + [(dep): i < children.size() - 1])
        }

        return out
    }

    String printHierarchy(String... depStrings) {
        printHierarchy(depStrings.inject([:].withDefault { [] }) { allDeps, line ->
            def dep = line.split('->') // split into [parent, child]
            if (dep.size() == 2)
                allDeps[dep[0]] += dep[1]
            return allDeps
        })
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy