#!/opt/local/bin/ruby -w ## # Modified (with permission) by Geoffrey Grosenbach to call growlnotify for # rspec and Test::Unit output. # # See the PeepCode screencast on rSpec or other blog articles for instructions on # setting up growlnotify. require 'open3' def growl(title, msg, img, pri=0, sticky="") system "growlnotify -n topcoder --image ~/Library/autotest/#{img} -p #{pri} -m #{msg.inspect} #{title.inspect} #{sticky}" end def self.growl_fail(output) growl "Topcoder ~ Failed Testcases", "#{output}", "fail.png", 2 end def self.growl_compile_fail(output) growl "Topcoder ~ Failed Compilation", "#{output}", "pending.png", 2 end def self.growl_pass(output) growl "Topcoder ~ All Green", "#{output}", "pass.png" end files = {} Dir['*.cpp'].each do |f| files[f] = File.mtime(f) end puts "Watching #{files.keys.join(', ')}\n\nFiles: #{files.keys.length}" trap('INT') do puts "\nQuitting..." exit end loop do sleep 1 Dir['*.cpp'].each do |f| unless files.include?(f) puts "~ Watching #{f}; Files: #{files.keys.size}" files[f] = File.mtime(f) end end changed_file, last_changed = files.find { |file, last_changed| File.mtime(file) > last_changed } compilation_failed = false if changed_file files[changed_file] = File.mtime(changed_file) exec_file = changed_file.sub(/\.cpp$/, '') puts "~ #{changed_file} changed, compiling #{changed_file}" Open3.popen3("g++ #{changed_file} -o #{exec_file}") do |stdin, stdout, stderr| unless stderr.eof? compilation_failed = true puts "\x1B[33m" puts "~ COMPILATION ERROR:" puts "#{stderr.read}" puts "\x1B[0m" growl_compile_fail('Check out the console') end end unless compilation_failed puts "~ Executing ./#{exec_file}" Open3.popen3("./#{exec_file}") do |stdin, stdout, stderr| message = stderr.read if message =~ /FAILED/i puts "\x1B[31m" puts "~ TEST CASE ERROR:" puts "#{message}" puts "\x1B[0m" growl_fail('Check out the console') else puts "\x1B[32m" puts "~ SUCCESSFUL TEST CASES!" puts "Send It!" puts "\x1B[0m" growl_pass('Send It!') end end end # TODO Generic growl notification for other actions puts "~ done" end end