forked from detekt/detekt
/
Generator.kt
72 lines (60 loc) · 2.41 KB
/
Generator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package io.gitlab.arturbosch.detekt.generator
import io.github.detekt.parser.KtCompiler
import io.gitlab.arturbosch.detekt.generator.collection.DetektCollector
import io.gitlab.arturbosch.detekt.generator.printer.CliOptionsPrinter
import org.jetbrains.kotlin.psi.KtFile
import java.io.PrintStream
import java.nio.file.Files
import java.nio.file.Path
import java.util.stream.Collectors
import kotlin.system.measureTimeMillis
class Generator(
private val arguments: GeneratorArgs,
private val outPrinter: PrintStream = System.out
) {
private val collector = DetektCollector()
private val printer = DetektPrinter(arguments)
private val cliOptionsPrinter = CliOptionsPrinter()
private fun parseAll(parser: KtCompiler, root: Path): Collection<KtFile> =
Files.walk(root)
.filter { it.fileName.toString().endsWith(".kt") }
.map { parser.compile(root, it) }
.collect(Collectors.toList())
fun execute() {
val parser = KtCompiler()
val time = measureTimeMillis {
val ktFiles = arguments.inputPath
.flatMap { parseAll(parser, it) }
ktFiles.forEach(collector::visit)
printer.print(collector.items)
cliOptionsPrinter.print(arguments.cliOptionsPath)
}
outPrinter.println("\nGenerated all detekt documentation in $time ms.")
}
fun executeCustomRuleConfig() {
val parser = KtCompiler()
val time = measureTimeMillis {
val ktFiles = arguments.inputPath
.flatMap { parseAll(parser, it) }
val folderMap = mutableMapOf<KtFile, String>()
ktFiles.forEach { ktFile ->
val pathParts = ktFile.name.split("/src/main/kotlin")
if (pathParts.count() > 1) {
folderMap[ktFile] = "${pathParts.first()}/src/main/resources/config/"
}
}
folderMap
.toList()
.groupBy { (_, folder) -> folder }
.toList()
.forEach { (folder, list) ->
val collector = DetektCollector()
list.forEach { (file, _) ->
collector.visit(file)
}
printer.printCustomRuleConfig(collector.items, folder)
}
}
outPrinter.println("\nGenerated custom rules config in $time ms.")
}
}