You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by as...@apache.org on 2008/08/11 21:02:01 UTC

svn commit: r684875 - /incubator/buildr/trunk/lib/buildr/core/application.rb

Author: assaf
Date: Mon Aug 11 12:02:00 2008
New Revision: 684875

URL: http://svn.apache.org/viewvc?rev=684875&view=rev
Log:
Another attempt at notification, still not exposing an API but makes it easier to write on_complete/on_failure hooks.

Modified:
    incubator/buildr/trunk/lib/buildr/core/application.rb

Modified: incubator/buildr/trunk/lib/buildr/core/application.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/core/application.rb?rev=684875&r1=684874&r2=684875&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/core/application.rb (original)
+++ incubator/buildr/trunk/lib/buildr/core/application.rb Mon Aug 11 12:02:00 2008
@@ -180,7 +180,8 @@
         task('buildr:initialize').invoke
         top_level
       end
-      @on_completion.each { |block| block.call }
+      title, message = 'Your build has completed', "#{Dir.pwd}\nbuildr #{@top_level_tasks.join(' ')}"
+      @on_completion.each { |block| block.call(title, message) rescue nil }
     end
 
     # Load artifact specs from the build.yaml file, making them available 
@@ -330,7 +331,8 @@
         # Exit silently
         exit(1)
       rescue Exception => ex
-        @on_failure.each { |block| block.call(ex) }
+        title, message = 'Your build failed with an error', "#{Dir.pwd}:\n#{ex.message}"
+        @on_failure.each { |block| block.call(title, message, ex) rescue nil }
         # Exit with error message
         $stderr.puts "buildr aborted!"
         $stderr.puts $terminal.color(ex.message, :red)
@@ -391,48 +393,42 @@
 end
 
 
-begin
-  # Let's see if we know how to use Growl.  Only when running in terminal,
-  # if you're running Buildr from CI, you'll want to get Growl notifications
-  # from there instead. 
-  if $stdout.isatty && RUBY_PLATFORM =~ /darwin/
-    def notify(type, title, message)
+# We only do this when running from the console in verbose mode.
+if $stdout.isatty && verbose
+  # Send notifications using BUILDR_NOTIFY environment variable, if defined 
+  if ENV['BUILDR_NOTIFY'] && ENV['BUILDR_NOTIFY'].length > 0
+    notify = lambda do |type, title, message|
+      require 'shellwords'
+      args = { '{type}'=>type, '{title}'=>title, '{message}'=>message }
+      system Shellwords.shellwords(ENV['BUILDR_NOTIFY']).map { |arg| args[arg] || arg }.map(&:inspect).join(' ')
+    end  
+    Buildr.application.on_completion { |title, message| notify['completed', title, message] }
+    Buildr.application.on_failure { |title, message, ex| notify['failed', title, message] }
+  elsif RUBY_PLATFORM =~ /darwin/
+    # Let's see if we can use Growl.  We do this at the very end, loading Ruby Cocoa
+    # could slow the build down, so later is better.
+    notify = lambda do |type, title, message|
       require 'osx/cocoa'
       icon = OSX::NSApplication.sharedApplication.applicationIconImage
       icon = OSX::NSImage.alloc.initWithContentsOfFile(File.join(File.dirname(__FILE__), '../resources/buildr.icns'))
-  
+
       # Register with Growl, that way you can turn notifications on/off from system preferences.
       OSX::NSDistributedNotificationCenter.defaultCenter.
         postNotificationName_object_userInfo_deliverImmediately(:GrowlApplicationRegistrationNotification, nil,
           { :ApplicationName=>'Buildr', :AllNotifications=>['Completed', 'Failed'], 
             :ApplicationIcon=>icon.TIFFRepresentation }, true)
-  
+
       OSX::NSDistributedNotificationCenter.defaultCenter.
         postNotificationName_object_userInfo_deliverImmediately(:GrowlNotification, nil,
-          { :ApplicationName=>'Buildr', :NotificationName=>type, :NotificationTitle=>title, :NotificationDescription=>message }, true)
-      rescue Exception
-    end
-  end
-  
-  # Send notifications using BUILDR_NOTIFY environment variable, if defined 
-  if ENV['BUILDR_NOTIFY'] && ENV['BUILDR_NOTIFY'].length > 0
-    def notify(type, title, message)
-      cmd = ENV['BUILDR_NOTIFY'].sub('{type}', type).sub('{title}', title).sub('{message}', message)
-      system cmd
-      rescue Exception
-    end
-  end
-
-  if defined? notify
-    Buildr.application.on_completion do
-      notify 'Completed', 'Your build has completed', Dir.pwd if verbose
-    end
-    Buildr.application.on_failure do |ex|
-      notify 'Failed', 'Your build failed with an error', "#{Dir.pwd}:\n#{ex.message}" if verbose
+          { :ApplicationName=>'Buildr', :NotificationName=>type,
+            :NotificationTitle=>title, :NotificationDescription=>message }, true)
     end
+    Buildr.application.on_completion { |title, message| notify['Completed', title, message] }
+    Buildr.application.on_failure { |title, message, ex| notify['Failed', title, message] }
   end
 end
 
+
 alias :warn_without_color :warn
 
 # Show warning message.