2010-10-07 20:37:30 +00:00
|
|
|
require 'bundler'
|
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
module Guard
|
|
|
|
|
|
|
|
autoload :UI, 'guard/ui'
|
|
|
|
autoload :Dsl, 'guard/dsl'
|
|
|
|
autoload :Interactor, 'guard/interactor'
|
|
|
|
autoload :Listener, 'guard/listener'
|
|
|
|
autoload :Watcher, 'guard/watcher'
|
|
|
|
autoload :Notifier, 'guard/notifier'
|
|
|
|
|
|
|
|
class << self
|
|
|
|
attr_accessor :options, :guards, :listener
|
|
|
|
|
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 = []
|
2010-11-25 23:58:36 +00:00
|
|
|
self
|
2010-10-27 13:18:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def start(options = {})
|
2010-11-30 20:15:03 +00:00
|
|
|
setup(options)
|
2010-10-03 21:00:33 +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)
|
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
|
2010-11-30 20:15:03 +00:00
|
|
|
UI.info "Guard is now watching at '#{Dir.pwd}'"
|
|
|
|
guards.each { |g| supervised_task(g, :start) }
|
2010-10-07 20:37:30 +00:00
|
|
|
|
2010-11-30 20:15:03 +00:00
|
|
|
Thread.new { listener.start }
|
|
|
|
wait_for_changes_and_launch_guards
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for_changes_and_launch_guards
|
|
|
|
loop do
|
|
|
|
if !running? && !listener.changed_files.empty?
|
|
|
|
changed_files = listener.get_and_clear_changed_files
|
2010-12-01 07:37:31 +00:00
|
|
|
if Watcher.match_files?(guards, changed_files)
|
2010-11-25 07:52:53 +00:00
|
|
|
run do
|
|
|
|
guards.each do |guard|
|
2010-11-30 20:23:53 +00:00
|
|
|
paths = Watcher.match_files(guard, changed_files)
|
2010-11-25 07:52:53 +00:00
|
|
|
supervised_task(guard, :run_on_change, paths) unless paths.empty?
|
|
|
|
end
|
2010-10-07 20:37:30 +00:00
|
|
|
end
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
|
|
|
end
|
2010-11-30 20:15:03 +00:00
|
|
|
sleep 0.2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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)
|
|
|
|
guard.send(task_to_supervise, *args)
|
|
|
|
rescue Exception
|
|
|
|
UI.error("#{guard.class.name} guard failed to achieve its <#{task_to_supervise.to_s}> command: #{$!}")
|
|
|
|
::Guard.guards.delete guard
|
|
|
|
UI.info("Guard #{guard.class.name} has just been fired")
|
|
|
|
return $!
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
@run = true
|
|
|
|
UI.clear if options[:clear]
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
rescue Interrupt
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2010-11-30 20:15:03 +00:00
|
|
|
@run = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def running?
|
|
|
|
@run == true
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_guard(name, watchers = [], options = {})
|
2010-10-07 20:37:30 +00:00
|
|
|
guard_class = get_guard_class(name)
|
|
|
|
@guards << guard_class.new(watchers, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_guard_class(name)
|
2010-10-03 21:00:33 +00:00
|
|
|
require "guard/#{name.downcase}"
|
2010-10-18 19:45:31 +00:00
|
|
|
klasses = []
|
|
|
|
ObjectSpace.each_object(Class) do |klass|
|
2010-12-16 14:08:05 +00:00
|
|
|
klasses << klass if klass.to_s.downcase.match(/^guard::#{name.downcase}/)
|
2010-10-18 19:45:31 +00:00
|
|
|
end
|
|
|
|
klasses.first
|
2010-10-07 20:37:30 +00:00
|
|
|
rescue LoadError
|
2010-11-30 20:15:03 +00:00
|
|
|
UI.error "Could not find gem 'guard-#{name}', please add it in your Gemfile."
|
2010-10-27 13:18:00 +00:00
|
|
|
end
|
|
|
|
|
2010-10-07 20:37:30 +00:00
|
|
|
def locate_guard(name)
|
2010-10-23 10:43:51 +00:00
|
|
|
`gem open guard-#{name} --latest --command echo`.chomp
|
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
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|