Class: Guard::Dsl
- Inherits:
-
Object
- Object
- Guard::Dsl
- Defined in:
- lib/guard/dsl.rb
Overview
The Dsl class provides the methods that are used in each ‘Guardfile` to describe the behaviour of Guard.
The main keywords of the DSL are #guard and #watch. These are necessary to define the used Guard plugins and the file changes they are watching.
You can optionally group the Guard plugins with the #group keyword and ignore and filter certain paths with the #ignore and #filter keywords.
You can set your preferred system notification library with #notification and pass some optional configuration options for the library. If you don’t configure a library, Guard will automatically pick one with default options (if you don’t want notifications, specify ‘:off` as library). Please see Notifier for more information about the supported libraries.
A more advanced DSL use is the #callback keyword that allows you to execute arbitrary code before or after any of the Plugin#start, Plugin#stop, Plugin#reload, Plugin#run_all, Plugin#run_on_changes, Plugin#run_on_additions, Plugin#run_on_modifications and Plugin#run_on_removals Guard plugins method. You can even insert more hooks inside these methods. Please [checkout the Wiki page](github.com/guard/guard/wiki/Hooks-and-callbacks) for more details.
The DSL will also evaluate normal Ruby code.
There are two possible locations for the ‘Guardfile`:
-
The ‘Guardfile` or `guardfile.rb` in the current directory where Guard has been started
-
The ‘.Guardfile` in your home directory.
In addition, if a user configuration ‘.guard.rb` in your home directory is found, it will be appended to the current project `Guardfile`.
Direct Known Subclasses
Defined Under Namespace
Classes: Error
Constant Summary collapse
- WARN_INVALID_LOG_LEVEL =
"Invalid log level `%s` ignored. "\ "Please use either :debug, :info, :warn or :error."
- WARN_INVALID_LOG_OPTIONS =
"You cannot specify the logger options"\ " :only and :except at the same time."
Instance Method Summary collapse
-
#callback(*args) { ... } ⇒ Object
Defines a callback to execute arbitrary code before or after any of the ‘start`, `stop`, `reload`, `run_all`, `run_on_changes`, `run_on_additions`, `run_on_modifications` and `run_on_removals` plugin method.
-
#clearing(on) ⇒ Object
Sets Guard to clear the screen before every task is run.
-
#directories(directories) ⇒ Object
Sets the directories to pass to Listen.
-
#evaluate(contents, filename, lineno) ⇒ Object
:nodoc.
-
#group(*args) { ... } ⇒ Object
Declares a group of Guard plugins to be run with ‘guard start –group group_name`.
-
#guard(name, options = {}) { ... } ⇒ Object
Declares a Guard plugin to be used when running ‘guard start`.
-
#ignore(*regexps) ⇒ Object
(also: #filter)
Ignores certain paths globally.
-
#ignore!(*regexps) ⇒ Object
(also: #filter!)
Replaces ignored paths globally.
-
#interactor(options) ⇒ Object
Sets the interactor options or disable the interactor.
-
#logger(options) ⇒ Object
Configures the Guard logger.
-
#notification(notifier, opts = {}) ⇒ Object
Set notification options for the system notifications.
-
#scope(scope = {}) ⇒ Object
Sets the default scope on startup.
-
#watch(pattern) {|m| ... } ⇒ Object
Defines a pattern to be watched in order to run actions on file modification.
Instance Method Details
#callback(*args) { ... } ⇒ Object
Defines a callback to execute arbitrary code before or after any of the ‘start`, `stop`, `reload`, `run_all`, `run_on_changes`, `run_on_additions`, `run_on_modifications` and `run_on_removals` plugin method.
246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/guard/dsl.rb', line 246 def callback(*args, &block) @plugin_options ||= nil fail "callback must be called within a guard block" unless @plugin_options block, events = if args.size > 1 # block must be the first argument in that case, the # yielded block is ignored args else [block, args[0]] end @plugin_options[:callbacks] << { events: events, listener: block } end |
#clearing(on) ⇒ Object
Sets Guard to clear the screen before every task is run
408 409 410 |
# File 'lib/guard/dsl.rb', line 408 def clearing(on) Guard.state.session.clearing(on == :on) end |
#directories(directories) ⇒ Object
Sets the directories to pass to Listen
394 395 396 397 398 399 |
# File 'lib/guard/dsl.rb', line 394 def directories(directories) directories.each do |dir| fail "Directory #{dir.inspect} does not exist!" unless Dir.exist?(dir) end Guard.state.session.watchdirs = directories end |
#evaluate(contents, filename, lineno) ⇒ Object
:nodoc
377 378 379 380 381 382 383 384 385 |
# File 'lib/guard/dsl.rb', line 377 def evaluate(contents, filename, lineno) # :nodoc instance_eval(contents, filename.to_s, lineno) rescue StandardError, ScriptError => e prefix = "\n\t(dsl)> " cleaned_backtrace = _cleanup_backtrace(e.backtrace) backtrace = "#{prefix}#{cleaned_backtrace.join(prefix)}" msg = "Invalid Guardfile, original error is: \n\n%s, \nbacktrace: %s" raise Error, format(msg, e, backtrace) end |
#group(*args) { ... } ⇒ Object
Declares a group of Guard plugins to be run with ‘guard start –group
group_name`.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/guard/dsl.rb', line 124 def group(*args) = args.last.is_a?(Hash) ? args.pop : {} groups = args groups.each do |group| next unless group.to_sym == :all fail ArgumentError, "'all' is not an allowed group name!" end if block_given? groups.each do |group| # TODO: let groups be added *after* evaluation Guard.state.session.groups.add(group, ) end @current_groups ||= [] @current_groups.push(groups) yield @current_groups.pop else UI.error \ "No Guard plugins found in the group '#{ groups.join(', ') }',"\ " please add at least one." end end |
#guard(name, options = {}) { ... } ⇒ Object
Declares a Guard plugin to be used when running ‘guard start`.
The name parameter is usually the name of the gem without the ‘guard-’ prefix.
The available options are different for each Guard implementation.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/guard/dsl.rb', line 176 def guard(name, = {}) @plugin_options = .merge(watchers: [], callbacks: []) yield if block_given? @current_groups ||= [] groups = @current_groups && @current_groups.last || [:default] groups.each do |group| opts = @plugin_options.merge(group: group) # TODO: let plugins be added *after* evaluation Guard.state.session.plugins.add(name, opts) end @plugin_options = nil end |
#ignore(*regexps) ⇒ Object Also known as: filter
Ignores certain paths globally.
267 268 269 270 |
# File 'lib/guard/dsl.rb', line 267 def ignore(*regexps) # TODO: use guardfile results class Guard.state.session.guardfile_ignore = regexps end |
#ignore!(*regexps) ⇒ Object Also known as: filter!
Replaces ignored paths globally
282 283 284 285 286 287 |
# File 'lib/guard/dsl.rb', line 282 def ignore!(*regexps) @ignore_regexps ||= [] @ignore_regexps << regexps # TODO: use guardfile results class Guard.state.session.guardfile_ignore_bang = @ignore_regexps end |
#interactor(options) ⇒ Object
Sets the interactor options or disable the interactor.
91 92 93 94 95 96 97 98 99 |
# File 'lib/guard/dsl.rb', line 91 def interactor() # TODO: remove dependency on Interactor (let session handle this) case when :off Interactor.enabled = false when Hash Interactor. = end end |
#logger(options) ⇒ Object
Configures the Guard logger.
-
Log level must be either ‘:debug`, `:info`, `:warn` or `:error`.
-
Template supports the following placeholders: ‘:time`, `:severity`, `:progname`, `:pid`, `:unit_of_work_id` and `:message`.
-
Time format directives are the same as ‘Time#strftime` or `:milliseconds`.
-
The ‘:only` and `:except` options must be a `RegExp`.
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/guard/dsl.rb', line 325 def logger() if [:level] [:level] = [:level].to_sym unless [:debug, :info, :warn, :error].include? [:level] UI.warning(format(WARN_INVALID_LOG_LEVEL, [:level])) .delete :level end end if [:only] && [:except] UI.warning WARN_INVALID_LOG_OPTIONS .delete :only .delete :except end # Convert the :only and :except options to a regular expression [:only, :except].each do |name| next unless [name] list = [].push([name]).flatten.map do |plugin| Regexp.escape(plugin.to_s) end [name] = Regexp.new(list.join("|"), Regexp::IGNORECASE) end UI. = UI..merge() end |
#notification(notifier, opts = {}) ⇒ Object
Set notification options for the system notifications. You can set multiple notifications, which allows you to show local system notifications and remote notifications with separate libraries. You can also pass ‘:off` as library to turn off notifications.
76 77 78 |
# File 'lib/guard/dsl.rb', line 76 def notification(notifier, opts = {}) Guard.state.session.guardfile_notification = { notifier.to_sym => opts } end |
#scope(scope = {}) ⇒ Object
Sets the default scope on startup
372 373 374 375 |
# File 'lib/guard/dsl.rb', line 372 def scope(scope = {}) # TODO: use a Guardfile::Results class Guard.state.session.guardfile_scope(scope) end |
#watch(pattern) {|m| ... } ⇒ Object
Defines a pattern to be watched in order to run actions on file modification.
modification
218 219 220 221 222 223 224 225 |
# File 'lib/guard/dsl.rb', line 218 def watch(pattern, &action) # Allow watches in the global scope (to execute arbitrary commands) by # building a generic Guard::Plugin. @plugin_options ||= nil return guard(:plugin) { watch(pattern, &action) } unless @plugin_options @plugin_options[:watchers] << Watcher.new(pattern, action) end |