2010-10-03 21:00:33 +00:00
|
|
|
module Guard
|
2011-09-20 10:07:34 +00:00
|
|
|
|
|
|
|
# Main class that every Guard implementation must subclass.
|
|
|
|
#
|
2011-09-30 09:30:05 +00:00
|
|
|
# Guard will trigger the `start`, `stop`, `reload`, `run_all`, `run_on_change` and
|
|
|
|
# `run_on_deletion` methods depending on user interaction and file modification.
|
|
|
|
#
|
|
|
|
# In each of these Guard methods you have to implement some work when you want to
|
|
|
|
# support this kind of task. The return value of each Guard method is ignored, but
|
|
|
|
# you can raise a `:task_has_failed` exception to indicate that your Guard method was
|
|
|
|
# not successful. When a `:task_has_failed` exception is raised, successive guard tasks
|
|
|
|
# can be aborted when the group has set the `:halt_on_fail` option.
|
|
|
|
#
|
|
|
|
# @see Guard::Group
|
|
|
|
#
|
|
|
|
# @example Raise a :task_has_failed exception
|
|
|
|
#
|
|
|
|
# def run_all
|
|
|
|
# if !runner.run(['all'])
|
|
|
|
# raise :task_has_failed
|
|
|
|
# end
|
|
|
|
# end
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
|
|
|
# Each Guard should provide a template Guardfile located within the Gem
|
|
|
|
# at `lib/guard/guard-name/templates/Guardfile`.
|
|
|
|
#
|
2011-09-30 09:30:05 +00:00
|
|
|
# If one of those methods raise an exception other than `:task_has_failed`,
|
|
|
|
# the Guard::GuardName instance will be removed from the active guards.
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
class Guard
|
2011-04-14 20:53:53 +00:00
|
|
|
include Hook
|
2011-04-16 21:13:29 +00:00
|
|
|
|
2011-08-16 22:36:02 +00:00
|
|
|
attr_accessor :watchers, :options, :group
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Initialize a Guard.
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
|
|
|
# @param [Array<Guard::Watcher>] watchers the Guard file watchers
|
2011-09-23 09:01:52 +00:00
|
|
|
# @param [Hash] options the custom Guard options
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
2011-08-17 08:04:42 +00:00
|
|
|
def initialize(watchers = [], options = {})
|
2011-09-22 22:22:25 +00:00
|
|
|
@group = options[:group] ? options.delete(:group).to_sym : :default
|
2010-10-03 21:00:33 +00:00
|
|
|
@watchers, @options = watchers, options
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 10:07:34 +00:00
|
|
|
# Initialize the Guard. This will copy the Guardfile template inside the Guard gem.
|
|
|
|
# The template Guardfile must be located within the Gem at `lib/guard/guard-name/templates/Guardfile`.
|
|
|
|
#
|
|
|
|
# @param [String] name the name of the Guard
|
|
|
|
#
|
2010-10-07 20:37:30 +00:00
|
|
|
def self.init(name)
|
2010-12-17 17:37:24 +00:00
|
|
|
if ::Guard::Dsl.guardfile_include?(name)
|
2011-09-20 10:07:34 +00:00
|
|
|
::Guard::UI.info "Guardfile already includes #{ name } guard"
|
2010-10-07 20:37:30 +00:00
|
|
|
else
|
|
|
|
content = File.read('Guardfile')
|
2011-09-20 10:07:34 +00:00
|
|
|
guard = File.read("#{ ::Guard.locate_guard(name) }/lib/guard/#{ name }/templates/Guardfile")
|
2010-10-07 20:37:30 +00:00
|
|
|
File.open('Guardfile', 'wb') do |f|
|
2010-11-25 23:58:36 +00:00
|
|
|
f.puts(content)
|
|
|
|
f.puts("")
|
|
|
|
f.puts(guard)
|
2010-10-07 20:37:30 +00:00
|
|
|
end
|
|
|
|
::Guard::UI.info "#{name} guard added to Guardfile, feel free to edit it"
|
|
|
|
end
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Call once when Guard starts. Please override initialize method to init stuff.
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
2011-09-30 09:30:05 +00:00
|
|
|
# @raise [:task_has_failed] when start has failed
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def start
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-30 09:30:05 +00:00
|
|
|
# Called when `stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
2011-09-30 09:30:05 +00:00
|
|
|
# @raise [:task_has_failed] when stop has failed
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def stop
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-30 09:30:05 +00:00
|
|
|
# Called when `reload|r|z + enter` is pressed.
|
|
|
|
# This method should be mainly used for "reload" (really!) actions like reloading passenger/spork/bundler/...
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
2011-09-30 09:30:05 +00:00
|
|
|
# @raise [:task_has_failed] when reload has failed
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def reload
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-30 09:30:05 +00:00
|
|
|
# Called when just `enter` is pressed
|
|
|
|
# This method should be principally used for long action like running all specs/tests/...
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
2011-09-30 09:30:05 +00:00
|
|
|
# @raise [:task_has_failed] when run_all has failed
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def run_all
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-30 09:30:05 +00:00
|
|
|
# Called on file(s) modifications that the Guard watches.
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
|
|
|
# @param [Array<String>] paths the changes files or paths
|
2011-09-30 09:30:05 +00:00
|
|
|
# @raise [:task_has_failed] when run_on_change has failed
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def run_on_change(paths)
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-30 09:30:05 +00:00
|
|
|
# Called on file(s) deletions that the Guard watches.
|
2011-09-27 18:29:52 +00:00
|
|
|
#
|
|
|
|
# @param [Array<String>] paths the deleted files or paths
|
2011-09-30 09:30:05 +00:00
|
|
|
# @raise [:task_has_failed] when run_on_change has failed
|
2011-09-27 18:29:52 +00:00
|
|
|
#
|
2011-08-23 16:07:23 +00:00
|
|
|
def run_on_deletion(paths)
|
|
|
|
end
|
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-09-22 22:22:25 +00:00
|
|
|
|
2011-08-19 20:53:48 +00:00
|
|
|
end
|