Created
July 23, 2010 18:16
-
-
Save raymyers/487813 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class DirWatcher extends TimerTask { | |
def path | |
def dir = [:] | |
// Exclude temp files created by vim, emacs, etc... | |
FileFilter fileFilter = {file -> !(file.name =~ /\.swp$|\~$|^\./)} as FileFilter | |
public DirWatcher(String path) { | |
this.path = path; | |
def files = new File(path).listFiles(fileFilter); | |
// transfer to the hashmap be used a reference and keep the lastModfied value | |
for(File file : files) { | |
dir.put(file, file.lastModified()); | |
} | |
} | |
public final void run() { | |
def checkedFiles = new HashSet(); | |
def files = new File(path).listFiles(fileFilter); | |
// scan the files and check for modification/addition | |
for (File file : files) { | |
Long current = dir.get(file) | |
checkedFiles.add(file) | |
if (current == null) { | |
// new file | |
dir.put(file, new Long(file.lastModified())) | |
onChange(file, "add") | |
} | |
else if (current.longValue() != file.lastModified()){ | |
// modified file | |
dir.put(file, new Long(file.lastModified())) | |
onChange(file, "modify") | |
} | |
} | |
// now check for deleted files | |
def deletedFiles = dir.clone().keySet() - checkedFiles | |
deletedFiles.each { | |
dir.remove(it) | |
onChange(it, "delete") | |
} | |
} | |
protected abstract void onChange(File file, String action); | |
} | |
class AppRunner extends DirWatcher { | |
def proc = null | |
def script | |
AppRunner(String script, String path) { | |
super(path) | |
this.script = script | |
} | |
def manageApp() { | |
runApp() | |
Timer timer = new Timer() | |
timer.schedule(this, new Date(), 1000) | |
} | |
def runApp() { | |
proc = "groovy ${script}".execute() | |
proc.consumeProcessOutput(System.out, System.err) | |
} | |
def killApp() { | |
proc.waitForOrKill(1000) | |
} | |
void onChange(File file, String action) { | |
println ("File "+ file.name +" action: " + action ) | |
if (proc) { | |
println "KILLING" | |
killApp() | |
println "RELOADING" | |
} else { | |
println "STARTING" | |
} | |
runApp() | |
} | |
} | |
if (args.length == 2) { | |
new AppRunner(args[0], args[1]).manageApp() | |
} else { | |
println "Usage:" | |
println "groovy runner.groovy [script] [dir to watch]" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment