View Javadoc
1   package com.github.searls.jasmine.mojo;
2   
3   import com.github.searls.jasmine.NullLog;
4   import com.github.searls.jasmine.io.RelativizesFilePaths;
5   import com.github.searls.jasmine.runner.CreatesRunner;
6   import com.github.searls.jasmine.runner.ReporterType;
7   import com.github.searls.jasmine.server.ResourceHandlerConfigurator;
8   import com.github.searls.jasmine.server.ServerManager;
9   import org.apache.maven.plugin.MojoExecutionException;
10  import org.apache.maven.plugin.logging.Log;
11  import org.apache.maven.plugins.annotations.Mojo;
12  import org.apache.maven.plugins.annotations.ResolutionScope;
13  import org.eclipse.jetty.server.Server;
14  
15  import java.io.File;
16  import java.io.IOException;
17  
18  /**
19   * Execute specs in a web browser. Monitors your sources/specs for changes as you develop.
20   */
21  @Mojo(name = "bdd", requiresDirectInvocation = true, requiresDependencyResolution = ResolutionScope.TEST)
22  public class ServerMojo extends AbstractJasmineMojo {
23  
24    public static final String INSTRUCTION_FORMAT =
25      "\n\n" +
26        "Server started--it's time to spec some JavaScript! You can run your specs as you develop by visiting this URL in a web browser: \n\n" +
27        " %s://localhost:%s" +
28        "\n\n" +
29        "The server will monitor these two directories for scripts that you add, remove, and change:\n\n" +
30        "  source directory: %s\n\n" +
31        "  spec directory: %s" +
32        "\n\n" +
33        "Just leave this process running as you test-drive your code, refreshing your browser window to re-run your specs. You can kill the server with Ctrl-C when you're done.";
34  
35    private final RelativizesFilePaths relativizesFilePaths;
36  
37    public ServerMojo() {
38      this(new RelativizesFilePaths());
39    }
40  
41    public ServerMojo(RelativizesFilePaths relativizesFilePaths) {
42      this.relativizesFilePaths = relativizesFilePaths;
43    }
44  
45    private String buildServerInstructions() throws IOException {
46      return String.format(
47        INSTRUCTION_FORMAT,
48        this.uriScheme,
49        this.serverPort,
50        this.getRelativePath(this.sources.getDirectory()),
51        this.getRelativePath(this.specs.getDirectory()));
52    }
53  
54    @Override
55    public void run() throws Exception {
56      ServerManager serverManager = this.getServerManager();
57  
58      serverManager.start(this.serverPort);
59      this.getLog().info(this.buildServerInstructions());
60      serverManager.join();
61    }
62  
63    private ServerManager getServerManager() throws MojoExecutionException {
64      Log log = this.debug ? this.getLog() : new NullLog();
65  
66      CreatesRunner createsRunner = new CreatesRunner(
67        this,
68        log,
69        this.manualSpecRunnerHtmlFileName,
70        ReporterType.HtmlReporter);
71  
72      ResourceHandlerConfigurator configurator = new ResourceHandlerConfigurator(
73        this,
74        this.relativizesFilePaths,
75        createsRunner);
76  
77      return new ServerManager(new Server(), getConnector(), configurator);
78    }
79  
80    private String getRelativePath(File absolutePath) throws IOException {
81      return this.relativizesFilePaths.relativize(this.mavenProject.getBasedir(), absolutePath);
82    }
83  }