-
Notifications
You must be signed in to change notification settings - Fork 275
/
updateFauxPasRules.groovy
117 lines (92 loc) · 3.51 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Swift SonarQube Plugin - Enables analysis of Swift and Objective-C projects into SonarQube.
* Copyright © 2015 Backelite (${email})
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Update profile-fauxpas.xml from Faux Pas online rules documentation
// Severity is determined from the category
import groovy.json.JsonBuilder
import groovy.xml.MarkupBuilder
@Grab(group = 'org.codehaus.groovy.modules.http-builder',
module = 'http-builder', version = '0.7')
import groovyx.net.http.*
@Grab(group = 'org.codehaus.groovy.modules.http-builder',
module = 'http-builder', version = '0.7')
import groovyx.net.http.*
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('objclang/src/main/resources/org/sonar/plugins/fauxpas/rules.json')
File profileXml = new File('objclang/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)