From 23d87bae7431182123fd9f987d71907e7c795ad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 26 Jul 2010 23:21:57 +0200 Subject: [PATCH] first stab at inotify listener --- Gemfile | 7 +++++++ lib/rspactor/listener.rb | 8 ++++++-- lib/rspactor/listener/inotify.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 Gemfile create mode 100644 lib/rspactor/listener/inotify.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..eaf85d7 --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source :rubygems + +gem 'rb-inotify', '~> 0.7.2' + +group :test do + gem 'rspec', '~> 1.3.0' +end diff --git a/lib/rspactor/listener.rb b/lib/rspactor/listener.rb index af93e4f..537e1b5 100644 --- a/lib/rspactor/listener.rb +++ b/lib/rspactor/listener.rb @@ -13,12 +13,15 @@ def initialize(options = {}, &callback) @pipe = nil timestamp_checked end + + def self.fsevent_bin + @fsevent_bin ||= File.expand_path('../../../bin/fsevent_watch', __FILE__) + end def start(directories) @dirs = Array(directories) - watcher = File.expand_path('../../../bin/fsevent_watch', __FILE__) # FIXME: support directories with spaces in them - @pipe = IO.popen("#{watcher} #{@dirs.join(' ')}", 'r') + @pipe = IO.popen("#{self.class.fsevent_bin} #{@dirs.join(' ')}", 'r') return self end @@ -66,6 +69,7 @@ def timestamp_checked end def fire_callback(files) + files = Array(files) relativize_path_names(files) if options[:relative_paths] timestamp_checked @callback.call(files) unless files.empty? diff --git a/lib/rspactor/listener/inotify.rb b/lib/rspactor/listener/inotify.rb new file mode 100644 index 0000000..aa83400 --- /dev/null +++ b/lib/rspactor/listener/inotify.rb @@ -0,0 +1,26 @@ +require 'rb-inotify' + +module RSpactor + class Listener + def start(directories) + @dirs = Array(directories) + @notifier = INotify::Notifier.new + watch_proc = lambda { |event| fire_callback(event.absolute_name) } + @dirs.each do |dir| + notifier.watch(dir, :modify, :moved_to, :create, :recursive, &watch_proc) + end + return self + end + + def run + raise ArgumentError, "no callback given" unless @callback + @notifier.run + rescue Interrupt + stop + end + + def stop + @notifier.stop if @notifier + end + end +end \ No newline at end of file