in aspoet/src/main/kotlin/com/google/androidstudiopoet/generators/DependencyImageGenerator.kt [51:105]
fun generate(blueprint: ProjectBlueprint) {
val numModules = blueprint.allModuleBlueprints.size
val imageSize = HEADER_SIZE + LINE_WIDTH + numModules * GRID_SIZE
// Create buffered image object
val img = BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_RGB)
val graphics = img.graphics
// Background
graphics.color = BACKGROUND_COLOR
graphics.fillRect(0, 0, imageSize, imageSize)
// Generate the grid
graphics.color = GRID_COLOR
for (i in 0..numModules) {
// Horizontal
val y = HEADER_SIZE + i * GRID_SIZE
graphics.fillRect(HEADER_SIZE, y, imageSize - HEADER_SIZE, LINE_WIDTH)
// Vertical
val x = HEADER_SIZE + i * GRID_SIZE
graphics.fillRect(x, HEADER_SIZE, LINE_WIDTH, imageSize - HEADER_SIZE)
}
// Add modules headers and generate indexes to use in dependencies
val moduleNameToIndex = mutableMapOf<String, Int>()
blueprint.allModuleBlueprints.withIndex().forEach{(index, blueprint) ->
moduleNameToIndex[blueprint.name] = index
graphics.drawHeader(index, getColorForModule(blueprint))
}
// Add dependencies
blueprint.allDependencies.forEach { (moduleName, dependencies) ->
// Split dependencies by name
val seenDependencies = mutableMapOf<Int?, MutableSet<String>>()
dependencies.forEach {
seenDependencies.getOrPut(moduleNameToIndex[it.name]) { mutableSetOf() }.add(it.method)
}
// Draw dependencies
val index = moduleNameToIndex[moduleName]!!
seenDependencies.forEach { (dependencyIndex, methodSet) ->
if (dependencyIndex != null) {
graphics.drawCell(index, dependencyIndex, getColorForDependencySet(methodSet))
}
else {
graphics.drawHeader(index, ERROR_COLOR)
}
}
}
// Save to file
val imgPath = blueprint.projectRoot.joinPath("dependencies.png")
imageWriter.writeToFile(img, imgPath)
println("Dependency matrix image saved to $imgPath")
}