forked from Leibnizhu/sonar-pmd-p3c-jdk17
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_rules.groovy
74 lines (57 loc) · 2.1 KB
/
create_rules.groovy
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
73
74
import groovy.io.FileType
println '===================================='
println 'Creating markdown rule documentation'
println '===================================='
def ruleSourcePath = '../sonar-pmd-plugin/src/main/resources/org/sonar/l10n/pmd/rules'
def ruleTargetPath = './rules'
def createDeprecationWarning = {
rules ->
if (!rules.isEmpty()) {
def parsedRules = rules.collect {
def ruleNumber = it.substring(1)
(ruleNumber.isInteger()) ?
"[${it}](https://rules.sonarsource.com/java/RSPEC-${ruleNumber.toInteger()})" : "`java:${it}`"
}
return "> :warning: This rule is **deprecated** in favour of ${parsedRules.join(', ')}."
}
""
}
def extractRulesFromContent = {
content ->
def pattern = /(rule):(squid):(\w+)/
def group = (content =~ /$pattern/)
return group.collect {
it[3]
}
}
def removeDeprecationMessage = {
content ->
def regex = /(?ms)<p>(\s*)This rule is deprecated, use \{rule:java:(\w+)\} (.*)instead.(\s*)<\/p>/
if (content =~ regex) {
return content.replaceFirst(regex, "")
}
return content
}
def createMarkdownPagesForCategory = {
category ->
def currentDir = new File("${ruleSourcePath}/${category}")
currentDir.eachFile FileType.FILES, {
String rulename = it.name.tokenize('.')[0]
println " * Processing Rule ${rulename}"
String htmlContent = it.text
String deprecationWarning = createDeprecationWarning(extractRulesFromContent(htmlContent))
htmlContent = removeDeprecationMessage(htmlContent).trim()
String ruleContent = """# ${rulename}
**Category:** `${category}`<br/>
**Rule Key:** `${category}:${rulename}`<br/>
${deprecationWarning}
-----
${htmlContent}
"""
def file = new File("${ruleTargetPath}/${rulename}.md").newWriter()
file << ruleContent
file.close()
}
}
createMarkdownPagesForCategory('pmd')
createMarkdownPagesForCategory('pmd-unit-tests')