Skip to content

Commit

Permalink
Compile on startup so shamrock:dev works on a clean project
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Dec 10, 2018
1 parent 62c280a commit 2abe6f3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
3 changes: 3 additions & 0 deletions devmode/src/main/java/org/jboss/shamrock/dev/DevModeMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public static void main(String... args) throws Exception {
}

runtimeUpdatesProcessor = RuntimeCompilationSetup.setup();
if(runtimeUpdatesProcessor != null) {
runtimeUpdatesProcessor.scanForChangedClasses();
}
//TODO: we can't handle an exception on startup with hot replacement, as Undertow might not have started

doStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.instrument.ClassDefinition;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
Expand All @@ -32,6 +33,7 @@

import org.fakereplace.core.Fakereplace;
import org.fakereplace.replacement.AddedClass;
import org.jboss.logging.Logger;

import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
Expand All @@ -52,6 +54,8 @@ public class RuntimeUpdatesProcessor {
private volatile Set<String> configFilePaths = Collections.emptySet();
private final Map<String, Long> configFileTimestamps = new ConcurrentHashMap<>();

private static final Logger log = Logger.getLogger(RuntimeUpdatesProcessor.class.getPackage().getName());

static {
UpdateHandler fr;
try {
Expand Down Expand Up @@ -98,9 +102,22 @@ public void handleRequest(HttpServerExchange exchange, HttpHandler next) throws
next.handleRequest(exchange);
}

private void doScan() throws IOException {
final Set<File> changedSourceFiles;
void doScan() throws IOException {
final long start = System.currentTimeMillis();
final ConcurrentMap<String, byte[]> changedClasses = scanForChangedClasses();
if (changedClasses == null) return;

if (FAKEREPLACE_HANDLER == null) {
DevModeMain.restartApp(false);
} else {
FAKEREPLACE_HANDLER.handle(changedClasses);
DevModeMain.restartApp(true);
}
log.info("Hot replace total time: " + (System.currentTimeMillis() - start) + "ms");
}

ConcurrentMap<String, byte[]> scanForChangedClasses() throws IOException {
final Set<File> changedSourceFiles;

if (sourcesDir != null) {
try (final Stream<Path> sourcesStream = Files.walk(sourcesDir)) {
Expand All @@ -116,11 +133,13 @@ private void doScan() throws IOException {
changedSourceFiles = Collections.EMPTY_SET;
}
if (!changedSourceFiles.isEmpty()) {
log.info("Changes source files detected, recompiling " + changedSourceFiles);

try {
compiler.compile(changedSourceFiles);
} catch (Exception e) {
DevModeMain.deploymentProblem = e;
return;
return null;
}
}
final ConcurrentMap<String, byte[]> changedClasses;
Expand All @@ -135,18 +154,11 @@ private void doScan() throws IOException {
);
}
if (changedClasses.isEmpty() && !configFilesChanged()) {
return;
return null;
}

lastChange = System.currentTimeMillis();

if (FAKEREPLACE_HANDLER == null) {
DevModeMain.restartApp(false);
} else {
FAKEREPLACE_HANDLER.handle(changedClasses);
DevModeMain.restartApp(true);
}
System.out.println("Hot replace total time: " + (System.currentTimeMillis() - start) + "ms");
return changedClasses;
}

private boolean configFilesChanged() {
Expand Down Expand Up @@ -174,7 +186,22 @@ private boolean configFilesChanged() {

private boolean wasRecentlyModified(final Path p) {
try {
return Files.getLastModifiedTime(p).toMillis() > lastChange;
long sourceMod = Files.getLastModifiedTime(p).toMillis();
boolean recent = sourceMod > lastChange;
if(recent) {
return true;
}
if(p.toString().endsWith(".java")) {
String pathName = sourcesDir.relativize(p).toString();
String classFileName = pathName.substring(0, pathName.length() - 5) + ".class";
Path classFile = classesDir.resolve(classFileName);
if (!Files.exists(classFile)) {
return true;
}
return sourceMod > Files.getLastModifiedTime(classFile).toMillis();
} else {
return false;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
6 changes: 5 additions & 1 deletion maven/src/main/java/org/jboss/shamrock/maven/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ public void execute() throws MojoFailureException {
classPath.append(((JarURLConnection) classFile.openConnection()).getJarFileURL().getFile());

//now we need to build a temporary jar to actually run
File tempFile = File.createTempFile("shamrock", "-runner.jar");

File tempFile = new File(buildDir, project.getArtifactId()+"-dev.jar");
tempFile.delete();
tempFile.deleteOnExit();

try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tempFile))) {
Expand All @@ -179,6 +181,8 @@ public void execute() throws MojoFailureException {
break;
}

outputDirectory.mkdirs();

args.add("-Dshamrock.runner.classes=" + outputDirectory.getAbsolutePath());
args.add("-Dshamrock.runner.sources=" + sourceDir.getAbsolutePath());
if(resources != null) {
Expand Down

0 comments on commit 2abe6f3

Please sign in to comment.