Skip to content

Instantly share code, notes, and snippets.

@draftcode
Created November 10, 2010 05:31
Show Gist options
  • Save draftcode/670404 to your computer and use it in GitHub Desktop.
Save draftcode/670404 to your computer and use it in GitHub Desktop.
#!/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