1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 16:28:12 -07:00

Don’t generate preBuildScript.jar if no buildScript{}.

This commit is contained in:
Cedric Beust 2017-03-27 15:45:25 -07:00
parent 76fdd1543a
commit e4d883890d

View file

@ -6,14 +6,10 @@ import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.internal.build.BuildFile 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.IBuildSources
import com.beust.kobalt.internal.build.VersionFile import com.beust.kobalt.internal.build.VersionFile
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.misc.BlockExtractor import com.beust.kobalt.misc.*
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.kobaltLog
import com.beust.kobalt.misc.warn
import com.beust.kobalt.plugin.kotlin.kotlinCompilePrivate import com.beust.kobalt.plugin.kotlin.kotlinCompilePrivate
import java.io.File import java.io.File
import java.net.URL import java.net.URL
@ -41,11 +37,13 @@ class ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContex
val buildScriptCode : String get() = nonBuildScript.joinToString("\n") val buildScriptCode : String get() = nonBuildScript.joinToString("\n")
init { init {
parseBuildFile() val buildScriptInfo = parseBuildFile()
initPluginUrls() 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 * If the current line matches one of the profiles, turn the declaration into
* val profile = true, otherwise return the same line * 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) 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 containsProfiles = true
val match = profileMatch(matcher) val match = profileMatch(matcher)
val oldMatch = profileMatch(oldMatcher) val oldMatch = profileMatch(oldMatcher)
@ -94,11 +93,17 @@ class ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContex
return result return result
} }
//
// Take all the build files and adjust them with the active profiles
//
val buildWithCorrectProfiles = arrayListOf<String>() val buildWithCorrectProfiles = arrayListOf<String>()
buildSources.findSourceFiles().forEach { buildSources.findSourceFiles().forEach {
buildWithCorrectProfiles.addAll(applyProfiles(it.readLines())) buildWithCorrectProfiles.addAll(applyProfiles(it.readLines()))
} }
//
// Now extract all the `buildScript{}` blocks from all these build files
//
val buildScriptInfo = BlockExtractor(Pattern.compile("^val.*buildScript.*\\{"), '{', '}') val buildScriptInfo = BlockExtractor(Pattern.compile("^val.*buildScript.*\\{"), '{', '}')
.extractBlock(buildWithCorrectProfiles) .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=====") kobaltLog(2, "About to compile build file:\n=====\n" + buildScriptInfo.content + "\n=====")
preBuildScript.add(buildScriptInfo.content) preBuildScript.add(buildScriptInfo.content)
} else { } else {
kobaltLog(2, "Didn't find any buildScript{} section, no preBuildScript.jar necessary")
repos.forEach { preBuildScript.add(it) } repos.forEach { preBuildScript.add(it) }
pluginList.forEach { preBuildScript.add(it) } pluginList.forEach { preBuildScript.add(it) }
buildFileClasspath.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 var lineNumber = 1
buildSources.findSourceFiles().forEach { buildFile -> buildSources.findSourceFiles().forEach { buildFile ->
@ -125,16 +131,18 @@ class ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContex
lineNumber++ lineNumber++
} }
} }
return buildScriptInfo
} }
private fun initPluginUrls() { private fun initPluginUrls(buildScriptInfo: BuildScriptInfo?) {
// //
// Compile and run preBuildScriptCode, which contains all the plugins() calls extracted. This // 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 // will add all the dynamic plugins found in this code to Plugins.dynamicPlugins
// //
val pluginSourceFile = KFiles.createTempBuildFileInTempDirectory(deleteOnExit = true) val buildScriptSourceFile = KFiles.createTempBuildFileInTempDirectory(deleteOnExit = true)
pluginSourceFile.writeText(preBuildScriptCode, Charset.defaultCharset()) buildScriptSourceFile.writeText(preBuildScriptCode, Charset.defaultCharset())
kobaltLog(2, "Saved " + KFiles.fixSlashes(pluginSourceFile.absolutePath)) kobaltLog(2, "Saved " + KFiles.fixSlashes(buildScriptSourceFile.absolutePath))
// //
// Compile to preBuildScript.jar // Compile to preBuildScript.jar
@ -146,7 +154,7 @@ class ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContex
// or not so recompile it every time. // or not so recompile it every time.
// if (! buildScriptUtil.isUpToDate(buildFile, File(buildScriptJar))) { // if (! buildScriptUtil.isUpToDate(buildFile, File(buildScriptJar))) {
buildScriptJarFile.parentFile.mkdirs() buildScriptJarFile.parentFile.mkdirs()
generateJarFile(context, listOf(pluginSourceFile.path), buildScriptJarFile) generateJarFile(context, listOf(buildScriptSourceFile.path), buildScriptJarFile)
VersionFile.generateVersionFile(buildScriptJarFile.parentFile) VersionFile.generateVersionFile(buildScriptJarFile.parentFile)
Kobalt.context!!.internalContext.buildFileOutOfDate = true 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) 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)
} }