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.
|
|
|
|
#
|
|
|
|
# Guard will trigger the `start`, `stop`, `reload`, `run_all` and `run_on_change`
|
|
|
|
# methods depending on user interaction and file modification.
|
|
|
|
#
|
|
|
|
# Each Guard should provide a template Guardfile located within the Gem
|
|
|
|
# at `lib/guard/guard-name/templates/Guardfile`.
|
|
|
|
#
|
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
|
|
|
|
# @param [Hash] options the custom Guard options.
|
|
|
|
#
|
2011-08-17 08:04:42 +00:00
|
|
|
def initialize(watchers = [], options = {})
|
|
|
|
@group = options.delete(:group) || :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
|
|
|
#
|
|
|
|
# @return [Boolean] Whether the start action was successful or not
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def start
|
|
|
|
true
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Call once when Guard quit.
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
|
|
|
# @return [Boolean] Whether the stop action was successful or not
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def stop
|
|
|
|
true
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 10:07:34 +00:00
|
|
|
# Should be used for "reload" (really!) actions like reloading passenger/spork/bundler/...
|
|
|
|
#
|
|
|
|
# @return [Boolean] Whether the reload action was successful or not
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def reload
|
|
|
|
true
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Should be used for long action like running all specs/tests/...
|
2011-09-20 10:07:34 +00:00
|
|
|
#
|
|
|
|
# @return [Boolean] Whether the run_all action was successful or not
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def run_all
|
|
|
|
true
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 10:07:34 +00:00
|
|
|
# Will be triggered when a file change matched a watcher.
|
|
|
|
#
|
|
|
|
# @param [Array<String>] paths the changes files or paths
|
|
|
|
# @return [Boolean] Whether the run_all action was successful or not
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def run_on_change(paths)
|
|
|
|
true
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-04-29 22:40:12 +00:00
|
|
|
end
|