/
DetektPrinter.kt
59 lines (54 loc) · 2.54 KB
/
DetektPrinter.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
package io.gitlab.arturbosch.detekt.generator
import io.github.detekt.utils.yaml
import io.gitlab.arturbosch.detekt.generator.collection.RuleSetPage
import io.gitlab.arturbosch.detekt.generator.out.MarkdownWriter
import io.gitlab.arturbosch.detekt.generator.out.PropertiesWriter
import io.gitlab.arturbosch.detekt.generator.out.YamlWriter
import io.gitlab.arturbosch.detekt.generator.printer.DeprecatedPrinter
import io.gitlab.arturbosch.detekt.generator.printer.RuleSetPagePrinter
import io.gitlab.arturbosch.detekt.generator.printer.defaultconfig.ConfigPrinter
import io.gitlab.arturbosch.detekt.generator.printer.defaultconfig.printRuleSetPage
import java.nio.file.Paths
class DetektPrinter(private val arguments: GeneratorArgs) {
private val markdownWriter = MarkdownWriter(System.out)
private val yamlWriter = YamlWriter(System.out)
private val propertiesWriter = PropertiesWriter(System.out)
fun print(pages: List<RuleSetPage>) {
pages.forEach {
markdownWriter.write(arguments.documentationPath, it.ruleSet.name) {
markdownHeader(it.ruleSet.name) + "\n" + RuleSetPagePrinter.print(it)
}
}
yamlWriter.write(arguments.configPath, "default-detekt-config") {
ConfigPrinter.print(pages.filterNot { it.ruleSet.name == "formatting" || it.ruleSet.name == "ruleauthors" })
}
propertiesWriter.write(arguments.configPath, "deprecation") {
// We intentionally not filter for "formatting" as we want to be able to deprecate
// properties from that ruleset as well.
DeprecatedPrinter.print(pages)
}
yamlWriter.write(Paths.get("../detekt-formatting/src/main/resources/config"), "config") {
yaml {
printRuleSetPage(pages.first { it.ruleSet.name == "formatting" })
}
}
yamlWriter.write(Paths.get("../detekt-rules-ruleauthors/src/main/resources/config"), "config") {
yaml {
printRuleSetPage(pages.first { it.ruleSet.name == "ruleauthors" })
}
}
}
private fun markdownHeader(ruleSet: String): String {
check(ruleSet.length > 1) { "Rule set name must be not empty or less than two symbols." }
return """
|---
|title: ${ruleSet[0].uppercaseChar()}${ruleSet.substring(1)} Rule Set
|sidebar: home_sidebar
|keywords: [rules, $ruleSet]
|permalink: $ruleSet.html
|toc: true
|folder: documentation
|---
""".trimMargin()
}
}