2010-10-17 19:42:40 +00:00
|
|
|
require 'rbconfig'
|
2010-10-03 21:00:33 +00:00
|
|
|
require 'pathname'
|
|
|
|
|
|
|
|
module Guard
|
|
|
|
module Notifier
|
2011-05-10 19:22:25 +00:00
|
|
|
@enabled = false
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-07 06:43:21 +00:00
|
|
|
@enable = false
|
|
|
|
@library = false
|
|
|
|
|
2011-04-10 20:32:29 +00:00
|
|
|
def self.turn_off
|
2011-05-05 09:05:58 +00:00
|
|
|
@enable = false
|
2011-04-10 20:32:29 +00:00
|
|
|
end
|
|
|
|
|
2011-05-06 17:09:00 +00:00
|
|
|
def self.turn_on
|
2011-05-05 09:05:58 +00:00
|
|
|
@enable = true
|
2011-05-07 06:43:21 +00:00
|
|
|
|
|
|
|
return true if @library #only do require_ once.
|
|
|
|
|
2011-05-06 21:19:31 +00:00
|
|
|
case Config::CONFIG['target_os']
|
|
|
|
when /darwin/i
|
|
|
|
require_growl
|
|
|
|
when /linux/i
|
|
|
|
require_libnotify
|
|
|
|
end
|
2011-05-06 17:09:00 +00:00
|
|
|
end
|
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
def self.should_send?
|
2011-05-07 06:43:21 +00:00
|
|
|
@enable && !!installed_lib
|
2011-04-10 20:32:29 +00:00
|
|
|
end
|
|
|
|
|
2011-05-07 06:43:21 +00:00
|
|
|
def self.disabled?
|
|
|
|
not should_send?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.installed_lib
|
|
|
|
@library
|
|
|
|
end
|
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
def self.notify(message, options = {})
|
2011-05-05 09:05:58 +00:00
|
|
|
if should_send?()
|
2010-10-03 21:00:33 +00:00
|
|
|
image = options[:image] || :success
|
|
|
|
title = options[:title] || "Guard"
|
2011-05-07 06:43:21 +00:00
|
|
|
case @library
|
|
|
|
when :growl
|
2010-10-19 19:49:17 +00:00
|
|
|
if growl_installed?
|
|
|
|
Growl.notify message, :title => title, :icon => image_path(image), :name => "Guard"
|
|
|
|
end
|
2011-05-07 06:43:21 +00:00
|
|
|
when :libnotify
|
2010-10-19 19:49:17 +00:00
|
|
|
if libnotify_installed?
|
|
|
|
Libnotify.show :body => message, :summary => title, :icon_path => image_path(image)
|
|
|
|
end
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-06 21:19:31 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
private
|
2011-04-10 20:32:29 +00:00
|
|
|
|
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-05-06 21:19:31 +00:00
|
|
|
def self.require_growl
|
2010-10-19 19:49:17 +00:00
|
|
|
@installed ||= begin
|
|
|
|
require 'growl'
|
2011-05-07 06:43:21 +00:00
|
|
|
@library = :growl
|
2010-10-19 19:49:17 +00:00
|
|
|
rescue LoadError
|
2010-10-25 19:36:45 +00:00
|
|
|
UI.info "Please install growl gem for Mac OS X notification support and add it to your Gemfile"
|
2011-05-07 06:43:21 +00:00
|
|
|
@enable = false
|
2010-10-19 19:49:17 +00:00
|
|
|
end
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-06 21:19:31 +00:00
|
|
|
def self.require_libnotify
|
2010-10-19 19:49:17 +00:00
|
|
|
@installed ||= begin
|
|
|
|
require 'libnotify'
|
2011-05-07 06:43:21 +00:00
|
|
|
@library = :libnotify
|
2010-10-19 19:49:17 +00:00
|
|
|
rescue LoadError
|
2010-10-25 19:36:45 +00:00
|
|
|
UI.info "Please install libnotify gem for Linux notification support and add it to your Gemfile"
|
2011-05-07 06:43:21 +00:00
|
|
|
@enable = false
|
2010-10-19 19:49:17 +00:00
|
|
|
end
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
|
|
|
end
|