From 575e87eb36a7ddcf618da89de80d82c601a75b0b Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Mon, 19 Oct 2015 20:23:29 -0700 Subject: [PATCH] Refactoring the server to use commands. --- src/main/kotlin/com/beust/kobalt/Main.kt | 34 ++---------- .../com/beust/kobalt/internal/KobaltServer.kt | 53 ++++++++++++++++--- .../beust/kobalt/kotlin/ScriptCompiler2.kt | 29 +++++++++- 3 files changed, 80 insertions(+), 36 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 3a0e9ff7..07fe57b4 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -3,6 +3,7 @@ package com.beust.kobalt import com.beust.jcommander.JCommander import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.KobaltContext +import com.beust.kobalt.api.Project import com.beust.kobalt.internal.* import com.beust.kobalt.kotlin.BuildFile import com.beust.kobalt.maven.* @@ -37,7 +38,7 @@ public fun mainNoExit(argv: Array) : Int { } private class Main @Inject constructor( - val script2: ScriptCompiler2.IFactory, + val buildFileCompilerFactory: ScriptCompiler2.IFactory, val plugins: Plugins, val taskManager: TaskManager, val http: Http, @@ -122,38 +123,13 @@ private class Main @Inject constructor( ProjectGenerator().run(args) } else if (args.usage) { jc.usage() + } else if (args.serverMode) { + server.run() } else { if (! buildFile.exists()) { jc.usage() } else { - val context = KobaltContext(args) - Kobalt.context = context - val scriptCompiler = script2.create(arrayListOf(buildFile)) - - if (args.serverMode) { - scriptCompiler.observable.subscribe { - info -> server.sendInfo(info) - } - executors.miscExecutor.submit(server) - } - val allProjects = scriptCompiler.findProjects() - - // - // Force each project.directory to be an absolute path, if it's not already - // - allProjects.forEach { - val fd = File(it.directory) - if (! fd.isAbsolute) { - it.directory = - if (args.buildFile != null) { - KFiles.findDotDir(File(args.buildFile)).parentFile.absolutePath - } else { - fd.absolutePath - } - } - } - - plugins.applyPlugins(context, allProjects) + val allProjects = buildFileCompilerFactory.create(listOf(buildFile)).compileBuildFiles(args) if (args.tasks) { // diff --git a/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt b/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt index cac31442..63585c73 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt @@ -1,7 +1,9 @@ package com.beust.kobalt.internal +import com.beust.klaxon.json import com.beust.kobalt.Args import com.beust.kobalt.api.Project +import com.beust.kobalt.kotlin.BuildFile import com.beust.kobalt.kotlin.ScriptCompiler2 import com.beust.kobalt.maven.IClasspathDependency import com.beust.kobalt.maven.MavenDependency @@ -9,19 +11,22 @@ import com.beust.kobalt.maven.SimpleDep import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.log import com.google.inject.Inject +import com.google.inject.assistedinject.Assisted import java.io.BufferedReader import java.io.InputStreamReader import java.io.PrintWriter import java.net.ServerSocket +import java.nio.file.Paths import java.util.concurrent.Executor import java.util.concurrent.ExecutorService -public class KobaltServer @Inject constructor(val executors: KobaltExecutors) : Runnable { +public class KobaltServer @Inject constructor(val args: Args, val executors: KobaltExecutors, + val buildFileCompilerFactory: ScriptCompiler2.IFactory) : Runnable { var outgoing: PrintWriter? = null - val pending = arrayListOf() + val pending = arrayListOf() override fun run() { - val portNumber = Args.DEFAULT_SERVER_PORT + val portNumber = args.port log(1, "Starting on port $portNumber") val serverSocket = ServerSocket(portNumber) @@ -38,16 +43,52 @@ public class KobaltServer @Inject constructor(val executors: KobaltExecutors) : var inputLine = ins.readLine() while (inputLine != null) { log(1, "Received $inputLine") + val command = getCommand(inputLine) + if (command != null) { + command!!.run() + } if (inputLine.equals("Bye.")) break; inputLine = ins.readLine() } } - fun sendInfo(info: ScriptCompiler2.BuildScriptInfo) { + interface Command { + fun run() + } + + inner class PingCommand(val s: String) : Command { + override fun run() = sendInfo("{ \"response\" : \"$s\" }") + } + + inner class GetDependenciesCommand(val s: String) : Command { + override fun run() { + val buildFile = BuildFile(Paths.get("c:\\Users\\cbeust\\java\\jcommander\\Build.kt"), "JCommander build") + val scriptCompiler = buildFileCompilerFactory.create(listOf(buildFile)) + scriptCompiler.observable.subscribe { + info -> sendInfo(toJson(info)) + } + scriptCompiler.compileBuildFiles(args) + } + } + + private fun toJson(info: ScriptCompiler2.BuildScriptInfo) : String { + log(1, "Returning JSON for BuildScriptInfo") + return toJson(info, executors.miscExecutor) + } + + private fun getCommand(command: String): Command? { + if (command == "g") { + return GetDependenciesCommand(command) + } else { + return PingCommand(command) + } + } + + fun sendInfo(info: String) { if (outgoing != null) { - val json = toJson(info, executors.miscExecutor) - outgoing!!.println(json) +// val json = toJson(info, executors.miscExecutor) + outgoing!!.println(info) } else { log(1, "Queuing $info") synchronized(pending) { diff --git a/src/main/kotlin/com/beust/kobalt/kotlin/ScriptCompiler2.kt b/src/main/kotlin/com/beust/kobalt/kotlin/ScriptCompiler2.kt index 4479c2a5..3fad7bf6 100644 --- a/src/main/kotlin/com/beust/kobalt/kotlin/ScriptCompiler2.kt +++ b/src/main/kotlin/com/beust/kobalt/kotlin/ScriptCompiler2.kt @@ -1,7 +1,9 @@ package com.beust.kobalt.kotlin +import com.beust.kobalt.Args import com.beust.kobalt.Plugins import com.beust.kobalt.api.Kobalt +import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Plugin import com.beust.kobalt.api.Project import com.beust.kobalt.api.annotation.Task @@ -33,7 +35,32 @@ public class ScriptCompiler2 @Inject constructor(@Assisted("buildFiles") val bui private val SCRIPT_JAR = "buildScript.jar" - fun findProjects(): List { + fun compileBuildFiles(args: Args): List { + val context = KobaltContext(args) + Kobalt.context = context + + val allProjects = findProjects() + + // + // Force each project.directory to be an absolute path, if it's not already + // + allProjects.forEach { + val fd = File(it.directory) + if (! fd.isAbsolute) { + it.directory = + if (args.buildFile != null) { + KFiles.findDotDir(File(args.buildFile)).parentFile.absolutePath + } else { + fd.absolutePath + } + } + } + + plugins.applyPlugins(context, allProjects) + return allProjects + } + + private fun findProjects(): List { val result = arrayListOf() buildFiles.forEach { buildFile -> val pluginUrls = findPlugInUrls(buildFile)