1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package searls.jasmine;
22
23 import java.io.File;
24 import java.io.IOException;
25
26 import org.apache.commons.io.FileUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.maven.artifact.DefaultArtifact;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.project.MavenProject;
32 import org.codehaus.mojo.javascript.archive.JavascriptArtifactManager;
33 import org.codehaus.plexus.archiver.ArchiverException;
34
35 import searls.jasmine.runner.SpecRunnerHtmlGenerator;
36 import searls.jasmine.runner.SpecRunnerHtmlGenerator.ReporterType;
37
38
39
40
41
42
43
44 public class GenerateManualRunnerMojo extends AbstractJasmineMojo {
45
46
47
48
49
50
51
52 private MavenProject project;
53
54
55
56
57 private JavascriptArtifactManager javascriptArtifactManager;
58
59
60 public void execute() throws MojoExecutionException, MojoFailureException {
61 if(jasmineTestSourceDirectory.exists()) {
62 try
63 {
64 javascriptArtifactManager.unpack( project, DefaultArtifact.SCOPE_TEST, new File(
65 jasmineTargetDir, libsDirectory ), useArtifactId );
66 }
67 catch ( ArchiverException e )
68 {
69 throw new MojoExecutionException( "Failed to unpack javascript dependencies", e );
70 }
71
72 getLog().info("Generating runner '"+manualSpecRunnerHtmlFileName+"' in the Jasmine plugin's target directory to open in a browser to facilitate faster feedback.");
73 try {
74 writeSpecRunnerToSourceSpecDirectory();
75 } catch (Exception e) {
76 throw new MojoFailureException(e,"JavaScript Test execution failed.","Failed to execute generated SpecRunner.html");
77 }
78 } else {
79 if( jasmineTestSourceDirectory.equals(jsunitTestSourceDirectory) ) {
80 getLog().warn("Skipping manual spec runner generation. Check to make sure that `"+ jasmineTestSourceDirectory.getAbsolutePath()+"` exist.");
81 }
82 }
83 }
84
85 private void writeSpecRunnerToSourceSpecDirectory() throws IOException {
86 SpecRunnerHtmlGenerator htmlGenerator = new SpecRunnerHtmlGenerator(preloadSources, sourceDirectory,
87 jasmineTestSourceDirectory, new File(jasmineTargetDir,libsDirectory), jasmineTargetDir);
88 String runner = htmlGenerator.generate(pluginArtifacts, ReporterType.TrivialReporter);
89
90 File destination = new File(jasmineTargetDir,manualSpecRunnerHtmlFileName);
91 String existingRunner = loadExistingManualRunner(destination);
92
93 if(!StringUtils.equals(runner, existingRunner)) {
94 FileUtils.writeStringToFile(destination, runner);
95 } else {
96 getLog().info("Skipping spec runner generation, because an identical spec runner already exists.");
97 }
98 }
99
100 private String loadExistingManualRunner(File destination) {
101 String existingRunner = null;
102 try {
103 if(destination.exists()) {
104 existingRunner = FileUtils.readFileToString(destination);
105 }
106 } catch(Exception e) {
107 getLog().warn("An error occurred while trying to open an existing manual spec runner. Continuing");
108 }
109 return existingRunner;
110 }
111
112 }