diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt index 52c1a1f0..0808014e 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt @@ -128,5 +128,9 @@ class Kobalt { fun addBuildSourceDirs(dirs: Array) { buildSourceDirs.addAll(dirs) } + + fun cleanUp() { + buildSourceDirs.clear() + } } } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/BlockExtractor.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/BlockExtractor.kt index 1edb66b0..a854156a 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/BlockExtractor.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/BlockExtractor.kt @@ -20,6 +20,8 @@ class BuildScriptInfo(val file: File, val fullBuildFile: List, val secti val includedBuildSourceDirs = arrayListOf() + fun addBuildSourceDir(dir: IncludedBuildSourceDir) = includedBuildSourceDirs.add(dir) + fun includedBuildSourceDirsForLine(line: Int): List { val result = includedBuildSourceDirs.find { it.line == line }?.dirs return result ?: emptyList() diff --git a/src/main/kotlin/com/beust/kobalt/Options.kt b/src/main/kotlin/com/beust/kobalt/Options.kt index b65e20e6..16166e11 100644 --- a/src/main/kotlin/com/beust/kobalt/Options.kt +++ b/src/main/kotlin/com/beust/kobalt/Options.kt @@ -46,7 +46,8 @@ class Options @Inject constructor( val buildSources = if (p.isDirectory) BuildSources(p.absoluteFile) else SingleFileBuildSources(p) var pluginClassLoader = javaClass.classLoader - val allProjects = projectFinder.initForBuildFile(buildSources, args) + val allProjectResult = projectFinder.initForBuildFile(buildSources, args) + val allProjects = allProjectResult.projects // Modify `args` with options found in buildScript { kobaltOptions(...) }, if any addOptionsFromBuild(args, Kobalt.optionsFromBuild) @@ -139,6 +140,7 @@ class Options @Inject constructor( private fun cleanUp() { pluginInfo.cleanUp() taskManager.cleanUp() + Kobalt.cleanUp() } private fun addOptionsFromBuild(args: Args, optionsFromBuild: ArrayList) { diff --git a/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt b/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt index 89250464..a8b427f9 100644 --- a/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt @@ -76,7 +76,7 @@ class BuildFileCompiler @Inject constructor(@Assisted("buildSources") val buildS } class FindProjectResult(val context: KobaltContext, val projects: List, val pluginUrls: List, - val taskResult: TaskResult) + val buildSourceDirectories: List, val taskResult: TaskResult) private fun findProjects(context: KobaltContext): FindProjectResult { val root = buildSources.root @@ -96,7 +96,8 @@ class BuildFileCompiler @Inject constructor(@Assisted("buildSources") val buildS // and possibly add new source build directories. The output of this process is a new Build.kt // file that contains the aggregation of all the build files with the profiles applied and with // the included build files inserted at the correct line. - val newBuildKt = buildFiles.parseBuildFiles(root.absolutePath, context) + val parseResult = buildFiles.parseBuildFiles(root.absolutePath, context) + val newBuildKt = parseResult.buildKt // // Save the current build script absolute directory @@ -128,7 +129,7 @@ class BuildFileCompiler @Inject constructor(@Assisted("buildSources") val buildS // Clear the absolute dir context.internalContext.absoluteDir = null - return FindProjectResult(context, projects, pluginUrls, + return FindProjectResult(context, projects, pluginUrls, parseResult.buildSourceDirectories, if (errorTaskResult != null) errorTaskResult else TaskResult()) } diff --git a/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt b/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt index 2d185f31..eb10fedd 100644 --- a/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt +++ b/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt @@ -40,13 +40,16 @@ class BuildFiles @Inject constructor(val factory: BuildFileCompiler.IFactory, class BuildFileWithBuildScript(val file: File, val buildScriptInfo: BuildScriptInfo) + class BuildFileParseResult(val buildKt: File, val buildSourceDirectories: List) + /** * @return the new Build.kt */ - fun parseBuildFiles(projectDir: String, context: KobaltContext) : File { + fun parseBuildFiles(projectDir: String, context: KobaltContext) : BuildFileParseResult { val profiles = Profiles(context) val bsiMap = hashMapOf() val newSourceDirs = arrayListOf() + // // Create a map of File -> FileWithBuildScript // @@ -94,7 +97,7 @@ class BuildFiles @Inject constructor(val factory: BuildFileCompiler.IFactory, // We're inside a buildScriptInfo section, see if it includes any buildSourceDirs // and if it does, include these build files here // - val isd = bsi.includedBuildSourceDirsForLine(lineNumber) + val isd = bsi.includedBuildSourceDirsForLine(lineNumber) log(2, " Skipping buildScript{} line $lineNumber from file $file") if (isd.any()) { // If we found any new buildSourceDirs, all all the files found in these directories @@ -111,7 +114,7 @@ class BuildFiles @Inject constructor(val factory: BuildFileCompiler.IFactory, // // Create the big Build.kt out of the imports and code we've found so far // - with(File(KFiles.findBuildScriptDir(projectDir), "Build.kt")) { + val newBuildFile = with(File(KFiles.findBuildScriptDir(projectDir), "Build.kt")) { parentFile.mkdirs() val imp = arrayListOf().apply { addAll(imports) @@ -119,9 +122,11 @@ class BuildFiles @Inject constructor(val factory: BuildFileCompiler.IFactory, Collections.sort(imp) writeText(imp.joinToString("\n")) appendText(code.joinToString("\n")) - - return this + this } + + val newDirs = listOf(projectDir) + newSourceDirs.flatMap{ it.dirs } + return BuildFileParseResult(newBuildFile, newDirs) } class SplitBuildFile(val imports: List, val code: List, val containsProfiles: Boolean) @@ -205,7 +210,7 @@ class BuildFiles @Inject constructor(val factory: BuildFileCompiler.IFactory, val newDirs = arrayListOf().apply { addAll(Kobalt.buildSourceDirs) } newDirs.removeAll(currentDirs) if (newDirs.any()) { - buildScriptInfo.includedBuildSourceDirs.add(IncludedBuildSourceDir(section.start, newDirs)) + buildScriptInfo.addBuildSourceDir(IncludedBuildSourceDir(section.start, newDirs)) } } diff --git a/src/main/kotlin/com/beust/kobalt/app/ProjectFinder.kt b/src/main/kotlin/com/beust/kobalt/app/ProjectFinder.kt index c4449284..30dc0f9e 100644 --- a/src/main/kotlin/com/beust/kobalt/app/ProjectFinder.kt +++ b/src/main/kotlin/com/beust/kobalt/app/ProjectFinder.kt @@ -7,7 +7,6 @@ import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.Project import com.beust.kobalt.internal.PluginInfo -import com.beust.kobalt.internal.build.BuildSources import com.beust.kobalt.internal.build.IBuildSources import com.beust.kobalt.misc.kobaltLog import com.google.inject.Inject @@ -16,7 +15,7 @@ import java.util.* class ProjectFinder @Inject constructor(val buildFileCompilerFactory: BuildFileCompiler.IFactory, val pluginInfo: PluginInfo, val plugins: Plugins) { - fun initForBuildFile(buildSources: IBuildSources, args: Args): List { + fun initForBuildFile(buildSources: IBuildSources, args: Args): BuildFileCompiler.FindProjectResult { val findProjectResult = buildFileCompilerFactory.create(buildSources, pluginInfo) .compileBuildFiles(args) if (! findProjectResult.taskResult.success) { @@ -49,7 +48,7 @@ class ProjectFinder @Inject constructor(val buildFileCompilerFactory: BuildFileC // plugins.applyPlugins(Kobalt.context!!, allProjects) - return allProjects + return findProjectResult } private fun runClasspathInterceptors(allProjects: List) { 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 d6a47912..f8a3b278 100644 --- a/src/main/kotlin/com/beust/kobalt/app/remote/GetDependencyGraphHandler.kt +++ b/src/main/kotlin/com/beust/kobalt/app/remote/GetDependencyGraphHandler.kt @@ -6,7 +6,6 @@ import com.beust.kobalt.app.ProjectFinder import com.beust.kobalt.internal.build.BuildSources 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 @@ -14,7 +13,6 @@ import org.eclipse.jetty.websocket.api.RemoteEndpoint import org.eclipse.jetty.websocket.api.Session import org.eclipse.jetty.websocket.api.WebSocketListener import java.io.File -import java.nio.file.Paths /** * Manage the websocket endpoint "/v1/getDependencyGraph". @@ -24,8 +22,9 @@ class GetDependencyGraphHandler : WebSocketListener { // so I have to do dependency injections manually :-( val projectFinder = Kobalt.INJECTOR.getInstance(ProjectFinder::class.java) + // URL parameters sent by the client val PARAMETER_PROJECT_ROOT = "projectRoot" - val PARAMETER_BUILD_FILE = "buildFile" + val PARAMETER_BUILD_FILE = "buildFile" // Deprecated val PARAMETER_PROFILES = "profiles" var session: Session? = null @@ -63,8 +62,9 @@ class GetDependencyGraphHandler : WebSocketListener { override fun onWebSocketConnect(s: Session) { session = s - val buildSources = findBuildFile(s.upgradeRequest.parameterMap) - val profiles = findProfiles(s.upgradeRequest.parameterMap) + val parameterMap = s.upgradeRequest.parameterMap + val buildSources = findBuildFile(parameterMap) + val profiles = findProfiles(parameterMap) fun getInstance(cls: Class) : T = Kobalt.INJECTOR.getInstance(cls) @@ -88,6 +88,7 @@ class GetDependencyGraphHandler : WebSocketListener { try { val dependencyData = getInstance(RemoteDependencyData::class.java) val args = getInstance(Args::class.java) + args.buildFile = buildSources.root.absolutePath args.profiles = profiles val allProjects = projectFinder.initForBuildFile(buildSources, args)