master/lib/guard/notifier.rb

133 lines
3.6 KiB
Ruby
Raw Normal View History

require 'rbconfig'
2010-10-03 21:00:33 +00:00
require 'pathname'
require 'guard/ui'
2010-10-03 21:00:33 +00:00
module Guard
module Notifier
def self.turn_off
ENV["GUARD_NOTIFY"] = 'false'
end
def self.turn_on
ENV["GUARD_NOTIFY"] = 'true'
case RbConfig::CONFIG['target_os']
when /darwin/i
require_growl
when /linux/i
require_libnotify
2011-05-20 23:19:42 +00:00
when /mswin|mingw/i
require_rbnotifu
end
end
2010-10-03 21:00:33 +00:00
def self.notify(message, options = {})
if enabled?
image = options.delete(:image) || :success
title = options.delete(:title) || "Guard"
case RbConfig::CONFIG['target_os']
when /darwin/i
notify_mac(title, message, image, options)
when /linux/i
notify_linux(title, message, image, options)
2011-05-20 23:19:42 +00:00
when /mswin|mingw/i
notify_windows(title, message, image, options)
2010-10-03 21:00:33 +00:00
end
end
end
def self.enabled?
ENV["GUARD_NOTIFY"] == 'true'
end
2010-10-03 21:00:33 +00:00
private
def self.notify_mac(title, message, image, options)
require_growl # need for guard-rspec formatter that is called out of guard scope
2011-06-28 20:15:14 +00:00
default_options = { :title => title, :icon => image_path(image), :name => "Guard" }
2011-06-28 20:15:14 +00:00
default_options.merge!(options)
if defined?(GrowlNotify)
default_options[:description] = message
default_options[:application_name] = default_options.delete(:name)
GrowlNotify.send_notification(default_options) if enabled?
else
Growl.notify message, default_options.merge(options) if enabled?
end
end
def self.notify_linux(title, message, image, options)
require_libnotify # need for guard-rspec formatter that is called out of guard scope
default_options = { :body => message, :summary => title, :icon_path => image_path(image) }
Libnotify.show default_options.merge(options) if enabled?
end
def self.notify_windows(title, message, image, options)
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 }
Notifu.show default_options.merge(options) if enabled?
end
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-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
def self.require_growl
2011-06-28 20:15:14 +00:00
begin
require 'growl_notify'
GrowlNotify.config do |c|
c.notifications = c.default_notifications = [ "Guard" ]
c.application_name = c.notifications.first
end
rescue LoadError
require 'growl'
end
rescue LoadError
turn_off
2011-06-28 20:15:14 +00:00
UI.info "Please install growl or growl_notify gem for Mac OS X notification support and add it to your Gemfile"
end
def self.require_libnotify
require 'libnotify'
rescue LoadError
turn_off
UI.info "Please install libnotify gem for Linux notification support and add it to your Gemfile"
end
2011-05-20 23:19:42 +00:00
def self.require_rbnotifu
require 'rb-notifu'
2011-05-20 23:19:42 +00:00
rescue LoadError
turn_off
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
2010-10-03 21:00:33 +00:00
end
end