Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable customization via base theme #18

Merged
merged 7 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 51 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,31 @@ You'll just need to add it to your project's **root** `build.gradle` or `build.g
<details>
<summary><b>build.gradle (Groovy DSL)</b></summary>

### Using the [plugins DSL](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block)
#### Using the plugins DSL

```groovy
plugins {
id "dev.iurysouza.modulegraph" version "0.4.0"
}
```

### Using [legacy plugin application](https://docs.gradle.org/current/userguide/plugins.html#sec:old_plugin_application)
<details>
<summary><b>Using Legacy Plugin application</b></summary>

```groovy
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "dev.iurysouza:modulegraph:0.4.0"
}
}
dependencies {
classpath "dev.iurysouza:modulegraph:0.4.0"
}
}

apply plugin: "dev.iurysouza.modulegraph"
apply plugin: "dev.iurysouza.modulegraph"
```
</details>

### Configuring the plugin

Expand All @@ -64,6 +65,17 @@ apply plugin: "dev.iurysouza.modulegraph"
theme = Theme.NEUTRAL // optional
orientation = Orientation.LEFT_TO_RIGHT // optional
linkText = LinkText.NONE // optional
// or you can fully customize it by using the BASE theme:
// theme = Theme.BASE(
// [
// "primaryTextColor": "#fff",
// "primaryColor": "#5a4f7c",
// "primaryBorderColor": "#5a4f7c",
// "lineColor": "#f5a623",
// "tertiaryColor": "#40375c",
// "fontSize": "11px"
// ]
// )
}
```

Expand All @@ -76,40 +88,54 @@ apply plugin: "dev.iurysouza.modulegraph"

<p></p>

#### Using the [plugins DSL](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block)
#### Using the plugins DSL

```kotlin
plugins {
id("dev.iurysouza.modulegraph") version "0.4.0"
}
```

#### Using [legacy plugin application](https://docs.gradle.org/current/userguide/plugins.html#sec:old_plugin_application)
<details>
<summary><b>Using Legacy Plugin application</b></summary>

```kotlin
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("dev.iurysouza:modulegraph:0.4.0")
}
}
dependencies {
classpath("dev.iurysouza:modulegraph:0.4.0")
}
}

apply(plugin = "dev.iurysouza:modulegraph")
```
</details>

### Configuring the plugin

```kotlin
moduleGraphConfig {
readmePath.set("./README.md")
heading.set("### Dependency Diagram")
theme.set(Theme.NEUTRAL) // optional
orientation.set(Orientation.LEFT_TO_RIGHT) //optional
linkText.set(LinkText.NONE) // optional
theme.set(Theme.NEUTRAL) // optional
// or you can fully customize it by using the BASE theme:
// theme.set(Theme.BASE(
// mapOf(
// "primaryTextColor" to "#fff",
// "primaryColor" to "#5a4f7c",
// "primaryBorderColor" to "#5a4f7c",
// "lineColor" to "#f5a623",
// "tertiaryColor" to "#40375c",
// "fontSize" to "11px"
// )
// )
// )
}
```

Expand All @@ -126,6 +152,8 @@ Optional settings:

