From 6598eb9e4db1498b578f2a11d2ac1f26f2456a7d Mon Sep 17 00:00:00 2001 From: ybonnel Date: Tue, 1 Jul 2014 11:57:19 +0200 Subject: [PATCH] Add livereload for inline goal --- pom.xml | 9 ++++++ .../com/ingenieux/mojo/jbake/InlineMojo.java | 30 +++++++++++++++++++ .../com/ingenieux/mojo/jbake/WatchMojo.java | 7 +++++ 3 files changed, 46 insertions(+) diff --git a/pom.xml b/pom.xml index 68e4ce35..6eb10d20 100644 --- a/pom.xml +++ b/pom.xml @@ -75,6 +75,10 @@ + + net.alchim31 + livereload-jvm + org.apache.maven maven-plugin-api @@ -121,6 +125,11 @@ + + net.alchim31 + livereload-jvm + 0.2.0 + org.jbake jbake-core diff --git a/src/main/java/br/com/ingenieux/mojo/jbake/InlineMojo.java b/src/main/java/br/com/ingenieux/mojo/jbake/InlineMojo.java index df2d6435..78bde29f 100644 --- a/src/main/java/br/com/ingenieux/mojo/jbake/InlineMojo.java +++ b/src/main/java/br/com/ingenieux/mojo/jbake/InlineMojo.java @@ -16,6 +16,8 @@ * limitations under the License. */ +import net_alchim31_livereload.LRServer; +import org.apache.commons.lang.BooleanUtils; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -25,6 +27,10 @@ import org.vertx.java.platform.Verticle; import java.io.File; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; /** * Runs jbake on a folder while watching and serving a folder with it @@ -49,7 +55,14 @@ public class InlineMojo extends WatchMojo { @Parameter(property = "jbake.port", defaultValue = "8080") private Integer port; + /** + * Use livereload. + */ + @Parameter(property = "jbake.livereload", defaultValue = "true") + private Boolean livereload; + Server server = new Server(); + LRServer lrServer; class Server extends Verticle { { @@ -66,7 +79,9 @@ public void handle(HttpServerRequest req) { if (new File(outputDirectory + file).isDirectory()) { req.response().setStatusCode(301).putHeader("Location", file + "/").close(); } else { + refreshLock.readLock().lock(); req.response().sendFile(outputDirectory.getAbsolutePath() + file); + refreshLock.readLock().unlock(); } } }).listen(port, listenAddress); @@ -74,10 +89,25 @@ public void handle(HttpServerRequest req) { } protected void stopServer() { + if (lrServer != null) { + try { + lrServer.stop(); + } catch (Exception e) { + e.printStackTrace(); + } + } server.stop(); } protected void initServer() throws MojoExecutionException { server.start(); + if (BooleanUtils.isTrue(livereload)) { + lrServer = new LRServer(35729, outputDirectory.toPath()); + try { + lrServer.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } } } diff --git a/src/main/java/br/com/ingenieux/mojo/jbake/WatchMojo.java b/src/main/java/br/com/ingenieux/mojo/jbake/WatchMojo.java index 38d67212..b44897cc 100644 --- a/src/main/java/br/com/ingenieux/mojo/jbake/WatchMojo.java +++ b/src/main/java/br/com/ingenieux/mojo/jbake/WatchMojo.java @@ -24,12 +24,17 @@ import java.io.InputStreamReader; import java.nio.file.Paths; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; /** * Runs jbake on a folder while watching for changes */ @Mojo(name = "watch", requiresDirectInvocation = true, requiresProject = false) public class WatchMojo extends GenerateMojo { + + ReadWriteLock refreshLock = new ReentrantReadWriteLock(); + public void execute() throws MojoExecutionException { super.execute(); @@ -65,9 +70,11 @@ public void run() { if (Boolean.FALSE.equals(result)) { Thread.sleep(1000); } else if (Boolean.TRUE.equals(result)) { + refreshLock.writeLock().lock(); getLog().info("Refreshing"); super.execute(); + refreshLock.writeLock().unlock(); } else if (null == result) { break; }