1
2
3
4
5
6
7 package net.jangaroo.smartsprites.maven;
8
9 import java.io.File;
10 import java.io.IOException;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Locale;
14
15 import org.apache.maven.plugin.AbstractMojo;
16 import org.apache.maven.plugin.MojoExecutionException;
17 import org.apache.maven.plugin.MojoFailureException;
18 import org.apache.maven.plugins.annotations.LifecyclePhase;
19 import org.apache.maven.plugins.annotations.Mojo;
20 import org.apache.maven.plugins.annotations.Parameter;
21 import org.carrot2.labs.smartsprites.SmartSpritesParameters;
22 import org.carrot2.labs.smartsprites.SpriteBuilder;
23 import org.carrot2.labs.smartsprites.message.Message;
24 import org.carrot2.labs.smartsprites.message.MessageLog;
25 import org.carrot2.labs.smartsprites.message.PrintStreamMessageSink;
26
27
28
29
30
31
32
33
34 @Mojo(name = "smartsprites", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresProject = false, threadSafe = true)
35 public class SmartSpritesMojo extends AbstractMojo {
36
37
38 private static final String ROOT_DIR_MODE = "rootDirMode";
39
40
41 private static final String CSS_FILES_MODE = "cssFilesMode";
42
43
44
45
46
47
48
49
50
51
52 @Parameter(defaultValue = "${project.basedir}/src/main/sprites", property = "rootDirPath")
53 private File rootDirPath;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 @Parameter(property = "cssFiles")
71 private List<File> cssFiles;
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @Parameter(defaultValue = "${project.build.directory}/generated-resources/META-INF/resources/spritesheets", property = "outPutDirPath")
89 private File outputDirPath;
90
91
92
93
94
95
96
97
98
99
100 @Parameter(property = "documentRootDirPath")
101 private File documentRootDirPath;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 @Parameter(defaultValue = "WARN", property = "logLevel")
121 private String logLevel;
122
123
124
125
126
127
128
129
130
131 @Parameter(defaultValue = "AUTO", property = "spritePngDepth")
132 private String spritePngDepth;
133
134
135
136
137
138 @Parameter(defaultValue = "UTF-8", property = "cssFileEncoding")
139 private String cssFileEncoding;
140
141
142
143
144 @Parameter(defaultValue = "", property = "cssFileSuffix")
145 private String cssFileSuffix;
146
147
148
149
150
151
152 @Parameter(defaultValue = SmartSpritesMojo.ROOT_DIR_MODE, property = "workingMode")
153 private String workingMode;
154
155
156
157
158 @Parameter(defaultValue = "false", alias = "skip", property = "skip")
159 private boolean skip;
160
161 @Override
162 public void execute() throws MojoExecutionException, MojoFailureException {
163
164 if (this.skip) {
165 this.getLog().info("Smartsprites is skipped");
166 return;
167 }
168
169
170 Message.MessageLevel msgLogLevel;
171 try {
172 msgLogLevel = Message.MessageLevel.valueOf(this.logLevel.toUpperCase(Locale.ENGLISH));
173 } catch (final Exception e) {
174 throw new MojoExecutionException("LogLevel Error - please select a valid value! (INFO, WARN) ", e);
175 }
176
177
178 SmartSpritesParameters.PngDepth pngDepth;
179 try {
180 pngDepth = SmartSpritesParameters.PngDepth.valueOf(this.spritePngDepth);
181 } catch (final Exception e) {
182 throw new MojoExecutionException("PngDepth Error - please select a valid value! (AUTO, DIRECT, INDEXED) ",
183 e);
184 }
185
186
187 if (!(this.workingMode.equals(SmartSpritesMojo.ROOT_DIR_MODE)
188 || this.workingMode.equals(SmartSpritesMojo.CSS_FILES_MODE)
189 || this.workingMode.equals("cssFilesWithOutputDirMode"))) {
190 throw new MojoExecutionException(
191 "workingMode Error - plese select a valid value! (rootDirMode, cssFilesMode, cssFilesWithOutputDirMode)");
192 }
193
194
195 String rootDirPathTemp = "";
196 String outputDirPathTemp = "";
197 String documentRootDirPathTemp = "";
198 List<String> cssFilesTemp = new ArrayList<>();
199
200
201
202 if (!this.workingMode.equals(SmartSpritesMojo.ROOT_DIR_MODE)) {
203 for (final File cssFile : this.cssFiles) {
204 if (cssFile.exists()) {
205 cssFilesTemp.add(cssFile.toString());
206 } else {
207 throw new MojoExecutionException("The following css-file doesn't exist: " + cssFile);
208 }
209 }
210 } else {
211 this.cssFiles = null;
212 }
213
214
215 if (this.rootDirPath != null && !this.workingMode.equals(SmartSpritesMojo.CSS_FILES_MODE)) {
216 if (this.rootDirPath.exists()) {
217 rootDirPathTemp = this.rootDirPath.toString();
218 } else {
219 throw new MojoExecutionException("The rootDirPath doesn't exist. " + this.rootDirPath.toString());
220 }
221 }
222
223
224 if (this.outputDirPath != null && !this.workingMode.equals(SmartSpritesMojo.CSS_FILES_MODE)) {
225 outputDirPathTemp = this.outputDirPath.toString();
226 }
227
228
229 if (this.documentRootDirPath != null) {
230 documentRootDirPathTemp = this.documentRootDirPath.toString();
231 }
232
233
234
235 if (this.workingMode.equals(SmartSpritesMojo.ROOT_DIR_MODE)) {
236 if (this.rootDirPath == null) {
237 throw new MojoExecutionException("Please configure a rootDirPath.");
238 }
239 cssFilesTemp = null;
240 } else if (this.workingMode.equals(SmartSpritesMojo.CSS_FILES_MODE)) {
241 if (this.cssFiles == null) {
242 throw new MojoExecutionException("Please configure some cssFiles.");
243 }
244 rootDirPathTemp = null;
245 outputDirPathTemp = null;
246 } else if ((this.workingMode.equals("cssFilesWithOutputDirMode") && this.cssFiles == null)
247 || this.rootDirPath == null || this.outputDirPath == null) {
248 throw new MojoExecutionException("Please configure cssFiles and/or a rootDirPath and/or an outputDirPath");
249 }
250
251
252 SmartSpritesParameters smartParameters = new SmartSpritesParameters(rootDirPathTemp, cssFilesTemp,
253 outputDirPathTemp, documentRootDirPathTemp, msgLogLevel, this.cssFileSuffix, pngDepth,
254 this.cssFileEncoding);
255
256 final MessageLog messageLog = new MessageLog(
257 new PrintStreamMessageSink(System.out, smartParameters.getLogLevel()));
258 SpriteBuilder spriteBuilder;
259
260
261 try {
262 spriteBuilder = new SpriteBuilder(smartParameters, messageLog);
263 spriteBuilder.buildSprites();
264 } catch (final IOException e) {
265 throw new MojoExecutionException("Smartsprites error: ", e);
266 }
267 }
268
269 }