rubyrakegrowlrakefilegrowlnotify

Adding Growl notifications after Rake tasks are finished


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


Solution

  • 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