Skip to content

Commit

Permalink
Add livereload for inline goal
Browse files Browse the repository at this point in the history
  • Loading branch information
ybonnel committed Jul 1, 2014
1 parent 8be2128 commit 6598eb9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.alchim31</groupId>
<artifactId>livereload-jvm</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down Expand Up @@ -121,6 +125,11 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>net.alchim31</groupId>
<artifactId>livereload-jvm</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.jbake</groupId>
<artifactId>jbake-core</artifactId>
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/br/com/ingenieux/mojo/jbake/InlineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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 {
{
Expand All @@ -66,18 +79,35 @@ 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);
}
}

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();
}
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/br/com/ingenieux/mojo/jbake/WatchMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 6598eb9

Please sign in to comment.