Skip to content

Commit 481e74a

Browse files
committed
feat(inspect): Add support for skipping affected files via regex
1 parent 54cbfdd commit 481e74a

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

README.asciidoc

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,22 @@ For a full list of options please run `ideainspect.groovy -h`:
7676

7777
----
7878
usage: groovy ideainspect.groovy -i <IDEA_HOME> -p <PROFILEXML> -r <PROJDIR>
79-
-d,--dir <dir> Limit IDEA inspection to this directory
80-
-h,--help Show usage information and quit
81-
-i,--ideahome <dir> IDEA installation home directory. Required
82-
-l,--levels <level> Levels to look for. Default: WARNING,ERROR
83-
-p,--profile <file> Use this inspection profile file located
84-
".idea/inspectionProfiles".
85-
Example: "myprofile.xml"
86-
-r,--rootdir <dir> IDEA project root directory containing the ".idea"
87-
directory
88-
-s,--skip <file> Analysis result files to skip. For example
89-
"TodoComment" or "TodoComment.xml".
90-
Default: <empty>
91-
-t,--resultdir <dir> Target directory to place the IDEA inspection XML
92-
result files. Default: target/inspection-results
79+
-d,--dir <dir> Limit IDEA inspection to this directory
80+
-h,--help Show usage information and quit
81+
-i,--ideahome <dir> IDEA installation home directory. Required
82+
-l,--levels <level> Levels to look for. Default: WARNING,ERROR
83+
-p,--profile <file> Use this inspection profile file located
84+
".idea/inspectionProfiles".
85+
Example: "myprofile.xml"
86+
-r,--rootdir <dir> IDEA project root directory containing the
87+
".idea" directory
88+
-s,--skip <file> Analysis result files to skip. For example
89+
"TodoComment" or "TodoComment.xml".
90+
-sf,--skipfile <regex> Ignore issues affecting source files matching
91+
given regex. Example ".*/generated/.*".
92+
-t,--resultdir <dir> Target directory to place the IDEA inspection
93+
XML result files. Default:
94+
target/inspection-results
9395
----
9496

9597

@@ -182,6 +184,14 @@ Entries found. return code: 1
182184
The source code is located under https://github.com/bentolor/idea-cli-inspector.
183185

184186

187+
== Changes
188+
[cols="1,5", options="header"]
189+
|===
190+
| Version | Change
191+
| 1.1 | Support for ignoring issues affecting specific source files using a regular expression (Option `-sf`)
192+
| 1.0 | First release
193+
|===
194+
185195
== License
186196
Licensed under the Apache License, Version 2.0 (the "License");
187197
you may not use this file except in compliance with the License.

ideainspect.groovy

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
import groovy.io.FileType
2626
import org.apache.commons.cli.Option
2727

28-
println "= IntellIJ IDEA Code Analysis Wrapper - v1.0 - @bentolor"
28+
println "= IntellIJ IDEA Code Analysis Wrapper - v1.1 - @bentolor"
2929

3030
// Defaults
3131
def resultDir = "target/inspection-results"
3232
def acceptedLeves = ["[WARNING]", "[ERROR]"]
33-
def skipFiles = []
33+
def skipResults = []
34+
def skipIssueFilesRegex = []
3435
def ideaTimeout = 20 // broken - has no effect
3536

3637

