🧜‍♀️ Mermaid Graphs legends

pull/1963/head
Simon Marquis 5 days ago
parent 6b14f4fec8
commit 5009c37bbc

@ -132,11 +132,13 @@ internal fun Project.configureGraphTasks() {
projectPath = this@configureGraphTasks.path projectPath = this@configureGraphTasks.path
dependencies = graph.dependencies() dependencies = graph.dependencies()
plugins = graph.plugins() plugins = graph.plugins()
output = this@configureGraphTasks.layout.buildDirectory.file("mermaid.txt") output = this@configureGraphTasks.layout.buildDirectory.file("mermaid/graph.txt")
legend = this@configureGraphTasks.layout.buildDirectory.file("mermaid/legend.txt")
} }
tasks.register<GraphUpdateTask>("graphUpdate") { tasks.register<GraphUpdateTask>("graphUpdate") {
projectPath = this@configureGraphTasks.path projectPath = this@configureGraphTasks.path
input = dumpTask.flatMap { it.output } input = dumpTask.flatMap { it.output }
legend = dumpTask.flatMap { it.legend }
output = this@configureGraphTasks.layout.projectDirectory.file("README.md") output = this@configureGraphTasks.layout.projectDirectory.file("README.md")
} }
} }
@ -156,11 +158,15 @@ private abstract class GraphDumpTask : DefaultTask() {
@get:OutputFile @get:OutputFile
abstract val output: RegularFileProperty abstract val output: RegularFileProperty
@get:OutputFile
abstract val legend: RegularFileProperty
override fun getDescription() = "Dumps project dependencies to a mermaid file." override fun getDescription() = "Dumps project dependencies to a mermaid file."
@TaskAction @TaskAction
operator fun invoke() { operator fun invoke() {
output.get().asFile.writeText(mermaid()) output.get().asFile.writeText(mermaid())
legend.get().asFile.writeText(legend())
logger.lifecycle(output.get().asFile.toPath().toUri().toString()) logger.lifecycle(output.get().asFile.toPath().toUri().toString())
} }
@ -208,6 +214,27 @@ private abstract class GraphDumpTask : DefaultTask() {
PluginType.entries.forEach { appendLine(it.classDef()) } PluginType.entries.forEach { appendLine(it.classDef()) }
} }
private fun legend() = buildString {
appendLine("graph TB")
listOf(
"application" to PluginType.AndroidApplication,
"feature" to PluginType.AndroidFeature,
"library" to PluginType.AndroidLibrary,
"jvm" to PluginType.Jvm,
).forEach { (name, type) ->
appendLine(name.alias(indent = 2, type))
}
appendLine()
listOf(
Dependency("application", "implementation", "feature"),
Dependency("library", "api", "jvm"),
).forEach {
appendLine(it.link(indent = 2))
}
appendLine()
PluginType.entries.forEach { appendLine(it.classDef()) }
}
private class Dependency(val project: String, val configuration: String, val dependency: String) private class Dependency(val project: String, val configuration: String, val dependency: String)
private fun Pair<String, String>.toDependency(project: String) = private fun Pair<String, String>.toDependency(project: String) =
@ -246,6 +273,10 @@ private abstract class GraphUpdateTask : DefaultTask() {
@get:PathSensitive(NONE) @get:PathSensitive(NONE)
abstract val input: RegularFileProperty abstract val input: RegularFileProperty
@get:InputFile
@get:PathSensitive(NONE)
abstract val legend: RegularFileProperty
@get:OutputFile @get:OutputFile
abstract val output: RegularFileProperty abstract val output: RegularFileProperty
@ -266,15 +297,24 @@ private abstract class GraphUpdateTask : DefaultTask() {
""".trimIndent(), """.trimIndent(),
) )
} }
val mermaid = input.get().asFile.readText().trimTrailingNewLines()
val legend = legend.get().asFile.readText().trimTrailingNewLines()
val regex = """(<!--region graph-->)(.*?)(<!--endregion-->)""".toRegex(DOT_MATCHES_ALL) val regex = """(<!--region graph-->)(.*?)(<!--endregion-->)""".toRegex(DOT_MATCHES_ALL)
val text = readText().replace(regex) { match -> val text = readText().replace(regex) { match ->
val (start, _, end) = match.destructured val (start, _, end) = match.destructured
val mermaid = input.get().asFile.readText().trimTrailingNewLines()
""" """
|$start |$start
|```mermaid |```mermaid
|$mermaid |$mermaid
|``` |```
|
|<details><summary>📋 Graph legend</summary>
|
|```mermaid
|$legend
|```
|
|</details>
|$end |$end
""".trimMargin() """.trimMargin()
} }

Loading…
Cancel
Save