From a7553279cfcffe018edd303508d26337c12dbc64 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Thu, 30 Mar 2017 13:16:17 -0700 Subject: [PATCH] Fix the topLines bug. --- .../main/kotlin/com/beust/kobalt/misc/BlockExtractor.kt | 7 ++++--- src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/BlockExtractor.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/BlockExtractor.kt index 1ed97120..1edb66b0 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/BlockExtractor.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/BlockExtractor.kt @@ -10,7 +10,7 @@ class Section(val start: Int, val end: Int) { class IncludedBuildSourceDir(val line: Int, val dirs: List) class BuildScriptInfo(val file: File, val fullBuildFile: List, val sections: List
, - val imports: List) { + val imports: List, val topLines: List) { fun isInSection(lineNumber: Int): Boolean { sections.forEach { if (lineNumber >= it.start && lineNumber <= it.end) return true @@ -42,6 +42,7 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char) var count = 0 val buildScript = arrayListOf() val topLines = arrayListOf() + val finalTopLines = arrayListOf() fun updateCount(line: String) { val currentLine = StringBuffer() @@ -72,7 +73,7 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char) foundKeyword = true count = 1 buildScript.add(line) - topLines.add(line) + finalTopLines.addAll(topLines) } else { if (line.startsWith("import")) { if (isAllowedImport(line)) { @@ -99,7 +100,7 @@ class BlockExtractor(val regexp: Pattern, val opening: Char, val closing: Char) if (sections.isNotEmpty()) { val result = (imports.distinct() + buildScript).joinToString("\n") + "\n" - return BuildScriptInfo(file, lines, sections, imports) + return BuildScriptInfo(file, lines, sections, imports, finalTopLines) } else { return null } diff --git a/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt b/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt index 273788ac..c287ea21 100644 --- a/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt +++ b/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt @@ -157,13 +157,14 @@ class BuildFiles @Inject constructor(val factory: BuildFileCompiler.IFactory, // Run every buildScriptInfo section in its own source file var counter = 0 analyzedFiles.forEach { af -> - af.buildScriptInfo.sections.forEach { section -> + val buildScriptInfo = af.buildScriptInfo + buildScriptInfo.sections.forEach { section -> // // Create a source file with just this buildScriptInfo{} // val bs = af.file.readLines().subList(section.start, section.end + 1) - val source = (af.buildScriptInfo.imports + bs).joinToString("\n") + val source = (buildScriptInfo.imports + buildScriptInfo.topLines + bs).joinToString("\n") val sourceFile = Files.createTempFile(null, ".kt").toFile().apply { writeText(source) } @@ -197,7 +198,7 @@ class BuildFiles @Inject constructor(val factory: BuildFileCompiler.IFactory, val newDirs = arrayListOf().apply { addAll(Kobalt.buildSourceDirs) } newDirs.removeAll(currentDirs) if (newDirs.any()) { - af.buildScriptInfo.includedBuildSourceDirs.add(IncludedBuildSourceDir(section.start, newDirs)) + buildScriptInfo.includedBuildSourceDirs.add(IncludedBuildSourceDir(section.start, newDirs)) } }