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

feat: enable parallel branch building #647

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM eclipse-temurin:21.0.5_11-jre-jammy

USER root
RUN apt update && apt install graphviz --yes && rm -rf /var/lib/apt/lists/*
RUN apt update && apt install graphviz git --yes && rm -rf /var/lib/apt/lists/*
RUN mkdir -p /var/model \
&& chown 65532:65532 /var/model
RUN useradd -d /home/generatr -u 65532 --create-home generatr
Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ dependencies {
implementation("org.eclipse.jetty:jetty-server:12.0.15")
implementation("org.eclipse.jetty.websocket:jetty-websocket-jetty-server:12.0.15")

implementation("org.apache.commons:commons-exec:1.4.0")

runtimeOnly("org.slf4j:slf4j-simple:2.0.16")
runtimeOnly("org.jetbrains.kotlin:kotlin-scripting-jsr223:2.0.21")
runtimeOnly("org.codehaus.groovy:groovy-jsr223:3.0.23")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ package nl.avisi.structurizr.site.generatr

import kotlinx.cli.*
import nl.avisi.structurizr.site.generatr.site.*
import org.apache.commons.exec.CommandLine
import org.apache.commons.exec.DefaultExecutor
import java.io.File


class GenerateSiteCommand : Subcommand(
"generate-site",
"Generate a site for the selected workspace."
Expand Down Expand Up @@ -59,7 +62,10 @@ class GenerateSiteCommand : Subcommand(
ArgType.String, "exclude-branches", "ex",
"Comma-separated list of branches to exclude from the generated site"
).default("")

private val parallel by option(
ArgType.Boolean, "parallel", "par",
"When set to TRUE will generate the site for each branch in parallel"
).default(value = false)
override fun execute() {
val siteDir = File(outputDir).apply { mkdirs() }
val gitUrl = gitUrl
Expand Down Expand Up @@ -105,22 +111,60 @@ class GenerateSiteCommand : Subcommand(
if (!branchesToGenerate.contains(defaultBranch)) {
throw Exception("$defaultBranch does not contain a valid structurizr workspace. Site generation halted.")
}
clonedRepository.checkoutBranch(defaultBranch)

if (parallel) {
System.setProperty("org.jruby.embed.localcontext.scope", "threadsafe");
branchesToGenerate.withIndex().toList().parallelStream().forEach { branchIndex ->
val branch = branchIndex.value
val index = branchIndex.index
val isDefaultBranch = branch == defaultBranch
println("Generating site for branch $branch")
// Default branch is already in git worktree
if(!isDefaultBranch) {
val gitCommand = CommandLine.parse("git worktree add ./$index $branch")
val executor = DefaultExecutor.builder().setWorkingDirectory(cloneDir).get()
executor.execute(gitCommand)
}
val path = if(isDefaultBranch) clonedRepository.cloneDir else File("${clonedRepository.cloneDir.path}/$index")
val branchWorkspaceFile = File(path, workspaceFile)
val workspace = createStructurizrWorkspace(branchWorkspaceFile)
writeStructurizrJson(workspace, File(siteDir, branch))
generateDiagrams(workspace, File(siteDir, branch))
generateSite(
version,
workspace,
assetsDir?.let { File(cloneDir, it) },
siteDir,
branchesToGenerate,
branch
)
// Remove the worktree branch directory after generating the site
if(!isDefaultBranch) {
val gitCommand = CommandLine.parse("git worktree remove ./$index")
val executor = DefaultExecutor.builder().setWorkingDirectory(cloneDir).get()
executor.execute(gitCommand)
}

}
} else {
branchesToGenerate.forEach { branch ->
println("Generating site for branch $branch")

clonedRepository.checkoutBranch(branch)

branchesToGenerate.forEach { branch ->
println("Generating site for branch $branch")
clonedRepository.checkoutBranch(branch)

val workspace = createStructurizrWorkspace(workspaceFileInRepo)
writeStructurizrJson(workspace, File(siteDir, branch))
generateDiagrams(workspace, File(siteDir, branch))
generateSite(
version,
workspace,
assetsDir?.let { File(cloneDir, it) },
siteDir,
branchesToGenerate,
branch
)
val workspace = createStructurizrWorkspace(workspaceFileInRepo)
writeStructurizrJson(workspace, File(siteDir, branch))
generateDiagrams(workspace, File(siteDir, branch))
generateSite(
version,
workspace,
assetsDir?.let { File(cloneDir, it) },
siteDir,
branchesToGenerate,
branch
)
}
}
}

Expand Down
Loading