2

Is there a way to add Growl notifications to the end of all Rake tasks?

I initially thought of creating a task that Growls, and adding it as a dependency to tasks I want alerts from, but realized the dependencies get run before the task begins. Is there a way to add tasks to be run after certain Rake tasks are finished?

It'd be really useful so I don't have to sit there waiting for long tasks.

** update 8/17/2010 **

Here is the solution for doing it with growlnotify...put this in your Rakefile:

def growl(message)
  growlnotify = `which growlnotify`.chomp
  system %(#{growlnotify} -sm #{message})
end

task_names = Rake.application.top_level_tasks
task_names.each do |name|
  Rake.application[name].enhance { growl "'Task #{name} completed (#{Time.now})'" }
end

-- Credit to Alkaline - see his solution for using ruby-growl below --

funkymunky
  • 1,811
  • 3
  • 20
  • 22

2 Answers2

2

Here's how you can implicitly invoke a growl action for the calling (top level) tasks

require 'rake'
require 'ruby-growl'

task :task1 do puts "Doing task 1"; sleep 1; end
task :task2 do puts "Doing task 2"; sleep 1; end
task :default => [:task1, :task2]

# Add a growl action to all top level tasks
task_names = Rake.application.top_level_tasks
task_names.each do |name|
  Rake.application[name].enhance {growl(name)}
end

def growl(name)
  g = Growl.new "localhost", "ruby-growl", ["ruby-growl Notification"]
  g.notify "ruby-growl Notification", "My Project", "Task #{name} completed"
end
Bernard
  • 16,149
  • 12
  • 63
  • 66
  • But this means I have to add every other task as a dependency for :default right? Is there no easy way to emulate this behavior without the default action running every other task I have on there? – funkymunky Aug 16 '10 at 17:57
  • Your question wasn't clear to me. I've updated the answer for implicitly calling a growl action at the end of every top level task (that's the task you request when calling rake). – Bernard Aug 17 '10 at 01:20
  • 1
    Thanks. I ended up using growlnotify instead of ruby-growl, but the tip here about the top_level_tasks and #enhance method helped a lot! – funkymunky Aug 17 '10 at 20:42
  • I went for growlnotify too with https://github.com/visionmedia/growl. Couldn't get ruby-growl to connect. – vaughan Mar 15 '12 at 05:03
0

The gem rakegrowl does this very simply. All you have to do is install the rakegrowl gem and alias rake to rake -rubygems -r rakegrowl in your .bashrc.

Eliza Brock Marcum
  • 1,570
  • 1
  • 14
  • 25