master/lib/guard.rb

68 lines
1.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
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
def start(options = {})
@options = options
@listener = Listener.init
2010-10-03 21:00:33 +00:00
@guards = []
Dsl.evaluate_guardfile
2010-10-07 20:37:30 +00:00
if guards.empty?
2010-10-08 13:00:45 +00:00
UI.error "No guards found in Guardfile, please add it at least one."
2010-10-07 20:37:30 +00:00
else
Interactor.init_signal_traps
listener.on_change do |files|
run do
guards.each do |guard|
paths = Watcher.match_files(guard, files)
guard.run_on_change(paths) unless paths.empty?
end
2010-10-03 21:00:33 +00:00
end
end
2010-10-07 20:37:30 +00:00
UI.info "Guard is now watching at '#{Dir.pwd}'"
guards.each(&:start)
listener.start
2010-10-03 21:00:33 +00:00
end
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}"
guard_class = ObjectSpace.each_object(Class).detect { |c| c.to_s.downcase.match "^guard::#{name.downcase}" }
2010-10-07 20:37:30 +00:00
rescue LoadError
2010-10-10 10:38:25 +00:00
UI.error "Could not find gem 'guard-#{name}' in the current Gemfile."
2010-10-07 20:37:30 +00:00
end
def locate_guard(name)
spec = Bundler.load.specs.find{|s| s.name == "guard-#{name}" }
spec.full_gem_path
2010-10-10 10:38:25 +00:00
rescue
UI.error "Could not find gem 'guard-#{name}' in the current Gemfile."
2010-10-03 21:00:33 +00:00
end
def run
listener.stop
UI. clear if options[:clear]
2010-10-03 21:00:33 +00:00
yield
listener.start
end
end
end