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

buildScript{} support.

This commit is contained in:
Cedric Beust 2017-01-22 09:19:41 -08:00
parent 574e305399
commit bfc787a4b4
3 changed files with 97 additions and 51 deletions

View file

@ -7,6 +7,7 @@ import com.beust.kobalt.internal.KobaltSettings
import com.beust.kobalt.internal.PluginInfo import com.beust.kobalt.internal.PluginInfo
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.dependency.FileDependency import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.misc.KobaltLogger
import org.eclipse.aether.repository.Proxy import org.eclipse.aether.repository.Proxy
import java.io.File import java.io.File
import java.net.InetSocketAddress import java.net.InetSocketAddress
@ -14,12 +15,17 @@ import java.net.InetSocketAddress
var BUILD_SCRIPT_CONFIG : BuildScriptConfig? = null var BUILD_SCRIPT_CONFIG : BuildScriptConfig? = null
class BuildScriptConfig { class BuildScriptConfig {
// var repos = listOf<String>() /** The list of repos used to locate plug-ins. */
// var plugins = listOf<String>() fun repos(vararg r: String) = newRepos(*r)
// The following settings are used to modify the compiler used to /** The list of plug-ins to use for this build file. */
// compile the build file. Projects should use kotlinCompiler { compilerVersion } to configure fun plugins(vararg pl: String) = newPlugins(*pl)
// the Kotin compiler for their source files.
/** The build file classpath. */
fun buildFileClasspath(vararg bfc: String) = newBuildFileClasspath(*bfc)
// The following settings modify the compiler used to compile the build file.
// Projects should use kotlinCompiler { compilerVersion } to configure the Kotin compiler for their source files.
var kobaltCompilerVersion : String? = null var kobaltCompilerVersion : String? = null
var kobaltCompilerRepo: String? = null var kobaltCompilerRepo: String? = null
var kobaltCompilerFlags: String? = null var kobaltCompilerFlags: String? = null
@ -37,13 +43,18 @@ fun homeDir(vararg dirs: String) : String = SystemProperties.homeDir +
@Directive @Directive
fun file(file: String) : String = FileDependency.PREFIX_FILE + file fun file(file: String) : String = FileDependency.PREFIX_FILE + file
@Directive
fun plugins(vararg dependency : IClasspathDependency) { fun plugins(vararg dependency : IClasspathDependency) {
dependency.forEach { Plugins.addDynamicPlugin(it) } dependency.forEach { Plugins.addDynamicPlugin(it) }
} }
@Directive
fun plugins(vararg dependencies : String) { fun plugins(vararg dependencies : String) {
KobaltLogger.logger.warn("Build.kt",
"Invoking plugins() directly is deprecated, use the buildScript{} directive")
newPlugins(*dependencies)
}
@Directive
fun newPlugins(vararg dependencies : String) {
val factory = Kobalt.INJECTOR.getInstance(DependencyManager::class.java) val factory = Kobalt.INJECTOR.getInstance(DependencyManager::class.java)
dependencies.forEach { dependencies.forEach {
Plugins.addDynamicPlugin(factory.create(it)) Plugins.addDynamicPlugin(factory.create(it))
@ -70,13 +81,23 @@ data class HostConfig(var url: String = "", var username: String? = null, var pa
} }
} }
@Directive
fun repos(vararg repos : String) { fun repos(vararg repos : String) {
KobaltLogger.logger.warn("Build.kt",
"Invoking repos() directly is deprecated, use the buildScript{} directive")
newRepos(*repos)
}
fun newRepos(vararg repos: String) {
repos.forEach { Kobalt.addRepo(HostConfig(it)) } repos.forEach { Kobalt.addRepo(HostConfig(it)) }
} }
@Directive
fun buildFileClasspath(vararg deps: String) { fun buildFileClasspath(vararg deps: String) {
KobaltLogger.logger.warn("Build.kt",
"Invoking buildFileClasspath() directly is deprecated, use the buildScript{} directive")
newBuildFileClasspath(*deps)
}
fun newBuildFileClasspath(vararg deps: String) {
deps.forEach { Kobalt.addBuildFileClasspath(it) } deps.forEach { Kobalt.addBuildFileClasspath(it) }
} }
@ -103,3 +124,4 @@ fun localMaven() : String {
} }
return result.toURI().toString() return result.toURI().toString()
} }

View file

