Skip to content

Instantly share code, notes, and snippets.

@SMUsamaShah
Forked from jrodbx/task_graph.groovy
Last active February 16, 2021 18:36
Show Gist options
  • Save SMUsamaShah/0a7b11003d57c17063fbcebcdb02a3b1 to your computer and use it in GitHub Desktop.
Save SMUsamaShah/0a7b11003d57c17063fbcebcdb02a3b1 to your computer and use it in GitHub Desktop.
Graphviz visualization of a Gradle project's task graph
gradle.taskGraph.whenReady {
println "rootProject: " + rootProject.name
println "childProjects: " + rootProject.childProjects
def dot = new File(rootProject.buildDir, 'project.dot')
dot.delete()
def command = "./gradlew " + gradle.startParameter.getTaskNames().join(" ")
println "command: " + command
dot << 'digraph {\n'
dot << " graph [label=\"${command}\\n \",labelloc=t,fontsize=30];\n"
dot << ' node [style=filled, fillcolor="#bbbbbb"];\n'
dot << ' rankdir=LR;\n\n'
def taskmap = [:] // a map to ignore dups
gradle.taskGraph.allTasks.each { task ->
println "task: " + task.name + ", class: " + task.class.name
task.taskDependencies.getDependencies(task).each { dep ->
taskmap[" \"${dep.name}\" -> \"${task.name}\""] = ""
dot << " \"${dep.name}\" -> \"${task.name}\""
dot << " [style=dotted]"
dot << '\n'
}
}
taskmap.each {
dot << "$it.key"
dot << " [style=dotted]"
dot << '\n'
}
dot << '\n'
dot << '}\n'
def p = 'dot -Tpng -O project.dot'.execute([], rootProject.buildDir)
p.waitFor()
if (p.exitValue() != 0) {
throw new RuntimeException(p.errorStream.text)
}
println("Task graph created at ${dot.absolutePath}.png")
}
gradle.taskGraph.afterTask { task ->
def inputFiles = task.inputs.files
def outputFiles = task.outputs.files
println " inputs: "
for (int i=0; i<inputFiles.size(); i++) {
println " - " + inputFiles[i]
if(i>10) {
println " - ${inputFiles.size() - 10} more files ..."
break
}
}
println " outputs: "
for (int i=0; i<outputFiles.size(); i++) {
println " - " + outputFiles[i]
if(i>10) {
println " - ${outputFiles.size() - 10} more files ..."
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment