Skip to content

Instantly share code, notes, and snippets.

@alexcheng1982
Last active August 29, 2015 13:57
Show Gist options
  • Save alexcheng1982/9715350 to your computer and use it in GitHub Desktop.
Save alexcheng1982/9715350 to your computer and use it in GitHub Desktop.
#!/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