Skip to content

Commit

Permalink
使用 mermaid 替代 dot 用于生成可视化文件
Browse files Browse the repository at this point in the history
  • Loading branch information
b7woreo committed May 23, 2023
1 parent 95322b1 commit e3d46fb
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 257 deletions.
3 changes: 3 additions & 0 deletions example/android-application/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("com.android.application")
id('gradle.assistant')
}

android {
Expand All @@ -15,4 +16,6 @@ android {

dependencies {
implementation project(":example:android-library")
implementation project(":example:java-library")
implementation libs.rxjava
}
1 change: 1 addition & 0 deletions example/android-library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ android {

dependencies{
implementation project(":example:java-library")
implementation libs.rxjava
}
Empty file modified gradlew
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ gradlePlugin {
plugins{
gdr {
id = "gradle.assistant"
implementationClass = "io.github.knownitwhy.gdr.GdrPlugin"
implementationClass = "gradle.assistant.GradleAssistant"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/main/kotlin/gradle/assistant/GradleAssistant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.configurationcache.extensions.capitalized

class GradleAssistant : Plugin<Project> {
abstract class GradleAssistant : Plugin<Project> {

companion object {
private const val TASK_GROUP = "GradleAssistant"
private const val TASK_GROUP = "assistant"
}

override fun apply(project: Project) {
Expand Down
140 changes: 0 additions & 140 deletions plugin/src/main/kotlin/gradle/assistant/dot/Dot.kt

This file was deleted.

24 changes: 0 additions & 24 deletions plugin/src/main/kotlin/gradle/assistant/dot/Graphviz.kt

This file was deleted.

83 changes: 83 additions & 0 deletions plugin/src/main/kotlin/gradle/assistant/graphic/Graphic.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package gradle.assistant.graphic

import java.io.File

interface Graphic {
fun render(output: File, builder: Builder.() -> Unit)

interface Builder {
fun node(content: String, shape: Shape)
fun edge(from: String, to: String)
}

sealed class Shape {
object Box : Shape()
object Oval : Shape()
object Diamond : Shape()
}
}

class MermaidGraphic : Graphic {
private fun template(content: String): String {
return """
<!DOCTYPE html>
<html lang="en">
<body>
<pre id="mermaid">$content</pre>
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
import he from 'https://cdn.jsdelivr.net/npm/he@1.2.0/+esm';
const { svg, _ } = await mermaid.render('mermaid', he.decode(document.getElementById('mermaid').innerHTML));
document.body.innerHTML = svg;
document.getElementById('mermaid').setAttribute('width', '99999%');
document.getElementById('mermaid').setAttribute('height', '99999%');
</script>
</body>
</html>
""".trimIndent()
}

override fun render(output: File, builder: Graphic.Builder.() -> Unit) {
val content = MermaidGraphicBuilder().apply {
builder()
}.build()
output.writeText(template(content))
}
}

private class MermaidGraphicBuilder : Graphic.Builder {

private var currentId = 0
private val ids = mutableMapOf<String, Int>()
private val statements = mutableSetOf<String>()

override fun node(content: String, shape: Graphic.Shape) {
if (ids[content] != null) return

ids[content] = currentId++

when (shape) {
is Graphic.Shape.Box -> {
statements.add("${ids[content]}[\"$content\"]")
}

is Graphic.Shape.Oval -> {
statements.add("${ids[content]}([\"$content\"])")
}

is Graphic.Shape.Diamond -> {
statements.add("${ids[content]}{{\"$content\"}}")
}
}
}

override fun edge(from: String, to: String) {
val fromId = ids[from] ?: return
val toId = ids[to] ?: return
statements.add("$fromId --> $toId")
}

fun build(): String {
return "flowchart TD\n${statements.joinToString(separator = "\n")}"
}
}
Loading

0 comments on commit e3d46fb

Please # to comment.