2010-10-17 19:42:40 +00:00
|
|
|
require 'rbconfig'
|
2010-10-03 21:00:33 +00:00
|
|
|
require 'pathname'
|
2011-05-23 20:35:37 +00:00
|
|
|
require 'guard/ui'
|
2010-10-03 21:00:33 +00:00
|
|
|
|
|
|
|
module Guard
|
2011-09-20 11:06:35 +00:00
|
|
|
|
|
|
|
# The notifier class handles cross-platform system notifications that supports:
|
2011-09-20 23:30:35 +00:00
|
|
|
#
|
2011-09-20 11:06:35 +00:00
|
|
|
# - Growl on Mac OS X
|
|
|
|
# - Libnotify on Linux
|
|
|
|
# - Notifu on Windows
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
module Notifier
|
2011-09-20 11:06:35 +00:00
|
|
|
|
|
|
|
# Application name as shown in the specific notification settings
|
2011-07-01 15:52:24 +00:00
|
|
|
APPLICATION_NAME = "Guard"
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Turn notifications off.
|
2011-09-20 11:06:35 +00:00
|
|
|
#
|
2011-04-10 20:32:29 +00:00
|
|
|
def self.turn_off
|
2011-05-13 19:48:30 +00:00
|
|
|
ENV["GUARD_NOTIFY"] = 'false'
|
2011-04-10 20:32:29 +00:00
|
|
|
end
|
|
|
|
|
2011-09-20 11:06:35 +00:00
|
|
|
# Turn notifications on. This tries to load the platform
|
|
|
|
# specific notification library.
|
|
|
|
#
|
|
|
|
# @return [Boolean] whether the notification could be enabled.
|
|
|
|
#
|
2011-05-06 17:09:00 +00:00
|
|
|
def self.turn_on
|
2011-05-13 19:48:30 +00:00
|
|
|
ENV["GUARD_NOTIFY"] = 'true'
|
2011-06-16 11:14:51 +00:00
|
|
|
case RbConfig::CONFIG['target_os']
|
2011-05-06 21:19:31 +00:00
|
|
|
when /darwin/i
|
|
|
|
require_growl
|
|
|
|
when /linux/i
|
|
|
|
require_libnotify
|
2011-05-20 23:19:42 +00:00
|
|
|
when /mswin|mingw/i
|
|
|
|
require_rbnotifu
|
2011-05-06 21:19:31 +00:00
|
|
|
end
|
2011-05-06 17:09:00 +00:00
|
|
|
end
|
|
|
|
|
2011-09-20 11:06:35 +00:00
|
|
|
# Show a message with the system notification.
|
|
|
|
#
|
|
|
|
# @see .image_path
|
|
|
|
#
|
|
|
|
# @param [String] the message to show
|
|
|
|
# @option options [Symbol, String] image the image symbol or path to an image
|
|
|
|
# @option options [String] title the notification title
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def self.notify(message, options = {})
|
2011-05-10 19:22:25 +00:00
|
|
|
if enabled?
|
2011-04-29 03:22:41 +00:00
|
|
|
image = options.delete(:image) || :success
|
2011-05-30 14:45:15 +00:00
|
|
|
title = options.delete(:title) || "Guard"
|
|
|
|
|
2011-06-16 11:14:51 +00:00
|
|
|
case RbConfig::CONFIG['target_os']
|
2010-10-17 19:42:40 +00:00
|
|
|
when /darwin/i
|
2011-05-30 14:45:15 +00:00
|
|
|
notify_mac(title, message, image, options)
|
2010-10-17 19:42:40 +00:00
|
|
|
when /linux/i
|
2011-05-30 14:45:15 +00:00
|
|
|
notify_linux(title, message, image, options)
|
2011-05-20 23:19:42 +00:00
|
|
|
when /mswin|mingw/i
|
2011-05-30 14:45:15 +00:00
|
|
|
notify_windows(title, message, image, options)
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 11:06:35 +00:00
|
|
|
# Test if the notifications are enabled and available.
|
|
|
|
#
|
|
|
|
# @return [Boolean] whether the notifications are available
|
|
|
|
#
|
2011-05-10 19:22:25 +00:00
|
|
|
def self.enabled?
|
2011-05-13 19:48:30 +00:00
|
|
|
ENV["GUARD_NOTIFY"] == 'true'
|
2011-05-06 21:19:31 +00:00
|
|
|
end
|
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
private
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Send a message to Growl either with the `growl` gem or the `growl_notify` gem.
|
2011-09-20 11:06:35 +00:00
|
|
|
#
|
|
|
|
# @param [String] title the notification title
|
|
|
|
# @param [String] message the message to show
|
|
|
|
# @param [Symbol, String] the image to user
|
|
|
|
# @param [Hash] options the growl options
|
|
|
|
#
|
2011-09-23 09:01:52 +00:00
|
|
|
def self.notify_mac(title, message, image, options = {})
|
2011-05-30 14:45:15 +00:00
|
|
|
require_growl # need for guard-rspec formatter that is called out of guard scope
|
2011-06-28 20:15:14 +00:00
|
|
|
|
2011-08-17 12:27:40 +00:00
|
|
|
default_options = { :title => title, :icon => image_path(image), :name => APPLICATION_NAME }
|
|
|
|
default_options.merge!(options)
|
2011-06-28 20:15:14 +00:00
|
|
|
|
2011-08-17 12:27:40 +00:00
|
|
|
if defined?(GrowlNotify)
|
|
|
|
default_options[:description] = message
|
|
|
|
default_options[:application_name] = APPLICATION_NAME
|
|
|
|
default_options.delete(:name)
|
|
|
|
|
|
|
|
GrowlNotify.send_notification(default_options) if enabled?
|
|
|
|
else
|
|
|
|
Growl.notify message, default_options.merge(options) if enabled?
|
|
|
|
end
|
2011-05-30 14:45:15 +00:00
|
|
|
end
|
|
|
|
|
2011-09-20 11:06:35 +00:00
|
|
|
# Send a message to libnotify.
|
|
|
|
#
|
|
|
|
# @param [String] title the notification title
|
|
|
|
# @param [String] message the message to show
|
|
|
|
# @param [Symbol, String] the image to user
|
|
|
|
# @param [Hash] options the libnotify options
|
|
|
|
#
|
2011-09-23 09:01:52 +00:00
|
|
|
def self.notify_linux(title, message, image, options = {})
|
2011-05-30 14:45:15 +00:00
|
|
|
require_libnotify # need for guard-rspec formatter that is called out of guard scope
|
2011-08-11 09:24:14 +00:00
|
|
|
default_options = { :body => message, :summary => title, :icon_path => image_path(image), :transient => true }
|
2011-06-07 15:58:02 +00:00
|
|
|
Libnotify.show default_options.merge(options) if enabled?
|
2011-05-30 14:45:15 +00:00
|
|
|
end
|
|
|
|
|
2011-09-20 11:06:35 +00:00
|
|
|
# Send a message to notifu.
|
|
|
|
#
|
|
|
|
# @param [String] title the notification title
|
|
|
|
# @param [String] message the message to show
|
|
|
|
# @param [Symbol, String] the image to user
|
|
|
|
# @param [Hash] options the notifu options
|
|
|
|
#
|
2011-09-23 09:01:52 +00:00
|
|
|
def self.notify_windows(title, message, image, options = {})
|
2011-05-30 14:45:15 +00:00
|
|
|
require_rbnotifu # need for guard-rspec formatter that is called out of guard scope
|
|
|
|
default_options = { :message => message, :title => title, :type => image_level(image), :time => 3 }
|
2011-06-07 15:58:02 +00:00
|
|
|
Notifu.show default_options.merge(options) if enabled?
|
2011-05-30 14:45:15 +00:00
|
|
|
end
|
|
|
|
|
2011-09-20 11:06:35 +00:00
|
|
|
# Get the image path for an image symbol.
|
|
|
|
#
|
|
|
|
# Known symbols are:
|
2011-09-20 23:30:35 +00:00
|
|
|
#
|
2011-09-20 11:06:35 +00:00
|
|
|
# - failed
|
|
|
|
# - pending
|
|
|
|
# - success
|
|
|
|
#
|
|
|
|
# @param [Symbol] image the image name
|
|
|
|
# @return [String] the image path
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def self.image_path(image)
|
|
|
|
images_path = Pathname.new(File.dirname(__FILE__)).join('../../images')
|
|
|
|
case image
|
|
|
|
when :failed
|
|
|
|
images_path.join("failed.png").to_s
|
|
|
|
when :pending
|
|
|
|
images_path.join("pending.png").to_s
|
|
|
|
when :success
|
|
|
|
images_path.join("success.png").to_s
|
|
|
|
else
|
|
|
|
# path given
|
|
|
|
image
|
|
|
|
end
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 11:06:35 +00:00
|
|
|
# The notification level type for the given image.
|
|
|
|
#
|
|
|
|
# @param [Symbol] image the image
|
|
|
|
# @return [Symbol] the level
|
|
|
|
#
|
2011-05-20 23:19:42 +00:00
|
|
|
def self.image_level(image)
|
|
|
|
case image
|
|
|
|
when :failed
|
|
|
|
:error
|
|
|
|
when :pending
|
|
|
|
:warn
|
|
|
|
when :success
|
|
|
|
:info
|
|
|
|
else
|
|
|
|
:info
|
|
|
|
end
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 11:06:35 +00:00
|
|
|
# Try to safely load growl and turns notifications
|
|
|
|
# off on load failure.
|
|
|
|
#
|
2011-05-06 21:19:31 +00:00
|
|
|
def self.require_growl
|
2011-08-17 12:27:40 +00:00
|
|
|
begin
|
|
|
|
require 'growl_notify'
|
|
|
|
|
|
|
|
if GrowlNotify.application_name != APPLICATION_NAME
|
|
|
|
GrowlNotify.config do |c|
|
|
|
|
c.notifications = c.default_notifications = [ APPLICATION_NAME ]
|
|
|
|
c.application_name = c.notifications.first
|
|
|
|
end
|
2011-07-01 15:52:24 +00:00
|
|
|
end
|
2011-08-17 12:27:40 +00:00
|
|
|
rescue LoadError
|
|
|
|
require 'growl'
|
2011-10-05 05:10:48 +00:00
|
|
|
rescue ::GrowlNotify::GrowlNotFound
|
|
|
|
turn_off
|
|
|
|
UI.info "Please install Growl from http://growl.info"
|
2011-06-28 20:15:14 +00:00
|
|
|
end
|
2011-05-06 21:19:31 +00:00
|
|
|
rescue LoadError
|
2011-05-08 19:56:46 +00:00
|
|
|
turn_off
|
2011-09-03 19:15:41 +00:00
|
|
|
UI.info "Please install growl_notify or growl gem for Mac OS X notification support and add it to your Gemfile"
|
2010-10-19 19:49:17 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 11:06:35 +00:00
|
|
|
# Try to safely load libnotify and turns notifications
|
|
|
|
# off on load failure.
|
|
|
|
#
|
2011-05-06 21:19:31 +00:00
|
|
|
def self.require_libnotify
|
|
|
|
require 'libnotify'
|
|
|
|
rescue LoadError
|
2011-05-08 19:56:46 +00:00
|
|
|
turn_off
|
2011-05-06 21:19:31 +00:00
|
|
|
UI.info "Please install libnotify gem for Linux notification support and add it to your Gemfile"
|
2010-10-19 19:49:17 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 11:06:35 +00:00
|
|
|
# Try to safely load rb-notifu and turns notifications
|
|
|
|
# off on load failure.
|
|
|
|
#
|
2011-05-20 23:19:42 +00:00
|
|
|
def self.require_rbnotifu
|
2011-05-21 21:47:02 +00:00
|
|
|
require 'rb-notifu'
|
2011-05-20 23:19:42 +00:00
|
|
|
rescue LoadError
|
|
|
|
turn_off
|
2011-05-21 21:47:02 +00:00
|
|
|
UI.info "Please install rb-notifu gem for Windows notification support and add it to your Gemfile"
|
2011-05-20 23:19:42 +00:00
|
|
|
end
|
2011-09-20 11:06:35 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-05-27 15:56:18 +00:00
|
|
|
end
|