Skip to content

Commit

Permalink
fix: Edit wasn't updating Gradle build files correctly (#1728)
Browse files Browse the repository at this point in the history
It was using existing, stale, Project instance to update the build file
which resulted in the build file never actually being updated.

Fixes #1693
  • Loading branch information
quintesse authored Feb 3, 2024
1 parent 7a8bc7d commit c1fff3d
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/main/java/dev/jbang/cli/Edit.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -180,11 +181,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);
}
Expand All @@ -195,13 +203,8 @@ public Integer doCall() throws IOException {
return EXIT_OK;
}

private void watchForChanges(Project prj, Callable<Object> action) throws IOException {
private void watchForChanges(Path orginalFile, Callable<Object> 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);
Expand All @@ -215,12 +218,10 @@ private void watchForChanges(Project prj, Callable<Object> 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);
Expand Down Expand Up @@ -383,7 +384,7 @@ private static List<String> 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);
}
Expand Down

0 comments on commit c1fff3d

Please sign in to comment.