Skip to content

Commit

Permalink
add mermaid graph configs
Browse files Browse the repository at this point in the history
  • Loading branch information
raamcosta committed Feb 28, 2024
1 parent 4692d04 commit f3d943d
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ data class CodeGenConfig(
val packageName: String?,
val moduleName: String?,
val generateNavGraphs: Boolean,
val htmlMermaidGraph: String?,
val mermaidGraph: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal val ServiceLocator.moduleRegistryWriter get() = ModuleRegistryWriter(
)

internal val ServiceLocator.mermaidGraphWriter get() = MermaidGraphWriter(
codeGenConfig,
codeGenerator
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import com.ramcosta.composedestinations.codegen.commons.plusAssign
import com.ramcosta.composedestinations.codegen.commons.snakeToCamelCase
import com.ramcosta.composedestinations.codegen.commons.toSnakeCase
import com.ramcosta.composedestinations.codegen.facades.CodeOutputStreamMaker
import com.ramcosta.composedestinations.codegen.model.CodeGenConfig
import com.ramcosta.composedestinations.codegen.model.CodeGenProcessedDestination
import com.ramcosta.composedestinations.codegen.model.ExternalRoute
import com.ramcosta.composedestinations.codegen.model.NavGraphGenParams
import com.ramcosta.composedestinations.codegen.model.SubModuleInfo
import java.io.File
import java.util.Locale

internal class MermaidGraphWriter(
private val codeGenConfig: CodeGenConfig,
private val codeGenerator: CodeOutputStreamMaker
) {

Expand All @@ -34,7 +37,7 @@ internal class MermaidGraphWriter(
appendLine("graph TD")
appendGraphTreeLinks(tree)
appendLine()
appendExternalNavGraphClicks(tree, submodules)
append("@clicksPlaceholder@")
appendLine()
val destinationIds = tree.destinationIds()
if (destinationIds.isNotEmpty()) {
Expand All @@ -48,40 +51,65 @@ internal class MermaidGraphWriter(
}
}.toString()

codeGenerator.makeFile(
name = tree.rawNavGraphGenParams.name,
packageName = "$DEFAULT_GEN_PACKAGE_NAME.mermaid",
extensionName = "mmd",
).use {
it += mermaidGraph
if (codeGenConfig.mermaidGraph != null) {
File(codeGenConfig.mermaidGraph, "${tree.rawNavGraphGenParams.name}.mmd")
.writeText(
mermaidGraph
.replace("@clicksPlaceholder@", externalNavGraphClicks(tree, null))
)
} else {
codeGenerator.makeFile(
name = tree.rawNavGraphGenParams.name,
packageName = "$DEFAULT_GEN_PACKAGE_NAME.mermaid",
extensionName = "mmd",
).use {
it += mermaidGraph
.replace("@clicksPlaceholder@", externalNavGraphClicks(tree, submodules))
}
}
codeGenerator.makeFile(
name = tree.rawNavGraphGenParams.name,
packageName = "$DEFAULT_GEN_PACKAGE_NAME.mermaid",
extensionName = "html",
).use {
it += html(title, mermaidGraph)

val htmlMermaid = html(title, mermaidGraph)
if (codeGenConfig.htmlMermaidGraph != null) {
File(codeGenConfig.htmlMermaidGraph, "${tree.rawNavGraphGenParams.name}.html")
.writeText(
htmlMermaid.replace("@clicksPlaceholder@", externalNavGraphClicks(tree, null))
)
} else {
codeGenerator.makeFile(
name = tree.rawNavGraphGenParams.name,
packageName = "$DEFAULT_GEN_PACKAGE_NAME.mermaid",
extensionName = "html",
).use {
it += htmlMermaid.replace("@clicksPlaceholder@", externalNavGraphClicks(tree, submodules))
}
}
}

private fun StringBuilder.appendExternalNavGraphClicks(
private fun externalNavGraphClicks(
tree: RawNavGraphTree,
submodules: List<SubModuleInfo>
) {
submodules: List<SubModuleInfo>?
): String {
val sb = StringBuilder()
tree.findAllExternalNavGraphs().forEach { externalGraph ->
val moduleRegistryFilePath =
submodules.first { externalGraph.generatedType.simpleName in it.topLevelGraphs }
.moduleRegistryFilePath
val splits = moduleRegistryFilePath.split("/")
val buildIndex = splits.indexOf("build")
val kotlinIndex = splits.indexOf("kotlin")
val pathWithoutUserDirs = splits.drop(buildIndex - 2) // keep module & project folder
.dropLast(splits.size - kotlinIndex) // drop after kotlin folder (included)
.joinToString("/")

val path = "/$pathWithoutUserDirs/resources/${DEFAULT_GEN_PACKAGE_NAME.replace(".", "/")}/mermaid/${externalGraph.generatedType.simpleName}.html"
appendLine("click ${externalGraph.mermaidId} \"$path\" \"See ${externalGraph.mermaidVisualName} details\" _blank")
if (submodules != null) {
val moduleRegistryFilePath =
submodules.first { externalGraph.generatedType.simpleName in it.topLevelGraphs }
.moduleRegistryFilePath
val splits = moduleRegistryFilePath.split("/")
val buildIndex = splits.indexOf("build")
val kotlinIndex = splits.indexOf("kotlin")
val pathWithoutUserDirs = splits.drop(buildIndex - 2) // keep module & project folder
.dropLast(splits.size - kotlinIndex) // drop after kotlin folder (included)
.joinToString("/")

val path = "/$pathWithoutUserDirs/resources/${DEFAULT_GEN_PACKAGE_NAME.replace(".", "/")}/mermaid/${externalGraph.generatedType.simpleName}.html"
sb.appendLine("click ${externalGraph.mermaidId} \"$path\" \"See ${externalGraph.mermaidVisualName} details\" _blank")
} else {
sb.appendLine("click ${externalGraph.mermaidId} \"${externalGraph.generatedType.simpleName}.html\" \"See ${externalGraph.mermaidVisualName} details\" _blank")
}
}

return sb.toString()
}

private fun StringBuilder.appendGraphTreeLinks(tree: RawNavGraphTree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ class ConfigParser(
private const val GEN_NAV_GRAPHS = "$PREFIX.generateNavGraphs"
private const val GEN_PACKAGE_NAME = "$PREFIX.codeGenPackageName"
private const val MODULE_NAME = "$PREFIX.moduleName"

private const val MERMAID_GRAPH = "$PREFIX.mermaidGraph"
private const val HTML_MERMAID_GRAPH = "$PREFIX.htmlMermaidGraph"
}

fun parse(): CodeGenConfig {
val packageName = options[GEN_PACKAGE_NAME]?.trim()?.removeSuffix(".")
val moduleName = options[MODULE_NAME]?.trim()?.filter { it.isLetter() }
val htmlMermaidGraph = options[HTML_MERMAID_GRAPH]?.trim()
val mermaidGraph = options[MERMAID_GRAPH]?.trim()

return CodeGenConfig(
moduleName = moduleName,
generateNavGraphs = parseBoolean(GEN_NAV_GRAPHS) ?: true,
packageName = packageName,
mermaidGraph = mermaidGraph,
htmlMermaidGraph = htmlMermaidGraph,
)
}

Expand Down
2 changes: 2 additions & 0 deletions playground-featurex/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ android {

ksp {
arg("compose-destinations.moduleName", "featureX")
arg("compose-destinations.htmlMermaidGraph", "$rootDir/docs")
arg("compose-destinations.mermaidGraph", "$rootDir/docs")
}
}

Expand Down
2 changes: 2 additions & 0 deletions playground-featurey/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ android {

ksp {
arg("compose-destinations.moduleName", "featureY")
arg("compose-destinations.htmlMermaidGraph", "$rootDir/docs")
arg("compose-destinations.mermaidGraph", "$rootDir/docs")
}
}

Expand Down
2 changes: 2 additions & 0 deletions playground-sub-featurey/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ android {

ksp {
arg("compose-destinations.moduleName", "subFeatureY")
arg("compose-destinations.htmlMermaidGraph", "$rootDir/docs")
arg("compose-destinations.mermaidGraph", "$rootDir/docs")
}
}

Expand Down
2 changes: 2 additions & 0 deletions playground/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ android {

// // If you want to manually create the nav graphs, use this:
// arg("compose-destinations.generateNavGraphs", "false")
arg("compose-destinations.htmlMermaidGraph", "$rootDir/docs")
arg("compose-destinations.mermaidGraph", "$rootDir/docs")

// To change the package name where the generated files will be placed
arg("compose-destinations.codeGenPackageName", "com.ramcosta.samples.playground.ui.screens")
Expand Down

0 comments on commit f3d943d

Please sign in to comment.