Skip to content

Commit f93deb0

Browse files
Support new config format. Fix #4
1 parent 82e5389 commit f93deb0

File tree

4 files changed

+106
-45
lines changed

4 files changed

+106
-45
lines changed

pmd.groovy

+75-40
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,100 @@
33
import groovy.json.JsonSlurper
44
import groovy.util.FileNameFinder
55

6-
def appContext = setupContext(args)
7-
def parsedConfig = new JsonSlurper().parse(new File(appContext.configFile), "UTF-8")
86

9-
def includePaths = parsedConfig.include_paths?.join(" ")
10-
def codeFolder = new File(appContext.codeFolder)
7+
class Config {
8+
static final FILE_LIST = "/tmp/files"
9+
def args
10+
def appContext
11+
def parsedConfig
12+
def filesToAnalyze
13+
14+
Config(args) {
15+
this.args = args
16+
this.appContext = setupContext()
17+
this.parsedConfig = new JsonSlurper().parse(new File(appContext.configFile), "UTF-8")
18+
this.filesToAnalyze = filesToAnalyze()
19+
saveFilesToAnalyze()
20+
}
21+
22+
def ruleSet() {
23+
def configFile = parsedConfig.config instanceof String ? parsedConfig.config : parsedConfig.config.file
24+
25+
if(fileExists(configFile)) {
26+
return configFile
27+
}
28+
29+
"/usr/src/app/ruleset.xml"
30+
}
31+
32+
def fileExists(file) {
33+
new File(file).exists()
34+
}
35+
36+
def noFiles() {
37+
filesToAnalyze.isEmpty()
38+
}
39+
40+
def saveFilesToAnalyze() {
41+
File analysisFilesTmp = createTempFile()
42+
analysisFilesTmp << filesToAnalyze
43+
}
1144

12-
def filesToAnalyse = new FileNameFinder().getFileNames(appContext.codeFolder, includePaths)
45+
def filesToAnalyze() {
46+
def includePaths = parsedConfig.include_paths?.join(" ")
47+
def codeFolder = new File(appContext.codeFolder)
1348

14-
File analysisFilesTmp = new File("/tmp/files")
15-
analysisFilesTmp.createNewFile()
16-
analysisFilesTmp.deleteOnExit()
49+
def files = new FileNameFinder().getFileNames(appContext.codeFolder, includePaths)
1750

18-
def i = filesToAnalyse.iterator()
19-
while(i.hasNext()) {
20-
string = i.next()
21-
if( !string.endsWith(".java") ) {
51+
def i = files.iterator()
52+
while(i.hasNext()) {
53+
def name = i.next()
54+
if(!name.endsWith(".java")) {
2255
i.remove()
56+
}
2357
}
24-
}
2558

26-
filesToAnalyse = filesToAnalyse.toString()
27-
filesToAnalyse = filesToAnalyse.substring(1, filesToAnalyse.length()-1).replaceAll("\\s+","")
28-
if (filesToAnalyse.isEmpty()) {
29-
System.exit(0)
30-
}
59+
def fileNames = files.toString()
60+
fileNames.substring(1, fileNames.length()-1).replaceAll("\\s+","")
61+
}
3162

32-
analysisFilesTmp << filesToAnalyse
63+
def createTempFile() {
64+
File tmp = new File(FILE_LIST)
65+
tmp.createNewFile()
66+
tmp.deleteOnExit()
67+
tmp
68+
}
3369

34-
def ruleSetPath
35-
if ( parsedConfig.config && (new File(parsedConfig.config).exists()) ) {
36-
ruleSetPath = parsedConfig.config
37-
} else {
38-
ruleSetPath = "/usr/src/app/ruleset.xml"
70+
def setupContext() {
71+
def cli = new CliBuilder(usage:"${this.class.name}")
72+
cli._(longOpt: "configFile", required: true, args: 1, "Path to config.json file")
73+
cli._(longOpt: "codeFolder", required: true, args: 1, "Path to code folder")
74+
cli.parse(args)
75+
}
3976
}
4077

41-
def pmdCommand = "/usr/src/app/lib/pmd/bin/run.sh pmd -filelist /tmp/files -f codeclimate -R ${ruleSetPath} -failOnViolation false"
78+
/* ********** MAIN ********** */
4279

43-
ProcessBuilder builder = new ProcessBuilder( pmdCommand.split(' ') )
80+
def config = new Config(args)
81+
if (config.noFiles()) {
82+
System.exit(0)
83+
}
4484

45-
Process process = builder.start()
85+
def command = "/usr/src/app/lib/pmd/bin/run.sh pmd -filelist ${Config.FILE_LIST} -f codeclimate -R ${config.ruleSet()} -failOnViolation false"
4686

47-
InputStream stdout = process.getInputStream ()
48-
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout))
87+
ProcessBuilder builder = new ProcessBuilder(command.split(' '))
88+
Process process = builder.start()
4989

