2010-10-03 21:00:33 +00:00
|
|
|
module Guard
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-06-21 13:48:27 +00:00
|
|
|
autoload :UI, 'guard/ui'
|
|
|
|
autoload :Dsl, 'guard/dsl'
|
|
|
|
autoload :DslDescriber, 'guard/dsl_describer'
|
|
|
|
autoload :Interactor, 'guard/interactor'
|
|
|
|
autoload :Listener, 'guard/listener'
|
|
|
|
autoload :Watcher, 'guard/watcher'
|
|
|
|
autoload :Notifier, 'guard/notifier'
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
class << self
|
2011-08-30 19:16:30 +00:00
|
|
|
attr_accessor :options, :guards, :groups, :interactor, :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 = {})
|
2011-08-30 19:13:51 +00:00
|
|
|
@options = options
|
|
|
|
@guards = []
|
2011-08-30 19:16:30 +00:00
|
|
|
@groups = [:default]
|
2011-08-30 19:13:51 +00:00
|
|
|
@interactor = Interactor.new
|
|
|
|
@listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd)
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-08 20:01:35 +00:00
|
|
|
@options[:notify] && ENV["GUARD_NOTIFY"] != 'false' ? Notifier.turn_on : Notifier.turn_off
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-07-28 22:22:53 +00:00
|
|
|
UI.clear if @options[:clear]
|
2011-08-05 12:37:08 +00:00
|
|
|
debug_command_execution if @options[:debug]
|
2011-07-02 10:16:06 +00:00
|
|
|
|
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-12-17 15:31:39 +00:00
|
|
|
Dsl.evaluate_guardfile(options)
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-08-30 19:13:51 +00:00
|
|
|
UI.info "Guard is now watching at '#{listener.directory}'"
|
|
|
|
guards.each { |guard| supervised_task(guard, :start) }
|
|
|
|
|
|
|
|
interactor.start
|
|
|
|
listener.start
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
UI.info "Bye bye...", :reset => true
|
|
|
|
listener.stop
|
|
|
|
guards.each { |guard| supervised_task(guard, :stop) }
|
|
|
|
abort
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-08-30 19:13:51 +00:00
|
|
|
def reload
|
|
|
|
run do
|
|
|
|
guards.each { |guard| supervised_task(guard, :reload) }
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
2011-08-30 19:13:51 +00:00
|
|
|
end
|
2011-07-02 08:01:45 +00:00
|
|
|
|
2011-08-30 19:13:51 +00:00
|
|
|
def run_all
|
|
|
|
run do
|
|
|
|
guards.each { |guard| supervised_task(guard, :run_all) }
|
|
|
|
end
|
|
|
|
end
|
2011-08-13 14:43:32 +00:00
|
|
|
|
2011-08-30 19:13:51 +00:00
|
|
|
def pause
|
|
|
|
if listener.locked
|
|
|
|
UI.info "Un-paused files modification listening", :reset => true
|
|
|
|
listener.clear_changed_files
|
|
|
|
listener.unlock
|
|
|
|
else
|
|
|
|
UI.info "Paused files modification listening", :reset => true
|
|
|
|
listener.lock
|
2011-08-29 19:25:58 +00:00
|
|
|
end
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-08-30 19:13:51 +00:00
|
|
|
def run_on_change(files)
|
|
|
|
run do
|
|
|
|
guards.each do |guard|
|
|
|
|
paths = Watcher.match_files(guard, files)
|
|
|
|
unless paths.empty?
|
|
|
|
UI.debug "#{guard.class.name}#run_on_change with #{paths.inspect}"
|
|
|
|
supervised_task(guard, :run_on_change, paths)
|
|
|
|
end
|
2011-07-20 23:21:04 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
end
|
2011-08-30 19:13:51 +00:00
|
|
|
end
|
2011-04-16 21:13:29 +00:00
|
|
|
|
2011-08-30 19:13:51 +00:00
|
|
|
def run
|
|
|
|
listener.lock
|
|
|
|
interactor.lock
|
|
|
|
UI.clear if options[:clear]
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
rescue Interrupt
|
|
|
|
end
|
|
|
|
interactor.unlock
|
|
|
|
listener.unlock
|
2010-11-30 20:15:03 +00:00
|
|
|
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)
|
|
|
|
guard.send(task_to_supervise, *args)
|
2011-06-19 18:24:47 +00:00
|
|
|
rescue Exception => ex
|
|
|
|
UI.error("#{guard.class.name} failed to achieve its <#{task_to_supervise.to_s}>, exception was:" +
|
|
|
|
"\n#{ex.class}: #{ex.message}\n#{ex.backtrace.join("\n")}")
|
2011-05-07 16:40:13 +00:00
|
|
|
guards.delete guard
|
2011-06-19 18:24:47 +00:00
|
|
|
UI.info("\n#{guard.class.name} has just been fired")
|
|
|
|
return ex
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
def add_guard(name, watchers = [], options = {})
|
2011-08-16 22:39:45 +00:00
|
|
|
if name.to_sym == :ego
|
2011-07-12 11:06:55 +00:00
|
|
|
UI.deprecation("Guard::Ego is now part of Guard. You can remove it from your Guardfile.")
|
2011-07-02 08:01:45 +00:00
|
|
|
else
|
2011-08-17 08:04:42 +00:00
|
|
|
guard = get_guard_class(name).new(watchers, options)
|
2011-08-16 22:39:45 +00:00
|
|
|
@guards << guard
|
2011-07-02 08:01:45 +00:00
|
|
|
end
|
2010-10-07 20:37:30 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-08-16 22:39:45 +00:00
|
|
|
def add_group(name)
|
|
|
|
@groups << name.to_sym unless name.nil?
|
|
|
|
end
|
|
|
|
|
2010-10-07 20:37:30 +00:00
|
|
|
def get_guard_class(name)
|
2011-08-16 23:36:44 +00:00
|
|
|
name = name.to_s
|
2011-05-26 16:53:58 +00:00
|
|
|
try_require = false
|
2011-08-16 23:36:44 +00:00
|
|
|
const_name = name.downcase.gsub('-', '')
|
2011-05-26 16:53:58 +00:00
|
|
|
begin
|
2011-08-16 22:39:45 +00:00
|
|
|
require "guard/#{name.downcase}" if try_require
|
|
|
|
self.const_get(self.constants.find { |c| c.to_s.downcase == const_name })
|
2011-05-26 16:53:58 +00:00
|
|
|
rescue TypeError
|
|
|
|
unless try_require
|
|
|
|
try_require = true
|
|
|
|
retry
|
|
|
|
else
|
|
|
|
UI.error "Could not find class Guard::#{const_name.capitalize}"
|
|
|
|
end
|
2011-08-02 10:49:15 +00:00
|
|
|
rescue LoadError => loadError
|
2011-05-26 16:53:58 +00:00
|
|
|
UI.error "Could not load 'guard/#{name.downcase}' or find class Guard::#{const_name.capitalize}"
|
2011-08-02 10:49:15 +00:00
|
|
|
UI.error loadError.to_s
|
2011-05-26 16:53:58 +00:00
|
|
|
end
|
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-05-06 19:51:50 +00:00
|
|
|
if Gem::Version.create(Gem::VERSION) >= Gem::Version.create('1.8.0')
|
|
|
|
Gem::Specification.find_by_name("guard-#{name}").full_gem_path
|
|
|
|
else
|
|
|
|
Gem.source_index.find_name("guard-#{name}").last.full_gem_path
|
|
|
|
end
|
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
|
|
|
|
2011-08-09 00:11:22 +00:00
|
|
|
##
|
|
|
|
# Returns a list of guard Gem names installed locally.
|
|
|
|
def guard_gem_names
|
|
|
|
if Gem::Version.create(Gem::VERSION) >= Gem::Version.create('1.8.0')
|
|
|
|
Gem::Specification.find_all.select { |x| x.name =~ /^guard-/ }
|
|
|
|
else
|
|
|
|
Gem.source_index.find_name(/^guard-/)
|
|
|
|
end.map { |x| x.name.sub /^guard-/, '' }
|
|
|
|
end
|
|
|
|
|
2011-08-05 12:37:08 +00:00
|
|
|
def debug_command_execution
|
|
|
|
Kernel.send(:alias_method, :original_system, :system)
|
|
|
|
Kernel.send(:define_method, :system) do |command, *args|
|
|
|
|
::Guard::UI.debug "Command execution: #{command} #{args.join(' ')}"
|
|
|
|
original_system command, *args
|
|
|
|
end
|
|
|
|
|
|
|
|
Kernel.send(:alias_method, :original_backtick, :"`")
|
|
|
|
Kernel.send(:define_method, :"`") do |command|
|
|
|
|
::Guard::UI.debug "Command execution: #{command}"
|
|
|
|
original_backtick command
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-02-18 18:53:05 +00:00
|
|
|
end
|