From da1fbf1b96d0414ae574e8601e994304f82489a7 Mon Sep 17 00:00:00 2001 From: Savvas Dalkitsis Date: Tue, 21 Sep 2021 12:12:03 +0100 Subject: [PATCH] Adding filtering options to the graph. Fixes #5 --- README.md | 11 +++++ build.gradle | 2 +- .../graph/ModuleDependencyGraphPlugin.groovy | 49 +++++++++++++------ 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ff33107..a79e9e6 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,17 @@ From the command line, simply run: This will create a png of the dependency graph of your modules and open the image +### Filtering + +You can specify a comma separated list of modules to include in the output. If this is +specified the graph will contain only these modules and their dependants and dependencies. +This can be helpful when trying to clean up a really complicated dependency tree +and you need to focus on only a part of it. + +```bash +./gradlew graphModules -PgraphFilter=core,presentation:api,network +``` + ### Specify file locations You can specify where the dot file and output file get created by specifying the following properties: diff --git a/build.gradle b/build.gradle index 1e205b1..ebe8b80 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group 'com.savvasdalkitsis' -version '0.9' +version '0.10' pluginBundle { website = 'https://github.com/savvasdalkitsis/' diff --git a/src/main/groovy/com/savvasdalkitsis/module/deps/graph/ModuleDependencyGraphPlugin.groovy b/src/main/groovy/com/savvasdalkitsis/module/deps/graph/ModuleDependencyGraphPlugin.groovy index 42ec66a..a4cb8b4 100644 --- a/src/main/groovy/com/savvasdalkitsis/module/deps/graph/ModuleDependencyGraphPlugin.groovy +++ b/src/main/groovy/com/savvasdalkitsis/module/deps/graph/ModuleDependencyGraphPlugin.groovy @@ -18,6 +18,7 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.Dependency +@SuppressWarnings('unused') class ModuleDependencyGraphPlugin implements Plugin { @Override @@ -25,17 +26,18 @@ class ModuleDependencyGraphPlugin implements Plugin { project.task('graphModules') { doLast { checkGraphVizIsInstalled(project) + + def filter = getFilter(project) + println("Using filter: $filter") + def graph = new StringBuilder() project.subprojects.each { module -> def impl = getDependencies(module, "implementation") def api = getDependencies(module, "api") def compile = getDependencies(module, "compile") - if (impl.empty && api.empty && compile.empty) { - graph.append("\t\t${strip(module)};").append("\n") - } - graphDeps(impl, module, "black", graph) - graphDeps(api, module, "red", graph) - graphDeps(compile, module, "green", graph) + graphDeps(impl, module, "black", graph, filter) + graphDeps(api, module, "red", graph, filter) + graphDeps(compile, module, "green", graph, filter) } def dot = """ digraph modules { @@ -57,7 +59,7 @@ class ModuleDependencyGraphPlugin implements Plugin { """.stripIndent() def dotFile = File.createTempFile("module_graph", ".dot") if (project.hasProperty('dotFilePath')) { - dotFile = new File(project.property('dotFilePath')) + dotFile = new File(project.property('dotFilePath').toString()) dotFile.createNewFile() } def graphOutputFormat = "png" @@ -66,12 +68,12 @@ class ModuleDependencyGraphPlugin implements Plugin { } def graphOutputFile = File.createTempFile("module_graph", ".$graphOutputFormat") if (project.hasProperty('graphOutputFilePath')) { - graphOutputFile = new File(project.property('graphOutputFilePath')) + graphOutputFile = new File(project.property('graphOutputFilePath').toString()) graphOutputFile.createNewFile() } def autoOpenGraph = true if (project.hasProperty('autoOpenGraph')) { - autoOpenGraph = Boolean.parseBoolean(project.property('autoOpenGraph')) + autoOpenGraph = Boolean.parseBoolean(project.property('autoOpenGraph').toString()) } dotFile.write(dot) project.exec { @@ -92,6 +94,16 @@ class ModuleDependencyGraphPlugin implements Plugin { } } + private static List getFilter(Project project) { + if (project.hasProperty('graphFilter')) { + return project.property('graphFilter').toString() + .split(",") + .collect { it.trim() } + } else { + return new ArrayList() + } + } + private static checkGraphVizIsInstalled(Project project) { def message = "You need GraphViz installed on your system to run this task. Please visit http://www.graphviz.org/ for more information on how to install it" try { @@ -108,9 +120,19 @@ class ModuleDependencyGraphPlugin implements Plugin { } } - private static Set graphDeps(Set dependencies, Object module, String color, StringBuilder graph) { + private static Set graphDeps( + Set dependencies, + Object module, + String color, + StringBuilder graph, + List filter + ) { dependencies.each { dep -> - graph.append("\t\t${strip(module)} -> ${strip(dep.dependencyProject)} [color = $color]").append("\n") + def dependant = strip(module) + def dependency = strip(dep.dependencyProject) + if (filter.isEmpty() || filter.contains(dependant) || filter.contains(dependency)) { + graph.append("\t\t\"$dependant\" -> \"$dependency\" [color = $color]").append("\n") + } } } @@ -121,7 +143,6 @@ class ModuleDependencyGraphPlugin implements Plugin { } private static def strip(Object o) { - o.toString().replaceAll("project ':", "\"").replaceAll("'", "\"") + o.toString().replaceAll("project ':", "").replaceAll("'", "") } -} - +} \ No newline at end of file