make LogFile nicer, move stuff around

master
Josha von Gizycki 4 weeks ago
parent beff30babf
commit 6f4326502c

@ -60,7 +60,7 @@ class Builds(
val path = Paths.get(home, "logs", fileName) val path = Paths.get(home, "logs", fileName)
path.toFile().parentFile.mkdirs() path.toFile().parentFile.mkdirs()
return Files.createFile(path).toFile() return LogFile(Files.createFile(path).toFile())
} }
} }

@ -103,13 +103,6 @@ data class BuildContext(
val buildId: BuildId val buildId: BuildId
) )
class WorkspaceIsNotEmpty(
private val ws: String
) : RuntimeException() {
override fun toString() =
"${this::class}: workspace $ws is not empty"
}
class FailedToClone( class FailedToClone(
private val buildId: BuildId, private val buildId: BuildId,
private val gitRepo: String private val gitRepo: String

@ -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"
)
}

@ -40,6 +40,6 @@ class ScriptRunner(
data class ProcessInfo( data class ProcessInfo(
val pid: Long, val pid: Long,
val logFile: File val logFile: LogFile
) )

@ -18,11 +18,13 @@ class Workspaces {
ctx.logFile.header(ctx.buildId, ctx.rev, workspacePath) ctx.logFile.header(ctx.buildId, ctx.rev, workspacePath)
if (!workspaceDir.exists()) { if (!workspaceDir.exists()) {
ctx.logFile.append("creating workspace ${workspaceDir.absolutePath}")
workspaceDir.mkdirs() workspaceDir.mkdirs()
} }
val wsList = workspaceDir.list() val wsList = workspaceDir.list()
if (wsList != null && wsList.isNotEmpty()) { if (wsList != null && wsList.isNotEmpty()) {
ctx.logFile.append("workspace ${workspaceDir.absolutePath} is not empty, aborting")
throw WorkspaceIsNotEmpty(workspaceDir.toString()) throw WorkspaceIsNotEmpty(workspaceDir.toString())
} }
@ -34,4 +36,11 @@ class Workspaces {
} }
} }
} }
class WorkspaceIsNotEmpty(
private val ws: String
) : RuntimeException() {
override fun toString() =
"${this::class}: workspace $ws is not empty"
}

@ -1,38 +1,11 @@
package alfred.web.core package alfred.web.core
import java.io.File fun processBuilder(config: BuildConfig, logFile: LogFile, rev: String): ProcessBuilder =
import java.nio.file.Path
import java.time.ZonedDateTime
fun processBuilder(config: BuildConfig, logFile: File, rev: String): ProcessBuilder =
ProcessBuilder() ProcessBuilder()
.redirectOutput(ProcessBuilder.Redirect.appendTo(logFile)) .redirectOutput(ProcessBuilder.Redirect.appendTo(logFile.backingFile))
.redirectError(ProcessBuilder.Redirect.appendTo(logFile)) .redirectError(ProcessBuilder.Redirect.appendTo(logFile.backingFile))
.apply { .apply {
environment().putAll(config.env) environment().putAll(config.env)
environment()["ALFRED_LOG_FILE"] = logFile.absolutePath environment()["ALFRED_LOG_FILE"] = logFile.backingFile.absolutePath
environment()["ALFRED_REV"] = rev 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"
)

@ -57,7 +57,7 @@ class Endpoints(
ResponseEntity.ok( ResponseEntity.ok(
mapOf( mapOf(
"log" to info.logFile.absolutePath, "log" to info.logFile.backingFile.absolutePath,
"pid" to info.pid "pid" to info.pid
) )
) )

Loading…
Cancel
Save