|
3 | 3 | import groovy.json.JsonSlurper
|
4 | 4 | import groovy.util.FileNameFinder
|
5 | 5 |
|
6 |
| -def appContext = setupContext(args) |
7 |
| -def parsedConfig = new JsonSlurper().parse(new File(appContext.configFile), "UTF-8") |
8 | 6 |
|
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 | + } |
11 | 44 |
|
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) |
13 | 48 |
|
14 |
| -File analysisFilesTmp = new File("/tmp/files") |
15 |
| -analysisFilesTmp.createNewFile() |
16 |
| -analysisFilesTmp.deleteOnExit() |
| 49 | + def files = new FileNameFinder().getFileNames(appContext.codeFolder, includePaths) |
17 | 50 |
|
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")) { |
22 | 55 | i.remove()
|
| 56 | + } |
23 | 57 | }
|
24 |
| -} |
25 | 58 |
|
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 | + } |
31 | 62 |
|
32 |
| -analysisFilesTmp << filesToAnalyse |
| 63 | + def createTempFile() { |
| 64 | + File tmp = new File(FILE_LIST) |
| 65 | + tmp.createNewFile() |
| 66 | + tmp.deleteOnExit() |
| 67 | + tmp |
| 68 | + } |
33 | 69 |
|
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 | + } |
39 | 76 | }
|
40 | 77 |
|
41 |
| -def pmdCommand = "/usr/src/app/lib/pmd/bin/run.sh pmd -filelist /tmp/files -f codeclimate -R ${ruleSetPath} -failOnViolation false" |
| 78 | +/* ********** MAIN ********** */ |
42 | 79 |
|
43 |
| -ProcessBuilder builder = new ProcessBuilder( pmdCommand.split(' ') ) |
| 80 | +def config = new Config(args) |
| 81 | +if (config.noFiles()) { |
| 82 | + System.exit(0) |
| 83 | +} |
44 | 84 |
|
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" |
46 | 86 |
|
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() |
49 | 89 |
|
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) |
52 | 94 | }
|
53 | 95 |
|
54 | 96 | process.waitForProcessOutput()
|
55 | 97 |
|
56 |
| -if ( process.exitValue() != 0 ) { |
57 |
| - System.exit(-1) |
| 98 | +if (process.exitValue() != 0) { |
| 99 | + System.exit(-1) |
58 | 100 | }
|
59 | 101 |
|
60 | 102 | 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 |
| -} |
|
0 commit comments