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

fix: Edit wasn't updating Gradle build files correctly #1728

Merged
merged 1 commit into from
Feb 3, 2024
Merged
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
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 @@ -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);
}
Expand All @@ -194,13 +202,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 @@ -214,12 +217,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 @@ -382,7 +383,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
Loading