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

Refactor parameter extraction.

This commit is contained in:
Cedric Beust 2017-04-03 10:00:18 -07:00
parent bb2eb19876
commit 0f7bb021c3

View file

@ -22,12 +22,6 @@ class GetDependencyGraphHandler : WebSocketListener {
// so I have to do dependency injections manually :-( // so I have to do dependency injections manually :-(
val projectFinder = Kobalt.INJECTOR.getInstance(ProjectFinder::class.java) val projectFinder = Kobalt.INJECTOR.getInstance(ProjectFinder::class.java)
// URL parameters sent by the client
val PARAMETER_PROJECT_ROOT = "projectRoot"
val PARAMETER_BUILD_FILE = "buildFile" // Deprecated
val PARAMETER_PROFILES = "profiles"
val PARAMETER_DOWNLOAD_SOURCES = "downloadSources"
var session: Session? = null var session: Session? = null
override fun onWebSocketClose(code: Int, reason: String?) { override fun onWebSocketClose(code: Int, reason: String?) {
@ -46,30 +40,39 @@ class GetDependencyGraphHandler : WebSocketListener {
endpoint.sendString(json) endpoint.sendString(json)
} }
private fun findProfiles(map: Map<String, List<String>>) = map[PARAMETER_PROFILES]?.getOrNull(0) /**
private fun findDownloadSources(map: Map<String, List<String>>) = map[PARAMETER_DOWNLOAD_SOURCES] * Convenience class to extract the parameters from the WebSocket connection.
?.getOrNull(0)?.toBoolean() ?: false */
class ParameterExtractor(val map: Map<String, List<String>>) {
// URL parameters sent by the client
private val PARAMETER_PROJECT_ROOT = "projectRoot"
private val PARAMETER_BUILD_FILE = "buildFile" // Deprecated
private val PARAMETER_PROFILES = "profiles"
private val PARAMETER_DOWNLOAD_SOURCES = "downloadSources"
private fun findBuildFile(map: Map<String, List<String>>) : BuildSources? { val profiles = map[PARAMETER_PROFILES]?.getOrNull(0)
val downloadSources = map[PARAMETER_DOWNLOAD_SOURCES]?.getOrNull(0)?.toBoolean() ?: false
val projectRoot = map[PARAMETER_PROJECT_ROOT] val projectRoot = map[PARAMETER_PROJECT_ROOT]
val buildFile = map[PARAMETER_BUILD_FILE] val buildFile: BuildSources?
val result = get() {
if (projectRoot != null) { val bf = map[PARAMETER_BUILD_FILE]
return if (projectRoot != null) {
BuildSources(File(projectRoot[0])) BuildSources(File(projectRoot[0]))
} else if (buildFile != null) { } else if (bf != null) {
BuildSources(File(buildFile[0])) BuildSources(File(bf[0]))
} else { } else {
null null
} }
return result }
} }
override fun onWebSocketConnect(s: Session) { override fun onWebSocketConnect(s: Session) {
session = s session = s
val parameterMap = s.upgradeRequest.parameterMap val parameterMap = s.upgradeRequest.parameterMap
val buildSources = findBuildFile(parameterMap) val parameters = ParameterExtractor(parameterMap)
val profiles = findProfiles(parameterMap) val buildSources = parameters.buildFile
val downloadSources = findDownloadSources(s.upgradeRequest.parameterMap) val profiles = parameters.profiles
val downloadSources = parameters.downloadSources
fun <T> getInstance(cls: Class<T>) : T = Kobalt.INJECTOR.getInstance(cls) fun <T> getInstance(cls: Class<T>) : T = Kobalt.INJECTOR.getInstance(cls)