public
Last active

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

  • Download Gist
on_update.task.rb
Ruby
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.