Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile on startup so shamrock:dev works on a clean project #237

Merged
merged 2 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -52,7 +52,7 @@ public static RuntimeUpdatesProcessor setup() throws Exception {
log.log(Level.SEVERE, "Failed to create compiler, runtime compilation will be unavailable", e);
return null;
}
RuntimeUpdatesProcessor processor = new RuntimeUpdatesProcessor(Paths.get(classesDir), Paths.get(sourcesDir), Paths.get(resourcesDir), compiler);
RuntimeUpdatesProcessor processor = new RuntimeUpdatesProcessor(Paths.get(classesDir), sourcesDir == null ? null : Paths.get(sourcesDir), resourcesDir == null ? null : Paths.get(resourcesDir), compiler);
HandlerWrapper wrapper = createHandlerWrapper(processor);
//TODO: we need to get these values from the config in runtime mode
HttpConfig config = new HttpConfig();
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