- **theme**: The [mermaid theme](https://mermaid.js.org/config/theming.html) to be used for styling
the graph. Default is `NEUTRAL`.
- Further customization is possible by setting the `themeVariables` property on the `BASE` theme. Check the
[mermaid docs](https://mermaid-js.github.io/mermaid/#/theming) for more info.
- **orientation**:
The [orientation](https://mermaid.js.org/syntax/flowchart.html#flowchart-orientation) that the
flowchart will have. Default is `LEFT_TO_RIGHT`.
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[versions]
detekt = "1.22.0"
kotlin = "1.8.10"
kotlinSerializationJson = "1.5.1"
ktlintGradle = "11.3.1"
pluginPublish = "1.1.0"
versionCheck = "0.46.0"
Expand All @@ -12,7 +13,9 @@ ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlintGradle"}
pluginPublish = { id = "com.gradle.plugin-publish", version.ref = "pluginPublish"}
versionCheck = { id = "com.github.ben-manes.versions", version.ref = "versionCheck"}
testKit = { id = "org.gradle.test-kit", version = "7.3.1" }
kotlinxSerialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }

[libraries]
junit5Api = "org.junit.jupiter:junit-jupiter-api:5.8.2"
junit5Engine = "org.junit.jupiter:junit-jupiter-engine:5.8.2"
kotlinxSerializationJson = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinSerializationJson" }
2 changes: 2 additions & 0 deletions plugin-build/modulegraph/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm")
`java-gradle-plugin`
alias(libs.plugins.kotlinxSerialization)
alias(libs.plugins.pluginPublish)
}

dependencies {
implementation(kotlin("stdlib"))
implementation(gradleApi())
implementation(libs.kotlinxSerializationJson)

testImplementation(libs.junit5Api)
testRuntimeOnly(libs.junit5Engine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dev.iurysouza.modulegraph

import java.io.File
import java.io.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.gradle.api.Project
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.logging.Logger
Expand Down Expand Up @@ -58,16 +60,17 @@ internal fun buildMermaidGraph(
}
}.joinToString(separator = "\n")

val mermaidConfig = """
%%{
init: {
'theme': '${theme.value}'
}
}%%
""".trimIndent()
return "${mermaidConfig}\n\ngraph ${orientation.value}\n$subgraphs\n$digraph"
return "${createConfig(theme)}\n\ngraph ${orientation.value}\n$subgraphs\n$digraph"
}

private fun createConfig(theme: Theme): String = """
%%{
init: {
'theme': '${theme.name}'${if (theme is Theme.BASE) ",\n\t'themeVariables': ${Json.encodeToString(theme.themeVariables).trimIndent()}" else ""}
}
}%%
""".trimIndent()

// Generate a Mermaid subgraph for the specified group and list of modules
fun createSubgraph(group: String, modules: List<List<String>>): String {
// Extract module names for the current group
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
package dev.iurysouza.modulegraph

import java.io.Serializable as JavaSerializable
import kotlinx.serialization.Serializable

/**
* More info at [mermaid docs](https://mermaid.js.org/config/theming.html#theme-configuration)
*/
enum class Theme(val value: String) {
@Serializable
sealed class Theme(val name: String) : JavaSerializable {

/**
* This theme goes well with dark-colored elements or dark-mode.
*/
DARK("dark"),
object DARK : Theme("dark")

/**
* This is the default theme for all diagrams.
*/
DEFAULT("default"),
object DEFAULT : Theme("default")

/**
* This theme contains shades of green.
*/
FOREST("forest"),
object FOREST : Theme("forest")

/**
* This theme is great for black and white documents that will be printed.
*/
NEUTRAL("neutral"),
object NEUTRAL : Theme("neutral")

/**
* The BASE theme can be used for customization. You just need to provide the themeVariables according to the specs.
* You can read more about it [here](https://mermaid.js.org/config/theming.html#theme-variables).
*/
data class BASE(val themeVariables: Map<String, String> = emptyMap()) : Theme("base")
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir

@Suppress("LongMethod")
class ModuleGraphPluginFunctionalTest {
@TempDir
lateinit var testProjectDir: File
Expand Down Expand Up @@ -91,6 +92,75 @@ class ModuleGraphPluginFunctionalTest {
assertEquals(expectedOutput, readmeFile.readText())
}

@Test
fun `when custom theme is applied it produces the expected output`() {
settingsFile.writeText(
"""
rootProject.name = "test"
include(":example")
include(":groupFolder:example2")
""".trimIndent()
)

exampleBuildFile.writeText(
"""
plugins {
java
id("dev.iurysouza.modulegraph")
}
moduleGraphConfig {
heading.set("### Dependency Diagram")
theme.set(dev.iurysouza.modulegraph.Theme.BASE(
mapOf(
"primaryTextColor" to "#fff",
"primaryColor" to "#5a4f7c",
"primaryBorderColor" to "#5a4f7c",
"lineColor" to "#f5a623",
"tertiaryColor" to "#40375c",
"fontSize" to "11px"
)
)
)
readmePath.set("${readmeFile.absolutePath.replace("\\", "\\\\")}")
}
dependencies {
implementation(project(":groupFolder:example2"))
}
""".trimIndent()
)
readmeFile.writeText("### Dependency Diagram")

// Run the plugin task
GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments("createModuleGraph")
.withPluginClasspath()
.build()

// Check if the output matches the expected result
val expectedOutput =
"""
### Dependency Diagram

```mermaid
%%{
init: {
'theme': 'base',
'themeVariables': {"primaryTextColor":"#fff","primaryColor":"#5a4f7c","primaryBorderColor":"#5a4f7c","lineColor":"#f5a623","tertiaryColor":"#40375c","fontSize":"11px"}
}
}%%

graph LR

subgraph groupFolder
example2
end
example --> example2
```
""".trimIndent()
assertEquals(expectedOutput, readmeFile.readText())
}

@Test
fun `plugin add configuration name to links if configured to`() {
settingsFile.writeText(
Expand Down
3 changes: 2 additions & 1 deletion sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
```mermaid
%%{
init: {
'theme': 'dark'
'theme': 'base',
'themeVariables': {"primaryTextColor":"#fff","primaryColor":"#5a4f7c","primaryBorderColor":"#5a4f7c","lineColor":"#f5a623","tertiaryColor":"#40375c","fontSize":"11px"}
}
}%%

Expand Down
13 changes: 12 additions & 1 deletion sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ plugins {
moduleGraphConfig {
heading.set("# Module Graph")
readmePath.set("./README.md")
theme.set(Theme.DARK)
theme.set(
Theme.BASE(
mapOf(
"primaryTextColor" to "#fff", // All text colors
"primaryColor" to "#5a4f7c", // Box colors
"primaryBorderColor" to "#5a4f7c", // Box border color
"lineColor" to "#f5a623", // Line color
"tertiaryColor" to "#40375c", // Container box background
"fontSize" to "11px"
)
)
)
orientation.set(Orientation.TOP_TO_BOTTOM)
}

Expand Down