Fix bugs in dev server

This commit is contained in:
Alexey Andreev 2018-12-11 16:07:43 +03:00
parent eec458089f
commit 5db4c11e10
6 changed files with 70 additions and 15 deletions

View File

@ -52,7 +52,7 @@ public final class TeaVMDevServerRunner {
.create('f')); .create('f'));
options.addOption(OptionBuilder options.addOption(OptionBuilder
.withArgName("classpath") .withArgName("classpath")
.hasArg() .hasArgs()
.withDescription("classpath element (either directory or jar file)") .withDescription("classpath element (either directory or jar file)")
.withLongOpt("classpath") .withLongOpt("classpath")
.create('p')); .create('p'));
@ -66,10 +66,16 @@ public final class TeaVMDevServerRunner {
.withArgName("number") .withArgName("number")
.hasArg() .hasArg()
.withDescription("port (default is 9090)") .withDescription("port (default is 9090)")
.create("port")); .withLongOpt("port")
.create());
options.addOption(OptionBuilder options.addOption(OptionBuilder
.withDescription("display indicator on web page") .withDescription("display indicator on web page")
.create("indicator")); .withLongOpt("indicator")
.create());
options.addOption(OptionBuilder
.withDescription("automatically reload page when compilation completes")
.withLongOpt("auto-reload")
.create());
options.addOption(OptionBuilder options.addOption(OptionBuilder
.withDescription("display more messages on server log") .withDescription("display more messages on server log")
.withLongOpt("verbose") .withLongOpt("verbose")
@ -107,6 +113,7 @@ public final class TeaVMDevServerRunner {
parseOutputOptions(); parseOutputOptions();
devServer.setIndicator(commandLine.hasOption("indicator")); devServer.setIndicator(commandLine.hasOption("indicator"));
devServer.setReloadedAutomatically(commandLine.hasOption("auto-reload"));
if (commandLine.hasOption("port")) { if (commandLine.hasOption("port")) {
try { try {
devServer.setPort(Integer.parseInt(commandLine.getOptionValue("port"))); devServer.setPort(Integer.parseInt(commandLine.getOptionValue("port")));

View File

@ -159,6 +159,9 @@ public class FileSystemWatcher {
return Collections.emptyList(); return Collections.emptyList();
} }
Path basePath = keysToPath.get(baseKey); Path basePath = keysToPath.get(baseKey);
if (basePath == null) {
return Collections.emptyList();
}
Path path = basePath.resolve((Path) event.context()); Path path = basePath.resolve((Path) event.context());
WatchKey key = pathsToKey.get(path); WatchKey key = pathsToKey.get(path);

View File

@ -75,6 +75,7 @@ public class CodeServlet extends HttpServlet {
private List<String> sourcePath = new ArrayList<>(); private List<String> sourcePath = new ArrayList<>();
private TeaVMToolLog log = new EmptyTeaVMToolLog(); private TeaVMToolLog log = new EmptyTeaVMToolLog();
private boolean indicator; private boolean indicator;
private boolean automaticallyReloaded;
private int port; private int port;
private Map<String, Supplier<InputStream>> sourceFileCache = new HashMap<>(); private Map<String, Supplier<InputStream>> sourceFileCache = new HashMap<>();
@ -132,6 +133,10 @@ public class CodeServlet extends HttpServlet {
this.wsEndpoint = wsEndpoint; this.wsEndpoint = wsEndpoint;
} }
public void setAutomaticallyReloaded(boolean automaticallyReloaded) {
this.automaticallyReloaded = automaticallyReloaded;
}
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String path = req.getPathInfo(); String path = req.getPathInfo();
@ -353,6 +358,9 @@ public class CodeServlet extends HttpServlet {
log.info("Starting build"); log.info("Starting build");
progressListener.last = 0; progressListener.last = 0;
progressListener.lastTime = System.currentTimeMillis(); progressListener.lastTime = System.currentTimeMillis();
if (wsEndpoint != null) {
wsEndpoint.progress(0);
}
vm.build(buildTarget, fileName); vm.build(buildTarget, fileName);
addIndicator(); addIndicator();
generateDebug(debugInformationBuilder); generateDebug(debugInformationBuilder);
@ -381,6 +389,7 @@ public class CodeServlet extends HttpServlet {
script = script.substring(script.indexOf("*/") + 2); script = script.substring(script.indexOf("*/") + 2);
script = script.replace("WS_PATH", "localhost:" + port + pathToFile + fileName + ".ws"); script = script.replace("WS_PATH", "localhost:" + port + pathToFile + fileName + ".ws");
script = script.replace("BOOT_FLAG", Boolean.toString(boot)); script = script.replace("BOOT_FLAG", Boolean.toString(boot));
script = script.replace("RELOAD_FLAG", Boolean.toString(automaticallyReloaded));
script = script.replace("FILE_NAME", "http://localhost:" + port + pathToFile + fileName); script = script.replace("FILE_NAME", "http://localhost:" + port + pathToFile + fileName);
return script; return script;
} catch (IOException e) { } catch (IOException e) {
@ -510,7 +519,7 @@ public class CodeServlet extends HttpServlet {
switch (phase) { switch (phase) {
case DEPENDENCY_ANALYSIS: case DEPENDENCY_ANALYSIS:
start = 0; start = 0;
end = 500; end = 400;
break; break;
case LINKING: case LINKING:
start = 400; start = 400;

View File

@ -23,6 +23,8 @@ import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/") @ServerEndpoint("/")
public class CodeWsEndpoint { public class CodeWsEndpoint {
private Session session; private Session session;
private boolean compiling;
private double progress;
@OnOpen @OnOpen
public void open(Session session) { public void open(Session session) {
@ -32,13 +34,27 @@ public class CodeWsEndpoint {
if (consumer != null) { if (consumer != null) {
consumer.accept(this); consumer.accept(this);
} }
if (compiling) {
sendProgress(progress);
}
} }
public void progress(double value) { public void progress(double value) {
session.getAsyncRemote().sendText("{ \"command\": \"compiling\", \"progress\": " + value + " }"); if (session != null) {
sendProgress(value);
}
compiling = true;
progress = value;
} }
public void complete(boolean success) { public void complete(boolean success) {
session.getAsyncRemote().sendText("{ \"command\": \"complete\", \"success\": " + success + " }"); if (session != null) {
session.getAsyncRemote().sendText("{ \"command\": \"complete\", \"success\": " + success + " }");
}
compiling = false;
}
private void sendProgress(double value) {
session.getAsyncRemote().sendText("{ \"command\": \"compiling\", \"progress\": " + value + " }");
} }
} }

View File

@ -37,10 +37,11 @@ import org.teavm.tooling.TeaVMToolLog;
public class DevServer { public class DevServer {
private String mainClass; private String mainClass;
private String[] classPath; private String[] classPath;
private String pathToFile = ""; private String pathToFile = "/";
private String fileName = "classes.js"; private String fileName = "classes.js";
private List<String> sourcePath = new ArrayList<>(); private List<String> sourcePath = new ArrayList<>();
private boolean indicator; private boolean indicator;
private boolean reloadedAutomatically;
private TeaVMToolLog log = new ConsoleTeaVMToolLog(false); private TeaVMToolLog log = new ConsoleTeaVMToolLog(false);
private Server server; private Server server;
@ -80,6 +81,10 @@ public class DevServer {
this.indicator = indicator; this.indicator = indicator;
} }
public void setReloadedAutomatically(boolean reloadedAutomatically) {
this.reloadedAutomatically = reloadedAutomatically;
}
public List<String> getSourcePath() { public List<String> getSourcePath() {
return sourcePath; return sourcePath;
} }
@ -99,6 +104,7 @@ public class DevServer {
servlet.setLog(log); servlet.setLog(log);
servlet.getSourcePath().addAll(sourcePath); servlet.getSourcePath().addAll(sourcePath);
servlet.setIndicator(indicator); servlet.setIndicator(indicator);
servlet.setAutomaticallyReloaded(reloadedAutomatically);
servlet.setPort(port); servlet.setPort(port);
context.addServlet(new ServletHolder(servlet), "/*"); context.addServlet(new ServletHolder(servlet), "/*");

View File

@ -15,6 +15,7 @@
*/ */
(function () { (function () {
var boot = BOOT_FLAG; var boot = BOOT_FLAG;
var reload = RELOAD_FLAG;
function createWebSocket() { function createWebSocket() {
var loc = window.location; var loc = window.location;
@ -90,7 +91,7 @@
timer: void 0, timer: void 0,
show: function(text, timeout) { show: function(text, timeout) {
this.container.style.display = ""; this.container.style.display = "block";
this.label.innerText = text; this.label.innerText = text;
if (this.timer) { if (this.timer) {
clearTimeout(this.timer); clearTimeout(this.timer);
@ -111,14 +112,27 @@
hideProgress() { hideProgress() {
this.progress.container.style.display = "none"; this.progress.container.style.display = "none";
} },
}; };
} }
var indicator = createIndicator(); var indicator = createIndicator();
window.addEventListener("load", function() { function onLoad() {
document.body.appendChild(indicator.container); document.body.appendChild(indicator.container);
}); }
if (document.body) {
onLoad();
} else {
window.addEventListener("load", onLoad);
}
function startMain() {
ws.close();
window.removeEventListener("load", onLoad);
document.body.removeChild(indicator.container);
main();
}
var ws = createWebSocket(); var ws = createWebSocket();
ws.onmessage = function(event) { ws.onmessage = function(event) {
@ -131,12 +145,12 @@
case "complete": case "complete":
if (message.success) { if (message.success) {
indicator.show("Compilation complete", 10); indicator.show("Compilation complete", 10);
if (boot) { if (reload) {
window.location.reload(true);
} else if (boot) {
var scriptElem = document.createElement("script"); var scriptElem = document.createElement("script");
scriptElem.src = "FILE_NAME"; scriptElem.src = "FILE_NAME";
scriptElem.onload = function() { scriptElem.onload = startMain;
main();
};
document.head.appendChild(scriptElem); document.head.appendChild(scriptElem);
} }
} else { } else {