master/lib/guard.rb

257 lines
8.1 KiB
Ruby
Raw Normal View History

2010-10-03 21:00:33 +00:00
module Guard
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'
autoload :Hook, 'guard/hook'
2010-10-03 21:00:33 +00:00
class << self
attr_accessor :options, :guards, :groups, :interactor, :listener
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
#
def setup(options = {})
@options = options
@guards = []
@groups = [:default]
@interactor = Interactor.new
@listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd)
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
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
#
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 }'"
guards.each { |guard| supervised_task(guard, :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
listener.stop
guards.each { |guard| supervised_task(guard, :stop) }
abort
end
2011-09-20 09:40:47 +00:00
# Reload all Guards currently enabled.
#
def reload
run do
guards.each { |guard| supervised_task(guard, :reload) }
end
end
2011-09-20 09:40:47 +00:00
# Trigger `run_all` on all Guards currently enabled.
#
def run_all
run do
guards.each { |guard| supervised_task(guard, :run_all) }
end
end
2011-09-20 09:40:47 +00:00
# Pause Guard listening to file changes.
#
def pause
if listener.locked
2011-09-20 09:40:47 +00:00
UI.info 'Un-paused files modification listening', :reset => true
listener.clear_changed_files
listener.unlock
else
2011-09-20 09:40:47 +00:00
UI.info 'Paused files modification listening', :reset => true
listener.lock
end
end
2011-09-20 09:40:47 +00:00
# Trigger `run_on_change` on all Guards currently enabled and
#
def run_on_change(files)
run do
guards.each do |guard|
paths = Watcher.match_files(guard, files)
unless paths.empty?
2011-09-20 09:40:47 +00:00
UI.debug "#{ guard.class.name }#run_on_change with #{ paths.inspect }"
supervised_task(guard, :run_on_change, paths)
end
end
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
listener.lock
interactor.lock
UI.clear if options[:clear]
begin
yield
rescue Interrupt
end
interactor.unlock
listener.unlock
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.
#
# @param [Guard::Guard] the guard to execute
# @param [Symbol] task_to_supervise the task to run
# @params [Array] args the arguments for the task
# @return [Boolean, Exception] the result of the Guard
#
def supervised_task(guard, task_to_supervise, *args)
2011-09-20 09:40:47 +00:00
guard.hook("#{ task_to_supervise }_begin", *args)
result = guard.send(task_to_supervise, *args)
2011-09-20 09:40:47 +00:00
guard.hook("#{ task_to_supervise }_end", result)
result
2011-09-20 09:40: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
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
# @param [Hash] the Guard options
#
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
#
def add_group(name)
@groups << name.to_sym unless name.nil?
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
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 09:40:47 +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|
::Guard::UI.debug 'Command execution: #{command}'
original_backtick command
end
end
2010-10-03 21:00:33 +00:00
end
end