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

Keep a thread safe in-memory cache of generated diagrams with links #308

Merged
merged 1 commit into from
Sep 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import net.sourceforge.plantuml.FileFormat
import net.sourceforge.plantuml.FileFormatOption
import net.sourceforge.plantuml.SourceStringReader
import nl.avisi.structurizr.site.generatr.site.C4PlantUmlExporterWithElementLinks.Companion.export
import java.io.ByteArrayOutputStream
import java.io.File
import java.net.URL
import java.util.concurrent.ConcurrentHashMap

fun generateDiagrams(workspace: Workspace, exportDir: File) {
val pumlDir = pumlDir(exportDir)
Expand All @@ -34,22 +36,19 @@ fun generateDiagrams(workspace: Workspace, exportDir: File) {
}
}

fun generateDiagramWithElementLinks(workspace: Workspace, view: View, url: String, exportDir: File): String {
val pumlDir = pumlDir(exportDir)
val svgDir = svgDir(exportDir)
private val diagramCache = ConcurrentHashMap<String, String>()

fun generateDiagramWithElementLinks(workspace: Workspace, view: View, url: String): String {
dirkgroot marked this conversation as resolved.
Show resolved Hide resolved
val diagram = generatePlantUMLDiagramWithElementLinks(workspace, view, url)

val name = "${diagram.key}-${view.key}"
val plantUMLFile = File(pumlDir, "$name.puml")
if (!plantUMLFile.exists() || plantUMLFile.readText() != diagram.definition) {
saveAsSvg(diagram, svgDir, name)
saveAsPUML(diagram, plantUMLFile)
jp7677 marked this conversation as resolved.
Show resolved Hide resolved
} else {
println("$name UP-TO-DATE")
}
return diagramCache.getOrPut(name) {
val reader = SourceStringReader(diagram.withCachedIncludes().definition)
val stream = ByteArrayOutputStream()

return readSvg(svgDir, name)
reader.outputImage(stream, FileFormatOption(FileFormat.SVG, false))
stream.toString(Charsets.UTF_8)
}
}

private fun generatePlantUMLDiagrams(workspace: Workspace): Collection<Diagram> {
Expand Down Expand Up @@ -80,16 +79,14 @@ private fun saveAsPng(diagram: Diagram, pngDir: File) {
}
}

private fun readSvg(svgDir: File, name: String): String {
val svgFile = File(svgDir, "$name.svg")
return svgFile.readText()
}

private fun generatePlantUMLDiagramWithElementLinks(workspace: Workspace, view: View, url: String): Diagram {
val plantUMLExporter = C4PlantUmlExporterWithElementLinks(url)

if (workspace.views.configuration.properties.containsKey("generatr.svglink.target")) {
plantUMLExporter.addSkinParam("svgLinkTarget", workspace.views.configuration.properties.getValue("generatr.svglink.target"))
plantUMLExporter.addSkinParam(
"svgLinkTarget",
workspace.views.configuration.properties.getValue("generatr.svglink.target")
)
}

return plantUMLExporter.export(view)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fun generateSite(
) {
val generatorContext = GeneratorContext(version, workspace, branches, currentBranch, serving) { key, url ->
workspace.views.views.singleOrNull { view -> view.key == key }
?.let { generateDiagramWithElementLinks(workspace, it, url, exportDir) }
?.let { generateDiagramWithElementLinks(workspace, it, url) }
}

val branchDir = File(exportDir, currentBranch)
Expand Down