Created
November 29, 2013 10:17
-
-
Save dorfire/7703870 to your computer and use it in GitHub Desktop.
Ruby file watch
This file contains 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 | |
require 'listen' | |
path = ARGV[0] | |
abspath = File.expand_path(path) | |
file_name = File.basename(path) | |
file_dir = File.dirname(path) | |
if File.exists?(path) | |
then | |
cmd = ARGV[1] % [path] # e.g. "ftp -u ftp://example.com/file.txt %s" | |
puts "Listening for changes to '#{file_name}' in '#{file_dir}'" | |
listener = Listen.to(file_dir) do |modified, added, removed| | |
if modified.include? abspath | |
then | |
puts "Executing `#{cmd}`..." | |
system(cmd) | |
end | |
end | |
listener.start # non blocking | |
sleep | |
else | |
puts "File does not exist" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment