guard 0.8.4 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +348 -330
- data/LICENSE +19 -19
- data/README.md +464 -431
- data/bin/guard +6 -6
- data/lib/guard.rb +452 -414
- data/lib/guard/cli.rb +119 -178
- data/lib/guard/dsl.rb +370 -370
- data/lib/guard/dsl_describer.rb +150 -60
- data/lib/guard/group.rb +37 -37
- data/lib/guard/guard.rb +129 -113
- data/lib/guard/hook.rb +118 -118
- data/lib/guard/interactor.rb +110 -44
- data/lib/guard/listener.rb +350 -350
- data/lib/guard/listeners/darwin.rb +66 -66
- data/lib/guard/listeners/linux.rb +97 -97
- data/lib/guard/listeners/polling.rb +55 -55
- data/lib/guard/listeners/windows.rb +61 -61
- data/lib/guard/notifier.rb +290 -211
- data/lib/guard/templates/Guardfile +2 -2
- data/lib/guard/ui.rb +193 -188
- data/lib/guard/version.rb +6 -6
- data/lib/guard/watcher.rb +114 -110
- data/man/guard.1 +93 -93
- data/man/guard.1.html +176 -176
- metadata +107 -59
data/bin/guard
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'guard'
|
4
|
-
require 'guard/cli'
|
5
|
-
|
6
|
-
Guard::CLI.start
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'guard'
|
4
|
+
require 'guard/cli'
|
5
|
+
|
6
|
+
Guard::CLI.start
|
data/lib/guard.rb
CHANGED
@@ -1,414 +1,452 @@
|
|
1
|
-
require 'thread'
|
2
|
-
|
3
|
-
# Guard is the main module for all Guard related modules and classes.
|
4
|
-
# Also other Guard implementation should use this namespace.
|
5
|
-
#
|
6
|
-
module Guard
|
7
|
-
|
8
|
-
autoload :UI, 'guard/ui'
|
9
|
-
autoload :Dsl, 'guard/dsl'
|
10
|
-
autoload :DslDescriber, 'guard/dsl_describer'
|
11
|
-
autoload :Group, 'guard/group'
|
12
|
-
autoload :Interactor, 'guard/interactor'
|
13
|
-
autoload :Listener, 'guard/listener'
|
14
|
-
autoload :Watcher, 'guard/watcher'
|
15
|
-
autoload :Notifier, 'guard/notifier'
|
16
|
-
autoload :Hook, 'guard/hook'
|
17
|
-
|
18
|
-
class << self
|
19
|
-
attr_accessor :options, :interactor, :listener
|
20
|
-
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
# @
|
25
|
-
#
|
26
|
-
# @
|
27
|
-
#
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
#
|
50
|
-
#
|
51
|
-
# @
|
52
|
-
#
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
#
|
101
|
-
#
|
102
|
-
# @
|
103
|
-
# @
|
104
|
-
# @
|
105
|
-
#
|
106
|
-
#
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
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
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
#
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
#
|
175
|
-
#
|
176
|
-
# @
|
177
|
-
#
|
178
|
-
def
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
#
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
#
|
252
|
-
#
|
253
|
-
#
|
254
|
-
# @
|
255
|
-
#
|
256
|
-
# @
|
257
|
-
#
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
#
|
293
|
-
#
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
#
|
299
|
-
#
|
300
|
-
#
|
301
|
-
#
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
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
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
#
|
370
|
-
#
|
371
|
-
# @
|
372
|
-
#
|
373
|
-
|
374
|
-
|
375
|
-
if
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
#
|
385
|
-
#
|
386
|
-
#
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
end
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
1
|
+
require 'thread'
|
2
|
+
|
3
|
+
# Guard is the main module for all Guard related modules and classes.
|
4
|
+
# Also other Guard implementation should use this namespace.
|
5
|
+
#
|
6
|
+
module Guard
|
7
|
+
|
8
|
+
autoload :UI, 'guard/ui'
|
9
|
+
autoload :Dsl, 'guard/dsl'
|
10
|
+
autoload :DslDescriber, 'guard/dsl_describer'
|
11
|
+
autoload :Group, 'guard/group'
|
12
|
+
autoload :Interactor, 'guard/interactor'
|
13
|
+
autoload :Listener, 'guard/listener'
|
14
|
+
autoload :Watcher, 'guard/watcher'
|
15
|
+
autoload :Notifier, 'guard/notifier'
|
16
|
+
autoload :Hook, 'guard/hook'
|
17
|
+
|
18
|
+
class << self
|
19
|
+
attr_accessor :options, :interactor, :listener, :lock
|
20
|
+
|
21
|
+
# Creates the initial Guardfile template or add a Guard implementation
|
22
|
+
# Guardfile template to an existing Guardfile.
|
23
|
+
#
|
24
|
+
# @see Guard::Guard.init
|
25
|
+
#
|
26
|
+
# @param [String] guard_name the name of the Guard to initialize
|
27
|
+
#
|
28
|
+
def initialize_template(guard_name = nil)
|
29
|
+
if guard_name
|
30
|
+
guard_class = ::Guard.get_guard_class(guard_name)
|
31
|
+
guard_class.init(guard_name)
|
32
|
+
else
|
33
|
+
if !File.exist?('Guardfile')
|
34
|
+
::Guard::UI.info "Writing new Guardfile to #{ Dir.pwd }/Guardfile"
|
35
|
+
FileUtils.cp(File.expand_path('../templates/Guardfile', __FILE__), 'Guardfile')
|
36
|
+
else
|
37
|
+
::Guard::UI.error "Guardfile already exists at #{ Dir.pwd }/Guardfile"
|
38
|
+
exit 1
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Initialize the Guard singleton.
|
44
|
+
#
|
45
|
+
# @option options [Boolean] clear if auto clear the UI should be done
|
46
|
+
# @option options [Boolean] notify if system notifications should be shown
|
47
|
+
# @option options [Boolean] debug if debug output should be shown
|
48
|
+
# @option options [Array<String>] group the list of groups to start
|
49
|
+
# @option options [String] watchdir the director to watch
|
50
|
+
# @option options [String] guardfile the path to the Guardfile
|
51
|
+
# @option options [Boolean] watch_all_modifications watches all file modifications if true
|
52
|
+
#
|
53
|
+
def setup(options = {})
|
54
|
+
@lock = Mutex.new
|
55
|
+
|
56
|
+
@options = options
|
57
|
+
@guards = []
|
58
|
+
@groups = [Group.new(:default)]
|
59
|
+
@interactor = Interactor.new unless @options[:no_interactions]
|
60
|
+
@listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd, options)
|
61
|
+
|
62
|
+
@options[:notify] && ENV['GUARD_NOTIFY'] != 'false' ? Notifier.turn_on : Notifier.turn_off
|
63
|
+
|
64
|
+
UI.clear if @options[:clear]
|
65
|
+
|
66
|
+
debug_command_execution if @options[:debug]
|
67
|
+
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
# Smart accessor for retrieving a specific guard or several guards at once.
|
72
|
+
#
|
73
|
+
# @param [String, Symbol] filter return the guard with the given name, or nil if not found
|
74
|
+
# @param [Regexp] filter returns all guards matching the Regexp, or [] if no guard found
|
75
|
+
# @param [Hash] filter returns all guards matching the given Hash.
|
76
|
+
# Example: `{ :name => 'rspec', :group => 'backend' }`, or [] if no guard found
|
77
|
+
# @param [NilClass] filter returns all guards
|
78
|
+
#
|
79
|
+
# @see Guard.groups
|
80
|
+
#
|
81
|
+
def guards(filter = nil)
|
82
|
+
case filter
|
83
|
+
when String, Symbol
|
84
|
+
@guards.find { |guard| guard.class.to_s.downcase.sub('guard::', '') == filter.to_s.downcase.gsub('-', '') }
|
85
|
+
when Regexp
|
86
|
+
@guards.find_all { |guard| guard.class.to_s.downcase.sub('guard::', '') =~ filter }
|
87
|
+
when Hash
|
88
|
+
filter.inject(@guards) do |matches, (k, v)|
|
89
|
+
if k.to_sym == :name
|
90
|
+
matches.find_all { |guard| guard.class.to_s.downcase.sub('guard::', '') == v.to_s.downcase.gsub('-', '') }
|
91
|
+
else
|
92
|
+
matches.find_all { |guard| guard.send(k).to_sym == v.to_sym }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
else
|
96
|
+
@guards
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Smart accessor for retrieving a specific group or several groups at once.
|
101
|
+
#
|
102
|
+
# @param [NilClass] filter returns all groups
|
103
|
+
# @param [String, Symbol] filter return the group with the given name, or nil if not found
|
104
|
+
# @param [Regexp] filter returns all groups matching the Regexp, or [] if no group found
|
105
|
+
#
|
106
|
+
# @see Guard.guards
|
107
|
+
#
|
108
|
+
def groups(filter = nil)
|
109
|
+
case filter
|
110
|
+
when String, Symbol
|
111
|
+
@groups.find { |group| group.name == filter.to_sym }
|
112
|
+
when Regexp
|
113
|
+
@groups.find_all { |group| group.name.to_s =~ filter }
|
114
|
+
else
|
115
|
+
@groups
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Start Guard by evaluate the `Guardfile`, initialize the declared Guards
|
120
|
+
# and start the available file change listener.
|
121
|
+
#
|
122
|
+
# @option options [Boolean] clear if auto clear the UI should be done
|
123
|
+
# @option options [Boolean] notify if system notifications should be shown
|
124
|
+
# @option options [Boolean] debug if debug output should be shown
|
125
|
+
# @option options [Array<String>] group the list of groups to start
|
126
|
+
# @option options [String] watchdir the director to watch
|
127
|
+
# @option options [String] guardfile the path to the Guardfile
|
128
|
+
#
|
129
|
+
def start(options = {})
|
130
|
+
setup(options)
|
131
|
+
|
132
|
+
Dsl.evaluate_guardfile(options)
|
133
|
+
|
134
|
+
listener.on_change do |files|
|
135
|
+
Dsl.reevaluate_guardfile if Watcher.match_guardfile?(files)
|
136
|
+
listener.changed_files += files if Watcher.match_files?(guards, files)
|
137
|
+
end
|
138
|
+
|
139
|
+
UI.info "Guard is now watching at '#{ listener.directory }'"
|
140
|
+
|
141
|
+
run_on_guards do |guard|
|
142
|
+
run_supervised_task(guard, :start)
|
143
|
+
end
|
144
|
+
|
145
|
+
interactor.start if interactor
|
146
|
+
listener.start
|
147
|
+
end
|
148
|
+
|
149
|
+
# Stop Guard listening to file changes
|
150
|
+
#
|
151
|
+
def stop
|
152
|
+
UI.info 'Bye bye...', :reset => true
|
153
|
+
|
154
|
+
run_on_guards do |guard|
|
155
|
+
run_supervised_task(guard, :stop)
|
156
|
+
end
|
157
|
+
|
158
|
+
listener.stop
|
159
|
+
abort
|
160
|
+
end
|
161
|
+
|
162
|
+
# Reload all Guards currently enabled.
|
163
|
+
#
|
164
|
+
# @param [Hash] An hash with a guard or a group scope
|
165
|
+
#
|
166
|
+
def reload(scopes)
|
167
|
+
run do
|
168
|
+
run_on_guards(scopes) do |guard|
|
169
|
+
run_supervised_task(guard, :reload)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# Trigger `run_all` on all Guards currently enabled.
|
175
|
+
#
|
176
|
+
# @param [Hash] An hash with a guard or a group scope
|
177
|
+
#
|
178
|
+
def run_all(scopes)
|
179
|
+
run do
|
180
|
+
run_on_guards(scopes) do |guard|
|
181
|
+
run_supervised_task(guard, :run_all)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
# Pause Guard listening to file changes.
|
187
|
+
#
|
188
|
+
def pause
|
189
|
+
if listener.paused?
|
190
|
+
UI.info 'Un-paused files modification listening', :reset => true
|
191
|
+
listener.clear_changed_files
|
192
|
+
listener.run
|
193
|
+
else
|
194
|
+
UI.info 'Paused files modification listening', :reset => true
|
195
|
+
listener.pause
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
# Trigger `run_on_change` on all Guards currently enabled.
|
200
|
+
#
|
201
|
+
def run_on_change(files)
|
202
|
+
run do
|
203
|
+
run_on_guards do |guard|
|
204
|
+
run_on_change_task(files, guard)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
# Run a block where the listener and the interactor is
|
210
|
+
# blocked.
|
211
|
+
#
|
212
|
+
# @yield the block to run
|
213
|
+
#
|
214
|
+
def run
|
215
|
+
UI.clear if options[:clear]
|
216
|
+
|
217
|
+
lock.synchronize do
|
218
|
+
begin
|
219
|
+
interactor.stop_if_not_current if interactor
|
220
|
+
yield
|
221
|
+
rescue Interrupt
|
222
|
+
end
|
223
|
+
|
224
|
+
interactor.start if interactor
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
# Loop through all groups and run the given task (as block) for each Guard.
|
229
|
+
#
|
230
|
+
# Stop the task run for the all Guards within a group if one Guard
|
231
|
+
# throws `:task_has_failed`.
|
232
|
+
#
|
233
|
+
# @param [Hash] An hash with a guard or a group scope
|
234
|
+
#
|
235
|
+
def run_on_guards(scopes = {})
|
236
|
+
if guard = scopes[:guard]
|
237
|
+
yield(guard)
|
238
|
+
else
|
239
|
+
groups = scopes[:group] ? [scopes[:group]] : @groups
|
240
|
+
groups.each do |group|
|
241
|
+
catch :task_has_failed do
|
242
|
+
guards(:group => group.name).each do |guard|
|
243
|
+
yield(guard)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
# Run the `:run_on_change` task. When the option `:watch_all_modifications` is set,
|
251
|
+
# the task is split to run changed paths on {Guard::Guard#run_on_change}, whereas
|
252
|
+
# deleted paths run on {Guard::Guard#run_on_deletion}.
|
253
|
+
#
|
254
|
+
# @param [Array<String>] files the list of files to pass to the task
|
255
|
+
# @param [Guard::Guard] guard the guard to run
|
256
|
+
# @raise [:task_has_failed] when task has failed
|
257
|
+
#
|
258
|
+
def run_on_change_task(files, guard)
|
259
|
+
paths = Watcher.match_files(guard, files)
|
260
|
+
changes = changed_paths(paths)
|
261
|
+
deletions = deleted_paths(paths)
|
262
|
+
|
263
|
+
unless changes.empty?
|
264
|
+
UI.debug "#{ guard.class.name }#run_on_change with #{ changes.inspect }"
|
265
|
+
run_supervised_task(guard, :run_on_change, changes)
|
266
|
+
end
|
267
|
+
|
268
|
+
unless deletions.empty?
|
269
|
+
UI.debug "#{ guard.class.name }#run_on_deletion with #{ deletions.inspect }"
|
270
|
+
run_supervised_task(guard, :run_on_deletion, deletions)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
# Detects the paths that have changed.
|
275
|
+
#
|
276
|
+
# Deleted paths are prefixed by an exclamation point.
|
277
|
+
# @see Guard::Listener#modified_files
|
278
|
+
#
|
279
|
+
# @param [Array<String>] paths the watched paths
|
280
|
+
# @return [Array<String>] the changed paths
|
281
|
+
#
|
282
|
+
def changed_paths(paths)
|
283
|
+
paths.select { |f| !f.start_with?('!') }
|
284
|
+
end
|
285
|
+
|
286
|
+
# Detects the paths that have been deleted.
|
287
|
+
#
|
288
|
+
# Deleted paths are prefixed by an exclamation point.
|
289
|
+
# @see Guard::Listener#modified_files
|
290
|
+
#
|
291
|
+
# @param [Array<String>] paths the watched paths
|
292
|
+
# @return [Array<String>] the deleted paths
|
293
|
+
#
|
294
|
+
def deleted_paths(paths)
|
295
|
+
paths.select { |f| f.start_with?('!') }.map { |f| f.slice(1..-1) }
|
296
|
+
end
|
297
|
+
|
298
|
+
# Run a Guard task, but remove the Guard when his work leads to a system failure.
|
299
|
+
#
|
300
|
+
# When the Group has `:halt_on_fail` disabled, we've to catch `:task_has_failed`
|
301
|
+
# here in order to avoid an uncaught throw error.
|
302
|
+
#
|
303
|
+
# @param [Guard::Guard] guard the Guard to execute
|
304
|
+
# @param [Symbol] task the task to run
|
305
|
+
# @param [Array] args the arguments for the task
|
306
|
+
# @raise [:task_has_failed] when task has failed
|
307
|
+
#
|
308
|
+
def run_supervised_task(guard, task, *args)
|
309
|
+
catch guard_symbol(guard) do
|
310
|
+
guard.hook("#{ task }_begin", *args)
|
311
|
+
result = guard.send(task, *args)
|
312
|
+
guard.hook("#{ task }_end", result)
|
313
|
+
|
314
|
+
result
|
315
|
+
end
|
316
|
+
|
317
|
+
rescue Exception => ex
|
318
|
+
UI.error("#{ guard.class.name } failed to achieve its <#{ task.to_s }>, exception was:" +
|
319
|
+
"\n#{ ex.class }: #{ ex.message }\n#{ ex.backtrace.join("\n") }")
|
320
|
+
|
321
|
+
guards.delete guard
|
322
|
+
UI.info("\n#{ guard.class.name } has just been fired")
|
323
|
+
|
324
|
+
ex
|
325
|
+
end
|
326
|
+
|
327
|
+
# Get the symbol we have to catch when running a supervised task.
|
328
|
+
# If we are within a Guard group that has the `:halt_on_fail`
|
329
|
+
# option set, we do NOT catch it here, it will be catched at the
|
330
|
+
# group level.
|
331
|
+
#
|
332
|
+
# @see .run_on_guards
|
333
|
+
#
|
334
|
+
# @param [Guard::Guard] guard the Guard to execute
|
335
|
+
# @return [Symbol] the symbol to catch
|
336
|
+
#
|
337
|
+
def guard_symbol(guard)
|
338
|
+
if guard.group.class == Symbol
|
339
|
+
group = groups(guard.group)
|
340
|
+
group.options[:halt_on_fail] ? :no_catch : :task_has_failed
|
341
|
+
else
|
342
|
+
:task_has_failed
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
# Add a Guard to use.
|
347
|
+
#
|
348
|
+
# @param [String] name the Guard name
|
349
|
+
# @param [Array<Watcher>] watchers the list of declared watchers
|
350
|
+
# @param [Array<Hash>] callbacks the list of callbacks
|
351
|
+
# @param [Hash] options the Guard options (see the given Guard documentation)
|
352
|
+
# @return [Guard::Guard] the guard added
|
353
|
+
#
|
354
|
+
def add_guard(name, watchers = [], callbacks = [], options = {})
|
355
|
+
if name.to_sym == :ego
|
356
|
+
UI.deprecation('Guard::Ego is now part of Guard. You can remove it from your Guardfile.')
|
357
|
+
else
|
358
|
+
guard_class = get_guard_class(name)
|
359
|
+
callbacks.each { |callback| Hook.add_callback(callback[:listener], guard_class, callback[:events]) }
|
360
|
+
guard = guard_class.new(watchers, options)
|
361
|
+
@guards << guard
|
362
|
+
guard
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
# Add a Guard group.
|
367
|
+
#
|
368
|
+
# @param [String] name the group name
|
369
|
+
# @option options [Boolean] halt_on_fail if a task execution
|
370
|
+
# should be halted for all Guards in this group if one Guard throws `:task_has_failed`
|
371
|
+
# @return [Guard::Group] the group added (or retrieved from the `@groups` variable if already present)
|
372
|
+
#
|
373
|
+
def add_group(name, options = {})
|
374
|
+
group = groups(name)
|
375
|
+
if group.nil?
|
376
|
+
group = Group.new(name, options)
|
377
|
+
@groups << group
|
378
|
+
end
|
379
|
+
group
|
380
|
+
end
|
381
|
+
|
382
|
+
# Tries to load the Guard main class.
|
383
|
+
#
|
384
|
+
# @param [String] name the name of the Guard
|
385
|
+
# @return [Class, nil] the loaded class
|
386
|
+
#
|
387
|
+
def get_guard_class(name)
|
388
|
+
name = name.to_s
|
389
|
+
try_require = false
|
390
|
+
const_name = name.downcase.gsub('-', '')
|
391
|
+
begin
|
392
|
+
require "guard/#{ name.downcase }" if try_require
|
393
|
+
self.const_get(self.constants.find { |c| c.to_s.downcase == const_name })
|
394
|
+
rescue TypeError
|
395
|
+
unless try_require
|
396
|
+
try_require = true
|
397
|
+
retry
|
398
|
+
else
|
399
|
+
UI.error "Could not find class Guard::#{ const_name.capitalize }"
|
400
|
+
end
|
401
|
+
rescue LoadError => loadError
|
402
|
+
UI.error "Could not load 'guard/#{ name.downcase }' or find class Guard::#{ const_name.capitalize }"
|
403
|
+
UI.error loadError.to_s
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
# Locate a path to a Guard gem.
|
408
|
+
#
|
409
|
+
# @param [String] name the name of the Guard without the prefix `guard-`
|
410
|
+
# @return [String] the full path to the Guard gem
|
411
|
+
#
|
412
|
+
def locate_guard(name)
|
413
|
+
if Gem::Version.create(Gem::VERSION) >= Gem::Version.create('1.8.0')
|
414
|
+
Gem::Specification.find_by_name("guard-#{ name }").full_gem_path
|
415
|
+
else
|
416
|
+
Gem.source_index.find_name("guard-#{ name }").last.full_gem_path
|
417
|
+
end
|
418
|
+
rescue
|
419
|
+
UI.error "Could not find 'guard-#{ name }' gem path."
|
420
|
+
end
|
421
|
+
|
422
|
+
# Returns a list of guard Gem names installed locally.
|
423
|
+
#
|
424
|
+
# @return [Array<String>] a list of guard gem names
|
425
|
+
#
|
426
|
+
def guard_gem_names
|
427
|
+
if Gem::Version.create(Gem::VERSION) >= Gem::Version.create('1.8.0')
|
428
|
+
Gem::Specification.find_all.select { |x| x.name =~ /^guard-/ }
|
429
|
+
else
|
430
|
+
Gem.source_index.find_name(/^guard-/)
|
431
|
+
end.map { |x| x.name.sub /^guard-/, '' }
|
432
|
+
end
|
433
|
+
|
434
|
+
# Adds a command logger in debug mode. This wraps common command
|
435
|
+
# execution functions and logs the executed command before execution.
|
436
|
+
#
|
437
|
+
def debug_command_execution
|
438
|
+
Kernel.send(:alias_method, :original_system, :system)
|
439
|
+
Kernel.send(:define_method, :system) do |command, *args|
|
440
|
+
::Guard::UI.debug "Command execution: #{ command } #{ args.join(' ') }"
|
441
|
+
original_system command, *args
|
442
|
+
end
|
443
|
+
|
444
|
+
Kernel.send(:alias_method, :original_backtick, :'`')
|
445
|
+
Kernel.send(:define_method, :'`') do |command|
|
446
|
+
::Guard::UI.debug "Command execution: #{ command }"
|
447
|
+
original_backtick command
|
448
|
+
end
|
449
|
+
end
|
450
|
+
|
451
|
+
end
|
452
|
+
end
|