2010-10-03 21:00:33 +00:00
|
|
|
module Guard
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
autoload :UI, 'guard/ui'
|
|
|
|
autoload :Dsl, 'guard/dsl'
|
|
|
|
autoload :Interactor, 'guard/interactor'
|
|
|
|
autoload :Listener, 'guard/listener'
|
|
|
|
autoload :Watcher, 'guard/watcher'
|
|
|
|
autoload :Notifier, 'guard/notifier'
|
2011-04-14 20:31:34 +00:00
|
|
|
autoload :Hook, 'guard/hook'
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
class << self
|
|
|
|
attr_accessor :options, :guards, :listener
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-27 13:18:00 +00:00
|
|
|
# initialize this singleton
|
2010-11-30 20:15:03 +00:00
|
|
|
def setup(options = {})
|
2010-10-03 21:00:33 +00:00
|
|
|
@options = options
|
2010-11-30 20:15:03 +00:00
|
|
|
@listener = Listener.select_and_init
|
2010-10-03 21:00:33 +00:00
|
|
|
@guards = []
|
2011-04-10 20:32:29 +00:00
|
|
|
|
|
|
|
Notifier.turn_off unless options[:notify]
|
|
|
|
|
2010-11-25 23:58:36 +00:00
|
|
|
self
|
2010-10-27 13:18:00 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-27 13:18:00 +00:00
|
|
|
def start(options = {})
|
2010-11-30 20:15:03 +00:00
|
|
|
setup(options)
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-11-30 20:15:03 +00:00
|
|
|
Interactor.init_signal_traps
|
2010-12-17 15:31:39 +00:00
|
|
|
Dsl.evaluate_guardfile(options)
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-07 20:37:30 +00:00
|
|
|
if guards.empty?
|
2010-10-28 06:47:26 +00:00
|
|
|
UI.error "No guards found in Guardfile, please add at least one."
|
2010-10-07 20:37:30 +00:00
|
|
|
else
|
2011-01-19 22:05:45 +00:00
|
|
|
listener.on_change do |files|
|
2011-04-16 21:13:29 +00:00
|
|
|
run { run_on_change_for_all_guards(files) } if Watcher.match_files?(guards, files)
|
2011-01-19 22:05:45 +00:00
|
|
|
end
|
|
|
|
|
2010-11-30 20:15:03 +00:00
|
|
|
UI.info "Guard is now watching at '#{Dir.pwd}'"
|
2011-01-19 22:05:45 +00:00
|
|
|
guards.each { |guard| supervised_task(guard, :start) }
|
|
|
|
listener.start
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
|
|
|
def run_on_change_for_all_guards(files)
|
|
|
|
guards.each do |guard|
|
|
|
|
paths = Watcher.match_files(guard, files)
|
|
|
|
supervised_task(guard, :run_on_change, paths) unless paths.empty?
|
|
|
|
end
|
2011-04-16 21:13:29 +00:00
|
|
|
|
2011-01-19 22:05:45 +00:00
|
|
|
# Reparse the whole directory to catch new files modified during the guards run
|
|
|
|
new_modified_files = listener.modified_files([Dir.pwd + '/'], :all => true)
|
|
|
|
listener.update_last_event
|
2011-04-19 07:40:34 +00:00
|
|
|
if !new_modified_files.empty? && Watcher.match_files?(guards, new_modified_files)
|
|
|
|
run { run_on_change_for_all_guards(new_modified_files) }
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-12-16 00:22:42 +00:00
|
|
|
# Let a guard execute its task but
|
|
|
|
# fire it if his work leads to a system failure
|
2010-11-30 20:15:03 +00:00
|
|
|
def supervised_task(guard, task_to_supervise, *args)
|
2011-04-18 00:06:45 +00:00
|
|
|
guard.hook("#{task_to_supervise}_begin", *args)
|
2011-04-14 20:53:53 +00:00
|
|
|
result = guard.send(task_to_supervise, *args)
|
2011-04-18 00:06:45 +00:00
|
|
|
guard.hook("#{task_to_supervise}_end", result)
|
2011-04-14 20:53:53 +00:00
|
|
|
result
|
2010-11-30 20:15:03 +00:00
|
|
|
rescue Exception
|
2011-04-16 21:02:13 +00:00
|
|
|
UI.error("#{guard.class.name} guard failed to achieve its <#{task_to_supervise}> command: #{$!}")
|
2010-11-30 20:15:03 +00:00
|
|
|
::Guard.guards.delete guard
|
|
|
|
UI.info("Guard #{guard.class.name} has just been fired")
|
|
|
|
return $!
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-11-30 20:15:03 +00:00
|
|
|
def run
|
2011-01-19 22:05:45 +00:00
|
|
|
listener.stop
|
2010-11-30 20:15:03 +00:00
|
|
|
UI.clear if options[:clear]
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
rescue Interrupt
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
listener.start
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
def add_guard(name, watchers = [], callbacks = [], options = {})
|
2010-10-07 20:37:30 +00:00
|
|
|
guard_class = get_guard_class(name)
|
2011-04-16 21:02:13 +00:00
|
|
|
watchers = watchers.map { |watcher| ::Guard::Watcher.new(watcher[:pattern], watcher[:action]) }
|
2010-10-07 20:37:30 +00:00
|
|
|
@guards << guard_class.new(watchers, options)
|
2011-04-16 21:02:13 +00:00
|
|
|
callbacks.each { |callback| ::Guard::Hook.add_callback(callback[:listener], guard_class, callback[:events]) }
|
2010-10-07 20:37:30 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-07 20:37:30 +00:00
|
|
|
def get_guard_class(name)
|
2011-04-21 21:39:46 +00:00
|
|
|
try_to_load_gem name
|
2011-02-18 18:53:05 +00:00
|
|
|
self.const_get(self.constants.find{|klass_name| klass_name.to_s.downcase == name.downcase })
|
2011-04-21 21:39:46 +00:00
|
|
|
rescue TypeError
|
|
|
|
UI.error "Could not find load find gem 'guard-#{name}' or find class Guard::#{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def try_to_load_gem(name)
|
2011-04-25 14:18:59 +00:00
|
|
|
require "guard/#{name.downcase}"
|
2010-10-07 20:37:30 +00:00
|
|
|
rescue LoadError
|
2010-10-27 13:18:00 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-07 20:37:30 +00:00
|
|
|
def locate_guard(name)
|
2011-04-16 19:43:50 +00:00
|
|
|
Gem.source_index.find_name("guard-#{name}").last.full_gem_path
|
2010-10-10 10:38:25 +00:00
|
|
|
rescue
|
2010-10-20 20:34:32 +00:00
|
|
|
UI.error "Could not find 'guard-#{name}' gem path."
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-02-18 18:53:05 +00:00
|
|
|
end
|