Skip to content

Commit

Permalink
first stab at inotify listener
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Oct 10, 2010
1 parent 522b6f0 commit 23d87ba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Gemfile
@@ -0,0 +1,7 @@
source :rubygems

gem 'rb-inotify', '~> 0.7.2'

group :test do
gem 'rspec', '~> 1.3.0'
end
8 changes: 6 additions & 2 deletions lib/rspactor/listener.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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?
Expand Down
26 changes: 26 additions & 0 deletions 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

0 comments on commit 23d87ba

Please sign in to comment.