Created
May 13, 2011 16:59
-
-
Save djspiewak/970887 to your computer and use it in GitHub Desktop.
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 | |
if ARGV.size < 2 | |
$stderr.puts 'usage: monitor <path> <command> [arg1 arg2 ...]' | |
exit 100 | |
end | |
PATH = ARGV[0] | |
COMMAND = ARGV[1] | |
ARGS = ARGV[2..-1] | |
INDEX = {} | |
def scan(path) | |
if File.exists? path | |
if File.directory? path | |
children = Dir.entries path | |
children.delete '.' | |
children.delete '..' | |
results = children.map { |f| scan "#{path}/#{f}" } | |
results.any? { |r| r } | |
else | |
if INDEX[path] != (File.mtime path) | |
INDEX[path] = File.mtime path | |
true | |
else | |
false | |
end | |
end | |
else | |
false | |
end | |
end | |
puts "Monitoring path '#{PATH}' for changes..." | |
while true | |
if scan PATH | |
system COMMAND, *ARGS | |
puts | |
puts "Monitoring path '#{PATH}' for changes..." | |
end | |
sleep 3 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment