guard 0.8.4 → 0.8.5

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.
@@ -1,211 +1,290 @@
1
- require 'rbconfig'
2
- require 'pathname'
3
- require 'guard/ui'
4
-
5
- module Guard
6
-
7
- # The notifier class handles cross-platform system notifications that supports:
8
- #
9
- # - Growl on Mac OS X
10
- # - Libnotify on Linux
11
- # - Notifu on Windows
12
- #
13
- module Notifier
14
-
15
- # Application name as shown in the specific notification settings
16
- APPLICATION_NAME = "Guard"
17
-
18
- # Turn notifications off.
19
- #
20
- def self.turn_off
21
- ENV["GUARD_NOTIFY"] = 'false'
22
- end
23
-
24
- # Turn notifications on. This tries to load the platform
25
- # specific notification library.
26
- #
27
- # @return [Boolean] whether the notification could be enabled.
28
- #
29
- def self.turn_on
30
- ENV["GUARD_NOTIFY"] = 'true'
31
- case RbConfig::CONFIG['target_os']
32
- when /darwin/i
33
- require_growl
34
- when /linux/i
35
- require_libnotify
36
- when /mswin|mingw/i
37
- require_rbnotifu
38
- end
39
- end
40
-
41
- # Show a message with the system notification.
42
- #
43
- # @see .image_path
44
- #
45
- # @param [String] the message to show
46
- # @option options [Symbol, String] image the image symbol or path to an image
47
- # @option options [String] title the notification title
48
- #
49
- def self.notify(message, options = {})
50
- if enabled?
51
- image = options.delete(:image) || :success
52
- title = options.delete(:title) || "Guard"
53
-
54
- case RbConfig::CONFIG['target_os']
55
- when /darwin/i
56
- notify_mac(title, message, image, options)
57
- when /linux/i
58
- notify_linux(title, message, image, options)
59
- when /mswin|mingw/i
60
- notify_windows(title, message, image, options)
61
- end
62
- end
63
- end
64
-
65
- # Test if the notifications are enabled and available.
66
- #
67
- # @return [Boolean] whether the notifications are available
68
- #
69
- def self.enabled?
70
- ENV["GUARD_NOTIFY"] == 'true'
71
- end
72
-
73
- private
74
-
75
- # Send a message to Growl either with the `growl` gem or the `growl_notify` gem.
76
- #
77
- # @param [String] title the notification title
78
- # @param [String] message the message to show
79
- # @param [Symbol, String] the image to user
80
- # @param [Hash] options the growl options
81
- #
82
- def self.notify_mac(title, message, image, options = {})
83
- require_growl # need for guard-rspec formatter that is called out of guard scope
84
-
85
- default_options = { :title => title, :icon => image_path(image), :name => APPLICATION_NAME }
86
- default_options.merge!(options)
87
-
88
- if defined?(GrowlNotify)
89
- default_options[:description] = message
90
- default_options[:application_name] = APPLICATION_NAME
91
- default_options.delete(:name)
92
-
93
- GrowlNotify.send_notification(default_options) if enabled?
94
- else
95
- Growl.notify message, default_options.merge(options) if enabled?
96
- end
97
- end
98
-
99
- # Send a message to libnotify.
100
- #
101
- # @param [String] title the notification title
102
- # @param [String] message the message to show
103
- # @param [Symbol, String] the image to user
104
- # @param [Hash] options the libnotify options
105
- #
106
- def self.notify_linux(title, message, image, options = {})
107
- require_libnotify # need for guard-rspec formatter that is called out of guard scope
108
- default_options = { :body => message, :summary => title, :icon_path => image_path(image), :transient => true }
109
- Libnotify.show default_options.merge(options) if enabled?
110
- end
111
-
112
- # Send a message to notifu.
113
- #
114
- # @param [String] title the notification title
115
- # @param [String] message the message to show
116
- # @param [Symbol, String] the image to user
117
- # @param [Hash] options the notifu options
118
- #
119
- def self.notify_windows(title, message, image, options = {})
120
- require_rbnotifu # need for guard-rspec formatter that is called out of guard scope
121
- default_options = { :message => message, :title => title, :type => image_level(image), :time => 3 }
122
- Notifu.show default_options.merge(options) if enabled?
123
- end
124
-
125
- # Get the image path for an image symbol.
126
- #
127
- # Known symbols are:
128
- #
129
- # - failed
130
- # - pending
131
- # - success
132
- #
133
- # @param [Symbol] image the image name
134
- # @return [String] the image path
135
- #
136
- def self.image_path(image)
137
- images_path = Pathname.new(File.dirname(__FILE__)).join('../../images')
138
- case image
139
- when :failed
140
- images_path.join("failed.png").to_s
141
- when :pending
142
- images_path.join("pending.png").to_s
143
- when :success
144
- images_path.join("success.png").to_s
145
- else
146
- # path given
147
- image
148
- end
149
- end
150
-
151
- # The notification level type for the given image.
152
- #
153
- # @param [Symbol] image the image
154
- # @return [Symbol] the level
155
- #
156
- def self.image_level(image)
157
- case image
158
- when :failed
159
- :error
160
- when :pending
161
- :warn
162
- when :success
163
- :info
164
- else
165
- :info
166
- end
167
- end
168
-
169
- # Try to safely load growl and turns notifications
170
- # off on load failure.
171
- #
172
- def self.require_growl
173
- begin
174
- require 'growl_notify'
175
-
176
- if GrowlNotify.application_name != APPLICATION_NAME
177
- GrowlNotify.config do |c|
178
- c.notifications = c.default_notifications = [ APPLICATION_NAME ]
179
- c.application_name = c.notifications.first
180
- end
181
- end
182
- rescue LoadError
183
- require 'growl'
184
- end
185
- rescue LoadError
186
- turn_off
187
- UI.info "Please install growl_notify or growl gem for Mac OS X notification support and add it to your Gemfile"
188
- end
189
-
190
- # Try to safely load libnotify and turns notifications
191
- # off on load failure.
192
- #
193
- def self.require_libnotify
194
- require 'libnotify'
195
- rescue LoadError
196
- turn_off
197
- UI.info "Please install libnotify gem for Linux notification support and add it to your Gemfile"
198
- end
199
-
200
- # Try to safely load rb-notifu and turns notifications
201
- # off on load failure.
202
- #
203
- def self.require_rbnotifu
204
- require 'rb-notifu'
205
- rescue LoadError
206
- turn_off
207
- UI.info "Please install rb-notifu gem for Windows notification support and add it to your Gemfile"
208
- end
209
-
210
- end
211
- end
1
+ require 'rbconfig'
2
+ require 'pathname'
3
+ require 'guard/ui'
4
+
5
+ module Guard
6
+
7
+ # The notifier class handles cross-platform system notifications that supports:
8
+ #
9
+ # - Growl on Mac OS X
10
+ # - Libnotify on Linux
11
+ # - Notifu on Windows
12
+ #
13
+ module Notifier
14
+
15
+ # Application name as shown in the specific notification settings
16
+ APPLICATION_NAME = "Guard"
17
+
18
+ class << self
19
+
20
+ attr_accessor :growl_library, :gntp
21
+
22
+ # Turn notifications off.
23
+ #
24
+ def turn_off
25
+ ENV["GUARD_NOTIFY"] = 'false'
26
+ end
27
+
28
+ # Turn notifications on. This tries to load the platform
29
+ # specific notification library.
30
+ #
31
+ # @return [Boolean] whether the notification could be enabled.
32
+ #
33
+ def turn_on
34
+ ENV["GUARD_NOTIFY"] = 'true'
35
+ case RbConfig::CONFIG['target_os']
36
+ when /darwin/i
37
+ require_growl
38
+ when /linux/i
39
+ require_libnotify
40
+ when /mswin|mingw/i
41
+ require_rbnotifu
42
+ end
43
+ end
44
+
45
+ # Show a message with the system notification.
46
+ #
47
+ # @see .image_path
48
+ #
49
+ # @param [String] the message to show
50
+ # @option options [Symbol, String] image the image symbol or path to an image
51
+ # @option options [String] title the notification title
52
+ #
53
+ def notify(message, options = { })
54
+ if enabled?
55
+ image = options.delete(:image) || :success
56
+ title = options.delete(:title) || "Guard"
57
+
58
+ case RbConfig::CONFIG['target_os']
59
+ when /darwin/i
60
+ notify_mac(title, message, image, options)
61
+ when /linux/i
62
+ notify_linux(title, message, image, options)
63
+ when /mswin|mingw/i
64
+ notify_windows(title, message, image, options)
65
+ end
66
+ end
67
+ end
68
+
69
+ # Test if the notifications are enabled and available.
70
+ #
71
+ # @return [Boolean] whether the notifications are available
72
+ #
73
+ def enabled?
74
+ ENV["GUARD_NOTIFY"] == 'true'
75
+ end
76
+
77
+ private
78
+
79
+ # Send a message to Growl either with the `growl` gem or the `growl_notify` gem.
80
+ #
81
+ # @param [String] title the notification title
82
+ # @param [String] message the message to show
83
+ # @param [Symbol, String] the image to user
84
+ # @param [Hash] options the growl options
85
+ #
86
+ def notify_mac(title, message, image, options = { })
87
+ require_growl # need for guard-rspec formatter that is called out of guard scope
88
+
89
+ notification = { :title => title, :icon => image_path(image) }.merge(options)
90
+
91
+ case self.growl_library
92
+ when :growl_notify
93
+ notification.delete(:name)
94
+
95
+ GrowlNotify.send_notification({
96
+ :description => message,
97
+ :application_name => APPLICATION_NAME
98
+ }.merge(notification))
99
+
100
+ when :ruby_gntp
101
+ icon = "file://#{ notification.delete(:icon) }"
102
+
103
+ self.gntp.notify({
104
+ :name => [:pending, :success, :failed].include?(image) ? image.to_s : 'notify',
105
+ :text => message,
106
+ :icon => icon
107
+ }.merge(notification))
108
+
109
+ when :growl
110
+ Growl.notify(message, {
111
+ :name => APPLICATION_NAME
112
+ }.merge(notification))
113
+ end
114
+ end
115
+
116
+ # Send a message to libnotify.
117
+ #
118
+ # @param [String] title the notification title
119
+ # @param [String] message the message to show
120
+ # @param [Symbol, String] the image to user
121
+ # @param [Hash] options the libnotify options
122
+ #
123
+ def notify_linux(title, message, image, options = { })
124
+ require_libnotify # need for guard-rspec formatter that is called out of guard scope
125
+
126
+ notification = { :body => message, :summary => title, :icon_path => image_path(image), :transient => true }
127
+ Libnotify.show notification.merge(options)
128
+ end
129
+
130
+ # Send a message to notifu.
131
+ #
132
+ # @param [String] title the notification title
133
+ # @param [String] message the message to show
134
+ # @param [Symbol, String] the image to user
135
+ # @param [Hash] options the notifu options
136
+ #
137
+ def notify_windows(title, message, image, options = { })
138
+ require_rbnotifu # need for guard-rspec formatter that is called out of guard scope
139
+
140
+ notification = { :message => message, :title => title, :type => image_level(image), :time => 3 }
141
+ Notifu.show notification.merge(options)
142
+ end
143
+
144
+ # Get the image path for an image symbol.
145
+ #
146
+ # Known symbols are:
147
+ #
148
+ # - failed
149
+ # - pending
150
+ # - success
151
+ #
152
+ # @param [Symbol] image the image name
153
+ # @return [String] the image path
154
+ #
155
+ def image_path(image)
156
+ images_path = Pathname.new(File.dirname(__FILE__)).join('../../images')
157
+ case image
158
+ when :failed
159
+ images_path.join("failed.png").to_s
160
+ when :pending
161
+ images_path.join("pending.png").to_s
162
+ when :success
163
+ images_path.join("success.png").to_s
164
+ else
165
+ # path given
166
+ image
167
+ end
168
+ end
169
+
170
+ # The notification level type for the given image.
171
+ #
172
+ # @param [Symbol] image the image
173
+ # @return [Symbol] the level
174
+ #
175
+ def image_level(image)
176
+ case image
177
+ when :failed
178
+ :error
179
+ when :pending
180
+ :warn
181
+ when :success
182
+ :info
183
+ else
184
+ :info
185
+ end
186
+ end
187
+
188
+ # Try to safely load growl and turns notifications off on load failure.
189
+ # The Guard notifier knows three different library to handle sending
190
+ # Growl messages and tries to loading them in the given order:
191
+ #
192
+ # - [Growl Notify](https://github.com/scottdavis/growl_notify)
193
+ # - [Ruby GNTP](https://github.com/snaka/ruby_gntp)
194
+ # - [Growl](https://github.com/visionmedia/growl)
195
+ #
196
+ # On successful loading of any of the libraries, the active library name is
197
+ # accessible through `.growl_library`.
198
+ #
199
+ def require_growl
200
+ self.growl_library = try_growl_notify || try_ruby_gntp || try_growl
201
+
202
+ unless self.growl_library
203
+ turn_off
204
+ UI.info "Please install growl_notify or growl gem for Mac OS X notification support and add it to your Gemfile"
205
+ end
206
+ end
207
+
208
+ # Try to load the `growl_notify` gem.
209
+ #
210
+ # @return [Symbol, nil] A symbol with the name of the loaded library
211
+ #
212
+ def try_growl_notify
213
+ require 'growl_notify'
214
+
215
+ begin
216
+ if GrowlNotify.application_name != APPLICATION_NAME
217
+ GrowlNotify.config do |c|
218
+ c.notifications = c.default_notifications = [APPLICATION_NAME]
219
+ c.application_name = c.notifications.first
220
+ end
221
+ end
222
+
223
+ rescue ::GrowlNotify::GrowlNotFound
224
+ turn_off
225
+ UI.info "Please install Growl from http://growl.info"
226
+ end
227
+
228
+ :growl_notify
229
+
230
+ rescue LoadError
231
+ end
232
+
233
+ # Try to load the `ruby_gntp` gem and register the available
234
+ # notification channels.
235
+ #
236
+ # @return [Symbol, nil] A symbol with the name of the loaded library
237
+ #
238
+ def try_ruby_gntp
239
+ require 'ruby_gntp'
240
+
241
+ self.gntp = GNTP.new(APPLICATION_NAME)
242
+ self.gntp.register(:notifications => [
243
+ { :name => 'notify', :enabled => true },
244
+ { :name => 'failed', :enabled => true },
245
+ { :name => 'pending', :enabled => true },
246
+ { :name => 'success', :enabled => true }
247
+ ])
248
+
249
+ :ruby_gntp
250
+
251
+ rescue LoadError
252
+ end
253
+
254
+ # Try to load the `growl_notify` gem.
255
+ #
256
+ # @return [Symbol, nil] A symbol with the name of the loaded library
257
+ #
258
+ def try_growl
259
+ require 'growl'
260
+
261
+ :growl
262
+
263
+ rescue LoadError
264
+ end
265
+
266
+ # Try to safely load libnotify and turns notifications
267
+ # off on load failure.
268
+ #
269
+ def require_libnotify
270
+ require 'libnotify'
271
+
272
+ rescue LoadError
273
+ turn_off
274
+ UI.info "Please install libnotify gem for Linux notification support and add it to your Gemfile"
275
+ end
276
+
277
+ # Try to safely load rb-notifu and turns notifications
278
+ # off on load failure.
279
+ #
280
+ def require_rbnotifu
281
+ require 'rb-notifu'
282
+
283
+ rescue LoadError
284
+ turn_off
285
+ UI.info "Please install rb-notifu gem for Windows notification support and add it to your Gemfile"
286
+ end
287
+
288
+ end
289
+ end
290
+ end