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-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-05-06 17:09:00 +00:00
|
|
|
def self.turn_on
|
2011-05-13 19:48:30 +00:00
|
|
|
ENV["GUARD_NOTIFY"] = 'true'
|
2011-05-06 21:19:31 +00:00
|
|
|
case Config::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
|
2011-05-06 21:19:31 +00:00
|
|
|
end
|
2011-05-06 17:09:00 +00:00
|
|
|
end
|
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
def self.notify(message, options = {})
|
2011-05-10 19:22:25 +00:00
|
|
|
if enabled?
|
2010-10-03 21:00:33 +00:00
|
|
|
image = options[:image] || :success
|
|
|
|
title = options[:title] || "Guard"
|
2010-10-17 19:42:40 +00:00
|
|
|
case Config::CONFIG['target_os']
|
|
|
|
when /darwin/i
|
2011-05-13 20:24:49 +00:00
|
|
|
require_growl # need for guard-rspec formatter that is called out of guard scope
|
2011-05-06 21:19:31 +00:00
|
|
|
Growl.notify message, :title => title, :icon => image_path(image), :name => "Guard"
|
2010-10-17 19:42:40 +00:00
|
|
|
when /linux/i
|
2011-05-13 20:24:49 +00:00
|
|
|
require_libnotify # need for guard-rspec formatter that is called out of guard scope
|
2011-05-06 21:19:31 +00:00
|
|
|
Libnotify.show :body => message, :summary => title, :icon_path => image_path(image)
|
2011-05-20 23:19:42 +00:00
|
|
|
when /mswin|mingw/i
|
|
|
|
require_rbnotifu
|
2011-05-21 21:47:02 +00:00
|
|
|
RbNotifu.show :message => message, :title => title, :type => image_level(image), :display => 3000
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
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
|
|
|
|
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-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-05-06 21:19:31 +00:00
|
|
|
def self.require_growl
|
|
|
|
require 'growl'
|
|
|
|
rescue LoadError
|
2011-05-08 19:56:46 +00:00
|
|
|
turn_off
|
2011-05-06 21:19:31 +00:00
|
|
|
UI.info "Please install 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-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-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
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
|
|
|
end
|