forked from Backelite/sonar-objective-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateFauxPasRules.groovy
94 lines (70 loc) · 2.45 KB
/
updateFauxPasRules.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
// Update profile-fauxpas.xml from Faux Pas online rules 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
import groovy.json.JsonBuilder
def parseRules(url, catMapping) {
def result = []
def http = new HTTPBuilder(url)
http.contentEncoding = ContentEncoding.Type.GZIP
def html = http.get(contentType : 'text/html;charset=UTF-8')
def categories = html."**".findAll {[email protected]().contains('tag-section')}
categories.each {cat ->
def rules = cat."**".findAll {[email protected]().contains('rule')}
rules.each {r ->
def k = r."**".find {[email protected]().contains("short-name")}.text()
def rule = [
category: cat.H2.text(),
key: k,
name: (r.H3.text().trim().replaceAll('\\n', ' ') - k).trim(),
description: r."**".find {[email protected]().contains("description")}.text().trim().replaceAll('\\n', ' '),
severity: catMapping[cat.H2.text()]
]
result.add(rule)
}
}
return result
}
def writeProfileFauxPas(rls, file) {
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.profile() {
name "FauxPas"
language "objc"
rules {
rls.each {rl ->
rule {
repositoryKey "FauxPas"
key rl.key
}
}
}
}
file.text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + writer.toString()
}
def writeRules(rls, file) {
def builder = new JsonBuilder()
builder(rls)
file.text = builder.toPrettyString()
}
// Files
File rulesJson = new File('src/main/resources/org/sonar/plugins/fauxpas/rules.json')
File profileXml = new File('src/main/resources/org/sonar/plugins/fauxpas/profile-fauxpas.xml')
// Parse online documentation
def rules = parseRules('http://fauxpasapp.com/rules/', [
BestPractice: 'MAJOR',
Resources: 'MAJOR',
Config: 'MINOR',
Localization: 'MAJOR',
APIUsage: 'CRITICAL',
VCS: 'INFO',
Style: 'MAJOR',
Pedantic: 'MINOR',
Miscellaneous: 'MINOR'
])
// Write profile
writeProfileFauxPas(rules, profileXml)
// Write rules
writeRules(rules, rulesJson)