mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-27 00:38:11 -07:00
Refactor commands.
This commit is contained in:
parent
ba03698f5b
commit
683afdb569
4 changed files with 56 additions and 25 deletions
|
@ -55,21 +55,18 @@ class GetDependenciesCommand @Inject constructor(val executors: KobaltExecutors,
|
||||||
projects.add(ProjectData(project.name!!, allDependencies))
|
projects.add(ProjectData(project.name!!, allDependencies))
|
||||||
}
|
}
|
||||||
log(1, "Returning BuildScriptInfo")
|
log(1, "Returning BuildScriptInfo")
|
||||||
val result = Gson().toJson(GetDependenciesData(projects).toData())
|
val result = toCommandDataJson(Gson().toJson(GetDependenciesData(projects)))
|
||||||
log(2, " $result")
|
log(2, " $result")
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/////
|
||||||
|
// The JSON payloads that this command uses
|
||||||
|
//
|
||||||
|
|
||||||
|
class DependencyData(val id: String, val scope: String, val path: String)
|
||||||
|
|
||||||
|
class ProjectData( val name: String, val dependencies: List<DependencyData>)
|
||||||
|
|
||||||
|
class GetDependenciesData(val projects: List<ProjectData>)
|
||||||
}
|
}
|
||||||
|
|
||||||
class DependencyData(val id: String, val scope: String, val path: String)
|
|
||||||
|
|
||||||
class ProjectData( val name: String, val dependencies: List<DependencyData>)
|
|
||||||
|
|
||||||
class GetDependenciesData(val projects: List<ProjectData>) {
|
|
||||||
fun toData() : CommandData {
|
|
||||||
val data = Gson().toJson(this)
|
|
||||||
return CommandData("getDependencies", data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ public class KobaltClient @Inject constructor() : Runnable {
|
||||||
done = true
|
done = true
|
||||||
} else {
|
} else {
|
||||||
val data = jo.get("data").asString
|
val data = jo.get("data").asString
|
||||||
val dd = Gson().fromJson(data, GetDependenciesData::class.java)
|
val dd = Gson().fromJson(data, GetDependenciesCommand.GetDependenciesData::class.java)
|
||||||
println("Read GetDependencyData, project count: ${dd.projects.size()}")
|
println("Read GetDependencyData, project count: ${dd.projects.size()}")
|
||||||
line = ins.readLine()
|
line = ins.readLine()
|
||||||
}
|
}
|
||||||
|
@ -59,8 +59,4 @@ public class KobaltClient @Inject constructor() : Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendInfo(info: BuildFileCompiler.BuildScriptInfo) {
|
|
||||||
outgoing!!.println("Sending info with project count: " + info.projects.size())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.beust.kobalt.internal.remote
|
||||||
import com.beust.kobalt.Args
|
import com.beust.kobalt.Args
|
||||||
import com.beust.kobalt.api.Kobalt
|
import com.beust.kobalt.api.Kobalt
|
||||||
import com.beust.kobalt.misc.log
|
import com.beust.kobalt.misc.log
|
||||||
|
import com.google.gson.Gson
|
||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
import com.google.gson.JsonParser
|
import com.google.gson.JsonParser
|
||||||
import com.google.inject.Inject
|
import com.google.inject.Inject
|
||||||
|
@ -13,15 +14,43 @@ import java.io.PrintWriter
|
||||||
import java.net.ServerSocket
|
import java.net.ServerSocket
|
||||||
import java.net.SocketException
|
import java.net.SocketException
|
||||||
|
|
||||||
interface ICommandSender {
|
/**
|
||||||
fun sendData(content: String)
|
* All commands implement this interface.
|
||||||
}
|
*/
|
||||||
|
|
||||||
interface ICommand {
|
interface ICommand {
|
||||||
|
/**
|
||||||
|
* The name of this command.
|
||||||
|
*/
|
||||||
val name: String
|
val name: String
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run this command based on the information received from the server. When done, use
|
||||||
|
* the sender object to send back a response.
|
||||||
|
*/
|
||||||
fun run(sender: ICommandSender, received: JsonObject)
|
fun run(sender: ICommandSender, received: JsonObject)
|
||||||
|
|
||||||
|
fun toCommandDataJson(data: String) = Gson().toJson(CommandData(name, data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Passed to a command in its `run` method so it can send information back to the caller.
|
||||||
|
* @param The string content that will be sent in the "data" field.
|
||||||
|
*/
|
||||||
|
interface ICommandSender {
|
||||||
|
fun sendData(data: String)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The JSON payload that commands exchange follow the following pattern:
|
||||||
|
* {
|
||||||
|
* name: "nameOfTheCommand"
|
||||||
|
* data: a JSON string containing the payload itself
|
||||||
|
* }
|
||||||
|
* This allows commands to be tested for their name first, after which each command can
|
||||||
|
* decode its own specific payload by parsing the JSON in the "data" field and mapping
|
||||||
|
* it into a Kotlin *Data class. The downside of this approach is a double parsing,
|
||||||
|
* but since the data part is parsed as a string first, this is probably not a huge deal.
|
||||||
|
*/
|
||||||
class CommandData(val commandName: String, val data: String)
|
class CommandData(val commandName: String, val data: String)
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.beust.kobalt.internal.remote
|
package com.beust.kobalt.internal.remote
|
||||||
|
|
||||||
|
import com.beust.kobalt.misc.log
|
||||||
|
import com.google.gson.Gson
|
||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,7 +14,14 @@ import com.google.gson.JsonObject
|
||||||
*/
|
*/
|
||||||
class PingCommand() : ICommand {
|
class PingCommand() : ICommand {
|
||||||
override val name = "ping"
|
override val name = "ping"
|
||||||
override fun run(sender: ICommandSender, received: JsonObject) =
|
|
||||||
sender.sendData("{ \"response\" : \"${received.toString()}\"" + " }")
|
override fun run(sender: ICommandSender, received: JsonObject) {
|
||||||
|
val commandData = toCommandDataJson(Gson().toJson(PingData(received.toString())))
|
||||||
|
val result = Gson().toJson(commandData)
|
||||||
|
log(1, "ping returning: $result")
|
||||||
|
sender.sendData(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
class PingData(val received: String)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue