Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Groovy script to auto open an browser based on folder change (same as the last example on https://gist.github.com/DinisCruz-Dev/9214909/ but on a separate gist so that it embeds ok in a blog post)
import java.nio.file.*;
import com.sun.nio.file.*
import java.nio.file.attribute.*;
def editorTitle = "index.html"
def refreshBrowser = { path ->
eclipse.log("Opening browser with: " + path);
def view = eclipse.views.create("Synced WebBrowser");
if (view.controls().size() == 0)
view.add.browser();
view.controls().first().open(path);
};
def startWatch = { file ->
def filePath = file.toString();
def folder = file.getParent();
def path_Root = Paths.get(folder);
refreshBrowser(filePath);
def watchService =FileSystems.getDefault().newWatchService();
path_Root.register(watchService, (WatchEvent.Kind[] )[StandardWatchEventKinds.ENTRY_MODIFY], SensitivityWatchEventModifier.HIGH);
Files.walkFileTree(path_Root, new SimpleFileVisitor<Path>()
{
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException
{
dir.register(watchService, (WatchEvent.Kind[] )[StandardWatchEventKinds.ENTRY_MODIFY], SensitivityWatchEventModifier.HIGH);
return FileVisitResult.CONTINUE;
}
});
while(true)
{
eclipse.log("About to start watching folder: " + folder);
def key = watchService.take();
eclipse.log("After .take()");
for (def event : key.pollEvents())
{
eclipse.log(" event: " + event.kind());
}
refreshBrowser(filePath);
key.reset()
}
return key;
}
def editor = eclipse.editors.get(editorTitle).getEditor(true);
def file = editor.getEditorInput().getPath().toFile();
startWatch(file);
//Config:UIThread_False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.