Skip to content

Commit

Permalink
the watcher will now detect full cleans of the output dir and rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
pfranza committed Nov 1, 2023
1 parent c7892d1 commit b05f0df
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public class ScopedFragmentResult {
private String newCss;
private String newHtml;

private ScopedFragmentResult() {
}

public ScopedFragmentResult(String oldCss, String oldHtml, String newCss, String newHtml,
CssScopeMetadata metadata) {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void process() {

try {
Output z = new Compiler().compileFile(in.toUri(), out.toUri(), options);
out.getParent().toFile().mkdirs();
Files.write(out,z.getCss().getBytes(),
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
} catch (CompilationException e) {
Expand Down
42 changes: 27 additions & 15 deletions src/main/java/net/ftlines/css/scoper/Watcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,35 @@ public void start() throws Exception {
}
Thread.sleep(500);

WatchService watchService = FileSystems.getDefault().newWatchService();
List<WatchKey> registeredKeys = new ArrayList<>();
registeredKeys.addAll(registerRecursiveSourceWatchables(watchService, inputRootPath));
registeredKeys.addAll(registerRecursiveTargetWatchables(watchService, outputRootPath));
try(WatchService watchService = FileSystems.getDefault().newWatchService()) {
List<WatchKey> registeredKeys = new ArrayList<>();
registeredKeys.addAll(registerRecursiveSourceWatchables(watchService, inputRootPath));
registeredKeys.addAll(registerRecursiveTargetWatchables(watchService, outputRootPath));

phase = Phase.WATCHING;
phaseChangeFunction.accept(phase);
registeredKeys.add(outputRootPath.register(watchService,
new WatchEvent.Kind[] { StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY },
SensitivityWatchEventModifier.HIGH));

WatchKey key;
while ((key = watchService.take()) != null && phase == Phase.WATCHING) {
processEvents(key.watchable(), key.pollEvents());
key.reset();
}
phase = Phase.WATCHING;
phaseChangeFunction.accept(phase);

WatchKey key;
while ((key = watchService.take()) != null && phase == Phase.WATCHING) {

if(!key.isValid()) {
System.out.println("Key '" + key.watchable() + "' is invalid .. rebuilding.");
break;
}

//Cleanup registered keys
registeredKeys.forEach(z -> z.cancel());
registeredKeys.clear();
processEvents(key.watchable(), key.pollEvents());
key.reset();
}

//Cleanup registered keys
registeredKeys.forEach(z -> z.cancel());
registeredKeys.clear();
}

Thread.sleep(2000);
}
Expand All @@ -120,7 +132,7 @@ && isChildPath(outputRootPath, workingDirPath)
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

Expand Down

0 comments on commit b05f0df

Please sign in to comment.