From fc284607a7ed1f31852aad248cb97ee3d4dcb795 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Mon, 28 Oct 2024 15:10:09 +0100 Subject: [PATCH] move processbuilder into service --- src/main/kotlin/alfred/web/core/GitRunner.kt | 9 +++++---- src/main/kotlin/alfred/web/core/Processes.kt | 18 ++++++++++++++++++ .../kotlin/alfred/web/core/ScriptRunner.kt | 5 +++-- src/main/kotlin/alfred/web/core/process.kt | 11 ----------- 4 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 src/main/kotlin/alfred/web/core/Processes.kt delete mode 100644 src/main/kotlin/alfred/web/core/process.kt diff --git a/src/main/kotlin/alfred/web/core/GitRunner.kt b/src/main/kotlin/alfred/web/core/GitRunner.kt index 9d77762..c23f0b5 100644 --- a/src/main/kotlin/alfred/web/core/GitRunner.kt +++ b/src/main/kotlin/alfred/web/core/GitRunner.kt @@ -10,7 +10,8 @@ import java.util.concurrent.TimeUnit class GitRunner( val builds: Builds, val handles: Handles, - val workspaces: Workspaces + val workspaces: Workspaces, + val processes: Processes ) { val scriptsDir = ".alfred" @@ -42,7 +43,7 @@ class GitRunner( private fun clone(ctx: BuildContext, wsPath: Path) { logger.info("build ${ctx.buildId}: cloning ${ctx.config.gitRepo} into $wsPath") - val process = processBuilder(ctx.config, ctx.logFile, "") + val process = processes.builder(ctx.config, ctx.logFile, "") .command("git", "clone", ctx.config.gitRepo, ".") .directory(wsPath.toFile()) .start() @@ -50,7 +51,7 @@ class GitRunner( val cloneSuccess = process.waitFor(ctx.config.gitCloneTimeout, TimeUnit.SECONDS) logger.info("build ${ctx.buildId}: checkout rev ${ctx.rev}") - processBuilder(ctx.config, ctx.logFile, "") + processes.builder(ctx.config, ctx.logFile, "") .command("git", "checkout", ctx.rev) .directory(wsPath.toFile()) .start() @@ -73,7 +74,7 @@ class GitRunner( ctx.logFile.append("\nRunning build file: $script\n") - val scriptProcess = processBuilder(ctx.config, ctx.logFile, ctx.rev) + val scriptProcess = processes.builder(ctx.config, ctx.logFile, ctx.rev) .command(scriptFile.absolutePath) .directory(wsPath.toFile()) .start() diff --git a/src/main/kotlin/alfred/web/core/Processes.kt b/src/main/kotlin/alfred/web/core/Processes.kt new file mode 100644 index 0000000..29b5741 --- /dev/null +++ b/src/main/kotlin/alfred/web/core/Processes.kt @@ -0,0 +1,18 @@ +package alfred.web.core + +import org.springframework.stereotype.Service + +@Service +class Processes { + + fun builder(config: BuildConfig, logFile: LogFile, rev: String): ProcessBuilder = + ProcessBuilder() + .redirectOutput(ProcessBuilder.Redirect.appendTo(logFile.backingFile)) + .redirectError(ProcessBuilder.Redirect.appendTo(logFile.backingFile)) + .apply { + environment().putAll(config.env) + environment()["ALFRED_LOG_FILE"] = logFile.backingFile.absolutePath + environment()["ALFRED_REV"] = rev + } + +} diff --git a/src/main/kotlin/alfred/web/core/ScriptRunner.kt b/src/main/kotlin/alfred/web/core/ScriptRunner.kt index 4e4badf..727876b 100644 --- a/src/main/kotlin/alfred/web/core/ScriptRunner.kt +++ b/src/main/kotlin/alfred/web/core/ScriptRunner.kt @@ -9,7 +9,8 @@ import java.nio.file.Paths @Service class ScriptRunner( val builds: Builds, - val handles: Handles + val handles: Handles, + val processes: Processes ) { fun run(buildId: BuildId, rev: String): ProcessInfo { @@ -21,7 +22,7 @@ class ScriptRunner( logFile.header(buildId, rev, Paths.get(config.gitWorkspace)) - val process = processBuilder(config, logFile, rev) + val process = processes.builder(config, logFile, rev) .command(config.script) .start() handles.add(Handle(process.toHandle(), buildId)) diff --git a/src/main/kotlin/alfred/web/core/process.kt b/src/main/kotlin/alfred/web/core/process.kt deleted file mode 100644 index c81e4c3..0000000 --- a/src/main/kotlin/alfred/web/core/process.kt +++ /dev/null @@ -1,11 +0,0 @@ -package alfred.web.core - -fun processBuilder(config: BuildConfig, logFile: LogFile, rev: String): ProcessBuilder = - ProcessBuilder() - .redirectOutput(ProcessBuilder.Redirect.appendTo(logFile.backingFile)) - .redirectError(ProcessBuilder.Redirect.appendTo(logFile.backingFile)) - .apply { - environment().putAll(config.env) - environment()["ALFRED_LOG_FILE"] = logFile.backingFile.absolutePath - environment()["ALFRED_REV"] = rev - }