From 043fc31c9f9b1071f37f507691161168603cf3bc Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sun, 26 Mar 2017 07:32:56 -0700 Subject: [PATCH] /v1/getDependencyGraph?projectRoot=... Deprecated buildFile=. --- .../kotlin/com/beust/kobalt/misc/KFiles.kt | 13 ++++++ src/main/kotlin/com/beust/kobalt/Main.kt | 15 +------ .../app/remote/GetDependencyGraphHandler.kt | 40 ++++++++++++++----- .../beust/kobalt/app/remote/KobaltClient.kt | 5 ++- 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt index 152dbc31..e5a1d37d 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt @@ -386,6 +386,19 @@ class KFiles { return false } } + + fun findBuildFile(projectRoot: String = "."): File { + val deprecatedLocation = File(Constants.BUILD_FILE_NAME) + val result: File = + if (deprecatedLocation.exists()) { + warn(Constants.BUILD_FILE_NAME + " is in a deprecated location, please move it to " + + Constants.BUILD_FILE_DIRECTORY) + deprecatedLocation + } else { + File(KFiles.joinDir(projectRoot, Constants.BUILD_FILE_DIRECTORY, Constants.BUILD_FILE_NAME)) + } + return result + } } fun findRecursively(directory: File, function: Function1): List { diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index f2d44f0f..063e9f7c 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -126,7 +126,7 @@ private class Main @Inject constructor( // val md5 = Md5.toMd5(file) // val md52 = MessageDigest.getInstance("MD5").digest(file.readBytes()).toHexString() var result = 0 - val p = if (args.buildFile != null) File(args.buildFile) else findBuildFile() + val p = if (args.buildFile != null) File(args.buildFile) else KFiles.findBuildFile() args.buildFile = p.absolutePath val buildFile = BuildFile(Paths.get(p.absolutePath), p.name) @@ -219,19 +219,6 @@ private class Main @Inject constructor( } } - private fun findBuildFile(): File { - val deprecatedLocation = File(Constants.BUILD_FILE_NAME) - val result: File = - if (deprecatedLocation.exists()) { - warn(Constants.BUILD_FILE_NAME + " is in a deprecated location, please move it to " - + Constants.BUILD_FILE_DIRECTORY) - deprecatedLocation - } else { - File(KFiles.joinDir(Constants.BUILD_FILE_DIRECTORY, Constants.BUILD_FILE_NAME)) - } - return result - } - private fun cleanUp() { pluginInfo.cleanUp() taskManager.cleanUp() diff --git a/src/main/kotlin/com/beust/kobalt/app/remote/GetDependencyGraphHandler.kt b/src/main/kotlin/com/beust/kobalt/app/remote/GetDependencyGraphHandler.kt index fb8c2f1b..8c795154 100644 --- a/src/main/kotlin/com/beust/kobalt/app/remote/GetDependencyGraphHandler.kt +++ b/src/main/kotlin/com/beust/kobalt/app/remote/GetDependencyGraphHandler.kt @@ -6,6 +6,7 @@ import com.beust.kobalt.app.ProjectFinder import com.beust.kobalt.internal.build.BuildFile import com.beust.kobalt.internal.eventbus.ArtifactDownloadedEvent import com.beust.kobalt.maven.aether.Exceptions +import com.beust.kobalt.misc.KFiles import com.google.common.eventbus.EventBus import com.google.common.eventbus.Subscribe import com.google.gson.Gson @@ -22,6 +23,9 @@ class GetDependencyGraphHandler : WebSocketListener { // so I have to do dependency injections manually :-( val projectFinder = Kobalt.INJECTOR.getInstance(ProjectFinder::class.java) + val PARAMETER_PROJECT_ROOT = "projectRoot" + val PARAMETER_BUILD_FILE = "buildFile" + var session: Session? = null override fun onWebSocketClose(code: Int, reason: String?) { @@ -39,15 +43,29 @@ class GetDependencyGraphHandler : WebSocketListener { errorMessage = errorMessage))) } + private fun findBuildFile(map: Map>) : String? { + val projectRoot = map[PARAMETER_PROJECT_ROOT] + val buildFile = map[PARAMETER_BUILD_FILE] + val result = + if (projectRoot != null) { + KFiles.findBuildFile(projectRoot[0]).absolutePath + } else if (buildFile != null) { + buildFile[0] + } else { + null + } + return result + } + override fun onWebSocketConnect(s: Session) { session = s - val buildFileParams = s.upgradeRequest.parameterMap["buildFile"] - if (buildFileParams != null) { - val buildFile = buildFileParams[0] + val buildFile = findBuildFile(s.upgradeRequest.parameterMap) - fun getInstance(cls: Class) : T = Kobalt.INJECTOR.getInstance(cls) + fun getInstance(cls: Class) : T = Kobalt.INJECTOR.getInstance(cls) - val result = if (buildFile != null) { + // Parse the request + val result = + if (buildFile != null) { // Track all the downloads that this dependency call might trigger and // send them as a progress message to the web socket val eventBus = getInstance(EventBus::class.java) @@ -76,8 +94,7 @@ class GetDependencyGraphHandler : WebSocketListener { }, useGraph = true) } catch(ex: Throwable) { Exceptions.printStackTrace(ex) - val errorMessage = ex.message - RemoteDependencyData.GetDependenciesData(errorMessage = errorMessage) + RemoteDependencyData.GetDependenciesData(errorMessage = ex.message) } finally { SparkServer.cleanUpCallback() eventBus.unregister(busListener) @@ -86,10 +103,11 @@ class GetDependencyGraphHandler : WebSocketListener { RemoteDependencyData.GetDependenciesData( errorMessage = "buildFile wasn't passed in the query parameter") } - sendWebsocketCommand(s.remote, RemoteDependencyData.GetDependenciesData.NAME, result, - errorMessage = result.errorMessage) - s.close() - } + + // Respond to the request + sendWebsocketCommand(s.remote, RemoteDependencyData.GetDependenciesData.NAME, result, + errorMessage = result.errorMessage) + s.close() } override fun onWebSocketText(message: String?) { diff --git a/src/main/kotlin/com/beust/kobalt/app/remote/KobaltClient.kt b/src/main/kotlin/com/beust/kobalt/app/remote/KobaltClient.kt index 0a969c1d..a7c0a312 100644 --- a/src/main/kotlin/com/beust/kobalt/app/remote/KobaltClient.kt +++ b/src/main/kotlin/com/beust/kobalt/app/remote/KobaltClient.kt @@ -32,10 +32,11 @@ class KobaltClient : Runnable { val client = OkHttpClient() val port = KobaltServer.port ?: 1240 val url = "ws://localhost:$port/v1/getDependencyGraph" - val buildFile = KFiles.fixSlashes(homeDir("kotlin/kobalt/kobalt/src/Build.kt")) + val buildFile = KFiles.fixSlashes(homeDir("java/testng/kobalt/src/Build.kt")) + val projectRoot = KFiles.fixSlashes(homeDir("java/testng")) val request = Request.Builder() // .url("ws://echo.websocket.org") - .url("$url?buildFile=$buildFile") + .url("$url?projectRoot=$projectRoot&buildFile=$buildFile") .build() var webSocket: WebSocket? = null val ws = WebSocketCall.create(client, request).enqueue(object: WebSocketListener {