Skip to content

Commit

Permalink
Add DotGraph.visualize() for graph visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonlee committed Apr 26, 2022
1 parent f057517 commit 2e5a4c4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
1 change: 1 addition & 0 deletions booster-cha/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply from: "$rootDir/gradle/booster.gradle"

dependencies {
api project(':booster-command')
api project(':booster-transform-util')
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package com.didiglobal.booster.cha.graph.dot

import com.didiglobal.booster.kotlinx.RGB
import com.didiglobal.booster.cha.graph.CallGraph
import com.didiglobal.booster.cha.graph.CallGraphRenderer
import com.didiglobal.booster.command.Command
import com.didiglobal.booster.command.CommandService
import com.didiglobal.booster.kotlinx.OS
import com.didiglobal.booster.kotlinx.RGB
import com.didiglobal.booster.kotlinx.execute
import com.didiglobal.booster.kotlinx.stderr
import com.didiglobal.booster.kotlinx.touch
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException

/**
* Represents the graph type
Expand Down Expand Up @@ -40,4 +49,16 @@ sealed class DotGraph : CallGraphRenderer {

}

fun visualize(graph: CallGraph, output: File, format: String = "png", dot: Command = CommandService.fromPath("dot${OS.executableSuffix}")) {
output.touch().writeText(render(graph).toString())
dot.location.file.let(::File).takeIf(File::exists)?.let {
"${it.canonicalPath} -T${format} -O ${output.canonicalPath}".also(::println).execute()
}?.let { p ->
p.waitFor()
if (p.exitValue() != 0) {
throw IOException(p.stderr)
}
} ?: throw FileNotFoundException(dot.location.file)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,12 @@ 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 {

Expand All @@ -41,20 +34,11 @@ class TaskGraphVariantProcessor : VariantProcessor {
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")
try {
DotGraph.DIGRAPH.visualize(graph, dot)
} catch (e: Exception) {
project.logger.error(e.message)
}
}
}

Expand Down

0 comments on commit 2e5a4c4

Please sign in to comment.