Skip to content

Instantly share code, notes, and snippets.

@bsodmike
Forked from ogredude/.watchr
Created July 8, 2011 21:57
Show Gist options
  • Save bsodmike/1072929 to your computer and use it in GitHub Desktop.
Save bsodmike/1072929 to your computer and use it in GitHub Desktop.
My .watchr with red/green and Growl support
# Run me with:
#
# $ watchr .watchr
# --------------------------------------------------
# Convenience Methods
# --------------------------------------------------
def growl(result)
result_lines = result.split("\n")
message = result_lines[result_lines.size - 1]
growlnotify = `which growlnotify`.chomp
title = "Watchr Test Results"
passed = message.include?('0 failures')
image = passed ? "~/Library/autotest/rails_ok.png" : "~/Library/autotest/rails_fail.png"
severity = passed ? "-1" : "2"
options = "-w -n Watchr --image '#{File.expand_path(image)}'"
options << " -m '#{strip_ansi(message)}' '#{title}' -p #{severity}"
system %(#{growlnotify} #{options} &)
end
def strip_ansi(m)
m.gsub(/\e\[[^m]*m/, '')
end
def all_spec_files
Dir['spec/**/*_spec.rb']
end
def run_spec_matching(thing_to_match)
matches = all_spec_files.grep(/#{thing_to_match}/i)
if matches.empty?
puts "Sorry, thanks for playing, but there were no matches for #{thing_to_match}"
else
run matches.join(' ')
end
end
def run(files_to_run)
puts("Running: #{files_to_run}")
system("clear")
result = `rspec --tty -cfs #{files_to_run}`
puts result
growl result rescue nil
no_int_for_you
end
def run_all_specs
run(all_spec_files.join(' '))
end
# --------------------------------------------------
# Watchr Rules
# --------------------------------------------------
watch('^spec/(.*)_spec\.rb') { |m| run_spec_matching(m[1]) }
watch('^lib/(.*)\.rb') { |m| run_spec_matching(m[1]) }
watch('^app/(.*)\.rb') { |m| run_spec_matching(m[1]) }
watch('^spec/spec_helper\.rb') { run_all_specs }
watch('^spec/support/.*\.rb') { run_all_specs }
# --------------------------------------------------
# Signal Handling
# --------------------------------------------------
def no_int_for_you
@sent_an_int = nil
end
Signal.trap 'INT' do
if @sent_an_int then
puts " A second INT? Ok, I get the message. Shutting down now."
exit
else
puts " Did you just send me an INT? Ugh. I'll quit for real if you do it again."
@sent_an_int = true
Kernel.sleep 1.5
run_all_specs
end
end
# vim:ft=ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment