Skip to content

Commit

Permalink
Fix style defaults according to Structurizr notation (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
goto1134 authored May 31, 2023
1 parent 7273373 commit 11e9fdb
Show file tree
Hide file tree
Showing 28 changed files with 225 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.structurizr.export.Diagram
import com.structurizr.export.IndentingWriter
import com.structurizr.model.*
import com.structurizr.view.*
import io.github.goto1134.structurizr.export.d2.model.D2Shape
import io.github.goto1134.structurizr.export.d2.model.GlobalObject
import io.github.goto1134.structurizr.export.d2.model.NamedObject
import io.github.goto1134.structurizr.export.d2.model.TextObject
Expand Down Expand Up @@ -224,9 +225,20 @@ open class D2Exporter : AbstractDiagramExporter() {
}

protected fun writeGroup(view: ModelView, groupWithPath: GroupWithPath, writer: IndentingWriter) {
val allGroupsStyle = view.viewSet.configuration.styles.findElementStyle("Group")
val currentGroupStyle = view.viewSet.configuration.styles.findElementStyle("Group:${groupWithPath.fullGroup}")
NamedObject.build(groupWithPath.absolutePathInView(view)) {
label(groupWithPath.name)
withGroupStyle()
label(groupWithPath.group)
shape(currentGroupStyle?.d2Shape ?: allGroupsStyle?.d2Shape ?: D2Shape.RECTANGLE)
icon(currentGroupStyle?.icon ?: allGroupsStyle?.icon)
stroke(currentGroupStyle?.stroke ?: currentGroupStyle?.stroke ?: "#cccccc")
strokeWidth(currentGroupStyle?.strokeWidth ?: allGroupsStyle?.strokeWidth ?: 2)
fontColor(currentGroupStyle?.color ?: allGroupsStyle?.color ?: "#cccccc")
fontSize(currentGroupStyle?.fontSize ?: allGroupsStyle?.fontSize ?: 24)
opacity(currentGroupStyle?.d2Opacity ?: currentGroupStyle?.d2Opacity ?: 1.0)
dashed()
fill(currentGroupStyle?.background ?: allGroupsStyle?.background ?: "#ffffff")
fillPattern(currentGroupStyle?.d2FillPattern ?: allGroupsStyle?.d2FillPattern)
}.writeObject(writer)
}

Expand Down Expand Up @@ -271,15 +283,15 @@ open class D2Exporter : AbstractDiagramExporter() {
NamedObject.build(relationshipView.relationshipNameInView(view)) {
animated(relationshipStyle.d2Animated)
label(relationshipView.d2LabelInView(view))
opacity(relationshipStyle.d2Opacity)
stroke(relationshipStyle.color)
fontSize(relationshipStyle.fontSize)
opacity(relationshipStyle.d2Opacity ?: 1.0)
stroke(relationshipStyle.color ?: "#707070")
strokeWidth(relationshipStyle.thickness ?: 2)
fontSize(relationshipStyle.fontSize ?: 24)
when (relationshipStyle.style) {
LineStyle.Dashed -> dashed()
LineStyle.Dotted -> dotted()
else -> Unit
LineStyle.Solid -> Unit
LineStyle.Dashed, null -> dashed()
}
strokeWidth(relationshipStyle.thickness)
}.writeObject(writer)
}

Expand All @@ -295,7 +307,7 @@ open class D2Exporter : AbstractDiagramExporter() {
fillPattern(style.d2FillPattern)
stroke(style.stroke)
strokeWidth(style.strokeWidth)
opacity(style.d2Opacity)
opacity(style.d2Opacity ?: 1.0)
when (style.border) {
Border.Dashed -> dashed()
Border.Dotted -> dotted()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.github.goto1134.structurizr.export.d2.D2Exporter.Companion.STRUCTURIZR
import io.github.goto1134.structurizr.export.d2.model.D2FillPattern
import io.github.goto1134.structurizr.export.d2.model.D2Shape

val ElementStyle.d2Opacity get() = opacity.toDouble() / 100
val ElementStyle.d2Opacity get() = opacity?.toDouble()?.div( 100)

val ElementStyle.d2FillPattern get() = D2FillPattern.get(properties[D2Exporter.D2_FILL_PATTERN])

Expand All @@ -22,6 +22,7 @@ val ElementStyle.d2Shape
Shape.Hexagon -> D2Shape.HEXAGON
Shape.Pipe -> D2Shape.QUEUE
Shape.Diamond -> D2Shape.DIAMOND
null -> null
else -> D2Shape.RECTANGLE
}

Expand All @@ -41,24 +42,30 @@ fun GroupableElement.parentGroupSequenceOrNull(): Sequence<String>? {
}
}

data class GroupWithPath(val parent: Element?, val relativePath: String, val name: String) {
data class GroupWithPath(val parent: Element?, val relativePath: String, val fullGroup: String, val group: String) {
fun absolutePathInView(view: ModelView) = buildString {
parent?.absolutePathInView(view)?.let { append(it, ".") }
append(relativePath)
}
}

fun GroupableElement.groupsWithPathsOrNull() =
parentGroupSequenceOrNull()?.scan(GroupWithPath(parent, "", "")) { wrapper, groupName ->
fun GroupableElement.groupsWithPathsOrNull(): Sequence<GroupWithPath>? {
val groupSeparator = model.properties[STRUCTURIZR_GROUP_SEPARATOR_PROPERTY_NAME]
return parentGroupSequenceOrNull()?.scan(GroupWithPath(parent, "", "", "")) { wrapper, groupName ->
GroupWithPath(
parent = parent,
relativePath = buildString {
if (wrapper.relativePath.isNotEmpty()) append(wrapper.relativePath, ".")
append("\"group_", groupName, "\"")
},
name = groupName
fullGroup = buildString {
if (wrapper.fullGroup.isNotEmpty()) append(wrapper.fullGroup, groupSeparator)
append(groupName)
},
group = groupName
)
}?.drop(1)
}

val Element.hasMultipleInstances get() = this is DeploymentNode && "1" != instances

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.github.goto1134.structurizr.export.d2.model.D2Connection

val RelationshipStyle.d2Animated get() = properties[D2_CONNECTION_ANIMATED].toBoolean()

val RelationshipStyle.d2Opacity get() = opacity.toDouble() / 100
val RelationshipStyle.d2Opacity get() = opacity?.toDouble()?.div(100)

val RelationshipView.d2Connection: D2Connection
get() = if (isResponse == true) D2Connection.REVERSE else D2Connection.DIRECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class PropertyBuilder {
properties.add(D2WrappedStringProperty(D2Keyword.LABEL, label))
}

fun shape(shape: D2Shape) = apply {
properties.add(D2Property(D2Keyword.SHAPE, shape))
fun shape(shape: D2Shape?) = apply {
if (shape != null) properties.add(D2Property(D2Keyword.SHAPE, shape))
}

fun icon(icon: String?) = apply {
Expand Down Expand Up @@ -174,8 +174,6 @@ class PropertyBuilder {
style.add(D2Property(D2StyleKeyword.OPACITY, opacity))
}

fun withGroupStyle() = fill("white").stroke("black")

fun animated(animated: Boolean) = apply {
if (animated) style.add(D2Property(D2StyleKeyword.CONNECTION_ANIMATED, true))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal class D2ExporterTest {

@Test
fun test_NestedGroupsExample() {
val workspace = WorkspaceUtils.loadWorkspaceFromJson(testFile("groups-nested.json"))
val workspace = WorkspaceUtils.loadWorkspaceFromJson(testFile("groups-nested/groups-nested.json"))
ThemeUtils.loadThemes(workspace)
val diagrams = D2Exporter().export(workspace)
assertEquals(1, diagrams.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand Down Expand Up @@ -120,6 +121,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand Down Expand Up @@ -170,6 +172,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/test/resources/amazon/AmazonWebServicesDeployment.d2
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ container_5.container_6.container_9.container_10.container_11 -> container_5.con
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -149,6 +150,7 @@ container_5.container_6.container_7 -> container_5.container_6.container_8: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -158,6 +160,7 @@ container_5.container_6.container_8 -> container_5.container_6.container_9.conta
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
1 change: 1 addition & 0 deletions lib/src/test/resources/animated-relation/SystemContext.d2
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ container_1 -> container_2: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
13 changes: 13 additions & 0 deletions lib/src/test/resources/bank-animated/Components.d2
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -128,6 +129,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -137,6 +139,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -146,6 +149,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand Down Expand Up @@ -184,6 +188,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -193,6 +198,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -202,6 +208,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -211,6 +218,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand Down Expand Up @@ -249,6 +257,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -258,6 +267,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -267,6 +277,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -276,6 +287,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -285,6 +297,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand Down
10 changes: 10 additions & 0 deletions lib/src/test/resources/bank-animated/Containers.d2
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand Down Expand Up @@ -89,6 +90,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -114,6 +116,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -123,6 +126,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -148,6 +152,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -173,6 +178,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -182,6 +188,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -191,6 +198,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -200,6 +208,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand All @@ -225,6 +234,7 @@ steps: {
font-size: 24
opacity: 1.0
stroke: "#707070"
stroke-dash: 5
stroke-width: 2
}
}
Expand Down
Loading

0 comments on commit 11e9fdb

Please sign in to comment.