diff --git a/src/main/java/dev/jbang/cli/Edit.java b/src/main/java/dev/jbang/cli/Edit.java index ac5d0f982..de379cc7d 100644 --- a/src/main/java/dev/jbang/cli/Edit.java +++ b/src/main/java/dev/jbang/cli/Edit.java @@ -1,6 +1,7 @@ package dev.jbang.cli; import static dev.jbang.Settings.CP_SEPARATOR; +import static dev.jbang.util.Util.freshly; import static dev.jbang.util.Util.pathToString; import static dev.jbang.util.Util.verboseMsg; import static java.lang.System.out; @@ -179,11 +180,18 @@ public Integer doCall() throws IOException { if (!live) { out.println(projectPathString); // quit(project.getAbsolutePath()); } else { - watchForChanges(prj, () -> { - // TODO only regenerate when dependencies changes. + Path orginalFile = prj.getResourceRef().getFile(); + if (!Files.exists(orginalFile)) { + throw new ExitException(EXIT_UNEXPECTED_STATE, + "Cannot live edit " + prj.getResourceRef().getOriginalResource()); + } + watchForChanges(orginalFile, () -> { + // TODO only regenerate when dependencies or file/resource refs changes. info("Regenerating project."); try { - createProjectForLinkedEdit(prj, Collections.emptyList(), true); + ProjectBuilder pblive = createProjectBuilder(); + Project prjlive = pblive.build(scriptMixin.scriptOrFile); + createProjectForLinkedEdit(prjlive, Collections.emptyList(), true); } catch (IOException e) { throw new RuntimeException(e); } @@ -194,13 +202,8 @@ public Integer doCall() throws IOException { return EXIT_OK; } - private void watchForChanges(Project prj, Callable action) throws IOException { + private void watchForChanges(Path orginalFile, Callable action) throws IOException { try (final WatchService watchService = FileSystems.getDefault().newWatchService()) { - Path orginalFile = prj.getResourceRef().getFile(); - if (!Files.exists(orginalFile)) { - throw new ExitException(EXIT_UNEXPECTED_STATE, - "Cannot live edit " + prj.getResourceRef().getOriginalResource()); - } Path watched = orginalFile.toAbsolutePath().getParent(); watched.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); @@ -214,12 +217,10 @@ private void watchForChanges(Project prj, Callable action) throws IOExce verboseMsg("Changed file: " + changed.toString()); if (Files.isSameFile(orginalFile, changed)) { try { - action.call(); + freshly(action); } catch (RuntimeException ee) { warn("Error when re-generating project. Ignoring it, but state might be undefined: " + ee.getMessage()); - } catch (IOException ioe) { - throw ioe; } catch (Exception e) { throw new ExitException(EXIT_GENERIC_ERROR, "Exception when re-generating project. Exiting", e); @@ -382,7 +383,7 @@ private static List findEditorsOnPath() { private static void showStartingMsg(String ed, boolean showConfig) { String msg = "Starting '" + ed + "'."; if (showConfig) { - msg += "If you want to make this the default, run 'jbang config set edit.open " + ed + "'"; + msg += " If you want to make this the default, run 'jbang config set edit.open " + ed + "'"; } Util.infoMsg(msg); }