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 package com.qulice.ant;
32
33 import com.jcabi.log.Logger;
34 import com.qulice.checkstyle.CheckstyleValidator;
35 import com.qulice.pmd.PmdValidator;
36 import com.qulice.spi.Environment;
37 import com.qulice.spi.ResourceValidator;
38 import com.qulice.spi.ValidationException;
39 import com.qulice.spi.Validator;
40 import com.qulice.spi.Violation;
41 import java.io.File;
42 import java.util.Arrays;
43 import java.util.Collection;
44 import java.util.LinkedList;
45 import org.apache.tools.ant.BuildException;
46 import org.apache.tools.ant.Task;
47 import org.apache.tools.ant.types.Path;
48
49
50
51
52
53
54
55 public final class QuliceTask extends Task {
56
57
58
59
60 private Path sources;
61
62
63
64
65 private File classes;
66
67
68
69
70 private Path classpath;
71
72
73
74
75
76 public void setSrcdir(final Path srcdr) {
77 this.sources = srcdr;
78 }
79
80
81
82
83
84 public void setClassesdir(final File clssedr) {
85 this.classes = clssedr;
86 }
87
88
89
90
91
92 public void setClasspath(final Path clsspth) {
93 this.classpath = clsspth;
94 }
95
96 @Override
97 @SuppressWarnings({ "PMD.GuardLogStatement", "PMD.PrematureDeclaration"})
98 public void execute() {
99 super.execute();
100 final Environment env = this.environment();
101 final long start = System.nanoTime();
102 try {
103 QuliceTask.validate(env);
104 } catch (final ValidationException ex) {
105 Logger.info(
106 this,
107 "Read our quality policy: http://www.qulice.com/quality.html"
108 );
109 throw new BuildException("Failure", ex);
110 }
111 Logger.info(
112 this,
113 "Qulice quality check completed in %[nano]s, no errors",
114 System.nanoTime() - start
115 );
116 }
117
118
119
120
121
122 private Environment environment() {
123 if (this.sources == null) {
124 throw new BuildException("srcdir not defined for QuliceTask");
125 }
126 if (this.classes == null) {
127 throw new BuildException("classesdir not defined for QuliceTask");
128 }
129 if (this.classpath == null) {
130 throw new BuildException("classpath not defined for QuliceTask");
131 }
132 return new AntEnvironment(
133 new AntProject.Default(this.getProject()),
134 new AntPath.Default(this.sources),
135 this.classes,
136 new AntPath.Default(this.classpath)
137 );
138 }
139
140
141
142
143
144
145 private static void validate(final Environment env)
146 throws ValidationException {
147 final Collection<Violation> results = new LinkedList<>();
148 final Collection<File> files = env.files("*.*");
149 if (!files.isEmpty()) {
150 final Collection<ResourceValidator> validators =
151 QuliceTask.validators(env);
152 for (final ResourceValidator validator : validators) {
153 results.addAll(validator.validate(files));
154 }
155 for (final Violation result : results) {
156 Logger.info(
157 QuliceTask.class,
158 "%s: %s[%s]: %s (%s)",
159 result.validator(),
160 result.file(),
161 result.lines(),
162 result.message(),
163 result.name()
164 );
165 }
166 }
167 for (final Validator validator : QuliceTask.validators()) {
168 validator.validate(env);
169 }
170 if (!results.isEmpty()) {
171 throw new ValidationException(
172 String.format("%d violations, see log above", results.size())
173 );
174 }
175 }
176
177
178
179
180
181 private static Collection<Validator> validators() {
182 return Arrays.asList();
183 }
184
185
186
187
188
189
190 private static Collection<ResourceValidator> validators(
191 final Environment env) {
192 return Arrays.asList(
193 new CheckstyleValidator(env),
194 new PmdValidator(env)
195 );
196 }
197 }