From 6f4326502c28c0880221fb6ede30d0594163fffe Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Mon, 28 Oct 2024 11:31:34 +0100 Subject: [PATCH] make LogFile nicer, move stuff around --- src/main/kotlin/alfred/web/core/Builds.kt | 2 +- src/main/kotlin/alfred/web/core/GitRunner.kt | 7 ---- src/main/kotlin/alfred/web/core/LogFile.kt | 33 +++++++++++++++++ .../kotlin/alfred/web/core/ScriptRunner.kt | 2 +- src/main/kotlin/alfred/web/core/Workspaces.kt | 11 +++++- src/main/kotlin/alfred/web/core/process.kt | 35 +++---------------- src/main/kotlin/alfred/web/http/Endpoints.kt | 2 +- 7 files changed, 50 insertions(+), 42 deletions(-) create mode 100644 src/main/kotlin/alfred/web/core/LogFile.kt diff --git a/src/main/kotlin/alfred/web/core/Builds.kt b/src/main/kotlin/alfred/web/core/Builds.kt index 7eae801..f909412 100644 --- a/src/main/kotlin/alfred/web/core/Builds.kt +++ b/src/main/kotlin/alfred/web/core/Builds.kt @@ -60,7 +60,7 @@ class Builds( val path = Paths.get(home, "logs", fileName) path.toFile().parentFile.mkdirs() - return Files.createFile(path).toFile() + return LogFile(Files.createFile(path).toFile()) } } diff --git a/src/main/kotlin/alfred/web/core/GitRunner.kt b/src/main/kotlin/alfred/web/core/GitRunner.kt index fd6ed4a..9d77762 100644 --- a/src/main/kotlin/alfred/web/core/GitRunner.kt +++ b/src/main/kotlin/alfred/web/core/GitRunner.kt @@ -103,13 +103,6 @@ data class BuildContext( val buildId: BuildId ) -class WorkspaceIsNotEmpty( - private val ws: String -) : RuntimeException() { - override fun toString() = - "${this::class}: workspace $ws is not empty" -} - class FailedToClone( private val buildId: BuildId, private val gitRepo: String diff --git a/src/main/kotlin/alfred/web/core/LogFile.kt b/src/main/kotlin/alfred/web/core/LogFile.kt new file mode 100644 index 0000000..6a549b0 --- /dev/null +++ b/src/main/kotlin/alfred/web/core/LogFile.kt @@ -0,0 +1,33 @@ +package alfred.web.core + +import java.io.File +import java.nio.file.Path +import java.time.ZonedDateTime +import kotlin.io.appendText + +class LogFile( + val backingFile: File +) { + + fun header(buildId: BuildId, rev: String, workspace: Path) { + append("At your service.") + append("Build $buildId, rev [$rev] started at ${ZonedDateTime.now()}") + append("Workspace directory is: $workspace\n") + } + + fun footer() { + append("Build finished at ${ZonedDateTime.now()}") + } + + fun append(content: String) = + backingFile.appendText( + content.lines().joinToString(separator = "\n") { + if (it.trim().isEmpty()) { + "" + } else { + "Alfred: $it" + } + } + "\n" + ) + +} \ No newline at end of file diff --git a/src/main/kotlin/alfred/web/core/ScriptRunner.kt b/src/main/kotlin/alfred/web/core/ScriptRunner.kt index 47f208f..4e4badf 100644 --- a/src/main/kotlin/alfred/web/core/ScriptRunner.kt +++ b/src/main/kotlin/alfred/web/core/ScriptRunner.kt @@ -40,6 +40,6 @@ class ScriptRunner( data class ProcessInfo( val pid: Long, - val logFile: File + val logFile: LogFile ) diff --git a/src/main/kotlin/alfred/web/core/Workspaces.kt b/src/main/kotlin/alfred/web/core/Workspaces.kt index 5a6140b..b02d36e 100644 --- a/src/main/kotlin/alfred/web/core/Workspaces.kt +++ b/src/main/kotlin/alfred/web/core/Workspaces.kt @@ -18,11 +18,13 @@ class Workspaces { ctx.logFile.header(ctx.buildId, ctx.rev, workspacePath) if (!workspaceDir.exists()) { + ctx.logFile.append("creating workspace ${workspaceDir.absolutePath}") workspaceDir.mkdirs() } val wsList = workspaceDir.list() if (wsList != null && wsList.isNotEmpty()) { + ctx.logFile.append("workspace ${workspaceDir.absolutePath} is not empty, aborting") throw WorkspaceIsNotEmpty(workspaceDir.toString()) } @@ -34,4 +36,11 @@ class Workspaces { } } -} \ No newline at end of file +} + +class WorkspaceIsNotEmpty( + private val ws: String +) : RuntimeException() { + override fun toString() = + "${this::class}: workspace $ws is not empty" +} diff --git a/src/main/kotlin/alfred/web/core/process.kt b/src/main/kotlin/alfred/web/core/process.kt index b134ce7..c81e4c3 100644 --- a/src/main/kotlin/alfred/web/core/process.kt +++ b/src/main/kotlin/alfred/web/core/process.kt @@ -1,38 +1,11 @@ package alfred.web.core -import java.io.File -import java.nio.file.Path -import java.time.ZonedDateTime - -fun processBuilder(config: BuildConfig, logFile: File, rev: String): ProcessBuilder = +fun processBuilder(config: BuildConfig, logFile: LogFile, rev: String): ProcessBuilder = ProcessBuilder() - .redirectOutput(ProcessBuilder.Redirect.appendTo(logFile)) - .redirectError(ProcessBuilder.Redirect.appendTo(logFile)) + .redirectOutput(ProcessBuilder.Redirect.appendTo(logFile.backingFile)) + .redirectError(ProcessBuilder.Redirect.appendTo(logFile.backingFile)) .apply { environment().putAll(config.env) - environment()["ALFRED_LOG_FILE"] = logFile.absolutePath + environment()["ALFRED_LOG_FILE"] = logFile.backingFile.absolutePath environment()["ALFRED_REV"] = rev } - -typealias LogFile = File - -fun LogFile.header(buildId: BuildId, rev: String, workspace: Path) { - this.append("At your service.") - this.append("Build $buildId, rev [$rev] started at ${ZonedDateTime.now()}") - this.append("Workspace directory is: $workspace\n") -} - -fun LogFile.footer() { - this.append("Build finished at ${ZonedDateTime.now()}") -} - -fun LogFile.append(content: String) = - this.appendText( - content.lines().joinToString(separator = "\n") { - if (it.trim().isEmpty()) { - "" - } else { - "Alfred: $it" - } - } + "\n" - ) diff --git a/src/main/kotlin/alfred/web/http/Endpoints.kt b/src/main/kotlin/alfred/web/http/Endpoints.kt index 79f5401..89d35e2 100644 --- a/src/main/kotlin/alfred/web/http/Endpoints.kt +++ b/src/main/kotlin/alfred/web/http/Endpoints.kt @@ -57,7 +57,7 @@ class Endpoints( ResponseEntity.ok( mapOf( - "log" to info.logFile.absolutePath, + "log" to info.logFile.backingFile.absolutePath, "pid" to info.pid ) )