mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 00:17:11 -07:00
Multiple build file work.
This commit is contained in:
parent
5b2d4296dc
commit
4ad0222f0a
6 changed files with 310 additions and 230 deletions
|
@ -1,8 +1,6 @@
|
|||
package com.beust.kobalt.internal.build
|
||||
|
||||
import com.beust.kobalt.api.Kobalt
|
||||
import com.beust.kobalt.homeDir
|
||||
import com.beust.kobalt.misc.kobaltLog
|
||||
import java.io.File
|
||||
import java.nio.file.*
|
||||
import java.nio.file.attribute.BasicFileAttributes
|
||||
|
@ -19,30 +17,27 @@ class SingleFileBuildSources(val file: File) : IBuildSources {
|
|||
override val root: File = file.parentFile.parentFile
|
||||
}
|
||||
|
||||
class BuildSources(val file: File) : IBuildSources {
|
||||
class BuildSources(val file: File = File("")) : IBuildSources {
|
||||
|
||||
override val root = file
|
||||
|
||||
override fun findSourceFiles() : List<File> {
|
||||
val result = arrayListOf("kobalt/src/Build.kt")
|
||||
if (Kobalt.buildSourceDirs.isNotEmpty()) result.addAll(findBuildFiles(Kobalt.buildSourceDirs))
|
||||
|
||||
return result.map(::File)
|
||||
return findBuildFiles(listOf(file))
|
||||
}
|
||||
|
||||
override fun exists() = findSourceFiles().isNotEmpty()
|
||||
|
||||
override fun toString() = "{BuildSources " + findSourceFiles().joinToString(", ") + "}"
|
||||
|
||||
fun findBuildFiles(roots: List<String>) : List<String> {
|
||||
val result = arrayListOf<String>()
|
||||
fun findBuildFiles(roots: List<File>) : List<File> {
|
||||
val result = arrayListOf<File>()
|
||||
roots.forEach { file ->
|
||||
Files.walkFileTree(Paths.get(file), object : SimpleFileVisitor<Path>() {
|
||||
Files.walkFileTree(Paths.get(file.path), object : SimpleFileVisitor<Path>() {
|
||||
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 }
|
||||
val sources = path.listFiles().filter { it.name.endsWith(".kt") }
|
||||
result.addAll(sources)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,29 @@
|
|||
package com.beust.kobalt.misc
|
||||
|
||||
import com.beust.kobalt.homeDir
|
||||
import java.io.File
|
||||
import java.util.regex.Pattern
|
||||
|
||||
fun main(argv: Array<String>) {
|
||||
val lines = File(homeDir("kotlin/kobalt/kobalt/src/Build.kt")).readLines()
|
||||
val result = BlockExtractor(Pattern.compile("val.*buildScript.*\\{"), '{', '}').extractBlock(lines)
|
||||
// BlockExtractor("plugins", '(', ')').extractBlock(lines)
|
||||
}
|
||||
|
||||
class Section(val start: Int, val end: Int) {
|
||||
override fun toString() = "$start-$end"
|
||||
}
|
||||
|
||||
class BuildScriptInfo(val content: String, val sections: List<Section>) {
|
||||
class IncludedBuildSourceDir(val line: Int, val dirs: List<String>)
|
||||
|
||||
class BuildScriptInfo(val file: File, val fullBuildFile: List<String>, val sections: List<Section>,
|
||||
val imports: List<String>) {
|
||||
fun isInSection(lineNumber: Int): Boolean {
|
||||
sections.forEach {
|
||||
if (lineNumber >= it.start && lineNumber <= it.end) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
val includedBuildSourceDirs = arrayListOf<IncludedBuildSourceDir>()
|
||||
|
||||
fun includedBuildSourceDirsForLine(line: Int): List<String> {
|
||||
val result = includedBuildSourceDirs.find { it.line == line }?.dirs
|
||||
return result ?: emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -28,7 +31,7 @@ class BuildScriptInfo(val content: String, val sections: List<Section>) {
|
|||
* e.g. buildScript { ... }.
|
||||
*/
|
||||
class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char) {
|
||||
fun extractBlock(lines: List<String>): BuildScriptInfo? {
|
||||
fun extractBlock(file: File, lines: List<String>): BuildScriptInfo? {
|
||||
var currentLineNumber = 0
|
||||
// First line of the buildScript block
|
||||
var startLine = 0
|
||||
|
@ -60,12 +63,9 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char)
|
|||
if (currentLine.isNotEmpty() && foundKeyword) buildScript.add(currentLine.toString())
|
||||
}
|
||||
|
||||
val allowedImports = listOf("com.beust", "java")
|
||||
val disallowedImports = listOf("com.beust.kobalt.plugin")
|
||||
val imports = arrayListOf<String>()
|
||||
val sections = arrayListOf<Section>()
|
||||
lines.forEach { line ->
|
||||
currentLineNumber++
|
||||
val found = regexp.matcher(line).matches()
|
||||
if (found) {
|
||||
startLine = currentLineNumber
|
||||
|
@ -75,7 +75,7 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char)
|
|||
topLines.add(line)
|
||||
} else {
|
||||
if (line.startsWith("import")) {
|
||||
if (allowedImports.any { line.contains(it) } && !disallowedImports.any { line.contains(it) }) {
|
||||
if (isAllowedImport(line)) {
|
||||
imports.add(line)
|
||||
}
|
||||
} else {
|
||||
|
@ -92,14 +92,25 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char)
|
|||
startLine = 0
|
||||
endLine = 0
|
||||
}
|
||||
|
||||
currentLineNumber++
|
||||
}
|
||||
|
||||
if (sections.isNotEmpty()) {
|
||||
val result = (imports.distinct() + buildScript).joinToString("\n") + "\n"
|
||||
|
||||
return BuildScriptInfo(result, sections)
|
||||
return BuildScriptInfo(file, lines, sections, imports)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val allowedImports = listOf("com.beust", "java")
|
||||
private val disallowedImports = listOf("com.beust.kobalt.plugin")
|
||||
|
||||
fun isAllowedImport(line: String) : Boolean {
|
||||
return allowedImports.any { line.contains(it) } && !disallowedImports.any { line.contains(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package com.beust.kobalt.misc
|
|||
import com.beust.kobalt.*
|
||||
import com.beust.kobalt.api.Kobalt
|
||||
import com.beust.kobalt.api.Project
|
||||
import com.beust.kobalt.internal.build.IBuildSources
|
||||
import com.beust.kobalt.maven.Md5
|
||||
import java.io.*
|
||||
import java.nio.file.Files
|
||||
|
@ -261,9 +260,9 @@ class KFiles {
|
|||
/**
|
||||
* The build location for build scripts is .kobalt/build
|
||||
*/
|
||||
fun findBuildScriptLocation(buildSources: IBuildSources, jarFile: String) : String {
|
||||
val result = joinDir(buildSources.root.path, KFiles.dotKobaltDir.path, KFiles.SCRIPT_BUILD_DIR, jarFile)
|
||||
kobaltLog(2, " Script jar file: $result")
|
||||
fun findBuildScriptDir(parent: String = ".") : File {
|
||||
val result = File(joinDir(parent, KFiles.dotKobaltDir.path, KFiles.SCRIPT_BUILD_DIR))
|
||||
kobaltLog(2, " Script jar files in: $result")
|
||||
return result
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue