View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Ant Task for Qulice.
51   *
52   * @since 0.13
53   * @checkstyle ClassDataAbstractionCouplingCheck (170 lines)
54   */
55  public final class QuliceTask extends Task {
56  
57      /**
58       * Sources dirs.
59       */
60      private Path sources;
61  
62      /**
63       * Classes dir (only one dir is supported).
64       */
65      private File classes;
66  
67      /**
68       * Classpath dirs and files.
69       */
70      private Path classpath;
71  
72      /**
73       * Set source dirs.
74       * @param srcdr Source dirs
75       */
76      public void setSrcdir(final Path srcdr) {
77          this.sources = srcdr;
78      }
79  
80      /**
81       * Set classes dir.
82       * @param clssedr Classes dir
83       */
84      public void setClassesdir(final File clssedr) {
85          this.classes = clssedr;
86      }
87  
88      /**
89       * Set classpath.
90       * @param clsspth Classpath
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      * Create Environment.
120      * @return Environment.
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      * Validate and throws exception if there are any problems.
142      * @param env Environment
143      * @throws ValidationException If there are any problems.
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      * Create collection of validators.
179      * @return Collection of validators.
180      */
181     private static Collection<Validator> validators() {
182         return Arrays.asList();
183     }
184 
185     /**
186      * Create collection of validators.
187      * @param env Environment to use.
188      * @return Collection of validators.
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 }