master/lib/guard/guard.rb

89 lines
2.5 KiB
Ruby
Raw Normal View History

2010-10-03 21:00:33 +00:00
module Guard
# 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
include Hook
attr_accessor :watchers, :options, :group
# initialize a Guard.
#
# @param [Array<Guard::Watcher>] watchers the Guard file watchers
# @param [Hash] options the custom Guard options.
#
def initialize(watchers = [], options = {})
@group = options.delete(:group) || :default
2010-10-03 21:00:33 +00:00
@watchers, @options = watchers, options
end
# 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)
if ::Guard::Dsl.guardfile_include?(name)
::Guard::UI.info "Guardfile already includes #{ name } guard"
2010-10-07 20:37:30 +00:00
else
content = File.read('Guardfile')
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|
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
# Call once when guard starts. Please override initialize method to init stuff.
#
# @return [Boolean] Whether the start action was successful or not
#
2010-10-03 21:00:33 +00:00
def start
true
end
# Call once when guard quit.
#
# @return [Boolean] Whether the stop action was successful or not
#
2010-10-03 21:00:33 +00:00
def stop
true
end
# 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
# Should be used for long action like running all specs/tests/...
#
# @return [Boolean] Whether the run_all action was successful or not
#
2010-10-03 21:00:33 +00:00
def run_all
true
end
# 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
2010-10-03 21:00:33 +00:00
end
end