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

BlockExtractor.

This commit is contained in:
Cedric Beust 2017-01-20 16:23:27 -08:00
parent 384550c62c
commit 574e305399

View file

@ -0,0 +1,60 @@
package com.beust.kobalt.misc
import com.beust.kobalt.homeDir
import java.io.File
fun main(argv: Array<String>) {
val lines = File(homeDir("kotlin/kobalt/kobalt/src/Build.kt")).readLines()
val result = BlockExtractor("buildScript", '{', '}').extractBlock(lines)
BlockExtractor("plugins", '(', ')').extractBlock(lines)
}
class BlockExtractor(val keyword: String, val opening: Char, val closing: Char) {
fun extractBlock(lines: List<String>): String? {
var foundKeyword = false
var foundOpening = false
var foundClosing = false
var count = 0
val result = StringBuffer()
fun updateCount(line: String) {
val onlyBlanks = foundKeyword && ! foundOpening
var blanks = true
line.toCharArray().forEach { c ->
if (c == opening) {
count++
if (onlyBlanks && blanks) foundOpening = true
}
if (c == closing) {
count--
foundClosing = true
}
if (c != ' ' && c != '\t' && c != '\n') blanks = false
result.append(c)
}
result.append("\n")
}
lines.forEach { line ->
if (! foundKeyword) {
val index = line.indexOf(keyword)
if (index >= 0) {
foundKeyword = true
result.append(keyword)
updateCount(line.substring(index + keyword.length))
}
} else {
updateCount(line)
}
if (foundKeyword && foundOpening && foundClosing && count == 0) {
println("Done extracting: @$result@")
return result.toString()
}
}
return null
}
}