package de.steamwar.commands.ci import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.core.Context import java.io.File import java.io.PrintStream import kotlin.concurrent.thread class CiCommand : CliktCommand("ci") { override val hiddenFromHelp = true override fun help(context: Context): String = "CI daemon for processing git push events" override fun run() { echo("SteamWar CI daemon started. Waiting for push events...") echo("Format: ") try { while (true) { val input = readlnOrNull() ?: break val parts = input.split(" ") if (parts.size < 3) { echo("Invalid input format. Expected: ", err = true) continue } val (oldref, newref, branch) = parts // Fork/detach the build process thread(isDaemon = false) { processBuild(oldref, newref, branch) } } } catch (e: Exception) { echo("CI daemon error: ${e.message}", err = true) } } private fun processBuild(oldref: String, newref: String, branch: String) { try { val config = CiConfig(oldref, newref, branch) // Create log file val logFile = File(config.logpath) logFile.parentFile?.mkdirs() PrintStream(logFile).use { logStream -> val runner = CiRunner(config) runner.run(logStream) } } catch (e: Exception) { System.err.println("Build failed: ${e.message}") e.printStackTrace() } } }