Skip to content

Commit

Permalink
Adding filtering options to the graph. Fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
savvasdalkitsis committed Sep 21, 2021
1 parent 8aed2ed commit da1fbf1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'com.savvasdalkitsis'
version '0.9'
version '0.10'

pluginBundle {
website = 'https://github.com/savvasdalkitsis/'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency

@SuppressWarnings('unused')
class ModuleDependencyGraphPlugin implements Plugin<Project> {

@Override
void apply(Project project) {
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 {
Expand All @@ -57,7 +59,7 @@ class ModuleDependencyGraphPlugin implements Plugin<Project> {
""".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"
Expand All @@ -66,12 +68,12 @@ class ModuleDependencyGraphPlugin implements Plugin<Project> {
}
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 {
Expand All @@ -92,6 +94,16 @@ class ModuleDependencyGraphPlugin implements Plugin<Project> {
}
}

private static List<String> getFilter(Project project) {
if (project.hasProperty('graphFilter')) {
return project.property('graphFilter').toString()
.split(",")
.collect { it.trim() }
} else {
return new ArrayList<String>()
}
}

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 {
Expand All @@ -108,9 +120,19 @@ class ModuleDependencyGraphPlugin implements Plugin<Project> {
}
}

private static Set<Dependency> graphDeps(Set<Dependency> dependencies, Object module, String color, StringBuilder graph) {
private static Set<Dependency> graphDeps(
Set<Dependency> dependencies,
Object module,
String color,
StringBuilder graph,
List<String> 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")
}
}
}

Expand All @@ -121,7 +143,6 @@ class ModuleDependencyGraphPlugin implements Plugin<Project> {
}

private static def strip(Object o) {
o.toString().replaceAll("project ':", "\"").replaceAll("'", "\"")
o.toString().replaceAll("project ':", "").replaceAll("'", "")
}
}

}

0 comments on commit da1fbf1

Please sign in to comment.