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 Guards and the file changes they are watching.
You can optionally group the Guards with the group keyword and ignore certain paths
with the ignore_paths keyword.
A more advanced DSL use is the callback keyword that allows you to execute arbitrary
code before or after any of the start, stop, reload, run_all and run_on_change
Guards' method. You can even insert more hooks inside these methods.
Please checkout the Wiki page for more details.
The DSL will also evaluate normal Ruby code.
There are two possible locations for the Guardfile:
- The
Guardfilein the current directory where Guard has been started - The
.Guardfilein 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
Constant Summary collapse
- @@options =
nil
Class Method Summary collapse
-
.evaluate_guardfile(options = {}) ⇒ Object
Evaluate the DSL methods in the
Guardfile. -
.fetch_guardfile_contents ⇒ Object
Get the content to evaluate and stores it into the options as
:guardfile_contents. -
.guardfile_contents ⇒ String
Get the content of the
Guardfile. -
.guardfile_contents_usable? ⇒ Boolean
Tests if the current
Guardfilecontent is usable. -
.guardfile_contents_with_user_config ⇒ String
Get the content of the
Guardfileand the global user configuration file. -
.guardfile_default_path ⇒ String
Gets the default path of the
Guardfile. -
.guardfile_include?(guard_name) ⇒ Boolean
Test if the current
Guardfilecontains a specific Guard. -
.guardfile_path ⇒ String
Get the file path to the project
Guardfile. -
.instance_eval_guardfile(contents) ⇒ Object
Evaluate the content of the
Guardfile. -
.read_guardfile(guardfile_path) ⇒ Object
Read the current
Guardfilecontent. -
.reevaluate_guardfile ⇒ Object
Re-evaluate the
Guardfileto update the current Guard configuration.
Instance Method Summary collapse
-
#callback(*args) { ... } ⇒ Object
Define a callback to execute arbitrary code before or after any of the
start,stop,reload,run_allandrun_on_changeguards' method. -
#group(name, options = {}) { ... } ⇒ Object
Declares a group of guards to be run with
guard start --group group_name. -
#guard(name, options = {}) { ... } ⇒ Object
Declare a guard to be used when running
guard start. -
#ignore_paths(*paths) ⇒ Object
Ignore certain paths globally.
-
#watch(pattern) {|m| ... } ⇒ Object
Define a pattern to be watched in order to run actions on file modification.
Class Method Details
.evaluate_guardfile(options = {}) ⇒ Object
Evaluate the DSL methods in the Guardfile.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/guard/dsl.rb', line 89 def evaluate_guardfile( = {}) raise ArgumentError.new('No option hash passed to evaluate_guardfile!') unless .is_a?(Hash) = .dup fetch_guardfile_contents instance_eval_guardfile(guardfile_contents_with_user_config) UI.error 'No guards found in Guardfile, please add at least one.' if !::Guard.guards.nil? && ::Guard.guards.empty? end |
.fetch_guardfile_contents ⇒ Object
Get the content to evaluate and stores it into
the options as :guardfile_contents.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/guard/dsl.rb', line 147 def fetch_guardfile_contents if [:guardfile_contents] UI.info 'Using inline Guardfile.' [:guardfile_path] = 'Inline Guardfile' elsif [:guardfile] if File.exist?([:guardfile]) read_guardfile([: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 |
.guardfile_contents ⇒ String
Get the content of the Guardfile.
180 181 182 |
# File 'lib/guard/dsl.rb', line 180 def guardfile_contents ? [:guardfile_contents] : '' end |
.guardfile_contents_usable? ⇒ Boolean
Tests if the current Guardfile content is usable.
208 209 210 |
# File 'lib/guard/dsl.rb', line 208 def guardfile_contents_usable? guardfile_contents && guardfile_contents.size >= 'guard :a'.size # Smallest Guard definition end |
.guardfile_contents_with_user_config ⇒ String
Get the content of the Guardfile and the global
user configuration file.
191 192 193 194 |
# File 'lib/guard/dsl.rb', line 191 def guardfile_contents_with_user_config config = File.read(user_config_path) if File.exist?(user_config_path) [guardfile_contents, config].join("\n") end |
.guardfile_default_path ⇒ String
Gets the default path of the Guardfile. This returns the Guardfile
from the current directory when existing, or the global .Guardfile
at the home directory.
218 219 220 |
# File 'lib/guard/dsl.rb', line 218 def guardfile_default_path File.exist?(local_guardfile_path) ? local_guardfile_path : home_guardfile_path end |
.guardfile_include?(guard_name) ⇒ Boolean
Test if the current Guardfile contains a specific Guard.
128 129 130 |
# File 'lib/guard/dsl.rb', line 128 def guardfile_include?(guard_name) guardfile_contents.match(/^guard\s*\(?\s*['":]#{ guard_name }['"]?/) end |
.guardfile_path ⇒ String
Get the file path to the project Guardfile.
200 201 202 |
# File 'lib/guard/dsl.rb', line 200 def guardfile_path ? [:guardfile_path] : '' end |
.instance_eval_guardfile(contents) ⇒ Object
Evaluate the content of the Guardfile.
116 117 118 119 120 121 |
# File 'lib/guard/dsl.rb', line 116 def instance_eval_guardfile(contents) new.instance_eval(contents, [:guardfile_path], 1) rescue UI.error "Invalid Guardfile, original error is:\n#{ $! }" exit 1 end |
.read_guardfile(guardfile_path) ⇒ Object
Read the current Guardfile content.
136 137 138 139 140 141 142 |
# File 'lib/guard/dsl.rb', line 136 def read_guardfile(guardfile_path) [:guardfile_path] = guardfile_path [:guardfile_contents] = File.read(guardfile_path) rescue UI.error("Error reading file #{ guardfile_path }") exit 1 end |
.reevaluate_guardfile ⇒ Object
Re-evaluate the Guardfile to update the current Guard configuration.
102 103 104 105 106 107 108 109 110 |
# File 'lib/guard/dsl.rb', line 102 def reevaluate_guardfile ::Guard.guards.clear ::Guard.reset_groups .delete(:guardfile_contents) Dsl.evaluate_guardfile() msg = 'Guardfile has been re-evaluated.' UI.info(msg) Notifier.notify(msg) end |
Instance Method Details
#callback(*args) { ... } ⇒ Object
Define a callback to execute arbitrary code before or after any of
the start, stop, reload, run_all and run_on_change guards' method.
350 351 352 353 |
# File 'lib/guard/dsl.rb', line 350 def callback(*args, &listener) listener, events = args.size > 1 ? args : [listener, args[0]] @callbacks << { :events => events, :listener => listener } end |
#group(name, options = {}) { ... } ⇒ Object
Declares a group of guards to be run with guard start --group group_name.
275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/guard/dsl.rb', line 275 def group(name, = {}) @groups = [:group] || [] name = name.to_sym if block_given? && (@groups.empty? || @groups.map(&:to_sym).include?(name)) ::Guard.add_group(name.to_s.downcase, ) @current_group = name yield if block_given? @current_group = nil end end |
#guard(name, options = {}) { ... } ⇒ Object
Declare a guard 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.
310 311 312 313 314 315 316 317 318 |
# File 'lib/guard/dsl.rb', line 310 def guard(name, = {}) @watchers = [] @callbacks = [] yield if block_given? .update(:group => (@current_group || :default)) ::Guard.add_guard(name.to_s.downcase, @watchers, @callbacks, ) end |
#ignore_paths(*paths) ⇒ Object
Ignore certain paths globally.
364 365 366 367 |
# File 'lib/guard/dsl.rb', line 364 def ignore_paths(*paths) UI.info "Ignoring paths: #{ paths.join(', ') }" ::Guard.listener.ignore_paths.push(*paths) end |
#watch(pattern) {|m| ... } ⇒ Object
Define a pattern to be watched in order to run actions on file modification.
338 339 340 |
# File 'lib/guard/dsl.rb', line 338 def watch(pattern, &action) @watchers << ::Guard::Watcher.new(pattern, action) end |