Last active
August 29, 2015 13:57
-
-
Save alexcheng1982/9715350 to your computer and use it in GitHub Desktop.
Watch File System Changes and Trigger Script http://midgetontoes.com/blog/2014/03/22/ruby-watch-file-system-changes-and-trigger-script/
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 'optparse' | |
require 'fssm' | |
require 'rbconfig' | |
THIS_FILE = File.expand_path(__FILE__) | |
RUBY = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name']) | |
options = {} | |
optparse = OptionParser.new do|opts| | |
opts.banner = "Usage: file_watch_sample.rb [options]" | |
options[:watch_file] = nil | |
opts.on('-f', '--file FILE', 'FILE to watch') do |file| | |
options[:watch_file] = file | |
end | |
opts.on('-h', '--help', 'Display this screen') do | |
puts opts | |
exit | |
end | |
end | |
optparse.parse! | |
raise OptionParser::MissingArgument if options[:watch_file].nil? | |
watch_file = options[:watch_file] | |
watch_dir = File.dirname(watch_file) | |
watch_filename = File.basename(watch_file) | |
process_script = File.join(File.dirname(THIS_FILE), 'test.rb') | |
FSSM.monitor(watch_dir, watch_filename) do | |
update {|base, relative| | |
puts "Watch file #{watch_file} changed" | |
output = `#{RUBY} #{process_script}` | |
puts output | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment