master/lib/guard.rb

101 lines
2.8 KiB
Ruby
Raw Normal View History

2010-10-07 20:37:30 +00:00
require 'bundler'
2010-10-03 21:00:33 +00:00
module Guard
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'
2010-10-03 21:00:33 +00:00
class << self
attr_accessor :options, :guards, :listener
# initialize this singleton
def setup(options = {})
2010-10-03 21:00:33 +00:00
@options = options
@listener = Listener.select_and_init
2010-10-03 21:00:33 +00:00
@guards = []
self
end
def start(options = {})
setup(options)
Interactor.init_signal_traps
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
listener.on_change do |files|
if Watcher.match_files?(guards, files)
run { run_on_change_for_all_guards(files) }
end
end
UI.info "Guard is now watching at '#{Dir.pwd}'"
guards.each { |guard| supervised_task(guard, :start) }
listener.start
end
end
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
# 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
unless new_modified_files.empty?
run_on_change_for_all_guards(new_modified_files)
end
end
# Let a guard execute its task but
# fire it if his work leads to a system failure
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
listener.stop
UI.clear if options[:clear]
begin
yield
rescue Interrupt
2010-10-03 21:00:33 +00:00
end
listener.start
end
2010-10-03 21:00:33 +00:00
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
2010-10-07 20:37:30 +00:00
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
UI.error "Could not find gem 'guard-#{name}', please add it in your Gemfile."
end
2010-10-07 20:37:30 +00:00
def locate_guard(name)
`gem open guard-#{name} --latest --command echo`.chomp
2010-10-10 10:38:25 +00:00
rescue
UI.error "Could not find 'guard-#{name}' gem path."
2010-10-03 21:00:33 +00:00
end
2010-10-03 21:00:33 +00:00
end
end