-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpmd.groovy
executable file
·67 lines (49 loc) · 1.83 KB
/
pmd.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
#!/usr/bin/env groovy
import groovy.json.JsonSlurper
import groovy.util.FileNameFinder
def appContext = setupContext(args)
def includePaths = new JsonSlurper().parse(new File(appContext.configFile), "UTF-8").include_paths?.join(" ").replace("./", "")
def codeFolder = new File(appContext.codeFolder)
def filesToAnalyse = new FileNameFinder().getFileNames(appContext.codeFolder, includePaths)
def i = filesToAnalyse.iterator()
while(i.hasNext()) {
string = i.next()
if( !string.endsWith(".cls") && !string.endsWith(".trigger") ) {
i.remove()
}
}
if (filesToAnalyse.isEmpty()) {
System.exit(0)
}
def fileList = File.createTempFile("apexmetrics-filelist-", null, null)
fileList.deleteOnExit()
fileList.write filesToAnalyse.join(",\n")
fileList << "\n"
def ruleset
def defaultRulesetLocation = "/usr/src/app/apex-ruleset.xml"
def customRulesetLocation = "/code/apex-ruleset.xml"
if ( new File(customRulesetLocation).exists() ) {
ruleset = customRulesetLocation
}
else {
ruleset = defaultRulesetLocation
}
def pmdCommand = "/usr/src/app/lib/pmd/bin/run.sh pmd -filelist ${fileList} -f codeclimate -R ${ruleset} -l apex -v 35 -failOnViolation false"
ProcessBuilder builder = new ProcessBuilder( pmdCommand.split(' ') )
Process process = builder.start()
InputStream stdout = process.getInputStream ()
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout))
while ((line = reader.readLine ()) != null) {
System.out.println ( line )
}
process.waitForProcessOutput()
if ( process.exitValue() != 0 ) {
System.exit(-1)
}
System.exit(0)
def setupContext(cmdArgs) {
def cli = new CliBuilder(usage:"${this.class.name}")
cli._(longOpt: "configFile", required: true, args: 1, "Path to config.json file")
cli._(longOpt: "codeFolder", required: true, args: 1, "Path to code folder")
cli.parse(cmdArgs)
}