@ -2,59 +2,65 @@ package com.beust.kobalt.misc
import com.beust.kobalt.homeDir import com.beust.kobalt.homeDir
import java.io.File import java.io.File
import java.util.regex.Pattern
fun main(argv: Array<String>) { fun main(argv: Array<String>) {
val lines = File(homeDir("kotlin/kobalt/kobalt/src/Build.kt")).readLines() val lines = File(homeDir("kotlin/kobalt/kobalt/src/Build.kt")).readLines()
val result = BlockExtractor("buildScript", '{', '}').extractBlock(lines) val result = BlockExtractor(Pattern.compile("val.*buildScript.*\\{"), '{', '}').extractBlock(lines)
BlockExtractor("plugins", '(', ')').extractBlock(lines) // BlockExtractor("plugins", '(', ')').extractBlock(lines)
} }
class BlockExtractor(val keyword: String, val opening: Char, val closing: Char) { /**
* Used to extract a keyword followed by opening and closing tags out of a list of strings,
* e.g. buildScript { ... }.
*/
class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char) {
fun extractBlock(lines: List<String>): String? { fun extractBlock(lines: List<String>): String? {
var foundKeyword = false var foundKeyword = false
var foundOpening = false
var foundClosing = false var foundClosing = false
var count = 0 var count = 0
val result = StringBuffer() val result = StringBuffer()
fun updateCount(line: String) { fun updateCount(line: String) {
val onlyBlanks = foundKeyword && ! foundOpening val currentLine = StringBuffer()
var blanks = true
line.toCharArray().forEach { c -> line.toCharArray().forEach { c ->
if (c == opening) { if (c == opening) {
count++ count++
if (onlyBlanks && blanks) foundOpening = true
} }
if (c == closing) { if (c == closing) {
count-- count--
foundClosing = true if (count == 0) {
currentLine.append(closing).append("\n")
foundClosing = true
}
} }
if (c != ' ' && c != '\t' && c != '\n') blanks = false if (foundKeyword && count > 0) currentLine.append(c)
result.append(c)
} }
result.append("\n")
if (currentLine.isNotEmpty()) result.append(currentLine.toString()).append("\n")
} }
lines.forEach { line -> lines.forEach { line ->
if (! foundKeyword) { val found = regexp.matcher(line).matches()
val index = line.indexOf(keyword) if (found) {
if (index >= 0) { foundKeyword = true
foundKeyword = true count = 1
result.append(keyword) result.append(line).append("\n")
updateCount(line.substring(index + keyword.length))
}
} else { } else {
updateCount(line) updateCount(line)
} }
if (foundKeyword && foundOpening && foundClosing && count == 0) { if (foundKeyword && foundClosing && count == 0) {
println("Done extracting: @$result@") println("Done extracting: @$result@")
return result.toString() return result.toString()
} }
} }
return null if (foundKeyword && foundClosing && count == 0) {
return result.toString()
} else {
return null
}
} }
} }

View file

@ -8,6 +8,7 @@ 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.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.KFiles import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.countChar import com.beust.kobalt.misc.countChar
import com.beust.kobalt.misc.kobaltLog import com.beust.kobalt.misc.kobaltLog
@ -17,6 +18,7 @@ import java.net.URL
import java.nio.charset.Charset import java.nio.charset.Charset
import java.nio.file.Paths import java.nio.file.Paths
import java.util.* import java.util.*
import java.util.regex.Pattern
class ParsedBuildFile(val buildFile: BuildFile, val context: KobaltContext, val buildScriptUtil: BuildScriptUtil, class ParsedBuildFile(val buildFile: BuildFile, val context: KobaltContext, val buildScriptUtil: BuildScriptUtil,
val dependencyManager: DependencyManager, val files: KFiles) { val dependencyManager: DependencyManager, val files: KFiles) {
@ -44,6 +46,36 @@ class ParsedBuildFile(val buildFile: BuildFile, val context: KobaltContext, val
private fun parseBuildFile() { private fun parseBuildFile() {
var parenCount = 0 var parenCount = 0
var current: ArrayList<String>? = null var current: ArrayList<String>? = null
/**
* If the current line matches one of the profiles, turn the declaration into
* val profile = true, otherwise return the same line
*/
fun correctProfileLine(line: String) : String {
(context.profiles as List<String>).forEach {
if (line.matches(Regex("[ \\t]*val[ \\t]+$it[ \\t]*=.*"))) {
with("val $it = true") {
kobaltLog(2, "Activating profile $it in build file")
activeProfiles.add(it)
profileLines.add(this)
return this
}
}
}
return line
}
fun applyProfiles(lines: List<String>) : List<String> {
val result = arrayListOf<String>()
lines.forEach { line ->
result.add(correctProfileLine(line))
}
return result
}
val buildWithCorrectProfiles = applyProfiles(buildFile.path.toFile().readLines())
val bs = BlockExtractor(Pattern.compile("^val.*buildScript.*\\{"), '{', '}').extractBlock(buildWithCorrectProfiles)
buildFile.path.toFile().forEachLine(Charset.defaultCharset()) { line -> buildFile.path.toFile().forEachLine(Charset.defaultCharset()) { line ->
var index = line.indexOf("plugins(") var index = line.indexOf("plugins(")
if (current == null) { if (current == null) {
@ -77,30 +109,16 @@ class ParsedBuildFile(val buildFile: BuildFile, val context: KobaltContext, val
current = null current = null
} }
/**
* If the current line matches one of the profiles, turn the declaration into
* val profile = true, otherwise return the same line
*/
fun correctProfileLine(line: String) : String {
(context.profiles as List<String>).forEach {
if (line.matches(Regex("[ \\t]*val[ \\t]+$it[ \\t]*=.*"))) {
with("val $it = true") {
kobaltLog(2, "Activating profile $it in build file")
activeProfiles.add(it)
profileLines.add(this)
return this
}
}
}
return line
}
buildScript.add(correctProfileLine(line)) buildScript.add(correctProfileLine(line))
} }
repos.forEach { preBuildScript.add(it) } if (bs != null) {
pluginList.forEach { preBuildScript.add(it) } preBuildScript.add(bs)
buildFileClasspath.forEach { preBuildScript.add(it) } } else {
repos.forEach { preBuildScript.add(it) }
pluginList.forEach { preBuildScript.add(it) }
buildFileClasspath.forEach { preBuildScript.add(it) }
}
} }
private fun initPluginUrls() { private fun initPluginUrls() {