mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Refactoring the server to use commands.
This commit is contained in:
parent
50fb9e6fd4
commit
575e87eb36
3 changed files with 80 additions and 36 deletions
|
@ -3,6 +3,7 @@ package com.beust.kobalt
|
||||||
import com.beust.jcommander.JCommander
|
import com.beust.jcommander.JCommander
|
||||||
import com.beust.kobalt.api.Kobalt
|
import com.beust.kobalt.api.Kobalt
|
||||||
import com.beust.kobalt.api.KobaltContext
|
import com.beust.kobalt.api.KobaltContext
|
||||||
|
import com.beust.kobalt.api.Project
|
||||||
import com.beust.kobalt.internal.*
|
import com.beust.kobalt.internal.*
|
||||||
import com.beust.kobalt.kotlin.BuildFile
|
import com.beust.kobalt.kotlin.BuildFile
|
||||||
import com.beust.kobalt.maven.*
|
import com.beust.kobalt.maven.*
|
||||||
|
@ -37,7 +38,7 @@ public fun mainNoExit(argv: Array<String>) : Int {
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Main @Inject constructor(
|
private class Main @Inject constructor(
|
||||||
val script2: ScriptCompiler2.IFactory,
|
val buildFileCompilerFactory: ScriptCompiler2.IFactory,
|
||||||
val plugins: Plugins,
|
val plugins: Plugins,
|
||||||
val taskManager: TaskManager,
|
val taskManager: TaskManager,
|
||||||
val http: Http,
|
val http: Http,
|
||||||
|
@ -122,38 +123,13 @@ private class Main @Inject constructor(
|
||||||
ProjectGenerator().run(args)
|
ProjectGenerator().run(args)
|
||||||
} else if (args.usage) {
|
} else if (args.usage) {
|
||||||
jc.usage()
|
jc.usage()
|
||||||
|
} else if (args.serverMode) {
|
||||||
|
server.run()
|
||||||
} else {
|
} else {
|
||||||
if (! buildFile.exists()) {
|
if (! buildFile.exists()) {
|
||||||
jc.usage()
|
jc.usage()
|
||||||
} else {
|
} else {
|
||||||
val context = KobaltContext(args)
|
val allProjects = buildFileCompilerFactory.create(listOf(buildFile)).compileBuildFiles(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)
|
|
||||||
|
|
||||||
if (args.tasks) {
|
if (args.tasks) {
|
||||||
//
|
//
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package com.beust.kobalt.internal
|
package com.beust.kobalt.internal
|
||||||
|
|
||||||
|
import com.beust.klaxon.json
|
||||||
import com.beust.kobalt.Args
|
import com.beust.kobalt.Args
|
||||||
import com.beust.kobalt.api.Project
|
import com.beust.kobalt.api.Project
|
||||||
|
import com.beust.kobalt.kotlin.BuildFile
|
||||||
import com.beust.kobalt.kotlin.ScriptCompiler2
|
import com.beust.kobalt.kotlin.ScriptCompiler2
|
||||||
import com.beust.kobalt.maven.IClasspathDependency
|
import com.beust.kobalt.maven.IClasspathDependency
|
||||||
import com.beust.kobalt.maven.MavenDependency
|
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.KobaltExecutors
|
||||||
import com.beust.kobalt.misc.log
|
import com.beust.kobalt.misc.log
|
||||||
import com.google.inject.Inject
|
import com.google.inject.Inject
|
||||||
|
import com.google.inject.assistedinject.Assisted
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
import java.io.InputStreamReader
|
import java.io.InputStreamReader
|
||||||
import java.io.PrintWriter
|
import java.io.PrintWriter
|
||||||
import java.net.ServerSocket
|
import java.net.ServerSocket
|
||||||
|
import java.nio.file.Paths
|
||||||
import java.util.concurrent.Executor
|
import java.util.concurrent.Executor
|
||||||
import java.util.concurrent.ExecutorService
|
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
|
var outgoing: PrintWriter? = null
|
||||||
val pending = arrayListOf<ScriptCompiler2.BuildScriptInfo>()
|
val pending = arrayListOf<String>()
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
val portNumber = Args.DEFAULT_SERVER_PORT
|
val portNumber = args.port
|
||||||
|
|
||||||
log(1, "Starting on port $portNumber")
|
log(1, "Starting on port $portNumber")
|
||||||
val serverSocket = ServerSocket(portNumber)
|
val serverSocket = ServerSocket(portNumber)
|
||||||
|
@ -38,16 +43,52 @@ public class KobaltServer @Inject constructor(val executors: KobaltExecutors) :
|
||||||
var inputLine = ins.readLine()
|
var inputLine = ins.readLine()
|
||||||
while (inputLine != null) {
|
while (inputLine != null) {
|
||||||
log(1, "Received $inputLine")
|
log(1, "Received $inputLine")
|
||||||
|
val command = getCommand(inputLine)
|
||||||
|
if (command != null) {
|
||||||
|
command!!.run()
|
||||||
|
}
|
||||||
if (inputLine.equals("Bye."))
|
if (inputLine.equals("Bye."))
|
||||||
break;
|
break;
|
||||||
inputLine = ins.readLine()
|
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) {
|
if (outgoing != null) {
|
||||||
val json = toJson(info, executors.miscExecutor)
|
// val json = toJson(info, executors.miscExecutor)
|
||||||
outgoing!!.println(json)
|
outgoing!!.println(info)
|
||||||
} else {
|
} else {
|
||||||
log(1, "Queuing $info")
|
log(1, "Queuing $info")
|
||||||
synchronized(pending) {
|
synchronized(pending) {
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package com.beust.kobalt.kotlin
|
package com.beust.kobalt.kotlin
|
||||||
|
|
||||||
|
import com.beust.kobalt.Args
|
||||||
import com.beust.kobalt.Plugins
|
import com.beust.kobalt.Plugins
|
||||||
import com.beust.kobalt.api.Kobalt
|
import com.beust.kobalt.api.Kobalt
|
||||||
|
import com.beust.kobalt.api.KobaltContext
|
||||||
import com.beust.kobalt.api.Plugin
|
import com.beust.kobalt.api.Plugin
|
||||||
import com.beust.kobalt.api.Project
|
import com.beust.kobalt.api.Project
|
||||||
import com.beust.kobalt.api.annotation.Task
|
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"
|
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>()
|
val result = arrayListOf<Project>()
|
||||||
buildFiles.forEach { buildFile ->
|
buildFiles.forEach { buildFile ->
|
||||||
val pluginUrls = findPlugInUrls(buildFile)
|
val pluginUrls = findPlugInUrls(buildFile)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue