2011-09-20 13:07:29 +00:00
|
|
|
# Guard is the main module for all Guard related modules and classes.
|
|
|
|
# Also other Guard implementation should use this namespace.
|
|
|
|
#
|
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'
|
2011-09-22 22:20:35 +00:00
|
|
|
autoload :Group, 'guard/group'
|
2011-06-21 13:48:27 +00:00
|
|
|
autoload :Interactor, 'guard/interactor'
|
|
|
|
autoload :Listener, 'guard/listener'
|
|
|
|
autoload :Watcher, 'guard/watcher'
|
|
|
|
autoload :Notifier, 'guard/notifier'
|
2011-08-16 23:34:27 +00:00
|
|
|
autoload :Hook, 'guard/hook'
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
class << self
|
2011-09-22 22:21:30 +00:00
|
|
|
attr_accessor :options, :interactor, :listener
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 09:40:47 +00:00
|
|
|
# Initialize the Guard singleton.
|
|
|
|
#
|
|
|
|
# @param [Hash] options the Guard options.
|
|
|
|
# @option options [Boolean] clear if auto clear the UI should be done
|
|
|
|
# @option options [Boolean] notify if system notifications should be shown
|
|
|
|
# @option options [Boolean] debug if debug output should be shown
|
|
|
|
# @option options [Array<String>] group the list of groups to start
|
|
|
|
# @option options [String] watchdir the director to watch
|
|
|
|
# @option options [String] guardfile the path to the Guardfile
|
|
|
|
#
|
2010-11-30 20:15:03 +00:00
|
|
|
def setup(options = {})
|
2011-08-30 19:13:51 +00:00
|
|
|
@options = options
|
|
|
|
@guards = []
|
2011-09-22 22:20:35 +00:00
|
|
|
@groups = [Group.new(: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-09-20 09:40:47 +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-04-10 20:32:29 +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
|
|
|
|
2011-09-22 22:21:30 +00:00
|
|
|
# Smart accessor for retrieving a specific guard or several guards at once.
|
|
|
|
#
|
|
|
|
# @param [Object] filter an optional filter to retrieve specific guard(s).
|
|
|
|
# @option filter [String, Symbol] return the guard with the given name, or nil if not found
|
|
|
|
# @option filter [Regexp] returns all guards matching the Regexp, or [] if no guard match
|
|
|
|
# @option filter [NilClass] returns all guards
|
|
|
|
#
|
|
|
|
def guards(filter = nil)
|
|
|
|
case filter
|
|
|
|
when String, Symbol
|
|
|
|
@guards.find { |guard| guard.class.to_s.downcase.sub('guard::', '') == filter.to_s.downcase.gsub('-', '') }
|
|
|
|
when Regexp
|
|
|
|
@guards.find_all { |guard| guard.class.to_s.downcase.sub('guard::', '') =~ filter }
|
|
|
|
when Hash
|
|
|
|
filter.inject(@guards) do |matches, (k, v)|
|
2011-09-22 22:39:27 +00:00
|
|
|
if k.to_sym == :name
|
|
|
|
matches.find_all { |guard| guard.class.to_s.downcase.sub('guard::', '') == v.to_s.downcase.gsub('-', '') }
|
|
|
|
else
|
|
|
|
matches.find_all { |guard| guard.send(k).to_sym == v.to_sym }
|
|
|
|
end
|
2011-09-22 22:21:30 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
@guards
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Smart accessor for retrieving a specific group or several groups at once.
|
|
|
|
#
|
|
|
|
# @param [Object] filter an optional filter to retrieve specific group(s).
|
|
|
|
# @option filter [String, Symbol] return the group with the given name, or nil if not found
|
|
|
|
# @option filter [Regexp] returns all groups matching the Regexp, or [] if no group match
|
|
|
|
# @option filter [NilClass] returns all groups
|
|
|
|
#
|
|
|
|
def groups(filter = nil)
|
|
|
|
case filter
|
|
|
|
when String, Symbol
|
|
|
|
@groups.find { |group| group.name == filter.to_sym }
|
|
|
|
when Regexp
|
|
|
|
@groups.find_all { |group| group.name =~ filter }
|
|
|
|
else
|
|
|
|
@groups
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-20 09:40:47 +00:00
|
|
|
# Start Guard by evaluate the `Guardfile`, initialize the declared Guards
|
|
|
|
# and start the available file change listener.
|
|
|
|
#
|
|
|
|
# @param [Hash] options the Guard options.
|
|
|
|
# @option options [Boolean] clear if auto clear the UI should be done
|
|
|
|
# @option options [Boolean] notify if system notifications should be shown
|
|
|
|
# @option options [Boolean] debug if debug output should be shown
|
|
|
|
# @option options [Array<String>] group the list of groups to start
|
|
|
|
# @option options [String] watchdir the director to watch
|
|
|
|
# @option options [String] guardfile the path to the Guardfile
|
|
|
|
#
|
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-07-02 08:01:45 +00:00
|
|
|
listener.on_change do |files|
|
2011-09-01 21:24:45 +00:00
|
|
|
Dsl.reevaluate_guardfile if Watcher.match_guardfile?(files)
|
|
|
|
listener.changed_files += files if Watcher.match_files?(guards, files)
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
2011-07-02 08:01:45 +00:00
|
|
|
|
2011-09-20 09:40:47 +00:00
|
|
|
UI.info "Guard is now watching at '#{ listener.directory }'"
|
2011-09-15 23:01:58 +00:00
|
|
|
|
|
|
|
execute_supervised_task_for_all_guards(:start)
|
2011-08-30 19:13:51 +00:00
|
|
|
|
|
|
|
interactor.start
|
2011-07-02 08:01:45 +00:00
|
|
|
listener.start
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 09:40:47 +00:00
|
|
|
# Stop Guard listening to file changes
|
|
|
|
#
|
2011-08-30 19:13:51 +00:00
|
|
|
def stop
|
2011-09-20 09:40:47 +00:00
|
|
|
UI.info 'Bye bye...', :reset => true
|
2011-08-30 19:13:51 +00:00
|
|
|
listener.stop
|
2011-09-15 23:01:58 +00:00
|
|
|
execute_supervised_task_for_all_guards(:stop)
|
2011-08-30 19:13:51 +00:00
|
|
|
abort
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 09:40:47 +00:00
|
|
|
# Reload all Guards currently enabled.
|
|
|
|
#
|
2011-08-30 19:13:51 +00:00
|
|
|
def reload
|
|
|
|
run do
|
2011-09-15 23:01:58 +00:00
|
|
|
execute_supervised_task_for_all_guards(: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-09-20 09:40:47 +00:00
|
|
|
# Trigger `run_all` on all Guards currently enabled.
|
|
|
|
#
|
2011-08-30 19:13:51 +00:00
|
|
|
def run_all
|
|
|
|
run do
|
2011-09-15 23:01:58 +00:00
|
|
|
execute_supervised_task_for_all_guards(:run_all)
|
2011-08-30 19:13:51 +00:00
|
|
|
end
|
|
|
|
end
|
2011-08-13 14:43:32 +00:00
|
|
|
|
2011-09-20 09:40:47 +00:00
|
|
|
# Pause Guard listening to file changes.
|
|
|
|
#
|
2011-08-30 19:13:51 +00:00
|
|
|
def pause
|
|
|
|
if listener.locked
|
2011-09-20 09:40:47 +00:00
|
|
|
UI.info 'Un-paused files modification listening', :reset => true
|
2011-08-30 19:13:51 +00:00
|
|
|
listener.clear_changed_files
|
|
|
|
listener.unlock
|
|
|
|
else
|
2011-09-20 09:40:47 +00:00
|
|
|
UI.info 'Paused files modification listening', :reset => true
|
2011-08-30 19:13:51 +00:00
|
|
|
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-09-22 20:44:15 +00:00
|
|
|
# Trigger `run_on_change` on all Guards currently enabled.
|
2011-09-20 09:40:47 +00:00
|
|
|
#
|
2011-09-22 20:44:15 +00:00
|
|
|
def run_on_change(paths)
|
2011-08-30 19:13:51 +00:00
|
|
|
run do
|
2011-09-22 20:44:15 +00:00
|
|
|
execute_supervised_task_for_all_guards(:run_on_change, paths)
|
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-09-20 09:40:47 +00:00
|
|
|
# Run a block where the listener and the interactor is
|
|
|
|
# blocked.
|
|
|
|
#
|
|
|
|
# @yield the block to run
|
|
|
|
#
|
2011-08-30 19:13:51 +00:00
|
|
|
def run
|
|
|
|
listener.lock
|
|
|
|
interactor.lock
|
|
|
|
UI.clear if options[:clear]
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
rescue Interrupt
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
2011-08-30 19:13:51 +00:00
|
|
|
interactor.unlock
|
|
|
|
listener.unlock
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-22 20:44:15 +00:00
|
|
|
# Loop through all groups and execute the given task for each Guard in it,
|
|
|
|
# but halt the task execution for the all Guards within a group if one Guard
|
|
|
|
# throws `:task_has_failed` and the group has its `:halt_on_fail` option to `true`.
|
|
|
|
#
|
|
|
|
# @param [Symbol] task the task to run
|
|
|
|
# @param [Array] files the list of files to pass to the task
|
|
|
|
#
|
2011-09-15 23:01:58 +00:00
|
|
|
def execute_supervised_task_for_all_guards(task, files = nil)
|
2011-09-22 22:20:35 +00:00
|
|
|
groups.each do |group|
|
|
|
|
catch group.options[:halt_on_fail] == true ? :task_has_failed : :no_catch do
|
2011-09-22 22:21:30 +00:00
|
|
|
guards(:group => group.name).each do |guard|
|
2011-09-22 20:44:15 +00:00
|
|
|
if task == :run_on_change
|
|
|
|
paths = Watcher.match_files(guard, files)
|
2011-09-15 23:01:58 +00:00
|
|
|
UI.debug "#{guard.class.name}##{task} with #{paths.inspect}"
|
|
|
|
supervised_task(guard, task, paths)
|
|
|
|
else
|
|
|
|
supervised_task(guard, task)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-20 09:40:47 +00:00
|
|
|
# Let a Guard execute its task, but fire it
|
|
|
|
# if his work leads to a system failure.
|
|
|
|
#
|
2011-09-20 19:52:59 +00:00
|
|
|
# @param [Guard::Guard] guard the Guard to execute
|
2011-09-20 09:40:47 +00:00
|
|
|
# @param [Symbol] task_to_supervise the task to run
|
2011-09-20 10:08:51 +00:00
|
|
|
# @param [Array] args the arguments for the task
|
2011-09-20 09:40:47 +00:00
|
|
|
# @return [Boolean, Exception] the result of the Guard
|
|
|
|
#
|
2010-11-30 20:15:03 +00:00
|
|
|
def supervised_task(guard, task_to_supervise, *args)
|
2011-09-20 09:40:47 +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-09-20 09:40:47 +00:00
|
|
|
guard.hook("#{ task_to_supervise }_end", result)
|
|
|
|
|
2011-09-15 23:20:22 +00:00
|
|
|
result
|
2011-09-15 23:01:58 +00:00
|
|
|
|
2011-06-19 18:24:47 +00:00
|
|
|
rescue Exception => ex
|
2011-09-20 09:40:47 +00:00
|
|
|
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-09-20 09:40:47 +00:00
|
|
|
UI.info("\n#{ guard.class.name } has just been fired")
|
|
|
|
|
|
|
|
ex
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 09:40:47 +00:00
|
|
|
# Add a Guard to use.
|
|
|
|
#
|
|
|
|
# @param [String] name the Guard name
|
|
|
|
# @param [Array<Watcher>] watchers the list of declared watchers
|
|
|
|
# @param [Array<Hash>] callbacks the list of callbacks
|
2011-09-20 19:52:59 +00:00
|
|
|
# @param [Hash] options the Guard options
|
2011-09-20 09:40:47 +00:00
|
|
|
#
|
2011-08-17 08:45:20 +00:00
|
|
|
def add_guard(name, watchers = [], callbacks = [], options = {})
|
2011-08-16 22:39:45 +00:00
|
|
|
if name.to_sym == :ego
|
2011-09-20 09:40:47 +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-16 23:34:27 +00:00
|
|
|
guard_class = get_guard_class(name)
|
2011-09-04 16:00:29 +00:00
|
|
|
callbacks.each { |callback| Hook.add_callback(callback[:listener], guard_class, callback[:events]) }
|
2011-08-17 08:45:20 +00:00
|
|
|
@guards << guard_class.new(watchers, options)
|
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-09-20 09:40:47 +00:00
|
|
|
# Add a Guard group.
|
|
|
|
#
|
|
|
|
# @param [String] name the group name
|
2011-09-22 20:44:15 +00:00
|
|
|
# @param [Hash] options the group options
|
2011-09-22 22:20:35 +00:00
|
|
|
# @option options [Boolean] halt_on_fail if a task execution
|
|
|
|
# should be halted for all Guards in this group if one Guard throws `:task_has_failed`
|
2011-09-20 09:40:47 +00:00
|
|
|
#
|
2011-09-15 23:01:58 +00:00
|
|
|
def add_group(name, options = {})
|
2011-09-22 22:20:35 +00:00
|
|
|
group = groups(name)
|
|
|
|
if group.nil?
|
|
|
|
group = Group.new(name, options)
|
|
|
|
@groups << group
|
|
|
|
end
|
|
|
|
group
|
2011-04-21 21:39:46 +00:00
|
|
|
end
|
|
|
|
|
2011-09-20 09:40:47 +00:00
|
|
|
# Tries to load the Guard main class.
|
|
|
|
#
|
|
|
|
# @param [String] name the name of the Guard
|
|
|
|
# @return [Class, nil] the loaded class
|
|
|
|
#
|
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-09-20 09:40:47 +00:00
|
|
|
require "guard/#{ name.downcase }" if try_require
|
2011-08-16 22:39:45 +00:00
|
|
|
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
|
2011-09-20 09:40:47 +00:00
|
|
|
UI.error "Could not find class Guard::#{ const_name.capitalize }"
|
2011-05-26 16:53:58 +00:00
|
|
|
end
|
2011-08-02 10:49:15 +00:00
|
|
|
rescue LoadError => loadError
|
2011-09-20 09:40:47 +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
|
|
|
|
2011-09-20 09:40:47 +00:00
|
|
|
# Locate a path to a Guard gem.
|
|
|
|
#
|
|
|
|
# @param [String] name the name of the Guard without the prefix `guard-`
|
|
|
|
# @return [String] the full path to the Guard gem
|
|
|
|
#
|
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')
|
2011-09-20 09:40:47 +00:00
|
|
|
Gem::Specification.find_by_name("guard-#{ name }").full_gem_path
|
2011-05-06 19:51:50 +00:00
|
|
|
else
|
2011-09-20 09:40:47 +00:00
|
|
|
Gem.source_index.find_name("guard-#{ name }").last.full_gem_path
|
2011-05-06 19:51:50 +00:00
|
|
|
end
|
2010-10-10 10:38:25 +00:00
|
|
|
rescue
|
2011-09-20 09:40:47 +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.
|
2011-09-20 09:40:47 +00:00
|
|
|
#
|
|
|
|
# @return [Array<String>] a list of guard gem names
|
|
|
|
#
|
2011-08-09 00:11:22 +00:00
|
|
|
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-09-20 09:40:47 +00:00
|
|
|
# Adds a command logger in debug mode. This wraps common command
|
|
|
|
# execution functions and logs the executed command before execution.
|
|
|
|
#
|
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|
|
2011-09-20 10:10:53 +00:00
|
|
|
::Guard::UI.debug "Command execution: #{ command } #{ args.join(' ') }"
|
2011-08-05 12:37:08 +00:00
|
|
|
original_system command, *args
|
|
|
|
end
|
|
|
|
|
2011-09-20 09:40:47 +00:00
|
|
|
Kernel.send(:alias_method, :original_backtick, :'`')
|
|
|
|
Kernel.send(:define_method, :'`') do |command|
|
2011-09-20 10:10:53 +00:00
|
|
|
::Guard::UI.debug "Command execution: #{ command }"
|
2011-08-05 12:37:08 +00:00
|
|
|
original_backtick command
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-02-18 18:53:05 +00:00
|
|
|
end
|