#!/usr/bin/env ruby # This script watches modifications on the given directories, using the new # FSEvents API in Leopard. # Depends on rubycocoa! # Needs rubygems and json gem! # Based on code by Dave Dribin # http://www.dribin.org/dave/blog/archives/2008/01/04/fswatch/ require "rubygems" require 'set' require 'optparse' require 'ostruct' require "yaml" require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'xrefresh-server.rb') # this form is important for local development module XRefreshServer ################################################################################ # command-line parsing COMMAND = File.basename($0) USAGE = "Usage: #{COMMAND} [OPTIONS]" options = OpenStruct.new options.output = "-" options.config = nil opts = OptionParser.new do |o| o.banner = USAGE o.separator "" o.separator "Specific options:" o.on("-c", "--config FILE", "Config file") do |fn| options.config = fn end o.on("-g", "--generate [FILE]", "Generates default config file on given path") do |fn| fn = "~/#{CONFIG_FILE}" if fn.nil? XRefreshServer::generate_config(File.expand_path(fn)) exit end o.on("-o", "--output FILE", "Write output to a file") do |fn| options.output = fn end o.on_tail("-h", "--help", "Show this message") do puts o exit end o.on_tail("-v", "--version", "Show version") do puts XRefreshServer::VERSION exit end end begin opts.parse!(ARGV) rescue die "Unable to parse options: #{$!}" end # initialize output handle if options.output == "-" OUT = $stdout.clone else OUT = File.open(options.output, "w") end ################################################################################ # load config unless options.config path = File.expand_path("~/#{CONFIG_FILE}") options.config = path if File.exists?(path) path = File.expand_path("./#{CONFIG_FILE}") options.config = path if File.exists?(path) end unless options.config puts "Config file #{CONFIG_FILE} not found in current folder or home." puts "It seems you are running xrefresh-server for first time." puts "Do you want to generate default config file in home directory? [Yn]" s = STDIN.getc.chr if s=='y' || s=='Y' options.config = File.expand_path("~/#{CONFIG_FILE}") generate_config(options.config) else die "Please launch xrefresh-server with -c option and specify path to your config" end end begin CONFIG = YAML::load_file(options.config) rescue die "Unable to load or parse config: #{options.config}" end # sanitize config values CONFIG["dir_include"] = '.*' unless CONFIG["dir_include"] CONFIG["file_include"] = '.*' unless CONFIG["file_include"] CONFIG["dir_exclude"] = '^$' unless CONFIG["dir_exclude"] CONFIG["file_exclude"] = '^$' unless CONFIG["file_exclude"] CONFIG["dir_include"] = Regexp.new(CONFIG["dir_include"]) CONFIG["file_include"] = Regexp.new(CONFIG["file_include"]) CONFIG["dir_exclude"] = Regexp.new(CONFIG["dir_exclude"]) CONFIG["file_exclude"] = Regexp.new(CONFIG["file_exclude"]) CONFIG["max_connections"] = 4 unless CONFIG["max_connections"] CONFIG["host"] = GServer::DEFAULT_HOST unless CONFIG["host"] CONFIG["debug"] = !!CONFIG["debug"] CONFIG["audit"] = !!CONFIG["audit"] CONFIG["defer_time"] = 0.5 unless CONFIG["defer_time"] CONFIG["sleep_time"] = 0.1 unless CONFIG["sleep_time"] CONFIG.freeze ################################################################################ # run server server = Server.new(CONFIG["port"], CONFIG["host"], CONFIG["max_connections"], $stderr, CONFIG["audit"], CONFIG["debug"]) server.start ################################################################################ # run filesystem monitoring loop start_id = FSEventsGetCurrentEventId() start_time = Time.now.to_i # used to compare with mtime, which only has second accuracy monitor = Monitor.new(server, CONFIG) monitor.schedule(start_id) monitor.run_loop(start_time) # blocking call ################################################################################ # leave in peace $out.flush end