-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathupdateDartAnalyzerRules.groovy
223 lines (184 loc) · 6.68 KB
/
updateDartAnalyzerRules.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
218
219
220
221
222
223
/*
* SonarQube Flutter Plugin - Enables analysis of Dart and Flutter projects into SonarQube.
* Copyright © 2020 inside|app (contact@insideapp.fr)
*
* 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 rules.json and profile-dartanalyzer.xml from Dart Linter rules website.
Severity is assigned based on category and rule naming
*/
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.*
@Grab(group = 'org.codehaus.groovy.modules.http-builder',
module = 'http-builder', version = '0.7')
import groovy.xml.*
import commons.RuleUpdater
import commons.ConsoleString
import commons.Rule
String.mixin(ConsoleString)
/** Parse rule HTML description
* @param ruleKey Rule key
* @return HTML content as String
*/
def parseRuleDescription(String ruleKey) {
try {
def http = new HTTPBuilder('https://dart.dev/tools/linter-rules/' + ruleKey)
def html = http.get([:])
def root = html."**".find { it.name() == "ARTICLE" }
def description = ""
def started = false
root."**".findAll { it.name() == "DIV" || it.name() == "P" || it.name() == "H2" || it.name() == "CODE" }.each {tag ->
switch (tag.name()) {
case 'DIV':
if (tag.text().startsWith("Details")) {
started = true
}
break
case 'H2':
if (tag.text().startsWith("Usage")) {
started = false
}
break
case 'P':
if (started) {
description += XmlUtil.serialize(tag).replace('<?xml version="1.0" encoding="UTF-8"?>', '')
}
break
case 'CODE':
if (started) {
description += XmlUtil.serialize(tag).replace('<?xml version="1.0" encoding="UTF-8"?>', '')
}
break
}
}
return '<SECTION>' + description + '</SECTION>'
} catch (Exception e) {
return null
}
}
/**
* Parse rules from the lint website
* @param url
* @return
*/
def parseRules(url) {
def result = []
def http = new HTTPBuilder(url)
def html = http.get([:])
def root = html."**".find { it.name() == "ARTICLE" }
def currentKey = ''
def currentActive = true
def ruleIds = []
root."**".findAll { it.name() == "P" }.each { pTag ->
currentActive = !pTag.text().contains("(Removed)")
pTag."**".findAll { it.name() == "A" }.each { aTag ->
aTag."**".findAll { it.name() == "CODE" }.each { codeTag ->
if (!codeTag.text().contains(' ')) {
currentKey = codeTag.text()
def description = parseRuleDescription(currentKey)
if (description != null) {
def rule = new Rule(
currentKey,
description,
null, // no severity
null, // no type
currentKey.replace("_", " ").capitalize(),
null, // no debt
currentActive
)
result.add(rule)
currentActive = true
currentKey = ''
}
}
}
}
}
result
}
/**
* Parse rules from the diagnostic messages website
* @param url
* @return
*/
def parseRulesFromDiagnostic(url) {
def result = []
def http = new HTTPBuilder(url)
def html = http.get([:])
def root = html."**".find { it.name() == "ARTICLE" }
def currentKey = ''
def currentDescription = ''
def descriptionOver = false
root."**".findAll { it.name() == "H3" || it.name() == "H4" || it.name() == "P" }.each { tag ->
switch (tag.name()) {
case 'H3':
currentKey = tag.text()
break
case 'H4':
if (tag.text() == "Description") {
//We might have several lines of entries
descriptionOver = false
currentDescription = ""
} else if (tag.text() == "Examples") {
descriptionOver = true
//When reaching examples, we consider the description over
//todo but we might want to provider nicer description with examples later !
def rule = new Rule(
currentKey,
currentDescription += "\n @see https://dart.dev/tools/diagnostic-messages#$currentKey",
null, // no severity
null, // no type
currentKey.replace("_", " ").capitalize(),
null, // no debt
true // active
)
result.add(rule)
}
break
case 'P':
if (!descriptionOver) {
currentDescription += tag.text()
}
break
}
}
result
}
/**
* Write default analysis_options.yaml
* @param rls
* @param file
* @return
*/
def writeAnalysisOptions(rls, file) {
text = "linter:\n"
text += " rules:\n"
rls.forEach { r ->
text += " - ${r.key}\n"
}
file.text = text
}
def updater = new RuleUpdater('dart-lang/src/main/resources/dartanalyzer/rules.json', {
def rules = parseRules('https://dart.dev/tools/linter-rules')
rules.addAll(parseRulesFromDiagnostic('https://dart.dev/tools/diagnostic-messages'))
writeAnalysisOptions(rules, new File('dart-lang/src/main/resources/dartanalyzer/analysis_options.yaml'))
return rules
})
updater.update(Integer.parseInt(project.properties['scripts.max-manual']))