1
+ package org.v1v.jenkins.jobdsl.jenkinslint.ant
2
+
3
+ import org.apache.tools.ant.BuildException
4
+ import org.apache.tools.ant.Task
5
+ import org.apache.tools.ant.types.FileSet
6
+ import org.v1v.jenkins.jobdsl.jenkinslint.JenkinsLintRunner
7
+
8
+ import java.util.logging.Logger
9
+
10
+ /**
11
+ * Ant Task for running JenkinsLint.
12
+ * At least one nested <code >fileset</code> element is required, and is used to specify the source files
13
+ * to be analyzed. This is the standard Ant <i >FileSet</i>, and is quite powerful and flexible.
14
+ * See the <i >Apache Ant Manual</i> for more information on <i >FileSets</i>.
15
+ * <p />
16
+ *
17
+ * @see "http://ant.apache.org/manual/index.html"
18
+ *
19
+ * @author Victor Martinez
20
+ */
21
+ class JenkinsLintTask extends Task {
22
+
23
+ /*
24
+ private static final LOG = Logger.getLogger(JenkinsLintTask)
25
+
26
+ String excludeBaseline
27
+
28
+ protected List fileSets = []
29
+ protected ruleSet
30
+
31
+ private final resourceFactory = new DefaultResourceFactory()
32
+
33
+ // Abstract creation of the CodeNarcRunner instance to allow substitution of test spy for unit tests
34
+ protected createJenkinsLintRunner = {
35
+ if (excludeBaseline) {
36
+ LOG.info("Loading baseline violations from [$excludeBaseline]")
37
+ def resource = resourceFactory.getResource(excludeBaseline)
38
+ def resultsProcessor = new BaselineResultsProcessor(resource)
39
+ return new JenkinsLintRunner(resultsProcessor:resultsProcessor)
40
+ }
41
+ return new JenkinsLintRunner()
42
+ }
43
+
44
+ void execute() throws BuildException {
45
+ assert ruleSetFiles
46
+ assert fileSets
47
+
48
+ def sourceAnalyzer = createSourceAnalyzer()
49
+ def codeNarcRunner = createJenkinsLintRunner()
50
+ codeNarcRunner.ruleSetFiles = ruleSetFiles
51
+ codeNarcRunner.reportWriters = reportWriters
52
+ codeNarcRunner.sourceAnalyzer = sourceAnalyzer
53
+
54
+ def results = codeNarcRunner.execute()
55
+
56
+ checkMaxViolations(results)
57
+ }
58
+
59
+ void addFileset(FileSet fileSet) {
60
+ assert fileSet
61
+ this.fileSets << fileSet
62
+ }
63
+
64
+ protected SourceAnalyzer createSourceAnalyzer() {
65
+ new AntFileSetSourceAnalyzer(getProject(), fileSets)
66
+
67
+ }
68
+
69
+ private void checkMaxViolations(Results results) {
70
+ def p1 = results.getNumberOfViolationsWithPriority(1, true)
71
+ def p2 = results.getNumberOfViolationsWithPriority(2, true)
72
+ def p3 = results.getNumberOfViolationsWithPriority(3, true)
73
+ def countsText = "(p1=$p1; p2=$p2; p3=$p3)"
74
+
75
+ checkMaxViolationForPriority(1, p1, countsText)
76
+ checkMaxViolationForPriority(2, p2, countsText)
77
+ checkMaxViolationForPriority(3, p3, countsText)
78
+ }
79
+
80
+ private void checkMaxViolationForPriority(int priority, int count, String countsText) {
81
+ if (count > this."maxPriority${priority}Violations") {
82
+ throw new BuildException("Exceeded maximum number of priority ${priority} violations: " + countsText)
83
+ }
84
+ }
85
+ */
86
+ }
0 commit comments