Skip to content

Commit

Permalink
The updated Gem supporting the different notifiers and their binaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wouter de Vos committed Aug 14, 2012
1 parent 35f5d2b commit e39c5ef
Show file tree
Hide file tree
Showing 20 changed files with 379 additions and 212 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ xcuserdata
Ruby/*.zip
Ruby/*.gem
Ruby/vendor
.rvmrc
Binary file removed Failed.icns
Binary file not shown.
10 changes: 6 additions & 4 deletions Ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
PATH
remote: .
specs:
terminal-notifier (1.4.1)
terminal-notifier-guard (1.4.2)

GEM
remote: http://rubygems.org/
specs:
bacon (1.1.0)
metaclass (0.0.1)
mocha (0.11.4)
mocha (0.12.3)
metaclass (~> 0.0.1)
mocha-on-bacon (0.2.0)
mocha-on-bacon (0.2.1)
mocha (>= 0.9.8)
rake (0.9.2.2)

PLATFORMS
ruby
Expand All @@ -20,4 +21,5 @@ DEPENDENCIES
bacon
mocha
mocha-on-bacon
terminal-notifier!
rake
terminal-notifier-guard!
23 changes: 16 additions & 7 deletions Ruby/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ A simple Ruby wrapper around the [`terminal-notifier`][HOMEPAGE] command-line
tool, which allows you to send User Notifications to the Notification Center on
Mac OS X 10.8, or higher.

This version has 4 different `terminal-notifiers` included for each status that
[Guard][GUARD] supports:

1. Failed
2. Notify
3. Pending
4. Success

And each one with their own icon representing it's status.


## Installation

```
$ gem install terminal-notifier
$ gem install terminal-notifier-guard
```


Expand All @@ -35,12 +45,11 @@ TerminalNotifier.list

## License

All the works are available under the MIT license. **Except** for
‘Terminal.icns’, which is a copy of Apple’s Terminal.app icon and as such is
copyright of Apple.
All the works are available under the MIT license.

See [LICENSE][LICENSE] for details.

[HOMEPAGE]: https://github.com/alloy/terminal-notifier
[README]: https://github.com/alloy/terminal-notifier/blob/master/README.markdown
[LICENSE]: https://github.com/alloy/terminal-notifier/blob/master/Ruby/LICENSE
[HOMEPAGE]: https://github.com/Springest/terminal-notifier-guard
[GUARD]: https://github.com/guard/guard
[README]: https://github.com/Springest/terminal-notifier-guard/blob/master/README.markdown
[LICENSE]: https://github.com/Springest/terminal-notifier-guard/blob/master/Ruby/LICENSE
40 changes: 26 additions & 14 deletions Ruby/Rakefile
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
def version
@version ||= begin
plist = File.expand_path('../../Terminal Notifier/Terminal Notifier-Info.plist', __FILE__)
plist = File.expand_path('../../Terminal Notifiers/notify/Terminal Notifier/Terminal Notifier-Info.plist', __FILE__)
`/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' '#{plist}'`.strip
end
end

def filename
"terminal-notifier_#{version}"
def types
%w(notify success pending failed)
end

def zipfile
"#{filename}.zip"
def type_file_name(type)
"tn_#{type}-#{version}"
end

def filenames
types.map{|type| type_file_name(type)}
end

def zipfiles
filenames.map{|n| "#{n}.zip"}
end

task :clean do
rm zipfile
rm zipfiles
rm_rf "vendor"
end

task :update_build do
unless File.exist?(zipfile)
sh "curl -O 'http://cloud.github.com/downloads/alloy/terminal-notifier/terminal-notifier_#{version}.zip'"
end

rm_rf "vendor"
mkdir "vendor"

sh "unzip -o -d vendor #{zipfile}"
mv "vendor/#{filename}", "vendor/terminal-notifier"
zipfiles.each do |zipfile|
unless File.exist?(zipfile)
sh "curl -O 'http://cloud.github.com/downloads/Springest/terminal-notifier-guard/#{zipfile}'"
end

sh "unzip -o -d vendor #{zipfile}"
end
types.each do |type|
mv "vendor/#{type_file_name(type)}.app", "vendor/terminal-notifier-#{type}.app"
end
end

desc "Build gem"
task :gem => :update_build do
sh "gem build terminal-notifier.gemspec"
sh "gem build terminal-notifier-guard.gemspec"
end

desc "Run specs"
task :spec do
sh "bundle exec ruby spec/terminal-notifier_spec.rb"
sh "bundle exec ruby spec/terminal-notifier-guard_spec.rb"
end

task :default => :spec
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ if $0 == __FILE__
$:.unshift File.expand_path('../../lib', __FILE__)
end

require 'terminal-notifier'
require 'terminal-notifier-guard'

exec TerminalNotifier::BIN_PATH, *ARGV
exec TerminalNotifier::Guard::Failed::BIN_PATH, *ARGV
9 changes: 9 additions & 0 deletions Ruby/bin/terminal-notifier-notify
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby

if $0 == __FILE__
$:.unshift File.expand_path('../../lib', __FILE__)
end

require 'terminal-notifier-guard'

exec TerminalNotifier::Guard::Notify::BIN_PATH, *ARGV
9 changes: 9 additions & 0 deletions Ruby/bin/terminal-notifier-pending
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby

if $0 == __FILE__
$:.unshift File.expand_path('../../lib', __FILE__)
end

require 'terminal-notifier-guard'

exec TerminalNotifier::Guard::Pending::BIN_PATH, *ARGV
9 changes: 9 additions & 0 deletions Ruby/bin/terminal-notifier-success
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby

if $0 == __FILE__
$:.unshift File.expand_path('../../lib', __FILE__)
end

require 'terminal-notifier-guard'

exec TerminalNotifier::Guard::Success::BIN_PATH, *ARGV
127 changes: 127 additions & 0 deletions Ruby/lib/terminal-notifier-guard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
%w(failed notify pending success).each do |type|
require File.expand_path("../terminal_notifier/guard/#{type}", __FILE__)
end

module TerminalNotifier
module Guard
include TerminalNotifier::Guard::Notify
include TerminalNotifier::Guard::Success
include TerminalNotifier::Guard::Failed
include TerminalNotifier::Guard::Pending

# Returns wether or not the current platform is Mac OS X 10.8, or higher.
def self.available?
if @available.nil?
@available = `uname`.strip == 'Darwin' && `sw_vers -productVersion`.strip >= '10.8'
end
@available
end

def self.execute(verbose, options)
if available?
case options[:type]
when :failed
bin_path = TerminalNotifier::Guard::Failed::BIN_PATH
when :success
bin_path = TerminalNotifier::Guard::Success::BIN_PATH
when :pending
bin_path = TerminalNotifier::Guard::Pending::BIN_PATH
else
bin_path = TerminalNotifier::Guard::Notify::BIN_PATH
end
options.delete(:type) if options[:type]

command = [bin_path, *options.map { |k,v| ["-#{k}", v.to_s] }.flatten]
if RUBY_VERSION < '1.9'
require 'shellwords'
command = Shellwords.shelljoin(command)
end
result = ''
IO.popen(command) do |stdout|
output = stdout.read
STDOUT.print output if verbose
result << output
end
result
else
raise "terminal-notifier is only supported on Mac OS X 10.8, or higher."
end
end

# Sends a User Notification and returns wether or not it was a success.
#
# The available options are `:title`, `:group`, `:activate`, `:open`, and
# `:execute`. For a description of each option see:
#
# https://github.com/alloy/terminal-notifier/blob/master/README.markdown
#
# Examples are:
#
# TerminalNotifier.notify('Hello World')
# TerminalNotifier.notify('Hello World', :title => 'Ruby')
# TerminalNotifier.notify('Hello World', :group => Process.pid)
# TerminalNotifier.notify('Hello World', :activate => 'com.apple.Safari')
# TerminalNotifier.notify('Hello World', :open => 'http://twitter.com/alloy')
# TerminalNotifier.notify('Hello World', :execute => 'say "OMG"')
#
# Raises if not supported on the current platform.
def notify(message, options = {}, verbose = false)
TerminalNotifier::Guard.execute(verbose, options.merge(:message => message))
$?.success?
end
module_function :notify

def failed(message, options = {}, verbose = false)
TerminalNotifier::Guard.execute(verbose, options.merge(:message => message, :type => :failed))
$?.success?
end
module_function :failed

def pending(message, options = {}, verbose = false)
TerminalNotifier::Guard.execute(verbose, options.merge(:message => message, :type => :pending))
$?.success?
end
module_function :pending

def success(message, options = {}, verbose = false)
TerminalNotifier::Guard.execute(verbose, options.merge(:message => message, :type => :success))
$?.success?
end
module_function :success

# Removes a notification that was previously sent with the specified
# ‘group’ ID, if one exists.
#
# If no ‘group’ ID is given, all notifications are removed.
def remove(group = 'ALL', verbose = false)
TerminalNotifier::Guard.execute(verbose, :remove => group)
$?.success?
end
module_function :remove

LIST_FIELDS = [:group, :title, :subtitle, :message, :delivered_at].freeze

# If a ‘group’ ID is given, and a notification for that group exists,
# returns a hash with details about the notification.
#
# If no ‘group’ ID is given, an array of hashes describing all
# notifications.
#
# If no information is available this will return `nil`.
def list(group = 'ALL', verbose = false)
output = TerminalNotifier::Guard.execute(verbose, :list => group)
return if output.strip.empty?

require 'time'
notifications = output.split("\n")[1..-1].map do |line|
LIST_FIELDS.zip(line.split("\t")).inject({}) do |hash, (key, value)|
hash[key] = key == :delivered_at ? Time.parse(value) : (value unless value == '(null)')
hash
end
end

group == 'ALL' ? notifications : notifications.first
end
module_function :list
end
end
Loading

0 comments on commit e39c5ef

Please sign in to comment.