mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Don’t run buildScript{} twice.
This commit is contained in:
parent
e297586e62
commit
bdd80bdedd
2 changed files with 31 additions and 49 deletions
|
@ -11,12 +11,19 @@ fun main(argv: Array<String>) {
|
||||||
// BlockExtractor("plugins", '(', ')').extractBlock(lines)
|
// BlockExtractor("plugins", '(', ')').extractBlock(lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class BuildScriptInfo(val content: String, val startLine: Int, val endLine: Int)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to extract a keyword followed by opening and closing tags out of a list of strings,
|
* Used to extract a keyword followed by opening and closing tags out of a list of strings,
|
||||||
* e.g. buildScript { ... }.
|
* e.g. buildScript { ... }.
|
||||||
*/
|
*/
|
||||||
class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char) {
|
class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char) {
|
||||||
fun extractBlock(lines: List<String>): String? {
|
fun extractBlock(lines: List<String>): BuildScriptInfo? {
|
||||||
|
var currentLineNumber = 0
|
||||||
|
// First line of the buildScript block
|
||||||
|
var startLine = 0
|
||||||
|
// Last line of the buildScript block
|
||||||
|
var endLine = 0
|
||||||
var foundKeyword = false
|
var foundKeyword = false
|
||||||
var foundClosing = false
|
var foundClosing = false
|
||||||
var count = 0
|
var count = 0
|
||||||
|
@ -34,6 +41,7 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char)
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
currentLine.append(closing).append("\n")
|
currentLine.append(closing).append("\n")
|
||||||
foundClosing = true
|
foundClosing = true
|
||||||
|
endLine = currentLineNumber
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (foundKeyword && count > 0) currentLine.append(c)
|
if (foundKeyword && count > 0) currentLine.append(c)
|
||||||
|
@ -43,8 +51,10 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char)
|
||||||
}
|
}
|
||||||
|
|
||||||
lines.forEach { line ->
|
lines.forEach { line ->
|
||||||
|
currentLineNumber++
|
||||||
val found = regexp.matcher(line).matches()
|
val found = regexp.matcher(line).matches()
|
||||||
if (found) {
|
if (found) {
|
||||||
|
startLine = currentLineNumber
|
||||||
foundKeyword = true
|
foundKeyword = true
|
||||||
count = 1
|
count = 1
|
||||||
result.append(topLines.joinToString("\n"))
|
result.append(topLines.joinToString("\n"))
|
||||||
|
@ -55,12 +65,12 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (foundKeyword && foundClosing && count == 0) {
|
if (foundKeyword && foundClosing && count == 0) {
|
||||||
return result.toString()
|
return BuildScriptInfo(result.toString(), startLine, endLine)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (foundKeyword && foundClosing && count == 0) {
|
if (foundKeyword && foundClosing && count == 0) {
|
||||||
return result.toString()
|
return BuildScriptInfo(result.toString(), startLine, endLine)
|
||||||
} else {
|
} else {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,14 +10,12 @@ 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.BlockExtractor
|
||||||
import com.beust.kobalt.misc.KFiles
|
import com.beust.kobalt.misc.KFiles
|
||||||
import com.beust.kobalt.misc.countChar
|
|
||||||
import com.beust.kobalt.misc.kobaltLog
|
import com.beust.kobalt.misc.kobaltLog
|
||||||
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
|
||||||
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.regex.Pattern
|
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,
|
||||||
|
@ -44,14 +42,11 @@ class ParsedBuildFile(val buildFile: BuildFile, val context: KobaltContext, val
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseBuildFile() {
|
private fun parseBuildFile() {
|
||||||
var parenCount = 0
|
|
||||||
var current: ArrayList<String>? = null
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
*/
|
*/
|
||||||
fun correctProfileLine(line: String) : String {
|
fun correctProfileLine(line: String): String {
|
||||||
(context.profiles as List<String>).forEach {
|
(context.profiles as List<String>).forEach {
|
||||||
if (line.matches(Regex("[ \\t]*val[ \\t]+$it[ \\t]*=.*"))) {
|
if (line.matches(Regex("[ \\t]*val[ \\t]+$it[ \\t]*=.*"))) {
|
||||||
with("val $it = true") {
|
with("val $it = true") {
|
||||||
|
@ -65,7 +60,7 @@ class ParsedBuildFile(val buildFile: BuildFile, val context: KobaltContext, val
|
||||||
return line
|
return line
|
||||||
}
|
}
|
||||||
|
|
||||||
fun applyProfiles(lines: List<String>) : List<String> {
|
fun applyProfiles(lines: List<String>): List<String> {
|
||||||
val result = arrayListOf<String>()
|
val result = arrayListOf<String>()
|
||||||
lines.forEach { line ->
|
lines.forEach { line ->
|
||||||
result.add(correctProfileLine(line))
|
result.add(correctProfileLine(line))
|
||||||
|
@ -74,51 +69,28 @@ class ParsedBuildFile(val buildFile: BuildFile, val context: KobaltContext, val
|
||||||
}
|
}
|
||||||
|
|
||||||
val buildWithCorrectProfiles = applyProfiles(buildFile.path.toFile().readLines())
|
val buildWithCorrectProfiles = applyProfiles(buildFile.path.toFile().readLines())
|
||||||
val bs = BlockExtractor(Pattern.compile("^val.*buildScript.*\\{"), '{', '}').extractBlock(buildWithCorrectProfiles)
|
val buildScriptInfo = BlockExtractor(Pattern.compile("^val.*buildScript.*\\{"), '{', '}')
|
||||||
|
.extractBlock(buildWithCorrectProfiles)
|
||||||
|
|
||||||
buildFile.path.toFile().forEachLine(Charset.defaultCharset()) { line ->
|
if (buildScriptInfo != null) {
|
||||||
var index = line.indexOf("plugins(")
|
preBuildScript.add(buildScriptInfo.content)
|
||||||
if (current == null) {
|
|
||||||
if (index >= 0) {
|
|
||||||
current = pluginList
|
|
||||||
} else {
|
|
||||||
index = line.indexOf("repos(")
|
|
||||||
if (index >= 0) {
|
|
||||||
current = repos
|
|
||||||
} else {
|
|
||||||
index = line.indexOf("buildFileClasspath(")
|
|
||||||
if (index >= 0) {
|
|
||||||
current = buildFileClasspath
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parenCount > 0 || current != null) {
|
|
||||||
if (index == -1) index = 0
|
|
||||||
with(line.substring(index)) {
|
|
||||||
parenCount += line countChar '('
|
|
||||||
if (parenCount > 0) {
|
|
||||||
current!!.add(line)
|
|
||||||
}
|
|
||||||
parenCount -= line countChar ')'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parenCount == 0) {
|
|
||||||
current = null
|
|
||||||
}
|
|
||||||
|
|
||||||
buildScript.add(correctProfileLine(line))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bs != null) {
|
|
||||||
preBuildScript.add(bs)
|
|
||||||
} else {
|
} else {
|
||||||
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 buildScript{} tag since we already ran it
|
||||||
|
//
|
||||||
|
var lineNumber = 1
|
||||||
|
buildFile.path.toFile().forEachLine { line ->
|
||||||
|
if (buildScriptInfo == null ||
|
||||||
|
(lineNumber < buildScriptInfo.startLine || lineNumber > buildScriptInfo.endLine)) {
|
||||||
|
buildScript.add(correctProfileLine(line))
|
||||||
|
}
|
||||||
|
lineNumber++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initPluginUrls() {
|
private fun initPluginUrls() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue