guard 1.4.0 → 2.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1 -677
- data/LICENSE +4 -2
- data/README.md +91 -753
- data/bin/_guard-core +11 -0
- data/bin/guard +108 -3
- data/lib/guard/aruba_adapter.rb +59 -0
- data/lib/guard/cli/environments/bundler.rb +22 -0
- data/lib/guard/cli/environments/evaluate_only.rb +35 -0
- data/lib/guard/cli/environments/valid.rb +69 -0
- data/lib/guard/cli.rb +129 -128
- data/lib/guard/commander.rb +104 -0
- data/lib/guard/commands/all.rb +37 -0
- data/lib/guard/commands/change.rb +31 -0
- data/lib/guard/commands/notification.rb +26 -0
- data/lib/guard/commands/pause.rb +29 -0
- data/lib/guard/commands/reload.rb +36 -0
- data/lib/guard/commands/scope.rb +38 -0
- data/lib/guard/commands/show.rb +24 -0
- data/lib/guard/config.rb +18 -0
- data/lib/guard/deprecated/dsl.rb +45 -0
- data/lib/guard/deprecated/evaluator.rb +39 -0
- data/lib/guard/deprecated/guard.rb +328 -0
- data/lib/guard/deprecated/guardfile.rb +84 -0
- data/lib/guard/deprecated/watcher.rb +27 -0
- data/lib/guard/dsl.rb +332 -363
- data/lib/guard/dsl_describer.rb +132 -122
- data/lib/guard/dsl_reader.rb +51 -0
- data/lib/guard/group.rb +34 -14
- data/lib/guard/guardfile/evaluator.rb +232 -0
- data/lib/guard/guardfile/generator.rb +128 -0
- data/lib/guard/guardfile.rb +24 -60
- data/lib/guard/interactor.rb +31 -255
- data/lib/guard/internals/debugging.rb +68 -0
- data/lib/guard/internals/groups.rb +40 -0
- data/lib/guard/internals/helpers.rb +13 -0
- data/lib/guard/internals/plugins.rb +53 -0
- data/lib/guard/internals/queue.rb +51 -0
- data/lib/guard/internals/scope.rb +121 -0
- data/lib/guard/internals/session.rb +180 -0
- data/lib/guard/internals/state.rb +25 -0
- data/lib/guard/internals/tracing.rb +33 -0
- data/lib/guard/internals/traps.rb +10 -0
- data/lib/guard/jobs/base.rb +21 -0
- data/lib/guard/jobs/pry_wrapper.rb +336 -0
- data/lib/guard/jobs/sleep.rb +26 -0
- data/lib/guard/notifier.rb +46 -212
- data/lib/guard/options.rb +22 -0
- data/lib/guard/plugin.rb +303 -0
- data/lib/guard/plugin_util.rb +191 -0
- data/lib/guard/rake_task.rb +42 -0
- data/lib/guard/runner.rb +80 -140
- data/lib/guard/templates/Guardfile +14 -0
- data/lib/guard/terminal.rb +13 -0
- data/lib/guard/ui/colors.rb +56 -0
- data/lib/guard/ui/config.rb +70 -0
- data/lib/guard/ui/logger.rb +30 -0
- data/lib/guard/ui.rb +163 -128
- data/lib/guard/version.rb +1 -2
- data/lib/guard/watcher/pattern/deprecated_regexp.rb +45 -0
- data/lib/guard/watcher/pattern/match_result.rb +18 -0
- data/lib/guard/watcher/pattern/matcher.rb +33 -0
- data/lib/guard/watcher/pattern/pathname_path.rb +15 -0
- data/lib/guard/watcher/pattern/simple_path.rb +23 -0
- data/lib/guard/watcher/pattern.rb +24 -0
- data/lib/guard/watcher.rb +52 -95
- data/lib/guard.rb +108 -376
- data/lib/tasks/releaser.rb +116 -0
- data/man/guard.1 +12 -9
- data/man/guard.1.html +18 -12
- metadata +148 -77
- data/images/guard.png +0 -0
- data/lib/guard/guard.rb +0 -156
- data/lib/guard/hook.rb +0 -120
- data/lib/guard/interactors/coolline.rb +0 -64
- data/lib/guard/interactors/helpers/completion.rb +0 -32
- data/lib/guard/interactors/helpers/terminal.rb +0 -46
- data/lib/guard/interactors/readline.rb +0 -94
- data/lib/guard/interactors/simple.rb +0 -19
- data/lib/guard/notifiers/emacs.rb +0 -69
- data/lib/guard/notifiers/gntp.rb +0 -118
- data/lib/guard/notifiers/growl.rb +0 -99
- data/lib/guard/notifiers/growl_notify.rb +0 -92
- data/lib/guard/notifiers/libnotify.rb +0 -96
- data/lib/guard/notifiers/notifysend.rb +0 -84
- data/lib/guard/notifiers/rb_notifu.rb +0 -102
- data/lib/guard/notifiers/terminal_notifier.rb +0 -66
- data/lib/guard/notifiers/tmux.rb +0 -69
- data/lib/guard/version.rbc +0 -130
data/lib/guard/plugin.rb
ADDED
@@ -0,0 +1,303 @@
|
|
1
|
+
require "guard"
|
2
|
+
require "guard/internals/groups"
|
3
|
+
|
4
|
+
module Guard
|
5
|
+
# Base class from which every Guard plugin implementation must inherit.
|
6
|
+
#
|
7
|
+
# Guard will trigger the {#start}, {#stop}, {#reload}, {#run_all} and
|
8
|
+
# {#run_on_changes} ({#run_on_additions}, {#run_on_modifications} and
|
9
|
+
# {#run_on_removals}) task methods depending on user interaction and file
|
10
|
+
# modification.
|
11
|
+
#
|
12
|
+
# {#run_on_changes} could be implemented to handle all the changes task case
|
13
|
+
# (additions, modifications, removals) in once, or each task can be
|
14
|
+
# implemented separately with a specific behavior.
|
15
|
+
#
|
16
|
+
# In each of these Guard task methods you have to implement some work when
|
17
|
+
# you want to support this kind of task. The return value of each Guard task
|
18
|
+
# method is not evaluated by Guard, but it'll be passed to the "_end" hook
|
19
|
+
# for further evaluation. You can throw `:task_has_failed` to indicate that
|
20
|
+
# your Guard plugin method was not successful, and successive Guard plugin
|
21
|
+
# tasks will be aborted when the group has set the `:halt_on_fail` option.
|
22
|
+
#
|
23
|
+
# @see Guard::Group
|
24
|
+
#
|
25
|
+
# @example Throw :task_has_failed
|
26
|
+
#
|
27
|
+
# def run_all
|
28
|
+
# if !runner.run(['all'])
|
29
|
+
# throw :task_has_failed
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# Each Guard plugin should provide a template Guardfile located within the Gem
|
34
|
+
# at `lib/guard/guard-name/templates/Guardfile`.
|
35
|
+
#
|
36
|
+
# Watchers for a Guard plugin should return a file path or an array of files
|
37
|
+
# paths to Guard, but if your Guard plugin wants to allow any return value
|
38
|
+
# from a watcher, you can set the `any_return` option to true.
|
39
|
+
#
|
40
|
+
# If one of those methods raises an exception other than `:task_has_failed`,
|
41
|
+
# the `Guard::GuardName` instance will be removed from the active Guard
|
42
|
+
# plugins.
|
43
|
+
#
|
44
|
+
class Plugin
|
45
|
+
TEMPLATE_FORMAT = "%s/lib/guard/%s/templates/Guardfile"
|
46
|
+
|
47
|
+
require "guard/ui"
|
48
|
+
|
49
|
+
# Get all callbacks registered for all Guard plugins present in the
|
50
|
+
# Guardfile.
|
51
|
+
#
|
52
|
+
def self.callbacks
|
53
|
+
@callbacks ||= Hash.new { |hash, key| hash[key] = [] }
|
54
|
+
end
|
55
|
+
|
56
|
+
# Add a callback.
|
57
|
+
#
|
58
|
+
# @param [Block] listener the listener to notify
|
59
|
+
# @param [Guard::Plugin] guard_plugin the Guard plugin to add the callback
|
60
|
+
# @param [Array<Symbol>] events the events to register
|
61
|
+
#
|
62
|
+
def self.add_callback(listener, guard_plugin, events)
|
63
|
+
Array(events).each do |event|
|
64
|
+
callbacks[[guard_plugin, event]] << listener
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Notify a callback.
|
69
|
+
#
|
70
|
+
# @param [Guard::Plugin] guard_plugin the Guard plugin to add the callback
|
71
|
+
# @param [Symbol] event the event to trigger
|
72
|
+
# @param [Array] args the arguments for the listener
|
73
|
+
#
|
74
|
+
def self.notify(guard_plugin, event, *args)
|
75
|
+
callbacks[[guard_plugin, event]].each do |listener|
|
76
|
+
listener.call(guard_plugin, event, *args)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Reset all callbacks.
|
81
|
+
#
|
82
|
+
# TODO: remove (not used anywhere)
|
83
|
+
def self.reset_callbacks!
|
84
|
+
@callbacks = nil
|
85
|
+
end
|
86
|
+
|
87
|
+
# When event is a Symbol, {#hook} will generate a hook name
|
88
|
+
# by concatenating the method name from where {#hook} is called
|
89
|
+
# with the given Symbol.
|
90
|
+
#
|
91
|
+
# @example Add a hook with a Symbol
|
92
|
+
#
|
93
|
+
# def run_all
|
94
|
+
# hook :foo
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
# Here, when {Guard::Plugin#run_all} is called, {#hook} will notify
|
98
|
+
# callbacks registered for the "run_all_foo" event.
|
99
|
+
#
|
100
|
+
# When event is a String, {#hook} will directly turn the String
|
101
|
+
# into a Symbol.
|
102
|
+
#
|
103
|
+
# @example Add a hook with a String
|
104
|
+
#
|
105
|
+
# def run_all
|
106
|
+
# hook "foo_bar"
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
# When {Guard::Plugin::run_all} is called, {#hook} will notify
|
110
|
+
# callbacks registered for the "foo_bar" event.
|
111
|
+
#
|
112
|
+
# @param [Symbol, String] event the name of the Guard event
|
113
|
+
# @param [Array] args the parameters are passed as is to the callbacks
|
114
|
+
# registered for the given event.
|
115
|
+
#
|
116
|
+
def hook(event, *args)
|
117
|
+
hook_name = if event.is_a? Symbol
|
118
|
+
calling_method = caller[0][/`([^']*)'/, 1]
|
119
|
+
"#{ calling_method }_#{ event }"
|
120
|
+
else
|
121
|
+
event
|
122
|
+
end
|
123
|
+
|
124
|
+
UI.debug "Hook :#{ hook_name } executed for #{ self.class }"
|
125
|
+
|
126
|
+
self.class.notify(self, hook_name.to_sym, *args)
|
127
|
+
end
|
128
|
+
|
129
|
+
attr_accessor :group, :watchers, :callbacks, :options
|
130
|
+
|
131
|
+
# Returns the non-namespaced class name of the plugin
|
132
|
+
#
|
133
|
+
#
|
134
|
+
# @example Non-namespaced class name for Guard::RSpec
|
135
|
+
# Guard::RSpec.non_namespaced_classname
|
136
|
+
# #=> "RSpec"
|
137
|
+
#
|
138
|
+
# @return [String]
|
139
|
+
#
|
140
|
+
def self.non_namespaced_classname
|
141
|
+
to_s.sub("Guard::", "")
|
142
|
+
end
|
143
|
+
|
144
|
+
# Returns the non-namespaced name of the plugin
|
145
|
+
#
|
146
|
+
#
|
147
|
+
# @example Non-namespaced name for Guard::RSpec
|
148
|
+
# Guard::RSpec.non_namespaced_name
|
149
|
+
# #=> "rspec"
|
150
|
+
#
|
151
|
+
# @return [String]
|
152
|
+
#
|
153
|
+
def self.non_namespaced_name
|
154
|
+
non_namespaced_classname.downcase
|
155
|
+
end
|
156
|
+
|
157
|
+
# Specify the source for the Guardfile template.
|
158
|
+
# Each Guard plugin can redefine this method to add its own logic.
|
159
|
+
#
|
160
|
+
# @param [String] plugin_location the plugin location
|
161
|
+
#
|
162
|
+
def self.template(plugin_location)
|
163
|
+
File.read(format(TEMPLATE_FORMAT, plugin_location, non_namespaced_name))
|
164
|
+
end
|
165
|
+
|
166
|
+
# Called once when Guard starts. Please override initialize method to
|
167
|
+
# init stuff.
|
168
|
+
#
|
169
|
+
# @raise [:task_has_failed] when start has failed
|
170
|
+
# @return [Object] the task result
|
171
|
+
#
|
172
|
+
# @!method start
|
173
|
+
|
174
|
+
# Called when `stop|quit|exit|s|q|e + enter` is pressed (when Guard
|
175
|
+
# quits).
|
176
|
+
#
|
177
|
+
# @raise [:task_has_failed] when stop has failed
|
178
|
+
# @return [Object] the task result
|
179
|
+
#
|
180
|
+
# @!method stop
|
181
|
+
|
182
|
+
# Called when `reload|r|z + enter` is pressed.
|
183
|
+
# This method should be mainly used for "reload" (really!) actions like
|
184
|
+
# reloading passenger/spork/bundler/...
|
185
|
+
#
|
186
|
+
# @raise [:task_has_failed] when reload has failed
|
187
|
+
# @return [Object] the task result
|
188
|
+
#
|
189
|
+
# @!method reload
|
190
|
+
|
191
|
+
# Called when just `enter` is pressed
|
192
|
+
# This method should be principally used for long action like running all
|
193
|
+
# specs/tests/...
|
194
|
+
#
|
195
|
+
# @raise [:task_has_failed] when run_all has failed
|
196
|
+
# @return [Object] the task result
|
197
|
+
#
|
198
|
+
# @!method run_all
|
199
|
+
|
200
|
+
# Default behaviour on file(s) changes that the Guard plugin watches.
|
201
|
+
#
|
202
|
+
# @param [Array<String>] paths the changes files or paths
|
203
|
+
# @raise [:task_has_failed] when run_on_changes has failed
|
204
|
+
# @return [Object] the task result
|
205
|
+
#
|
206
|
+
# @!method run_on_changes(paths)
|
207
|
+
|
208
|
+
# Called on file(s) additions that the Guard plugin watches.
|
209
|
+
#
|
210
|
+
# @param [Array<String>] paths the changes files or paths
|
211
|
+
# @raise [:task_has_failed] when run_on_additions has failed
|
212
|
+
# @return [Object] the task result
|
213
|
+
#
|
214
|
+
# @!method run_on_additions(paths)
|
215
|
+
|
216
|
+
# Called on file(s) modifications that the Guard plugin watches.
|
217
|
+
#
|
218
|
+
# @param [Array<String>] paths the changes files or paths
|
219
|
+
# @raise [:task_has_failed] when run_on_modifications has failed
|
220
|
+
# @return [Object] the task result
|
221
|
+
#
|
222
|
+
# @!method run_on_modifications(paths)
|
223
|
+
|
224
|
+
# Called on file(s) removals that the Guard plugin watches.
|
225
|
+
#
|
226
|
+
# @param [Array<String>] paths the changes files or paths
|
227
|
+
# @raise [:task_has_failed] when run_on_removals has failed
|
228
|
+
# @return [Object] the task result
|
229
|
+
#
|
230
|
+
# @!method run_on_removals(paths)
|
231
|
+
|
232
|
+
# Returns the plugin's name (without "guard-").
|
233
|
+
#
|
234
|
+
# @example Name for Guard::RSpec
|
235
|
+
# Guard::RSpec.new.name
|
236
|
+
# #=> "rspec"
|
237
|
+
#
|
238
|
+
# @return [String]
|
239
|
+
#
|
240
|
+
def name
|
241
|
+
@name ||= self.class.non_namespaced_name
|
242
|
+
end
|
243
|
+
|
244
|
+
# Returns the plugin's class name without the Guard:: namespace.
|
245
|
+
#
|
246
|
+
# @example Title for Guard::RSpec
|
247
|
+
# Guard::RSpec.new.title
|
248
|
+
# #=> "RSpec"
|
249
|
+
#
|
250
|
+
# @return [String]
|
251
|
+
#
|
252
|
+
def title
|
253
|
+
@title ||= self.class.non_namespaced_classname
|
254
|
+
end
|
255
|
+
|
256
|
+
# String representation of the plugin.
|
257
|
+
#
|
258
|
+
# @example String representation of an instance of the Guard::RSpec plugin
|
259
|
+
#
|
260
|
+
# Guard::RSpec.new.title
|
261
|
+
# #=> "#<Guard::RSpec @name=rspec @group=#<Guard::Group @name=default
|
262
|
+
# @options={}> @watchers=[] @callbacks=[] @options={all_after_pass:
|
263
|
+
# true}>"
|
264
|
+
#
|
265
|
+
# @return [String] the string representation
|
266
|
+
#
|
267
|
+
def to_s
|
268
|
+
"#<#{self.class} @name=#{name} @group=#{group} @watchers=#{watchers}"\
|
269
|
+
" @callbacks=#{callbacks} @options=#{options}>"
|
270
|
+
end
|
271
|
+
|
272
|
+
private
|
273
|
+
|
274
|
+
# Initializes a Guard plugin.
|
275
|
+
# Don't do any work here, especially as Guard plugins get initialized even
|
276
|
+
# if they are not in an active group!
|
277
|
+
#
|
278
|
+
# @param [Hash] options the Guard plugin options
|
279
|
+
# @option options [Array<Guard::Watcher>] watchers the Guard plugin file
|
280
|
+
# watchers
|
281
|
+
# @option options [Symbol] group the group this Guard plugin belongs to
|
282
|
+
# @option options [Boolean] any_return allow any object to be returned from
|
283
|
+
# a watcher
|
284
|
+
#
|
285
|
+
def initialize(options = {})
|
286
|
+
group_name = options.delete(:group) { :default }
|
287
|
+
@group = Guard.state.session.groups.add(group_name)
|
288
|
+
@watchers = options.delete(:watchers) { [] }
|
289
|
+
@callbacks = options.delete(:callbacks) { [] }
|
290
|
+
@options = options
|
291
|
+
_register_callbacks
|
292
|
+
end
|
293
|
+
|
294
|
+
# Add all the Guard::Plugin's callbacks to the global @callbacks array
|
295
|
+
# that's used by Guard to know which callbacks to notify.
|
296
|
+
#
|
297
|
+
def _register_callbacks
|
298
|
+
callbacks.each do |callback|
|
299
|
+
self.class.add_callback(callback[:listener], self, callback[:events])
|
300
|
+
end
|
301
|
+
end
|
302
|
+
end
|
303
|
+
end
|
@@ -0,0 +1,191 @@
|
|
1
|
+
require "guard/ui"
|
2
|
+
|
3
|
+
module Guard
|
4
|
+
# This class contains useful methods to:
|
5
|
+
#
|
6
|
+
# * Fetch all the Guard plugins names;
|
7
|
+
# * Initialize a plugin, get its location;
|
8
|
+
# * Return its class name;
|
9
|
+
# * Add its template to the Guardfile.
|
10
|
+
#
|
11
|
+
class PluginUtil
|
12
|
+
ERROR_NO_GUARD_OR_CLASS = "Could not load 'guard/%s' or" \
|
13
|
+
" find class Guard::%s"
|
14
|
+
|
15
|
+
INFO_ADDED_GUARD_TO_GUARDFILE = "%s guard added to Guardfile,"\
|
16
|
+
" feel free to edit it"
|
17
|
+
|
18
|
+
attr_accessor :name
|
19
|
+
|
20
|
+
# Returns a list of Guard plugin Gem names installed locally.
|
21
|
+
#
|
22
|
+
# @return [Array<String>] a list of Guard plugin gem names
|
23
|
+
#
|
24
|
+
def self.plugin_names
|
25
|
+
valid = Gem::Specification.find_all.select do |gem|
|
26
|
+
_gem_valid?(gem)
|
27
|
+
end
|
28
|
+
|
29
|
+
valid.map { |x| x.name.sub(/^guard-/, "") }.uniq
|
30
|
+
end
|
31
|
+
|
32
|
+
# Initializes a new `Guard::PluginUtil` object.
|
33
|
+
#
|
34
|
+
# @param [String] name the name of the Guard plugin
|
35
|
+
#
|
36
|
+
def initialize(name)
|
37
|
+
@name = name.to_s.sub(/^guard-/, "")
|
38
|
+
end
|
39
|
+
|
40
|
+
# Initializes a new `Guard::Plugin` with the given `options` hash. This
|
41
|
+
# methods handles plugins that inherit from the deprecated `Guard::Guard`
|
42
|
+
# class as well as plugins that inherit from `Guard::Plugin`.
|
43
|
+
#
|
44
|
+
# @see Guard::Plugin
|
45
|
+
# @see https://github.com/guard/guard/wiki/Upgrading-to-Guard-2.0 How to
|
46
|
+
# upgrade for Guard 2.0
|
47
|
+
#
|
48
|
+
# @return [Guard::Plugin] the initialized plugin
|
49
|
+
# @return [Guard::Guard] the initialized plugin. This return type is
|
50
|
+
# deprecated and the plugin's maintainer should update it to be
|
51
|
+
# compatible with Guard 2.0. For more information on how to upgrade for
|
52
|
+
# Guard 2.0, please head over to:
|
53
|
+
# https://github.com/guard/guard/wiki/Upgrading-to-Guard-2.0
|
54
|
+
#
|
55
|
+
def initialize_plugin(options)
|
56
|
+
klass = plugin_class
|
57
|
+
fail "Could not load class: #{_constant_name.inspect}" unless klass
|
58
|
+
if klass.ancestors.include?(Guard)
|
59
|
+
klass.new(options.delete(:watchers), options)
|
60
|
+
else
|
61
|
+
begin
|
62
|
+
klass.new(**options)
|
63
|
+
rescue ArgumentError => e
|
64
|
+
fail "Failed to call #{klass}.new(options): #{e}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Locates a path to a Guard plugin gem.
|
70
|
+
#
|
71
|
+
# @return [String] the full path to the plugin gem
|
72
|
+
#
|
73
|
+
def plugin_location
|
74
|
+
@plugin_location ||= _full_gem_path("guard-#{name}")
|
75
|
+
rescue Gem::LoadError
|
76
|
+
UI.error "Could not find 'guard-#{ name }' gem path."
|
77
|
+
end
|
78
|
+
|
79
|
+
# Tries to load the Guard plugin main class. This transforms the supplied
|
80
|
+
# plugin name into a class name:
|
81
|
+
#
|
82
|
+
# * `guardname` will become `Guard::Guardname`
|
83
|
+
# * `dashed-guard-name` will become `Guard::DashedGuardName`
|
84
|
+
# * `underscore_guard_name` will become `Guard::UnderscoreGuardName`
|
85
|
+
#
|
86
|
+
# When no class is found with the strict case sensitive rules, another
|
87
|
+
# try is made to locate the class without matching case:
|
88
|
+
#
|
89
|
+
# * `rspec` will find a class `Guard::RSpec`
|
90
|
+
#
|
91
|
+
# @option options [Boolean] fail_gracefully whether error messages should
|
92
|
+
# not be printed
|
93
|
+
#
|
94
|
+
# @return [Class, nil] the loaded class
|
95
|
+
#
|
96
|
+
def plugin_class(options = {})
|
97
|
+
options = { fail_gracefully: false }.merge(options)
|
98
|
+
|
99
|
+
const = _plugin_constant
|
100
|
+
fail TypeError, "no constant: #{_constant_name}" unless const
|
101
|
+
@plugin_class ||= Guard.const_get(const)
|
102
|
+
|
103
|
+
rescue TypeError
|
104
|
+
begin
|
105
|
+
require "guard/#{ name.downcase }"
|
106
|
+
const = _plugin_constant
|
107
|
+
@plugin_class ||= Guard.const_get(const)
|
108
|
+
|
109
|
+
rescue TypeError => error
|
110
|
+
UI.error "Could not find class Guard::#{ _constant_name }"
|
111
|
+
UI.error error.backtrace.join("\n")
|
112
|
+
# TODO: return value or move exception higher
|
113
|
+
rescue LoadError => error
|
114
|
+
unless options[:fail_gracefully]
|
115
|
+
msg = format(ERROR_NO_GUARD_OR_CLASS, name.downcase, _constant_name)
|
116
|
+
UI.error(msg)
|
117
|
+
UI.error("Error is: #{error}")
|
118
|
+
UI.error(error.backtrace.join("\n"))
|
119
|
+
# TODO: return value or move exception higher
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# Adds a plugin's template to the Guardfile.
|
125
|
+
#
|
126
|
+
def add_to_guardfile
|
127
|
+
klass = plugin_class # call here to avoid failing later
|
128
|
+
|
129
|
+
require_relative "guardfile/evaluator"
|
130
|
+
# TODO: move this to Generator?
|
131
|
+
options = Guard.state.session.evaluator_options
|
132
|
+
evaluator = Guardfile::Evaluator.new(options)
|
133
|
+
begin
|
134
|
+
evaluator.evaluate
|
135
|
+
rescue Guard::Guardfile::Evaluator::NoPluginsError
|
136
|
+
end
|
137
|
+
|
138
|
+
if evaluator.guardfile_include?(name)
|
139
|
+
UI.info "Guardfile already includes #{ name } guard"
|
140
|
+
else
|
141
|
+
content = File.read("Guardfile")
|
142
|
+
File.open("Guardfile", "wb") do |f|
|
143
|
+
f.puts(content)
|
144
|
+
f.puts("")
|
145
|
+
f.puts(klass.template(plugin_location))
|
146
|
+
end
|
147
|
+
|
148
|
+
UI.info INFO_ADDED_GUARD_TO_GUARDFILE % name
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
private
|
153
|
+
|
154
|
+
# Returns the constant for the current plugin.
|
155
|
+
#
|
156
|
+
# @example Returns the constant for a plugin
|
157
|
+
# > Guard::PluginUtil.new('rspec').send(:_plugin_constant)
|
158
|
+
# => Guard::RSpec
|
159
|
+
#
|
160
|
+
def _plugin_constant
|
161
|
+
@_plugin_constant ||= Guard.constants.detect do |c|
|
162
|
+
c.to_s.casecmp(_constant_name.downcase).zero?
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
# Guesses the most probable name for the current plugin based on its name.
|
167
|
+
#
|
168
|
+
# @example Returns the most probable name for a plugin
|
169
|
+
# > Guard::PluginUtil.new('rspec').send(:_constant_name)
|
170
|
+
# => "Rspec"
|
171
|
+
#
|
172
|
+
def _constant_name
|
173
|
+
@_constant_name ||= name.gsub(%r{/(.?)}) { "::#{ $1.upcase }" }.
|
174
|
+
gsub(/(?:^|[_-])(.)/) { $1.upcase }
|
175
|
+
end
|
176
|
+
|
177
|
+
def _full_gem_path(name)
|
178
|
+
Gem::Specification.find_by_name(name).full_gem_path
|
179
|
+
end
|
180
|
+
|
181
|
+
class << self
|
182
|
+
def _gem_valid?(gem)
|
183
|
+
return false if gem.name == "guard-compat"
|
184
|
+
return true if gem.name =~ /^guard-/
|
185
|
+
full_path = gem.full_gem_path
|
186
|
+
file = File.join(full_path, "lib", "guard", "#{gem.name}.rb")
|
187
|
+
File.exist?(file)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "rake"
|
4
|
+
require "rake/tasklib"
|
5
|
+
|
6
|
+
require "guard/cli"
|
7
|
+
|
8
|
+
module Guard
|
9
|
+
# Provides a method to define a Rake task that
|
10
|
+
# runs the Guard plugins.
|
11
|
+
#
|
12
|
+
class RakeTask < ::Rake::TaskLib
|
13
|
+
# Name of the main, top level task
|
14
|
+
attr_accessor :name
|
15
|
+
|
16
|
+
# CLI options
|
17
|
+
attr_accessor :options
|
18
|
+
|
19
|
+
# Initialize the Rake task
|
20
|
+
#
|
21
|
+
# @param [Symbol] name the name of the Rake task
|
22
|
+
# @param [String] options the CLI options
|
23
|
+
# @yield [Guard::RakeTask] the task
|
24
|
+
#
|
25
|
+
def initialize(name = :guard, options = "")
|
26
|
+
@name = name
|
27
|
+
@options = options
|
28
|
+
|
29
|
+
yield self if block_given?
|
30
|
+
|
31
|
+
desc "Starts Guard with options: '#{options}'"
|
32
|
+
task name => ["#{name}:start"]
|
33
|
+
|
34
|
+
namespace(name) do
|
35
|
+
desc "Starts Guard with options: '#{options}'"
|
36
|
+
task(:start) do
|
37
|
+
::Guard::CLI.start(options.split)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|