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

Preliminary work for multiple build files.

This commit is contained in:
Cedric Beust 2017-03-27 14:54:51 -07:00
parent 2478040bff
commit ccbfe3bd94
15 changed files with 137 additions and 254 deletions

View file

@ -1,10 +1,8 @@
package com.beust.kobalt.internal.build
import com.beust.kobalt.misc.KFiles
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.BasicFileAttributes
/**
* Sometimes, build files are moved to temporary files, so we give them a specific name for clarity.
* @param path is the path where that file was moved, @param realPath is where the actual file is.
@ -12,23 +10,5 @@ import java.nio.file.attribute.BasicFileAttributes
class BuildFile(val path: Path, val name: String, val realPath: Path = path) {
fun exists() : Boolean = Files.exists(path)
val lastModified : Long
get() = Files.readAttributes(realPath, BasicFileAttributes::class.java).lastModifiedTime().toMillis()
val directory : File get() = path.toFile().parentFile
/**
* @return the .kobalt directory where this build file will be compiled.
*/
val dotKobaltDir: File get() = File(directory.parentFile.parentFile, KFiles.KOBALT_DOT_DIR).apply {
mkdirs()
}
/**
* @return the absolute directory of this project's location, assuming the build file is in
* $project/kobalt/src/Build.kt.
*/
val absoluteDir : File? get() {
return path.parent?.parent?.parent?.toFile()
}
}

View file

@ -10,7 +10,18 @@ fun main(argv: Array<String>) {
// BlockExtractor("plugins", '(', ')').extractBlock(lines)
}
class BuildScriptInfo(val content: String, val startLine: Int, val endLine: Int)
class Section(val start: Int, val end: Int) {
override fun toString() = "$start-$end"
}
class BuildScriptInfo(val content: String, val sections: List<Section>) {
fun isInSection(lineNumber: Int): Boolean {
sections.forEach {
if (lineNumber >= it.start && lineNumber <= it.end) return true
}
return false
}
}
/**
* Used to extract a keyword followed by opening and closing tags out of a list of strings,
@ -26,7 +37,7 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char)
var foundKeyword = false
var foundClosing = false
var count = 0
val result = StringBuffer()
val buildScript = arrayListOf<String>()
val topLines = arrayListOf<String>()
fun updateCount(line: String) {
@ -46,9 +57,13 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char)
if (foundKeyword && count > 0) currentLine.append(c)
}
if (currentLine.isNotEmpty()) result.append(currentLine.toString()).append("\n")
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()
@ -56,26 +71,33 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char)
startLine = currentLineNumber
foundKeyword = true
count = 1
result.append(topLines.joinToString("\n")).append("\n")
result.append(line).append("\n")
buildScript.add(line)
topLines.add(line)
} else {
val allowedImports = listOf("com.beust", "java")
val disallowedImports = listOf("com.beust.kobalt.plugin")
if (! line.startsWith("import") ||
(line.startsWith("import") && allowedImports.any { line.contains(it) }
&& ! disallowedImports.any { line.contains(it) })) {
if (line.startsWith("import")) {
if (allowedImports.any { line.contains(it) } && !disallowedImports.any { line.contains(it) }) {
imports.add(line)
}
} else {
topLines.add(line)
}
updateCount(line)
}
if (foundKeyword && foundClosing && count == 0) {
return BuildScriptInfo(result.toString(), startLine, endLine)
sections.add(Section(startLine, endLine))
foundKeyword = false
foundClosing = false
count = 0
startLine = 0
endLine = 0
}
}
if (foundKeyword && foundClosing && count == 0) {
return BuildScriptInfo(result.toString(), startLine, endLine)
if (sections.isNotEmpty()) {
val result = (imports.distinct() + buildScript).joinToString("\n") + "\n"
return BuildScriptInfo(result, sections)
} else {
return null
}

View file

@ -3,7 +3,7 @@ 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.BuildFile
import com.beust.kobalt.internal.build.IBuildSources
import com.beust.kobalt.maven.Md5
import java.io.*
import java.nio.file.Files
@ -261,9 +261,9 @@ class KFiles {
/**
* The build location for build scripts is .kobalt/build
*/
fun findBuildScriptLocation(buildFile: BuildFile, jarFile: String) : String {
val result = joinDir(buildFile.dotKobaltDir.path, KFiles.SCRIPT_BUILD_DIR, jarFile)
kobaltLog(2, "Build file dotKobaltDir: " + buildFile.dotKobaltDir)
fun findBuildScriptLocation(buildSources: IBuildSources, jarFile: String) : String {
val result = joinDir(buildSources.root.path, KFiles.dotKobaltDir.path, KFiles.SCRIPT_BUILD_DIR, jarFile)
kobaltLog(2, "Build file dotKobaltDir: " + KFiles.dotKobaltDir)
kobaltLog(2, "Script jar file: $result")
return result
}
@ -387,18 +387,7 @@ class KFiles {
}
}
fun findBuildFile(projectRoot: String = "."): File {
val deprecatedLocation = File(Constants.BUILD_FILE_NAME)
val result: File =
if (deprecatedLocation.exists()) {
warn(Constants.BUILD_FILE_NAME + " is in a deprecated location, please move it to "
+ Constants.BUILD_FILE_DIRECTORY)
deprecatedLocation
} else {
File(KFiles.joinDir(projectRoot, Constants.BUILD_FILE_DIRECTORY, Constants.BUILD_FILE_NAME))
}
return result
}
val dotKobaltDir = File(KFiles.joinAndMakeDir(KFiles.KOBALT_DOT_DIR))
}
fun findRecursively(directory: File, function: Function1<String, Boolean>): List<String> {