50-
while ((line = reader.readLine ()) != null) {
51-
System.out.println ( line )
90+
InputStream stdout = process.getInputStream()
91+
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout))
92+
while ((line = reader.readLine()) != null) {
93+
System.out.println(line)
5294
}
5395

5496
process.waitForProcessOutput()
5597

56-
if ( process.exitValue() != 0 ) {
57-
System.exit(-1)
98+
if (process.exitValue() != 0) {
99+
System.exit(-1)
58100
}
59101

60102
System.exit(0)
61-
62-
def setupContext(cmdArgs) {
63-
def cli = new CliBuilder(usage:"${this.class.name}")
64-
cli._(longOpt: "configFile", required: true, args: 1, "Path to config.json file")
65-
cli._(longOpt: "codeFolder", required: true, args: 1, "Path to code folder")
66-
cli.parse(cmdArgs)
67-
}

test.groovy

+19-5
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,31 @@
33
import groovy.json.JsonSlurper
44
import groovy.util.FileNameFinder
55

6-
def sanityCheck() {
7-
def command = "/usr/src/app/pmd.groovy --codeFolder=/code/test --configFile=/code/test/config.json"
8-
6+
def execute(command) {
97
def proc = command.execute()
108
def out = new StringBuffer()
119
def err = new StringBuffer()
1210

1311
proc.waitForProcessOutput(out, err)
1412

15-
assert proc.exitValue() == 0
13+
return [proc, out, err]
14+
}
15+
16+
def sanityCheck() {
17+
def (proc, out, err) = execute("/usr/src/app/pmd.groovy --codeFolder=/code/test --configFile=/code/test/config.json")
18+
1619
assert !out.toString().isEmpty()
1720
assert err.toString().isEmpty()
21+
assert proc.exitValue() == 0
22+
}
23+
24+
def checkConfigBackwardCompatibility() {
25+
def (proc, out, _err) = execute("/usr/src/app/pmd.groovy --codeFolder=/code/test --configFile=/code/test/config.json")
26+
def (procOld, outOld, _errOld) = execute("/usr/src/app/pmd.groovy --codeFolder=/code/test --configFile=/code/test/config.old.json")
27+
28+
assert proc.exitValue() == procOld.exitValue()
29+
assert out.toString().equals(outOld.toString())
30+
assert proc.exitValue() == 0
1831
}
1932

2033
def engineCheckList() {
@@ -45,6 +58,7 @@ def dockerfileCheckList() {
4558

4659
/** MAIN **/
4760

48-
sanityCheck()
4961
engineCheckList()
5062
dockerfileCheckList()
63+
sanityCheck()
64+
checkConfigBackwardCompatibility()

test/config.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"enabled": true,
33
"channel": "beta",
4+
"config": {
5+
"file": "test/rules.xml",
6+
},
47
"include_paths": [
58
"Main.java"
69
]

test/config.old.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"enabled": true,
3+
"channel": "beta",
4+
"config": "test/rules.xml",
5+
"include_paths": [
6+
"Main.java"
7+
]
8+
}
9+

0 commit comments

Comments
 (0)