Created
May 4, 2009 07:36
-
-
Save jamesp/106362 to your computer and use it in GitHub Desktop.
RStakeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Originally by Mike Clark. | |
# | |
# From http://www.pragmaticautomation.com/cgi-bin/pragauto.cgi/Monitor/StakingOutFileChanges.rdoc | |
# | |
# Runs a user-defined command when files are modified. | |
# | |
# Can use Ruby's Dir[] to get file glob. Quote your args to take advantage of this. | |
# | |
# rstakeout 'rake test:recent' **/*.rb | |
# => Only watches Ruby files one directory down (no quotes) | |
# | |
# rstakeout 'rake test:recent' '**/*.rb' | |
# => Watches all Ruby files in all directories and subdirectories | |
# | |
# Modified by James Penn | |
# - (27 AUG 08) Removed Growl::Notify etc | |
# - (04 MAY 09) Added logic to handle new/deleted files | |
@command = ARGV.shift | |
@splats = ARGV | |
@files = {} | |
def find_new_files(verbose=true) | |
# check for new files matching the splats | |
@splats.each do |splat| | |
Dir[splat].each do |file| | |
unless @files[file] | |
@files[file] = File.mtime(file) | |
puts "=> adding new file #{file} to the watch list" if verbose | |
end | |
end | |
end | |
end | |
find_new_files false | |
puts "=> watching #{@files.keys.join(', ')}\nfile count: #{@files.keys.length}" | |
trap('INT') do | |
puts "\n=> quitting..." | |
exit | |
end | |
loop do | |
sleep 1 | |
find_new_files | |
changed_file, last_changed = @files.find { |file, last_changed| | |
begin | |
File.mtime(file) > last_changed | |
rescue Exception => e | |
@files.delete file | |
puts "=> removing deleted file #{file} from the watch list." | |
end | |
} | |
if changed_file | |
@files[changed_file] = File.mtime(changed_file) | |
puts "=> #{changed_file} changed, running #{@command}" | |
results = `#{@command}` | |
puts results | |
puts "=> done" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment