1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 00:17:11 -07:00

Refactoring the server to use commands.

This commit is contained in:
Cedric Beust 2015-10-19 20:23:29 -07:00
parent 50fb9e6fd4
commit 575e87eb36
3 changed files with 80 additions and 36 deletions

View file

@ -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<String>) : 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) {
//

View file

@ -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<ScriptCompiler2.BuildScriptInfo>()
val pending = arrayListOf<String>()
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) {

View file

@ -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<Project> {
fun compileBuildFiles(args: Args): List<Project> {
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<Project> {
val result = arrayListOf<Project>()
buildFiles.forEach { buildFile ->
val pluginUrls = findPlugInUrls(buildFile)