1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.codehaus.mojo.javascript;
15
16 import org.apache.maven.artifact.Artifact;
17 import org.apache.maven.artifact.DefaultArtifact;
18 import org.apache.maven.plugin.AbstractMojo;
19 import org.apache.maven.plugin.MojoExecutionException;
20 import org.apache.maven.plugin.MojoFailureException;
21 import org.apache.maven.project.MavenProject;
22 import org.apache.maven.surefire.shade.org.codehaus.plexus.util.FileUtils;
23 import org.codehaus.mojo.javascript.archive.JavascriptArtifactManager;
24 import org.codehaus.plexus.archiver.ArchiverException;
25 import org.codehaus.plexus.util.DirectoryScanner;
26 import org.codehaus.plexus.util.IOUtil;
27 import searls.jasmine.io.IOUtilsWrapper;
28 import searls.jasmine.runner.SpecRunnerTitaniumGenerator;
29 import org.antlr.stringtemplate.StringTemplate;
30 import org.antlr.stringtemplate.language.DefaultTemplateLexer;
31
32 import java.io.*;
33 import java.util.*;
34
35
36
37
38
39
40
41
42
43 public class PrepareTitaniumJasmineTestMojo extends AbstractMojo {
44
45
46 protected static final String[] DEFAULT_INCLUDES = { "**/*.js" };
47 private static final String JAVASCRIPT_TYPE = "js";
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 protected List<String> preloadSources;
63
64
65
66
67
68
69 protected String libsDirectory;
70
71
72
73
74
75 protected String specsDirectory;
76
77
78
79
80
81 protected File jasmineTestSourceDirectory;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 protected String platform;
101
102
103
104
105
106
107 protected File sourceDirectory;
108
109
110
111
112
113
114 protected File outputDirectory;
115
116
117
118
119
120 protected File testOutputDirectory;
121
122
123
124
125 protected List<Artifact> pluginArtifacts;
126
127
128
129
130
131
132
133 protected boolean skipTests;
134
135
136
137
138
139
140
141 protected String[] specExcludes;
142
143 protected File getPlatformTestSourceDirectory() {
144 return new File(jasmineTestSourceDirectory, platform);
145 }
146
147 protected File getPlatformTestOutputDirectory() {
148 return new File(testOutputDirectory, platform);
149 }
150
151 protected File getTiProjectDirectory() {
152 return new File(outputDirectory, platform);
153 }
154
155 protected File getTiProjectResourceDirectory() {
156 return new File(getTiProjectDirectory(), "Resources");
157 }
158
159
160
161
162
163
164 protected boolean useArtifactId;
165
166
167
168
169
170
171
172
173 private MavenProject project;
174
175
176
177
178 private JavascriptArtifactManager javascriptArtifactManager;
179
180 public void execute() throws MojoExecutionException, MojoFailureException {
181 if(skipTests || !jasmineTestSourceDirectory.exists()) {
182 getLog().info("No Jasmine tests, skipping Jasmine tests preparation.");
183 return;
184 }
185
186 getLog().info("Processing source folder: " + outputDirectory.getAbsolutePath());
187 getPlatformTestOutputDirectory().mkdirs();
188 File depsDirectory = new File(getPlatformTestOutputDirectory(), "Resources" + File.separator + libsDirectory);
189 depsDirectory.mkdirs();
190
191 try
192 {
193 javascriptArtifactManager.unpack( project, DefaultArtifact.SCOPE_TEST, depsDirectory, useArtifactId );
194 }
195 catch ( ArchiverException e )
196 {
197 throw new MojoExecutionException( "Failed to unpack javascript dependencies", e );
198 }
199
200
201
202 List<String> sourceFiles = new ArrayList<String>();
203 try {
204 File appDestDirectory = new File(getPlatformTestOutputDirectory(), "Resources");
205 if (getTiProjectResourceDirectory().exists()) {
206 appDestDirectory.mkdirs();
207 FileUtils.copyDirectoryStructure(getTiProjectResourceDirectory(), appDestDirectory);
208
209 DirectoryScanner scanner = new DirectoryScanner();
210 scanner.setBasedir(getTiProjectResourceDirectory());
211 scanner.setIncludes(DEFAULT_INCLUDES);
212 scanner.setExcludes(new String[] { "**/app.js" });
213 scanner.addDefaultExcludes();
214 scanner.scan();
215 String[] foundFiles = scanner.getIncludedFiles();
216 if (foundFiles != null) {
217 for (String fFile : foundFiles) {
218 sourceFiles.add(fFile);
219 }
220 }
221 } else {
222 getLog().info("Titanium resources folder doesn't exist.");
223 if (getTiProjectDirectory().exists()) {
224 getLog().info("Trying to copy source script from: " + getTiProjectDirectory().getAbsolutePath());
225 appDestDirectory.mkdirs();
226 FileUtils.copyDirectoryStructure(getTiProjectDirectory(), appDestDirectory);
227
228 DirectoryScanner scanner = new DirectoryScanner();
229 scanner.setBasedir(getTiProjectDirectory());
230 scanner.setIncludes(DEFAULT_INCLUDES);
231 scanner.setExcludes(new String[] { "**/app.js" });
232 scanner.addDefaultExcludes();
233 scanner.scan();
234 String[] foundFiles = scanner.getIncludedFiles();
235 if (foundFiles != null) {
236 for (String fFile : foundFiles) {
237 sourceFiles.add(fFile);
238 }
239 }
240 } else if (outputDirectory.exists()) {
241 getLog().info("Processing output directory: " + outputDirectory.getAbsolutePath());
242 appDestDirectory.mkdirs();
243 FileUtils.copyDirectoryStructure(outputDirectory, appDestDirectory);
244
245 DirectoryScanner scanner = new DirectoryScanner();
246 scanner.setBasedir(outputDirectory);
247 scanner.setIncludes(DEFAULT_INCLUDES);
248 scanner.setExcludes(new String[] { "**/app.js" });
249 scanner.addDefaultExcludes();
250 scanner.scan();
251 String[] foundFiles = scanner.getIncludedFiles();
252 if (foundFiles != null) {
253 for (String fFile : foundFiles) {
254 sourceFiles.add(fFile);
255 }
256 }
257 }
258 }
259 } catch (IOException ioe) {
260 getLog().error("Failed to copy titanium project files to test directory", ioe);
261 return;
262 }
263
264
265 copySpecFiles();
266
267
268 createTestAppJs(sourceFiles);
269
270
271 try {
272 ensureTiApp();
273 } catch (Throwable t) {
274 throw new MojoExecutionException("Unable to create test application tiapp.xml file");
275 }
276 }
277
278 protected void ensureTiApp() throws IOException {
279 File tiapp = new File(getPlatformTestOutputDirectory(), "tiapp.xml");
280 if (!tiapp.exists()) {
281 getLog().info("Generating custom tiapp.xml file.");
282 IOUtilsWrapper wrapper = new IOUtilsWrapper();
283 String content = wrapper.toString("/titanium/tiapp.xml");
284 StringTemplate template = new StringTemplate(content, DefaultTemplateLexer.class);
285 template.setAttribute("id", project.getGroupId() + "." + project.getArtifactId() + ".test");
286 template.setAttribute("name", project.getName() + " Test");
287 template.setAttribute("version", project.getVersion());
288 template.setAttribute("guid", UUID.randomUUID().toString());
289 if (project.getDescription() != null) {
290 template.setAttribute("description", project.getDescription());
291 } else {
292 template.setAttribute("description", "not specified");
293 }
294
295 FileWriter writer = new FileWriter(tiapp);
296 try {
297 IOUtil.copy(template.toString(), writer);
298 } finally {
299 IOUtil.close(writer);
300 }
301 }
302 }
303
304 protected void copySpecFiles() throws MojoExecutionException {
305 DirectoryScanner scanner = new DirectoryScanner();
306 scanner.setBasedir(jasmineTestSourceDirectory);
307 scanner.setIncludes(DEFAULT_INCLUDES);
308 scanner.setExcludes(specExcludes);
309 scanner.addDefaultExcludes();
310
311 scanner.scan();
312 String[] files = scanner.getIncludedFiles();
313
314 File testFolder = new File(getPlatformTestOutputDirectory(), "Resources" + File.separator + specsDirectory);
315 testFolder.mkdirs();
316
317 try {
318 for (String file : files) {
319 File srcFile = new File(jasmineTestSourceDirectory, file);
320 File destFile = new File(testFolder, file);
321 destFile.getParentFile().mkdirs();
322 FileUtils.copyFile(srcFile, destFile);
323 }
324 } catch (IOException ioe) {
325 throw new MojoExecutionException("Error while creating jasmine test file script", ioe);
326 }
327 }
328
329 protected void createTestAppJs(List<String> sourceFiles)
330 throws MojoExecutionException {
331 File appFile = new File(getPlatformTestOutputDirectory(), "Resources" + File.separatorChar + "app.js");
332 PrintWriter writer = null;
333
334 try {
335 writer = new PrintWriter(appFile);
336 SpecRunnerTitaniumGenerator generator = new SpecRunnerTitaniumGenerator(preloadSources,
337 sourceFiles,
338 new File(getPlatformTestOutputDirectory(), "Resources"),
339 libsDirectory,
340 specsDirectory);
341 writer.write(generator.generate(SpecRunnerTitaniumGenerator.ReporterType.TITANIUM));
342 } catch (IOException ioe) {
343 throw new MojoExecutionException("Error while generating jasmine test application file", ioe);
344 } finally {
345 IOUtil.close(writer);
346 }
347 }
348 }