class Guard::Dsl
end
end
watch(%r{^lib/(.+).rb}) { |m| “spec/lib/#{m}_spec.rb” }
watch(%r{^app/(.+).rb}) { |m| “spec/#{m}_spec.rb” }
watch(%r{^app/controllers/(.+)_(controller).rb}) { |m| [“spec/routing/#{m}_routing_spec.rb”, “spec/#{m}s/#{m}_#{m}_spec.rb”, “spec/acceptance/#{m}_spec.rb”] }
watch(%r{^spec/.+_spec.rb})
watch(%r{^spec/support/(controllers|acceptance)_helpers.rb}) { |m| “spec/#{m}” }
watch(‘config/routes.rb’) { “spec/routing” }
watch(‘app/controllers/application_controller.rb’) { “spec/controllers” }
watch(‘spec/spec_helper.rb’) { “spec” }
guard ‘rspec’, :version => 2, :cli => “–color –drb -f doc”, :bundler => false do
# use RSpec 2, from the system’s gem and with some direct RSpec CLI options
end
watch(‘spec/spec_helper.rb’)
watch(%r{^config/initializers/.+.rb})
watch(%r{^config/environments/.+.rb})
watch(‘config/environment.rb’)
watch(‘config/application.rb’)
watch(‘Gemfile’)
guard ‘spork’, :wait => 40 do
# for big project you can fine tune the “timeout” before Spork’s launch is considered failed
end
watch(‘Gemfile’)
guard ‘bundler’ do
# Reload the bundle when the Gemfile is modified
group ‘backend’ do
end
end
watch(%r{^config/locales/.+.yml})
watch(%r{^public/.+.html})
watch(%r{^public/stylesheets/.+.css})
watch(%r{^public/javascripts/.+.js})
watch(%r{^app/helpers/.+.rb})
watch(%r{^app/.+.(erb|haml)})
guard ‘livereload’, :apply_js_live => false do
end
watch(%r{^config/initializers/.+.rb})
watch(%r{^config/environments/.+.rb})
watch(‘config/environment.rb’)
watch(‘config/application.rb’)
guard ‘passenger’, :ping => true do
group ‘frontend’ do
notification :growl
@example A sample of a complex Guardfile
be appended to the current project ‘Guardfile`.
In addition, if a user configuration `.guard.rb` in your home directory is found, it will
- The `.Guardfile` in your home directory.
- The `Guardfile` in the current directory where Guard has been started
There are two possible locations for the `Guardfile`:
The DSL will also evaluate normal Ruby code.
Please [checkout the Wiki page](github.com/guard/guard/wiki/Hooks-and-callbacks) for more details.
Guards’ method. You can even insert more hooks inside these methods.
code before or after any of the ‘start`, `stop`, `reload`, `run_all` and `run_on_change`
A more advanced DSL use is the `callback` keyword that allows you to execute arbitrary
specify `:off` as library). @see ::Guard::Notifier for more information about the supported libraries.
Guard will automatically pick one with default options (if you don’t want notifications,
some optional configuration options for the library. If you don’t configure a library,
You can set your preferred system notification library with ‘notification` and pass
with the `ignore_paths` keyword.
You can optionally group the Guards with the `group` keyword and ignore certain paths
the used Guards and the file changes they are watching.
The main keywords of the DSL are `guard` and `watch`. These are necessary to define
the behaviour of Guard.
The DSL class provides the methods that are used in each `Guardfile` to describe
def callback(*args, &listener)
- See: Guard::Hook -
Other tags:
- Yield: - a block with listeners
Parameters:
-
args
(Array
) -- the callback arguments
def callback(*args, &listener) listener, events = args.size > 1 ? args : [listener, args[0]] @callbacks << { :events => events, :listener => listener } end
def evaluate_guardfile(options = {})
-
(ArgumentError)
- when options are not a Hash
Options Hash:
(**options)
-
guardfile_contents
(String
) -- a string representing the content of a valid Guardfile -
guardfile
(String
) -- the path to a valid Guardfile -
groups
(Array
) -- the groups to evaluate
def evaluate_guardfile(options = {}) raise ArgumentError.new('No option hash passed to evaluate_guardfile!') unless options.is_a?(Hash) @@options = options.dup fetch_guardfile_contents instance_eval_guardfile(guardfile_contents_with_user_config) end
def fetch_guardfile_contents
the options as `:guardfile_contents`.
Get the content to evaluate and stores it into
def fetch_guardfile_contents if @@options[:guardfile_contents] UI.info 'Using inline Guardfile.' @@options[:guardfile_path] = 'Inline Guardfile' elsif @@options[:guardfile] if File.exist?(@@options[:guardfile]) read_guardfile(@@options[:guardfile]) UI.info "Using Guardfile at #{ @@options[:guardfile] }." else UI.error "No Guardfile exists at #{ @@options[:guardfile] }." exit 1 end else if File.exist?(guardfile_default_path) read_guardfile(guardfile_default_path) else UI.error 'No Guardfile found, please create one with `guard init`.' exit 1 end end unless guardfile_contents_usable? UI.error "The command file(#{ @@options[:guardfile] }) seems to be empty." exit 1 end end
def group(name, options = {})
- See: Guard::DslDescriber -
See: Dsl#guard -
See: Guard.add_group -
Other tags:
- Yield: - a block where you can declare several guards
Parameters:
-
options
(Hash
) -- the options accepted by the group -
name
(Symbol, String
) -- the group's name called from the CLI
Other tags:
- Example: Declare two groups of Guards -
def group(name, options = {}) @groups = @@options[:group] || [] name = name.to_sym if block_given? && (@groups.empty? || @groups.map(&:to_sym).include?(name)) ::Guard.add_group(name.to_s.downcase, options) @current_group = name yield if block_given? @current_group = nil end end
def guard(name, options = {})
- See: Guard::DslDescriber -
See: Dsl#watch -
See: Dsl#group -
See: Guard.add_guard -
Other tags:
- Yield: - a block where you can declare several watch patterns and actions
Parameters:
-
options
(Hash
) -- the options accepted by the Guard -
name
(String
) -- the Guard name
Other tags:
- Example: Declare a Guard -
def guard(name, options = {}) @watchers = [] @callbacks = [] @current_group ||= :default yield if block_given? options.update(:group => @current_group) ::Guard.add_guard(name.to_s.downcase, @watchers, @callbacks, options) end
def guardfile_contents
-
(String)
- the Guardfile content
def guardfile_contents @@options ? @@options[:guardfile_contents] : '' end
def guardfile_contents_usable?
-
(Boolean)
- if the Guardfile is usable
def guardfile_contents_usable? guardfile_contents && guardfile_contents.size >= 'guard :a'.size # Smallest Guard definition end
def guardfile_contents_with_user_config
-
(String)
- the Guardfile content
Other tags:
- See: #user_config_path -
def guardfile_contents_with_user_config config = File.read(user_config_path) if File.exist?(user_config_path) [guardfile_contents, config].join("\n") end
def guardfile_default_path
-
(String)
- the path to the Guardfile
def guardfile_default_path File.exist?(local_guardfile_path) ? local_guardfile_path : home_guardfile_path end
def guardfile_include?(guard_name)
-
(Boolean)
- whether the Guard has been declared
Parameters:
-
guard_name
(String
) -- the name of the Guard
def guardfile_include?(guard_name) guardfile_contents.match(/^guard\s*\(?\s*['":]#{ guard_name }['"]?/) end
def guardfile_path
-
(String)
- the path to the Guardfile
def guardfile_path @@options ? @@options[:guardfile_path] : '' end
def home_guardfile_path
-
(String)
- the path to ~/.Guardfile
def home_guardfile_path File.expand_path(File.join('~', '.Guardfile')) end
def ignore_paths(*paths)
- See: Guard::Listener -
Parameters:
-
paths
(Array
) -- the list of paths to ignore
Other tags:
- Example: Ignore some paths -
def ignore_paths(*paths) UI.info "Ignoring paths: #{ paths.join(', ') }" ::Guard.listener.ignore_paths.push(*paths) end
def instance_eval_guardfile(contents)
-
contents
(String
) -- the content to evaluate.
def instance_eval_guardfile(contents) new.instance_eval(contents, @@options[:guardfile_path], 1) rescue UI.error "Invalid Guardfile, original error is:\n#{ $! }" exit 1 end
def interactor(interactor)
- Example: Turn off interactions -
Example: Use the gets interactor -
Example: Use the readline interactor -
def interactor(interactor) ::Guard::Interactor.interactor = interactor.to_sym end
def local_guardfile_path
-
(String)
- the path to the local Guardfile
def local_guardfile_path File.join(Dir.pwd, 'Guardfile') end
def notification(notifier, options = {})
-
options
(Hash
) -- the notification library options -
notifier
(Symbol, String
) -- the name of the notifier to use
Other tags:
- See: Guard::Notifier - for available notifier and its options.
Other tags:
- Example: Define multiple notifications -
def notification(notifier, options = {}) ::Guard::Notifier.add_notification(notifier.to_sym, options, false) end
def read_guardfile(guardfile_path)
-
guardfile_path
(String
) -- the path to the Guardfile
def read_guardfile(guardfile_path) @@options[:guardfile_path] = guardfile_path @@options[:guardfile_contents] = File.read(guardfile_path) rescue UI.error("Error reading file #{ guardfile_path }") exit 1 end
def reevaluate_guardfile
Re-evaluate the `Guardfile` to update the current Guard configuration.
def reevaluate_guardfile ::Guard.run do # Stop each old guards ::Guard.run_on_guards do |guard| ::Guard.run_supervised_task(guard, :stop) end ::Guard.guards.clear ::Guard.reset_groups ::Guard::Notifier.clear_notifications @@options.delete(:guardfile_contents) Dsl.evaluate_guardfile(@@options) UI.error 'No guards found in Guardfile, please add at least one.' if ::Guard.guards.empty? msg = 'Guardfile has been re-evaluated.' UI.info(msg) Notifier.notify(msg) # Start each new guards ::Guard.run_on_guards do |guard| ::Guard.run_supervised_task(guard, :start) end end end
def user_config_path
-
(String)
- the path to ~/.guard.rb
def user_config_path File.expand_path(File.join('~', '.guard.rb')) end
def watch(pattern, &action)
- See: Dsl#guard -
See: Guard::Watcher -
Other tags:
- Yieldreturn: - a directory, a filename, an array of directories / filenames, or nothing (can be an arbitrary command)
Other tags:
- Yieldparam: m - matches of the pattern
Other tags:
- Yield: - a block to be run when the pattern is matched
Parameters:
-
pattern
(String, Regexp
) -- the pattern to be watched by the guard
Other tags:
- Example: Declare watchers for a Guard -
def watch(pattern, &action) @watchers << ::Guard::Watcher.new(pattern, action) end