Skip to content

Commit

Permalink
Hide internal properties
Browse files Browse the repository at this point in the history
The Structurizr DSL library introduced element/relationship identifiers
as properties recently. Hide those for the site generation since we are
not really interested in presenting them to users.
  • Loading branch information
jp7677 committed Feb 24, 2023
1 parent c5ab7e7 commit 429c315
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ val Model.includedSoftwareSystems: List<SoftwareSystem>
val SoftwareSystem.includedSoftwareSystem
get() = this.location != Location.External

val internalProperties = arrayOf("structurizr.dsl.identifier")

fun SoftwareSystem.hasDecisions() = documentation.decisions.isNotEmpty()

fun SoftwareSystem.hasDocumentationSections() = documentation.sections.size >= 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import com.structurizr.model.Element
import com.structurizr.model.SoftwareSystem
import com.structurizr.view.*
import nl.avisi.structurizr.site.generatr.includedSoftwareSystem
import nl.avisi.structurizr.site.generatr.internalProperties
import nl.avisi.structurizr.site.generatr.normalize

class C4PlantUmlExporterWithElementLinks(
private val url: String
): C4PlantUMLExporter() {
) : C4PlantUMLExporter() {
companion object {
const val TEMP_URI = "https://will-be-changed-to-relative/"

Expand All @@ -35,7 +36,7 @@ class C4PlantUmlExporterWithElementLinks(

override fun writeElement(view: ModelView?, element: Element?, writer: IndentingWriter?) {
if (element !is SoftwareSystem || !element.linkNeeded(view))
return super.writeElement(view, element, writer)
return writeElementWithSanitizedProperties(view, element, writer)

setElementUrl(element)
writeModifiedElement(view, element, writer)
Expand All @@ -55,13 +56,27 @@ class C4PlantUmlExporterWithElementLinks(
element: Element?,
writer: IndentingWriter?
) = IndentingWriter().let {
super.writeElement(view, element, it)
writeElementWithSanitizedProperties(view, element, it)
it.toString()
.replace(TEMP_URI, "")
.split(System.lineSeparator())
.forEach { line -> writer?.writeLine(line) }
}

private fun writeElementWithSanitizedProperties(
view: ModelView?,
element: Element?,
writer: IndentingWriter?
) = IndentingWriter().let {
super.writeElement(view, element, it)
it.toString()
.split(System.lineSeparator())
.filterNot { line ->
internalProperties.any { prop -> line.startsWith("AddProperty(\"$prop\",") }
}
.forEach { line -> writer?.writeLine(line) }
}

private fun restoreElement(element: Element) {
element.url = null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package nl.avisi.structurizr.site.generatr.site.model

import com.structurizr.model.SoftwareSystem
import nl.avisi.structurizr.site.generatr.internalProperties
import nl.avisi.structurizr.site.generatr.site.GeneratorContext

class SoftwareSystemHomePageViewModel(generatorContext: GeneratorContext, softwareSystem: SoftwareSystem) :
SoftwareSystemPageViewModel(generatorContext, softwareSystem, Tab.HOME) {
val hasProperties = softwareSystem.properties.any()
val propertiesTable = createPropertiesTableViewModel(softwareSystem.properties)
val hasProperties = softwareSystem.sanitizedProperties().any()
val propertiesTable = createPropertiesTableViewModel(softwareSystem.sanitizedProperties())
val content = markdownToHtml(this, softwareSystem.info(), generatorContext.svgFactory)

private fun SoftwareSystem.info() = documentation.sections
.minByOrNull { it.order }
?.content
?: "# Description${System.lineSeparator()}${description}"
?: "# Description${System.lineSeparator()}$description"

private fun SoftwareSystem.sanitizedProperties() = properties
.filterNot { (name, _) -> internalProperties.contains(name) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ class C4PlantUmlExporterWithElementLinksTest {
)
}

@Test
fun `internal and custom properties present`() {
val view = createWorkspaceWithOneSystemAndProperties()

val diagram = C4PlantUmlExporterWithElementLinks("/landscape/")
.export(view)

assertThat(diagram.definition.withoutHeaderAndFooter()).isEqualTo(
"""
WithoutPropertyHeader()
AddProperty("prop","val")
System(System1, "System 1", "", ${'$'}tags="")
""".trimIndent()
)
}

@Test
fun `link to other software system`() {
val view = createWorkspaceWithTwoSystems()
Expand Down Expand Up @@ -86,6 +102,19 @@ class C4PlantUmlExporterWithElementLinksTest {
.apply { addAllElements() }
}

private fun createWorkspaceWithOneSystemAndProperties(): SystemContextView {
val workspace = Workspace("workspace name", "").apply {
views.configuration.addProperty("c4plantuml.elementProperties", "true")
}
val system = workspace.model.addSoftwareSystem("System 1").apply {
addProperty("prop", "val")
addProperty("structurizr.dsl.identifier", "system1")
}

return workspace.views.createSystemContextView(system, "Context1", "")
.apply { addAllElements() }
}

private fun createWorkspaceWithTwoSystems(): SystemContextView {
val workspace = Workspace("workspace name", "")
val system = workspace.model.addSoftwareSystem("System 1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,22 @@ class SoftwareSystemHomePageViewModelTest : ViewModelTest() {
.isEqualTo(createPropertiesTableViewModel(softwareSystem.properties))
}
}

@Test
fun `internal properties present`() {
val generatorContext = generatorContext()
val softwareSystem: SoftwareSystem = generatorContext.workspace.model.addSoftwareSystem("Software system")
.apply {
description = "It's a system."
addProperty("structurizr.dsl.identifier", "id")
}
val viewModel = SoftwareSystemHomePageViewModel(generatorContext, softwareSystem)

assertThat(viewModel)
.all {
prop(SoftwareSystemHomePageViewModel::hasProperties).isEqualTo(false)
prop(SoftwareSystemHomePageViewModel::propertiesTable)
.isEqualTo(createPropertiesTableViewModel(mapOf()))
}
}
}

0 comments on commit 429c315

Please sign in to comment.