|
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 | + def args |
| 9 | + def appContext |
| 10 | + def parsedConfig |
| 11 | + def filesToAnalyze |
| 12 | + def filesList |
| 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 | + this.filesList = createTempFile() |
| 20 | + filesList << filesToAnalyze |
| 21 | + } |
| 22 | + |
| 23 | + def noFiles() { |
| 24 | + filesToAnalyze.isEmpty() |
| 25 | + } |
| 26 | + |
| 27 | + def ruleSet() { |
| 28 | + if(parsedConfig.config) { |
| 29 | + def configFile = parsedConfig.config instanceof String ? parsedConfig.config : parsedConfig.config.file |
| 30 | + |
| 31 | + if(fileExists(configFile)) { |
| 32 | + return configFile |
| 33 | + } else { |
| 34 | + System.err.println "Config file ${configFile} not found" |
| 35 | + System.exit(1) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + "/usr/src/app/ruleset.xml" |
| 40 | + } |
| 41 | + |
| 42 | + def filesListPath() { |
| 43 | + filesList.absolutePath |
| 44 | + } |
11 | 45 |
|
12 |
| -def filesToAnalyse = new FileNameFinder().getFileNames(appContext.codeFolder, includePaths) |
| 46 | + private def fileExists(file) { |
| 47 | + new File(file).exists() |
| 48 | + } |
13 | 49 |
|
14 |
| -File analysisFilesTmp = new File("/tmp/files") |
15 |
| -analysisFilesTmp.createNewFile() |
16 |
| -analysisFilesTmp.deleteOnExit() |
| 50 | + private def filesToAnalyze() { |
| 51 | + def includePaths = parsedConfig.include_paths?.join(" ") |
| 52 | + def codeFolder = new File(appContext.codeFolder) |
17 | 53 |
|
18 |
| -def i = filesToAnalyse.iterator() |
19 |
| -while(i.hasNext()) { |
20 |
| - string = i.next() |
21 |
| - if( !string.endsWith(".java") ) { |
| 54 | + def files = new FileNameFinder().getFileNames(appContext.codeFolder, includePaths) |
| 55 | + |
| 56 | + def i = files.iterator() |
| 57 | + while(i.hasNext()) { |
| 58 | + def name = i.next() |
| 59 | + if(!name.endsWith(".java")) { |
22 | 60 | i.remove()
|
| 61 | + } |
23 | 62 | }
|
24 |
| -} |
25 | 63 |
|
26 |
| -filesToAnalyse = filesToAnalyse.toString() |
27 |
| -filesToAnalyse = filesToAnalyse.substring(1, filesToAnalyse.length()-1).replaceAll("\\s+","") |
28 |
| -if (filesToAnalyse.isEmpty()) { |
29 |
| - System.exit(0) |
30 |
| -} |
| 64 | + def fileNames = files.toString() |
| 65 | + fileNames.substring(1, fileNames.length()-1).replaceAll("\\s+","") |
| 66 | + } |
31 | 67 |
|
32 |
| -analysisFilesTmp << filesToAnalyse |
| 68 | + private def createTempFile() { |
| 69 | + File tmp = File.createTempFile("files", ".txt") |
| 70 | + tmp.deleteOnExit() |
| 71 | + tmp |
| 72 | + } |
33 | 73 |
|
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" |
| 74 | + private def setupContext() { |
| 75 | + def cli = new CliBuilder(usage:"${this.class.name}") |
| 76 | + cli._(longOpt: "configFile", required: true, args: 1, "Path to config.json file") |
| 77 | + cli._(longOpt: "codeFolder", required: true, args: 1, "Path to code folder") |
| 78 | + cli.parse(args) |
| 79 | + } |
39 | 80 | }
|
40 | 81 |
|
41 |
| -def pmdCommand = "/usr/src/app/lib/pmd/bin/run.sh pmd -filelist /tmp/files -f codeclimate -R ${ruleSetPath} -failOnViolation false" |
| 82 | +/* ********** MAIN ********** */ |
42 | 83 |
|
43 |
| -ProcessBuilder builder = new ProcessBuilder( pmdCommand.split(' ') ) |
| 84 | +def config = new Config(args) |
| 85 | +if (config.noFiles()) { |
| 86 | + System.exit(0) |
| 87 | +} |
44 | 88 |
|
45 |
| -Process process = builder.start() |
| 89 | +def command = "/usr/src/app/lib/pmd/bin/run.sh pmd -filelist ${config.filesListPath()} -f codeclimate -R ${config.ruleSet()} -failOnViolation false" |
46 | 90 |
|
47 |
| -InputStream stdout = process.getInputStream () |
48 |
| -BufferedReader reader = new BufferedReader (new InputStreamReader(stdout)) |
| 91 | +ProcessBuilder builder = new ProcessBuilder(command.split(' ')) |
| 92 | +Process process = builder.start() |
49 | 93 |
|
50 |
| -while ((line = reader.readLine ()) != null) { |
51 |
| - System.out.println ( line ) |
| 94 | +InputStream stdout = process.getInputStream() |
| 95 | +BufferedReader reader = new BufferedReader(new InputStreamReader(stdout)) |
| 96 | +while ((line = reader.readLine()) != null) { |
| 97 | + System.out.println(line) |
52 | 98 | }
|
53 | 99 |
|
54 | 100 | process.waitForProcessOutput()
|
55 | 101 |
|
56 |
| -if ( process.exitValue() != 0 ) { |
57 |
| - System.exit(-1) |
| 102 | +if (process.exitValue() != 0) { |
| 103 | + System.exit(-1) |
58 | 104 | }
|
59 | 105 |
|
60 | 106 | 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