Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ class ProjectGenerator(
classesPerModule
).generate()

NameMappings.configure(
ProjectNameMappingFactory.create(
layers = layers,
nodes = nodes,
layerNames = layerNames,
moduleNameParts = moduleNameParts
)
val nameMaps = ProjectNameMappingFactory.create(
layers = layers,
nodes = nodes,
layerNames = layerNames,
moduleNameParts = moduleNameParts
)
NameMappings.configure(nameMaps)

val projectLanguageAttributes = ProjectLayout.languageAttributes(projectRootPath, language)
ProjectWriter(
Expand All @@ -59,7 +58,7 @@ class ProjectGenerator(
projectName
).write()
projectLanguageAttributes.forEach { attributes ->
GraphWriter(nodes, attributes.projectName).write()
GraphWriter(nodes, attributes.projectName, nameMaps).write()
}
println("Project created in ${projectLanguageAttributes.first().projectName}")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
package io.github.cdsap.projectgenerator.writer

import io.github.cdsap.projectgenerator.ProjectNameMaps
import io.github.cdsap.projectgenerator.model.ProjectGraph
import io.github.cdsap.projectgenerator.NameMappings
import java.io.File

class GraphWriter(private val nodes: List<ProjectGraph>, val path: String) {
class GraphWriter(
private val nodes: List<ProjectGraph>,
val path: String,
private val nameMaps: ProjectNameMaps
) {

fun write() {
println("Creating graph file")
val file = File("$path/graph.dot")
file.createNewFile()
file.writeText(render(nodes))
file.writeText(render(nodes, nameMaps))
}

companion object {
fun render(nodes: List<ProjectGraph>): String {
fun render(nodes: List<ProjectGraph>, nameMaps: ProjectNameMaps): String {
val content = StringBuilder()
content.appendLine("digraph G {")

for (nodeGraph in nodes) {
val node = "${NameMappings.layerName(nodeGraph.layer)}:${NameMappings.moduleName(nodeGraph.id)}"
val node = "${layerName(nameMaps, nodeGraph.layer)}:${moduleName(nameMaps, nodeGraph.id)}"
for (dep in nodeGraph.nodes) {
content.appendLine(
"\"$node\" -> \"${NameMappings.layerName(dep.layer)}:${NameMappings.moduleName(dep.id)}\";"
"\"$node\" -> \"${layerName(nameMaps, dep.layer)}:${moduleName(nameMaps, dep.id)}\";"
)
}
}

content.appendLine("}")
return content.toString()
}

private fun layerName(nameMaps: ProjectNameMaps, layer: Int): String =
nameMaps.layerNames[layer] ?: "layer_$layer"

private fun moduleName(nameMaps: ProjectNameMaps, id: String): String =
nameMaps.moduleNames[id] ?: id
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.cdsap.projectgenerator.writer

import io.github.cdsap.projectgenerator.NameMappings
import io.github.cdsap.projectgenerator.ProjectNameMaps
import io.github.cdsap.projectgenerator.model.ProjectGraph
import io.github.cdsap.projectgenerator.model.TypeProject
import org.junit.jupiter.api.Assertions.assertEquals
Expand All @@ -15,53 +15,40 @@ class GraphWriterTest {
@TempDir
lateinit var tempDir: Path

private val nameMaps = ProjectNameMaps(
layerNames = mapOf(1 to "layer_1", 2 to "app"),
moduleNames = mapOf("module_1_1" to "sample-lib", "module_2_1" to "app")
)

@Test
fun `render builds digraph edges with layer module labels from NameMappings`() {
val previousLayerNames = NameMappings.layerNames
val previousModuleNames = NameMappings.moduleNames
NameMappings.layerNames = mapOf(1 to "layer_1", 2 to "app")
NameMappings.moduleNames = mapOf("module_1_1" to "sample-lib", "module_2_1" to "app")
try {
val lib = ProjectGraph("module_1_1", 1, emptyList(), TypeProject.LIB, 1)
val app = ProjectGraph("module_2_1", 2, listOf(lib), TypeProject.ANDROID_APP, 1)
fun `render builds digraph edges with layer module labels from ProjectNameMaps`() {
val lib = ProjectGraph("module_1_1", 1, emptyList(), TypeProject.LIB, 1)
val app = ProjectGraph("module_2_1", 2, listOf(lib), TypeProject.ANDROID_APP, 1)

val dot = GraphWriter.render(listOf(lib, app))
val dot = GraphWriter.render(listOf(lib, app), nameMaps)

assertEquals(
"""
digraph G {
"app:app" -> "layer_1:sample-lib";
}
assertEquals(
"""
digraph G {
"app:app" -> "layer_1:sample-lib";
}

""".trimIndent(),
dot
)
} finally {
NameMappings.layerNames = previousLayerNames
NameMappings.moduleNames = previousModuleNames
}
""".trimIndent(),
dot
)
}

@Test
fun `write writes render output to graph dot`() {
val previousLayerNames = NameMappings.layerNames
val previousModuleNames = NameMappings.moduleNames
NameMappings.layerNames = mapOf(1 to "layer_1", 2 to "app")
NameMappings.moduleNames = mapOf("module_1_1" to "sample-lib", "module_2_1" to "app")
try {
val lib = ProjectGraph("module_1_1", 1, emptyList(), TypeProject.LIB, 1)
val app = ProjectGraph("module_2_1", 2, listOf(lib), TypeProject.ANDROID_APP, 1)
val nodes = listOf(lib, app)
val outDir = tempDir.resolve("out").toFile().also { it.mkdirs() }
val lib = ProjectGraph("module_1_1", 1, emptyList(), TypeProject.LIB, 1)
val app = ProjectGraph("module_2_1", 2, listOf(lib), TypeProject.ANDROID_APP, 1)
val nodes = listOf(lib, app)
val outDir = tempDir.resolve("out").toFile().also { it.mkdirs() }

GraphWriter(nodes, outDir.path).write()
GraphWriter(nodes, outDir.path, nameMaps).write()

val graphFile = File(outDir, "graph.dot")
assertTrue(graphFile.exists())
assertEquals(GraphWriter.render(nodes), graphFile.readText())
} finally {
NameMappings.layerNames = previousLayerNames
NameMappings.moduleNames = previousModuleNames
}
val graphFile = File(outDir, "graph.dot")
assertTrue(graphFile.exists())
assertEquals(GraphWriter.render(nodes, nameMaps), graphFile.readText())
}
}
Loading