Skip to content

Commit

Permalink
fix(quarkusio#443): edit reacts to all .java/.jsh not so //SOURCES im…
Browse files Browse the repository at this point in the history
…plicit support
  • Loading branch information
maxandersen committed Oct 25, 2020
1 parent 8e74b5a commit d309856
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/main/java/dev/jbang/cli/Edit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static dev.jbang.Settings.CP_SEPARATOR;
import static dev.jbang.Util.isWindows;
import static java.lang.System.out;
import static java.util.stream.Collectors.joining;

import java.io.File;
import java.io.IOException;
Expand All @@ -15,7 +16,9 @@
import java.nio.file.WatchService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import dev.jbang.ExitException;
Expand Down Expand Up @@ -67,13 +70,12 @@ public Integer doCall() throws IOException {

String[] cmd;
if (isWindows()) {
cmd = new String[] { "cmd", "/c", optionList.stream().collect(Collectors.joining(" ")) };
cmd = new String[] { "cmd", "/c", optionList.stream().collect(joining(" ")) };
} else {
cmd = new String[] { "sh", "-c", optionList.stream().collect(Collectors.joining(" ")) };
cmd = new String[] { "sh", "-c", optionList.stream().collect(joining(" ")) };
}
info("Running `" + String.join(" ", cmd) + "`");
new ProcessBuilder(cmd).start();

}
}

Expand All @@ -85,17 +87,31 @@ public Integer doCall() throws IOException {
if (!orginalFile.exists()) {
throw new ExitException(2, "Cannot live edit " + script.getOriginalFile());
}
Path watched = orginalFile.getAbsoluteFile().getParentFile().toPath();
watched.register(watchService,
new WatchEvent.Kind[] { StandardWatchEventKinds.ENTRY_MODIFY });
info("Watching for changes in " + watched);

// TODO: should extract this to be testable!
Set<Path> rootsToWatch = new HashSet<Path>();

rootsToWatch.add(orginalFile.getAbsoluteFile().getParentFile().toPath());
rootsToWatch.addAll(
script.getResolvedSourcePaths().stream().map(x -> x.getParent()).collect(Collectors.toList()));

for (Path toWatch : rootsToWatch) {
toWatch.register(watchService,
new WatchEvent.Kind[] { StandardWatchEventKinds.ENTRY_MODIFY });
info("Watching for changes in " + toWatch);
}

while (true) {
final WatchKey wk = watchService.take();
for (WatchEvent<?> event : wk.pollEvents()) {
// we only register "ENTRY_MODIFY" so the context is always a Path.
final Path changed = (Path) event.context();
// info(changed.toString());
if (Files.isSameFile(orginalFile.toPath(), changed)) {
// instead of check all resolved paths we just optimistically assume if same
// file as root or known suffix we will regenerate
// System.out.println("Change at " + changed);
if (Files.isSameFile(orginalFile.toPath(), changed) || changed.endsWith(".jsh")
|| changed.endsWith(".java")) {
try {
// TODO only regenerate when dependencies changes.
info("Regenerating project.");
Expand All @@ -117,7 +133,7 @@ public Integer doCall() throws IOException {
warn("edit-live interrupted");
}
}
return EXIT_EXECUTE;
return EXIT_OK;
}

/** Create Project to use for editing **/
Expand Down

0 comments on commit d309856

Please sign in to comment.