views:

95

answers:

1

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 --

+1  A: 

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
Alkaline
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
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).
Alkaline
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