@@ -47,11 +48,10 @@ if (opt.l) {
4748
acceptedLeves.clear()
4849
opt.ls.each { level -> acceptedLeves << "[" + level + "]" }
4950
}
50-
// Skip files
51-
if (opt.s) {
52-
skipFiles.clear()
53-
opt.ss.each { skipFile -> skipFiles << skipFile.replace(".xml", "") }
54-
}
51+
// Skip result XML files
52+
if (opt.s) opt.ss.each { skipFile -> skipResults << skipFile.replace(".xml", "") }
53+
// Skip issues affecting given file name regex
54+
if (opt.sf) opt.sfs.each { skipRegex -> skipIssueFilesRegex << skipRegex }
5555
// target directory
5656
if (opt.t) resultDir = opt.t
5757
// timeout
@@ -102,7 +102,7 @@ if (exitValue != 0) fail("IDEA Process returned with an unexpected return code o
102102
//
103103
// --- Now lets look on the results
104104
//
105-
analyzeResult(resultPath, acceptedLeves, skipFiles)
105+
analyzeResult(resultPath, acceptedLeves, skipResults, skipIssueFilesRegex)
106106

107107

108108
//
@@ -125,13 +125,15 @@ void fail(String message) {
125125

126126

127127
private OptionAccessor parseCli() {
128-
def cliBuilder = new CliBuilder(usage: 'groovy ideainspect.groovy -i <IDEA_HOME> -p <PROFILEXML> -r <PROJDIR>')
128+
def cliBuilder = new CliBuilder(usage: 'groovy ideainspect.groovy\n -i <IDEA_HOME> -p <PROFILEXML> -r <PROJDIR>')
129129
cliBuilder.with {
130130
h argName: 'help', longOpt: 'help', 'Show usage information and quit'
131131
l argName: 'level', longOpt: 'levels', args: Option.UNLIMITED_VALUES, valueSeparator: ',',
132132
'Levels to look for. Default: WARNING,ERROR'
133133
s argName: 'file', longOpt: 'skip', args: Option.UNLIMITED_VALUES, valueSeparator: ',',
134-
'Analysis result files to skip. For example "TodoComment" or "TodoComment.xml". \nDefault: <empty>'
134+
'Analysis result files to skip. For example "TodoComment" or "TodoComment.xml".'
135+
sf argName: 'regex', longOpt: 'skipfile', args: Option.UNLIMITED_VALUES, valueSeparator: ',',
136+
'Ignore issues affecting source files matching given regex. Example ".*/generated/.*".'
135137
t argName: 'dir', longOpt: 'resultdir', args: 1,
136138
'Target directory to place the IDEA inspection XML result files. Default: target/inspection-results'
137139
i argName: 'dir', longOpt: 'ideahome', args: 1, required: true,
@@ -163,13 +165,15 @@ private OptionAccessor parseCli() {
163165
}
164166

165167

166-
private analyzeResult(File resultPath, List<String> acceptedLeves, List skipFiles) {
168+
private analyzeResult(File resultPath, List<String> acceptedLeves,
169+
List skipResults, List skipIssueFilesRegex) {
167170
println " "
168171
println "#"
169172
println "# Inspecting produced result files in $resultPath"
170173
println "#"
171-
println "# Looking for: $acceptedLeves"
172-
println "# Ignoring : $skipFiles"
174+
println "# Looking for levels : $acceptedLeves"
175+
println "# Ignoring result files : $skipResults"
176+
println "# Ignoring source files : $skipIssueFilesRegex"
173177

174178
def allGood = true;
175179

@@ -186,15 +190,17 @@ private analyzeResult(File resultPath, List<String> acceptedLeves, List skipFile
186190
def fileIssues = []
187191
def xmlFileName = file.name
188192

189-
if (skipFiles.contains(xmlFileName.replace(".xml", ""))) {
193+
if (skipResults.contains(xmlFileName.replace(".xml", ""))) {
190194
println "--- Skipping $xmlFileName"
191195
return
192196
}
193197

194198
xmlDocument.problem.each { problem ->
195199
String severity = problem.problem_class.@severity
196-
if (acceptedLeves.contains(severity)) {
197-
String affectedFile = problem.file.text()
200+
String affectedFile = problem.file.text()
201+
boolean fileShouldBeIgnored = false
202+
skipIssueFilesRegex.each { regex -> fileShouldBeIgnored = (fileShouldBeIgnored || affectedFile.matches(regex)) }
203+
if (acceptedLeves.contains(severity) && !fileShouldBeIgnored) {
198204
String problemDesc = problem.description.text()
199205
String line = problem.line.text()
200206
fileIssues << "$severity $affectedFile:$line -- $problemDesc";

0 commit comments

Comments
 (0)