diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt index 0f594fcd..e61cdb34 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt @@ -31,6 +31,10 @@ class BuildScriptConfig { @Directive fun kobaltOptions(vararg options: String) = Kobalt.addKobaltOptions(options) + /** Where to find additional build files */ + @Directive + fun buildSourceDirs(vararg dirs: String) = Kobalt.addBuildSourceDirs(dirs) + // The following settings modify the compiler used to compile the build file, which regular users should // probably never need to do. Projects should use kotlinCompiler { compilerVersion } to configure the // Kotin compiler for their source files. @@ -127,4 +131,3 @@ fun localMaven() : String { } return result.toURI().toString() } - 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 2c381b1d..52c1a1f0 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 @@ -120,9 +120,13 @@ class Kobalt { fun findPlugin(name: String) = Plugins.findPlugin(name) val optionsFromBuild = arrayListOf() - fun addKobaltOptions(options: Array) { optionsFromBuild.addAll(options) } + + val buildSourceDirs = arrayListOf() + fun addBuildSourceDirs(dirs: Array) { + buildSourceDirs.addAll(dirs) + } } } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/build/BuildSources.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/build/BuildSources.kt index 63a8d5be..3ab4ceea 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/build/BuildSources.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/build/BuildSources.kt @@ -1,5 +1,6 @@ package com.beust.kobalt.internal.build +import com.beust.kobalt.api.Kobalt import com.beust.kobalt.homeDir import java.io.File import java.nio.file.* @@ -22,31 +23,33 @@ class BuildSources(val file: File) : IBuildSources { override val root = file override fun findSourceFiles() : List { - return listOf(/* "kobalt/src/a.kt", */ "kobalt/src/Build.kt") - .map(::File) -// .map { BuildFile(Paths.get(it), it)} + val result = arrayListOf("kobalt/src/Build.kt") + if (Kobalt.buildSourceDirs.isNotEmpty()) result.addAll(findBuildFiles(Kobalt.buildSourceDirs)) + + return result.map(::File) } override fun exists() = findSourceFiles().isNotEmpty() - override fun toString() = "{BuildSources " + findSourceFiles()[0] + "...}" + override fun toString() = "{BuildSources " + findSourceFiles().joinToString(", ") + "}" - fun _findSourceFiles() : List { - val result = arrayListOf() - Files.walkFileTree(Paths.get(file.absolutePath), object : SimpleFileVisitor() { - override fun preVisitDirectory(dir: Path?, attrs: BasicFileAttributes?): FileVisitResult { - if (dir != null) { - val path = dir.toFile() - println(path.name) - if (path.name == "src" && path.parentFile.name == "kobalt") { - val sources = path.listFiles().filter { it.name.endsWith(".kt")} - result.addAll(sources) + fun findBuildFiles(roots: List) : List { + val result = arrayListOf() + roots.forEach { file -> + Files.walkFileTree(Paths.get(file), object : SimpleFileVisitor() { + override fun preVisitDirectory(dir: Path?, attrs: BasicFileAttributes?): FileVisitResult { + if (dir != null) { + val path = dir.toFile() + if (path.name == "src" && path.parentFile.name == "kobalt") { + val sources = path.listFiles().filter { it.name.endsWith(".kt") }.map { it.path } + result.addAll(sources) + } } - } - return FileVisitResult.CONTINUE - } - }) + return FileVisitResult.CONTINUE + } + }) + } return result } } diff --git a/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt b/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt index 3c88892d..4d53fac9 100644 --- a/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt @@ -86,6 +86,8 @@ class BuildFileCompiler @Inject constructor(@Assisted("buildSources") val buildS val projects = arrayListOf() run { // buildFiles.forEach { buildFile -> + + // Parse kobalt/src/Build.kt val parsedBuildFile = parseBuildFile(context, buildSources) parsedBuildFiles.add(parsedBuildFile) val pluginUrls = parsedBuildFile.pluginUrls @@ -105,13 +107,19 @@ class BuildFileCompiler @Inject constructor(@Assisted("buildSources") val buildS } } + // + // Now that Build.kt has been parsed, we might have additional build files (buildSources will + // return additional build files) so we parse again. + // + val newParsedBuildFile = parseBuildFile(context, buildSources) + // Write the modified Build.kt (e.g. maybe profiles were applied) to a temporary file, // compile it, jar it in buildScript.jar and run it val modifiedBuildFile = KFiles.createTempBuildFileInTempDirectory(deleteOnExit = true) - KFiles.saveFile(modifiedBuildFile, parsedBuildFile.nonBuildScriptCode) + KFiles.saveFile(modifiedBuildFile, newParsedBuildFile.nonBuildScriptCode) val taskResult = maybeCompileBuildFile(context, listOf(modifiedBuildFile.path), buildScriptJarFile, pluginUrls, context.internalContext.forceRecompile, - parsedBuildFile.containsProfiles) + newParsedBuildFile.containsProfiles) if (taskResult.success) { projects.addAll(buildScriptUtil.runBuildScriptJarFile(buildScriptJarFile, pluginUrls, context)) } else {