1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt-line-count.git synced 2025-04-26 03:27:11 -07:00
This commit is contained in:
Cedric Beust 2015-10-11 09:45:07 -07:00
parent 1ef8e4b945
commit 3989ef293d
7 changed files with 89 additions and 64 deletions

View file

@ -0,0 +1,70 @@
package com.beust.kobalt.plugin.linecount
import com.beust.kobalt.Plugins
import com.beust.kobalt.api.BasePlugin
import com.beust.kobalt.internal.TaskResult
import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.misc.KobaltLogger
import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.*
fun main(argv: Array<String>) {
com.beust.kobalt.main(argv)
}
public class LineCountMain : BasePlugin(), KobaltLogger {
companion object {
const val NAME : String = "kobalt-line-count"
}
override val name = NAME
var info: LineCountInfo = LineCountInfo()
override fun apply(project: Project, context: KobaltContext) {
println("*** Applying plugin ${name} with project ${project}")
println("*** Adding dynamic task")
addTask(project, "dynamicTask", wrapAfter = arrayListOf("compile")) {
println("DYNAMIC")
TaskResult()
}
}
@Task(name = "lineCount", description = "Count the lines", runBefore = arrayOf("compile"))
fun lineCount(project: Project): TaskResult {
var fileCount = 0
var lineCount : Long = 0
log(1, "Finding files that end in ${info.suffix}")
val matcher = FileSystems.getDefault().getPathMatcher("glob:" + info.suffix)
val path = Paths.get(project.directory)
if (path.toFile().exists()) {
Files.walkFileTree(path, object : SimpleFileVisitor<Path>() {
override public fun visitFile(path: Path, attrs: BasicFileAttributes): FileVisitResult {
log(2, "File: $path")
if (matcher.matches(path)) {
fileCount++
lineCount += Files.lines(path).count()
log(2, " MATCH $path")
}
return FileVisitResult.CONTINUE
}
})
}
log(1, "Found $lineCount lines in $fileCount files")
return TaskResult()
}
}
data class LineCountInfo(var suffix: String = "**kt")
@Directive
public fun lineCount(init: LineCountInfo.() -> Unit): LineCountInfo {
with(LineCountInfo()) {
init()
(Plugins.getPlugin(LineCountMain.NAME) as LineCountMain).info = this
return this
}
}

View file

@ -1,48 +0,0 @@
package com.beust.kobalt.plugin.linecount
import com.beust.kobalt.api.BasePlugin
import com.beust.kobalt.internal.TaskResult
import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.misc.KobaltLogger
import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.*
fun main(argv: Array<String>) {
com.beust.kobalt.main(argv)
}
public class Main : BasePlugin(), KobaltLogger {
override val name = "kobalt-line-count"
override fun apply(project: Project, context: KobaltContext) {
println("*** Applying plugin ${name} with project ${project}")
}
@Task(name = "lineCount", description = "Count the lines", runBefore = arrayOf("compile"))
fun lineCount(project: Project): TaskResult {
var fileCount = 0
var lineCount : Long = 0
val matcher = FileSystems.getDefault().getPathMatcher("glob:**.kt")
project.sourceDirectories.forEach {
val path = Paths.get(it)
if (path.toFile().exists()) {
Files.walkFileTree(path, object : SimpleFileVisitor<Path>() {
override public fun visitFile(path: Path, attrs: BasicFileAttributes): FileVisitResult {
log(2, "File: ${path}")
if (matcher.matches(path)) {
fileCount++
lineCount += Files.lines(path).count()
log(2, " MATCH")
}
return FileVisitResult.CONTINUE
}
})
}
}
log(1, "Found ${lineCount} lines in ${fileCount} files")
return TaskResult()
}
}