From e4d883890d8d2f0032a422a51436867f9c3f81cb Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Mon, 27 Mar 2017 15:45:25 -0700 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20generate=20preBuildScript.jar?= =?UTF-8?q?=20if=20no=20buildScript{}.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/beust/kobalt/app/ParsedBuildFile.kt | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt b/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt index c6d82e05..64f60c28 100644 --- a/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt +++ b/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt @@ -6,14 +6,10 @@ import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project import com.beust.kobalt.internal.build.BuildFile -import com.beust.kobalt.internal.build.BuildSources import com.beust.kobalt.internal.build.IBuildSources import com.beust.kobalt.internal.build.VersionFile import com.beust.kobalt.maven.DependencyManager -import com.beust.kobalt.misc.BlockExtractor -import com.beust.kobalt.misc.KFiles -import com.beust.kobalt.misc.kobaltLog -import com.beust.kobalt.misc.warn +import com.beust.kobalt.misc.* import com.beust.kobalt.plugin.kotlin.kotlinCompilePrivate import java.io.File import java.net.URL @@ -41,11 +37,13 @@ class ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContex val buildScriptCode : String get() = nonBuildScript.joinToString("\n") init { - parseBuildFile() - initPluginUrls() + val buildScriptInfo = parseBuildFile() + if (buildScriptInfo != null) { + initPluginUrls(buildScriptInfo) + } } - private fun parseBuildFile() { + private fun parseBuildFile() : BuildScriptInfo? { /** * If the current line matches one of the profiles, turn the declaration into * val profile = true, otherwise return the same line @@ -62,7 +60,8 @@ class ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContex return Pair(profile == variable, variable) } - if ((matcher != null && matcher.groups.size > 0) || (oldMatcher != null && oldMatcher.groups.size> 0)) { + if ((matcher != null && matcher.groups.isNotEmpty()) + || (oldMatcher != null && oldMatcher.groups.isNotEmpty())) { containsProfiles = true val match = profileMatch(matcher) val oldMatch = profileMatch(oldMatcher) @@ -94,11 +93,17 @@ class ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContex return result } + // + // Take all the build files and adjust them with the active profiles + // val buildWithCorrectProfiles = arrayListOf() buildSources.findSourceFiles().forEach { buildWithCorrectProfiles.addAll(applyProfiles(it.readLines())) } + // + // Now extract all the `buildScript{}` blocks from all these build files + // val buildScriptInfo = BlockExtractor(Pattern.compile("^val.*buildScript.*\\{"), '{', '}') .extractBlock(buildWithCorrectProfiles) @@ -106,13 +111,14 @@ class ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContex kobaltLog(2, "About to compile build file:\n=====\n" + buildScriptInfo.content + "\n=====") preBuildScript.add(buildScriptInfo.content) } else { + kobaltLog(2, "Didn't find any buildScript{} section, no preBuildScript.jar necessary") repos.forEach { preBuildScript.add(it) } pluginList.forEach { preBuildScript.add(it) } buildFileClasspath.forEach { preBuildScript.add(it) } } // - // Write the build file excluding the nonBuildScript{} tag since we already ran it + // Write the build file excluding the buildScript{} tag since we already ran it // var lineNumber = 1 buildSources.findSourceFiles().forEach { buildFile -> @@ -125,16 +131,18 @@ class ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContex lineNumber++ } } + + return buildScriptInfo } - private fun initPluginUrls() { + private fun initPluginUrls(buildScriptInfo: BuildScriptInfo?) { // // Compile and run preBuildScriptCode, which contains all the plugins() calls extracted. This // will add all the dynamic plugins found in this code to Plugins.dynamicPlugins // - val pluginSourceFile = KFiles.createTempBuildFileInTempDirectory(deleteOnExit = true) - pluginSourceFile.writeText(preBuildScriptCode, Charset.defaultCharset()) - kobaltLog(2, "Saved " + KFiles.fixSlashes(pluginSourceFile.absolutePath)) + val buildScriptSourceFile = KFiles.createTempBuildFileInTempDirectory(deleteOnExit = true) + buildScriptSourceFile.writeText(preBuildScriptCode, Charset.defaultCharset()) + kobaltLog(2, "Saved " + KFiles.fixSlashes(buildScriptSourceFile.absolutePath)) // // Compile to preBuildScript.jar @@ -146,7 +154,7 @@ class ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContex // or not so recompile it every time. // if (! buildScriptUtil.isUpToDate(buildFile, File(buildScriptJar))) { buildScriptJarFile.parentFile.mkdirs() - generateJarFile(context, listOf(pluginSourceFile.path), buildScriptJarFile) + generateJarFile(context, listOf(buildScriptSourceFile.path), buildScriptJarFile) VersionFile.generateVersionFile(buildScriptJarFile.parentFile) Kobalt.context!!.internalContext.buildFileOutOfDate = true // } @@ -185,9 +193,5 @@ class ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContex throw KobaltException("Couldn't compile $org:\n" + result.errorMessage) } } - - private fun generateJarFile(context: KobaltContext, buildSources: BuildSources, - buildScriptJarFile: File) - = generateJarFile(context, buildSources.findSourceFiles().map { it.path }, buildScriptJarFile) }