master/lib/guard.rb

415 lines
13 KiB
Ruby
Raw Normal View History

require 'thread'
# 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
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'
autoload :Interactor, 'guard/interactor'
autoload :Listener, 'guard/listener'
autoload :Watcher, 'guard/watcher'
autoload :Notifier, 'guard/notifier'
autoload :Hook, 'guard/hook'
2010-10-03 21:00:33 +00:00
class << self
attr_accessor :options, :interactor, :listener
2011-09-20 09:40:47 +00:00
# Initialize the Guard singleton.
#
# @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
# @option options [Boolean] watch_all_modifications watches all file modifications if true
2011-09-27 18:44:17 +00:00
#
def setup(options = {})
@lock = Mutex.new
@options = options
@guards = []
2011-09-22 22:20:35 +00:00
@groups = [Group.new(:default)]
@interactor = Interactor.new
@listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd, options)
2011-09-20 09:40:47 +00:00
@options[:notify] && ENV['GUARD_NOTIFY'] != 'false' ? Notifier.turn_on : Notifier.turn_off
UI.clear if @options[:clear]
debug_command_execution if @options[:debug]
self
end
# Smart accessor for retrieving a specific guard or several guards at once.
#
2011-09-23 09:01:52 +00:00
# @param [String, Symbol] filter return the guard with the given name, or nil if not found
# @param [Regexp] filter returns all guards matching the Regexp, or [] if no guard found
2011-09-23 08:52:58 +00:00
# @param [Hash] filter returns all guards matching the given Hash.
2011-09-23 09:01:52 +00:00
# Example: `{ :name => 'rspec', :group => 'backend' }`, or [] if no guard found
# @param [NilClass] filter returns all guards
2011-09-23 08:52:58 +00:00
#
# @see Guard.groups
#
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)|
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
end
else
@guards
end
end
# Smart accessor for retrieving a specific group or several groups at once.
#
2011-09-23 09:01:52 +00:00
# @param [NilClass] filter returns all groups
# @param [String, Symbol] filter return the group with the given name, or nil if not found
# @param [Regexp] filter returns all groups matching the Regexp, or [] if no group found
2011-09-23 08:52:58 +00:00
#
# @see Guard.guards
#
def groups(filter = nil)
case filter
when String, Symbol
@groups.find { |group| group.name == filter.to_sym }
when Regexp
2011-09-22 22:53:13 +00:00
@groups.find_all { |group| group.name.to_s =~ 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.
#
# @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
#
def start(options = {})
setup(options)
Dsl.evaluate_guardfile(options)
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)
end
2011-09-20 09:40:47 +00:00
UI.info "Guard is now watching at '#{ listener.directory }'"
run_guard_task(:start)
interactor.start
listener.start
end
2011-09-20 09:40:47 +00:00
# Stop Guard listening to file changes
#
def stop
2011-09-20 09:40:47 +00:00
UI.info 'Bye bye...', :reset => true
run_guard_task(:stop)
listener.stop
abort
end
2011-09-20 09:40:47 +00:00
# Reload all Guards currently enabled.
#
def reload
run do
run_guard_task(:reload)
end
end
2011-09-20 09:40:47 +00:00
# Trigger `run_all` on all Guards currently enabled.
#
def run_all
run do
run_guard_task(:run_all)
end
end
2011-09-20 09:40:47 +00:00
# Pause Guard listening to file changes.
#
def pause
if listener.paused?
2011-09-20 09:40:47 +00:00
UI.info 'Un-paused files modification listening', :reset => true
listener.clear_changed_files
listener.run
else
2011-09-20 09:40:47 +00:00
UI.info 'Paused files modification listening', :reset => true
listener.pause
end
end
# Trigger `run_on_change` on all Guards currently enabled.
2011-09-20 09:40:47 +00:00
#
def run_on_change(paths)
run do
run_guard_task(:run_on_change, paths)
end
end
2011-09-20 09:40:47 +00:00
# Run a block where the listener and the interactor is
# blocked.
#
# @yield the block to run
#
def run
UI.clear if options[:clear]
@lock.synchronize do
begin
@interactor.stop_if_not_current
yield
rescue Interrupt
end
@interactor.start
end
end
# Loop through all groups and run the given task for each Guard.
#
# Stop the task run for the all Guards within a group if one Guard
# throws `:task_has_failed`.
#
# @param [Symbol] task the task to run
# @param [Array<String>] files the list of files to pass to the task
#
def run_guard_task(task, files = nil)
2011-09-22 22:20:35 +00:00
groups.each do |group|
catch :task_has_failed do
guards(:group => group.name).each do |guard|
if task == :run_on_change
run_on_change_task(files, guard, task)
else
run_supervised_task(guard, task)
end
end
end
end
end
2011-09-28 11:41:29 +00:00
# Run the `:run_on_change` task. When the option `:watch_all_modifications` is set,
# the task is split to run changed paths on {Guard::Guard#run_on_change}, whereas
# deleted paths run on {Guard::Guard#run_on_deletion}.
#
# @param [Array<String>] files the list of files to pass to the task
# @param [Guard::Guard] guard the guard to run
# @param [Symbol] task the task to run
# @raise [:task_has_failed] when task has failed
#
def run_on_change_task(files, guard, task)
paths = Watcher.match_files(guard, files)
changes = changed_paths(paths)
deletions = deleted_paths(paths)
unless changes.empty?
UI.debug "#{ guard.class.name }##{ task } with #{ changes.inspect }"
run_supervised_task(guard, task, changes)
end
unless deletions.empty?
UI.debug "#{ guard.class.name }#run_on_deletion with #{ deletions.inspect }"
run_supervised_task(guard, :run_on_deletion, deletions)
end
end
# Detects the paths that have changed.
#
2011-09-28 11:41:29 +00:00
# Deleted paths are prefixed by an exclamation point.
# @see Guard::Listener#modified_files
#
# @param [Array<String>] paths the watched paths
# @return [Array<String>] the changed paths
#
def changed_paths(paths)
paths.select { |f| !f.start_with?('!') }
end
# Detects the paths that have been deleted.
#
2011-09-28 11:41:29 +00:00
# Deleted paths are prefixed by an exclamation point.
# @see Guard::Listener#modified_files
#
# @param [Array<String>] paths the watched paths
# @return [Array<String>] the deleted paths
#
def deleted_paths(paths)
paths.select { |f| f.start_with?('!') }.map { |f| f.slice(1..-1) }
end
# Run a Guard task, but remove the Guard when his work leads to a system failure.
2011-09-20 09:40:47 +00:00
#
# When the Group has `:halt_on_fail` disabled, we've to catch `:task_has_failed`
# here in order to avoid an uncaught throw error.
#
2011-09-20 19:52:59 +00:00
# @param [Guard::Guard] guard the Guard to execute
# @param [Symbol] task the task to run
2011-09-20 10:08:51 +00:00
# @param [Array] args the arguments for the task
# @raise [:task_has_failed] when task has failed
2011-09-20 09:40:47 +00:00
#
def run_supervised_task(guard, task, *args)
catch guard_symbol(guard) do
guard.hook("#{ task }_begin", *args)
result = guard.send(task, *args)
guard.hook("#{ task }_end", result)
2011-09-20 09:40:47 +00:00
result
end
2011-09-20 09:40:47 +00:00
rescue Exception => ex
UI.error("#{ guard.class.name } failed to achieve its <#{ task.to_s }>, exception was:" +
2011-09-20 09:40:47 +00:00
"\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
end
# Get the symbol we have to catch when running a supervised task.
# If we are within a Guard group that has the `:halt_on_fail`
# option set, we do NOT catch it here, it will be catched at the
# group level.
#
# @see .run_guard_task
#
# @param [Guard::Guard] guard the Guard to execute
# @return [Symbol] the symbol to catch
#
def guard_symbol(guard)
if guard.group.class == Symbol
group = groups(guard.group)
group.options[:halt_on_fail] ? :no_catch : :task_has_failed
else
:task_has_failed
end
end
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-23 09:01:52 +00:00
# @param [Hash] options the Guard options (see the given Guard documentation)
2011-09-20 09:40:47 +00:00
#
def add_guard(name, watchers = [], callbacks = [], options = {})
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.')
else
guard_class = get_guard_class(name)
callbacks.each { |callback| Hook.add_callback(callback[:listener], guard_class, callback[:events]) }
@guards << guard_class.new(watchers, options)
end
2010-10-07 20:37:30 +00:00
end
2011-09-20 09:40:47 +00:00
# Add a Guard group.
#
# @param [String] name the group name
2011-09-22 22:20:35 +00:00
# @option options [Boolean] halt_on_fail if a task execution
2011-09-23 09:01:52 +00:00
# should be halted for all Guards in this group if one Guard throws `:task_has_failed`
2011-09-23 08:52:58 +00:00
# @return [Guard::Group] the group added (or retrieved from the `@groups` variable if already present)
2011-09-20 09:40:47 +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
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
try_require = false
2011-08-16 23:36:44 +00:00
const_name = name.downcase.gsub('-', '')
begin
2011-09-20 09:40:47 +00:00
require "guard/#{ name.downcase }" if try_require
self.const_get(self.constants.find { |c| c.to_s.downcase == const_name })
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 }"
end
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 }"
UI.error loadError.to_s
end
end
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)
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
else
2011-09-20 09:40:47 +00:00
Gem.source_index.find_name("guard-#{ name }").last.full_gem_path
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
# 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
#
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.
#
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(' ') }"
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 }"
original_backtick command
end
end
2010-10-03 21:00:33 +00:00
end
end