View Javadoc
1   /*
2    * Copyright 2015-2016 Mark Prins, GeoDienstenCentrum
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package nl.geodienstencentrum.maven.plugin.sass.report;
17  
18  import java.io.File;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Locale;
22  import java.util.Map;
23  import java.util.ResourceBundle;
24  import nl.geodienstencentrum.maven.plugin.sass.Resource;
25  import org.apache.maven.doxia.siterenderer.Renderer;
26  import org.apache.maven.plugins.annotations.Component;
27  import org.apache.maven.plugins.annotations.Execute;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.plugins.annotations.ResolutionScope;
32  import org.apache.maven.project.MavenProject;
33  import org.apache.maven.reporting.AbstractMavenReport;
34  
35  /**
36   * SCSSLintMojo executes scss-lint-report goal.
37   *
38   * @author mprins
39   * @since 2.3
40   */
41  @Mojo(name = "scss-lint-report",
42          defaultPhase = LifecyclePhase.SITE,
43          requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
44          threadSafe = false)
45  @Execute(goal = "scss-lint"
46  //        , phase = LifecyclePhase.COMPILE
47  )
48  public class SCSSLintReportMojo extends AbstractMavenReport {
49  
50  	/**
51  	 * ignored for linting, sass options are not used.
52  	 *
53  	 * @since 2.0
54  	 */
55  	@Parameter
56  	private Map<String, String> sassOptions;
57  
58  	/**
59  	 * Directory containing Sass files, defaults to the Maven Web application
60  	 * sources directory (${basedir}/src/main/sass).
61  	 *
62  	 * @since 2.3
63  	 */
64  	@Parameter(defaultValue = "${basedir}/src/main/sass", property = "sassSourceDirectory")
65  	private File sassSourceDirectory;
66  
67  	/**
68  	 * Sources for compilation with their destination directory containing Sass
69  	 * files. Allows for multiple resource sources and destinations. If
70  	 * specified it precludes the direct specification of
71  	 * sassSourceDirectory/relativeOutputDirectory/destination parameters.
72  	 *
73  	 * Example configuration:
74  	 *
75  	 * <pre>
76  	 *      &lt;resource&gt;
77  	 *          &lt;source&gt;
78  	 *              &lt;directory&gt;${basedir}/src/main/webapp&lt;/directory&gt;
79  	 *              &lt;includes&gt;
80  	 *                  &lt;include&gt;**&#47;scss&lt;/include&gt;
81  	 *              &lt;/includes&gt;
82  	 *          &lt;/source&gt;
83  	 *          &lt;relativeOutputDirectory&gt;..&lt;/relativeOutputDirectory&gt;
84  	 *          &lt;destination&gt;
85  	 *              ${project.build.directory}/${project.build.finalName}
86  	 *          &lt;/destination&gt;
87  	 *      &lt;/resource&gt;
88  	 * </pre>
89  	 *
90  	 * <em>Only {@code source/directory} paths are considered during
91  	 * linting.</em>
92  	 *
93  	 * @since 2.0
94  	 */
95  	@Parameter
96  	private List<Resource> resources = Collections.emptyList();
97  
98  	/**
99  	 * Specifies if the build should fail upon an error violation.
100 	 */
101 	@Parameter(defaultValue = "false")
102 	private boolean failOnError;
103 
104 	/**
105 	 * Output file name for the plugin.
106 	 *
107 	 * @since 2.3
108 	 */
109 	@Parameter(defaultValue = "scss-lint", property = "outputName", required = true)
110 	private String outputName;
111 
112 	/**
113 	 * Output directory for the plugin.
114 	 *
115 	 * @since 2.3
116 	 */
117 	@Parameter(defaultValue = "${project.build.directory}/site/", required = true)
118 	private File outputDirectory;
119 
120 	@Parameter(defaultValue = "${project}", readonly = true, required = true)
121 	private MavenProject project;
122 
123 	@Component
124 	private Renderer siteRenderer;
125 
126 	/**
127 	 * skip execution.
128 	 *
129 	 * @since 2.9
130 	 */
131 	@Parameter(defaultValue = "false")
132 	private boolean skip;
133 
134 	/**
135 	 * Build the report, for now ignoring the locale.
136 	 *
137 	 * @param locale ignored
138 	 *
139 	 * @see org.apache.maven.plugin.Mojo#execute()
140 	 * @see #getBundle(java.util.Locale)
141 	 */
142 	@Override
143 	public void executeReport(final Locale locale) {
144 		if (this.skip) {
145 			return;
146 		}
147 		try {
148 			final SCSSLintReportGeneratorport/SCSSLintReportGenerator.html#SCSSLintReportGenerator">SCSSLintReportGenerator generator = new SCSSLintReportGenerator(
149                     getSink(),
150                     this.getDescription(locale),
151                     new File(getProject().getBasedir() + "/target", "scss-lint.xml"),
152                     getLog());
153 			generator.generateReport();
154 		} catch (Exception t) {
155 			getLog().error("Error during SCSS Lint report generation", t);
156 			if (failOnError) {
157 				throw t;
158 			}
159 		}
160 	}
161 
162 	@Override
163 	protected Renderer getSiteRenderer() {
164 		return siteRenderer;
165 	}
166 
167 	@Override
168 	protected String getOutputDirectory() {
169 		return outputDirectory.getAbsolutePath();
170 	}
171 
172 	@Override
173 	public void setReportOutputDirectory(final File reportOutputDirectory) {
174 		this.outputDirectory = reportOutputDirectory;
175 	}
176 
177 	@Override
178 	public File getReportOutputDirectory() {
179 		return outputDirectory;
180 	}
181 
182 	@Override
183 	protected MavenProject getProject() {
184 		return project;
185 	}
186 
187 	@Override
188 	public String getOutputName() {
189 		return this.outputName;
190 	}
191 
192 	@Override
193     public String getName(final Locale locale) {
194 		return "scss-lint report";
195 	}
196 
197 	@Override
198 	public String getDescription(final Locale locale) {
199 		return "A scss-lint report.";
200 	}
201 
202 	private ResourceBundle getBundle(final Locale locale) {
203 		return ResourceBundle.getBundle("scss-lint-report", locale,
204                 this.getClass().getClassLoader());
205 	}
206 }