-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
booster-task-graph
for task graph visualization
- Loading branch information
1 parent
5e2f136
commit f057517
Showing
6 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# booster-task-graph | ||
|
||
Generate task graph in [dot](https://graphviz.org/), please make sure you have installed the [dot command line](https://graphviz.org/doc/info/command.html). | ||
|
||
## Getting Started | ||
|
||
Executing tasks and then find the `*.dot` and `*.dot.png` files under `${rootProject}/build` directory, for example, if I run the following command line | ||
|
||
```bash | ||
./gradlew assembleDebug --dry-run | ||
``` | ||
|
||
Then, the following files will be generated under `${rootProject}/build`: | ||
|
||
* `assembleDebug.dot` | ||
* `assembleDebug.dot.png` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apply from: "$rootDir/gradle/booster.gradle" | ||
|
||
dependencies { | ||
kapt "com.google.auto.service:auto-service:1.0" | ||
implementation project(':booster-api') | ||
implementation project(':booster-cha') | ||
implementation project(':booster-command') | ||
compileOnly 'com.android.tools.build:gradle:4.0.0' | ||
compileOnly 'com.android.tools.build:builder:4.0.0' | ||
testCompileOnly 'com.android.tools.build:gradle:4.0.0' | ||
testCompileOnly 'com.android.tools.build:builder:4.0.0' | ||
} |
61 changes: 61 additions & 0 deletions
61
...task-graph/src/main/kotlin/com/didiglobal/booster/task/graph/TaskGraphVariantProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.didiglobal.booster.task.graph | ||
|
||
import com.android.build.gradle.api.BaseVariant | ||
import com.didiglobal.booster.cha.graph.CallGraph | ||
import com.didiglobal.booster.cha.graph.dot.DotGraph | ||
import com.didiglobal.booster.command.CommandService | ||
import com.didiglobal.booster.gradle.project | ||
import com.didiglobal.booster.kotlinx.OS | ||
import com.didiglobal.booster.kotlinx.execute | ||
import com.didiglobal.booster.kotlinx.file | ||
import com.didiglobal.booster.task.spi.VariantProcessor | ||
import com.google.auto.service.AutoService | ||
import java.io.BufferedReader | ||
import java.io.File | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
private val DOT = "dot${OS.executableSuffix}" | ||
|
||
@AutoService(VariantProcessor::class) | ||
class TaskGraphVariantProcessor : VariantProcessor { | ||
|
||
private var generating = AtomicBoolean(false) | ||
|
||
override fun process(variant: BaseVariant) { | ||
generating.compareAndSet(false, true).takeIf { it } ?: return | ||
|
||
val project = variant.project | ||
|
||
project.gradle.taskGraph.whenReady { | ||
val taskNames = project.gradle.startParameter.taskNames | ||
val dot = project.rootProject.buildDir.file("${taskNames.joinToString("-")}.dot") | ||
val title = "./gradlew ${taskNames.joinToString(" ")}" | ||
val graph = project.gradle.taskGraph.allTasks.map { task -> | ||
task.taskDependencies.getDependencies(task).map { dep -> | ||
dep to task | ||
} | ||
}.flatten().map { (dep, task) -> | ||
CallGraph.Edge(TaskNode(dep.path), TaskNode(task.path)) | ||
}.fold(CallGraph.Builder().setTitle(title)) { builder, edge -> | ||
builder.addEdge(edge) | ||
builder | ||
}.build() | ||
|
||
// write dot file | ||
dot.writeText(DotGraph.DIGRAPH.render(graph).toString()) | ||
|
||
// convert dot to png | ||
CommandService.fromPath(DOT).location.file.let(::File).takeIf(File::exists)?.let { | ||
val cmdline = "${it.canonicalPath} -Tpng -O ${dot.canonicalPath}" | ||
project.logger.info(cmdline) | ||
cmdline.execute() | ||
}?.let { p -> | ||
p.waitFor() | ||
if (p.exitValue() != 0) { | ||
project.logger.error(p.errorStream.bufferedReader().use(BufferedReader::readText)) | ||
} | ||
} ?: project.logger.warn("Command `${DOT}` not found") | ||
} | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
booster-task-graph/src/main/kotlin/com/didiglobal/booster/task/graph/TaskNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.didiglobal.booster.task.graph | ||
|
||
import com.didiglobal.booster.cha.graph.CallGraph | ||
|
||
data class TaskNode(val path: String) : CallGraph.Node("", path, "") { | ||
|
||
override fun toPrettyString(): String = path | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters