Skip to content

Instantly share code, notes, and snippets.

@jadb
Created October 3, 2012 00:06
Show Gist options
  • Save jadb/3824129 to your computer and use it in GitHub Desktop.
Save jadb/3824129 to your computer and use it in GitHub Desktop.
CakePHP + Watchr
###
#
# Watchr Configuration.
#
# This is used to watch the entire application's folder for modifications to
# models, controllers, tests, configuration files and logs. For logs, notify
# with log file's name. For the rest, run appropriate test(s) and notify of
# results. Test results also appear on the terminal window where you launched
# `watchr`.
#
# Inspired by http://www.amitsamtani.com/2010/05/09/cakephp-autotest-using-watchr/
#
# Requires the `watchr` gem. If you don't have it, run:
#
# gem install watchr
#
# The `watchr` configuration file (this file) needs to be stored in `app/Config`
# and used when starting the `watchr`:
#
# watchr app/Config/watchr.rb
#
# It is assumed that you also have the required images:
#
# app/Config/.watchr_images/passed.png
# app/Config/.watchr_images/failed.png
#
###
$cake = "app/Console/cake test -app app"
watch('app/(Model|Controller)/(.*).php') { |m| test_changed_model_or_controller(m[0]) }
watch('app/Test/Case/(Model|Controller)/(.*)Test.php') { |m| test_changed_test_case(m[0]) }
watch('app/Config/(.*).php') { |m| test_app() }
watch('app/tmp/logs/(.*).log') { |m| notify_changed_log(m[0]) }
def test_changed_model_or_controller(file)
type = file.split('/')[1]
name = file.split('/')[2].split('.')[0]
run "#{$cake} app #{type}/#{name}"
end
def test_changed_test_case(file)
type = file.split('/')[3]
name = file.split('/')[4].split('.')[0]
run "#{$cake} app #{type}/#{name}"
end
def test_app()
run "#{$cake} app AllTests"
end
def run(cmd)
puts(cmd)
result = `#{cmd}`
if cmd.include?('AllTests')
test = 'AllTests'
else
test = cmd.split('/')[3]
end
image = "app/Config/.watchr_images/passed.png"
message = "#{test}Test completed successfully."
if result.include?('error')
image = "app/Config/.watchr_images/failed.png"
message = "#{test}Test failed to complete successfully."
end
growl message, image, result.include?('error')
puts result
end
def notify_changed_log(file)
name = file.split('/')[3].split('.')[0]
growl "Detected change in #{name}", "app/Config/.watchr_images/failed.png"
end
def growl(message, image = '', sticky = false)
growlnotify = `which growlnotify`.chomp
if sticky
options = "--sticky"
end
options = "#{options} --wait --priority Normal --identifier buildingo --name Watchr"
message = "--message '#{message}'"
image = image.include?('png') ? "--image '#{File.expand_path(image)}'" : ""
system %(#{growlnotify} #{options} #{message} #{image} Buildingo &)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment