-
Notifications
You must be signed in to change notification settings - Fork 206
/
updateRules.groovy
217 lines (157 loc) · 5.64 KB
/
updateRules.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Update rules.txt and profile-clint.xml from OCLint documentation
// Severity is determined from the category
@Grab(group='org.codehaus.groovy.modules.http-builder',
module='http-builder', version='0.7')
import groovyx.net.http.*
import groovy.xml.MarkupBuilder
def splitCamelCase(value) {
value.replaceAll(
String.format("%s|%s|%s",
"(?<=[A-Z])(?=[A-Z][a-z])",
"(?<=[^A-Z])(?=[A-Z])",
"(?<=[A-Za-z])(?=[^A-Za-z])"
),
" "
).toLowerCase()
}
def parseCategory(url, name, severity) {
def result = []
def http = new HTTPBuilder(url)
def html = http.get([:])
def root = html."**".find { [email protected]().contains(name)}
root."**".findAll { [email protected]() == 'section'}.each {rule ->
def entry = [:]
def ruleName = splitCamelCase(rule.H2.text() - '¶').capitalize()
// Original name
entry.originalName = null
try {
def sourceHttp = new HTTPBuilder(rule."**".findAll {it.name() == 'A'}.last().@href)
def sourceHtml = sourceHttp.get[:]
def found = sourceHtml."**".find {it.name() == "TR" && it.text().contains("return\"")}.text()
def match = found =~ /"([^"]*)"/
entry.originalName = match[0][1]
} catch (Exception e) {
}
if (entry.originalName) {
// Name
entry.name = ruleName
println "Retrieving rule $entry.originalName"
// Summary
entry.summary = rule.P[1].text()
// Severity
entry.severity = severity
result.add entry
} else {
println "Unable to retrieve rule with name $entry.name"
}
}
result
}
def writeRulesTxt(rules, file) {
def text = "Available issues:\n" +
"\n" +
"OCLint\n" +
"======\n\n"
rules.each {rule ->
if (rule.name != '') {
text += rule.originalName + '\n'
text += '----------\n'
text += '\n'
// Summary
text += "Summary: $rule.summary\n"
text += '\n'
text += "Severity: $rule.severity\n"
text += "Category: OCLint\n"
text += '\n'
}
}
file.text = text
}
def readRulesTxt(file) {
def result = []
def previousLine = ''
def rule = null
file.eachLine {line ->
if (line.startsWith('--')) {
rule = [:]
rule.originalName = previousLine.trim()
rule.name = rule.originalName
}
if (line.startsWith('Summary:') && rule) {
rule.summary = (line - 'Summary:').trim()
}
if (line.startsWith('Severity:') && rule) {
rule.severity = Integer.parseInt((line - 'Severity:').trim())
}
if (line.startsWith('Category:') && rule) {
rule.category = (line - 'Category:').trim()
result.add rule
rule = null
}
previousLine = line
}
result
}
def writeProfileOCLint(rls, file) {
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.profile() {
name "OCLint"
language "objc"
rules {
rls.each {rl ->
rule {
repositoryKey "OCLint"
key rl.originalName
}
}
}
}
file.text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + writer.toString()
}
def mergeRules(existingRules, freshRules) {
def result = []
// Update existing rules
existingRules.each {rule ->
def freshRule = freshRules.find {it.originalName?.trim() == rule.originalName?.trim()}
if (freshRule) {
println "Updating rule [$rule.originalName]"
rule.severity = freshRule.severity
rule.category = freshRule.category
rule.summary = freshRule.summary
}
if (!result.find {it.originalName?.trim() == rule.originalName?.trim()}) {
result.add rule
} else {
println "Skipping rule [$rule.originalName]"
}
}
// Add new rules (if any)
freshRules.each {rule ->
def existingRule = existingRules.find {it.originalName?.trim() == rule.originalName?.trim()}
if (!existingRule) {
result.add rule
}
}
result
}
// Files
File rulesTxt = new File('src/main/resources/org/sonar/plugins/oclint/rules.txt')
File profileXml = new File('src/main/resources/org/sonar/plugins/oclint/profile-oclint.xml')
// Parse OCLint online documentation
def rules = []
rules.addAll parseCategory("http://docs.oclint.org/en/dev/rules/basic.html", "basic", 3)
rules.addAll parseCategory("http://docs.oclint.org/en/dev/rules/convention.html", "convention", 2)
rules.addAll parseCategory("http://docs.oclint.org/en/dev/rules/empty.html", "empty", 3)
rules.addAll parseCategory("http://docs.oclint.org/en/dev/rules/migration.html", "migration", 1)
rules.addAll parseCategory("http://docs.oclint.org/en/dev/rules/naming.html", "naming", 2)
rules.addAll parseCategory("http://docs.oclint.org/en/dev/rules/redundant.html", "redundant", 1)
rules.addAll parseCategory("http://docs.oclint.org/en/dev/rules/size.html", "size", 3)
rules.addAll parseCategory("http://docs.oclint.org/en/dev/rules/unused.html", "unused", 0)
println "${rules.size()} rules found"
// Read existing rules
def existingRules = readRulesTxt(rulesTxt)
// Update existing rules with fresh rules
def finalRules = mergeRules(existingRules, rules)
writeRulesTxt(finalRules, rulesTxt)
writeProfileOCLint(finalRules, profileXml)