Created
November 10, 2010 05:31
-
-
Save draftcode/670404 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
#!/usr/bin/env ruby | |
require 'rake' | |
module FileWatchHandler | |
def execute | |
EM.stop_event_loop | |
end | |
alias :file_modified :execute | |
alias :file_moved :execute | |
alias :file_deleted :execute | |
end | |
def autotask(*args) | |
def resolve_dependency(tasknames) | |
files = [] | |
tasks = [] | |
tasknames.each do |taskname| | |
if Rake::Task.task_defined?(taskname) | |
task = Rake::Task[taskname] | |
files << task.name if task.is_a? Rake::FileTask | |
tasks << task.name | |
dep_files, dep_tasks = resolve_dependency(task.prerequisites) | |
files += dep_files | |
tasks += dep_tasks | |
else | |
files << taskname | |
end | |
end | |
return files, tasks | |
end | |
fail "Task Argument Error" if args.size != 1 | |
hash = args.pop | |
fail "Task Argument Error" if hash.size != 1 | |
key, value = hash.map { |k, v| [k,v] }.first | |
require 'eventmachine' | |
EM.kqueue = true if EM.kqueue? | |
value = [value] if !value.is_a?(Array) | |
task key => value do | |
loop do | |
files, tasks = resolve_dependency(value) | |
EM.run do | |
files.each {|v| EM.watch_file(v, FileWatchHandler)} | |
end | |
tasks.each do |taskname| | |
Rake::Task[taskname].reenable | |
end | |
value.each do |taskname| | |
Rake::Task[taskname].invoke | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment