Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Rake task to automatically run something (i.e. specs) when code files are changed

View on_update.task.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
# 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

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
Something went wrong with that request. Please try again.