Skip to content

Instantly share code, notes, and snippets.

@peterc
Created May 17, 2009 23:55
Show Gist options
  • Save peterc/113226 to your computer and use it in GitHub Desktop.
Save peterc/113226 to your computer and use it in GitHub Desktop.
Rake task to automatically run something (i.e. specs) when code files are changed
# Rake task to automatically run something (i.e. specs) when code files are changed
# By Peter Çoopér
#
# Motivation: I couldn't get autotest to run on my Sinatra project for some reason but realized
# it didn't require overengineering. Just detect when a file is changed and then re-run the specs!
#
# Examples:
# # rake on_update "rake"
# # rake on_update "spec spec/user_spec.rb"
#
# Installation: Just add this Rake task into your Rakefile or whatever task inclusion system
# you have set up.
#
# License: Public domain.
desc "Automatically run something when code is changed"
task :on_update do
require 'find'
files = {}
loop do
changed = false
Find.find(File.dirname(__FILE__)) do |file|
next unless file =~ /\.rb$/
ctime = File.ctime(file).to_i
if ctime != files[file]
files[file] = ctime
changed = true
end
end
if changed
system ARGV[1]
puts "\nWaiting for a *.rb change"
end
sleep 1
end
end
@daniely
Copy link

daniely commented Dec 11, 2010

Have you looking into watchr? One benefit I see with watchr is that it's platform agnostic...incase you want to use it in other platforms/systems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment