From 9c44f89a61b8973bf9a13769cdd65483257c3409 Mon Sep 17 00:00:00 2001 From: Niklas Hofer Date: Fri, 13 May 2011 20:54:39 +0200 Subject: [PATCH] can specify directory to listen to, still defaulting to pwd --- lib/guard/listener.rb | 7 ++++--- lib/guard/listeners/darwin.rb | 2 +- lib/guard/listeners/linux.rb | 4 ++-- lib/guard/listeners/polling.rb | 2 +- lib/guard/listeners/windows.rb | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/guard/listener.rb b/lib/guard/listener.rb index 9ea5948..51d0106 100644 --- a/lib/guard/listener.rb +++ b/lib/guard/listener.rb @@ -8,7 +8,7 @@ module Guard autoload :Polling, 'guard/listeners/polling' class Listener - attr_reader :last_event, :sha1_checksums_hash + attr_reader :last_event, :sha1_checksums_hash, :directory def self.select_and_init if mac? && Darwin.usable? @@ -23,7 +23,8 @@ module Guard end end - def initialize + def initialize(directory=Dir.pwd) + @directory = directory @sha1_checksums_hash = {} update_last_event end @@ -34,7 +35,7 @@ module Guard def modified_files(dirs, options = {}) files = potentially_modified_files(dirs, options).select { |path| File.file?(path) && file_modified?(path) && file_content_modified?(path) } - files.map! { |file| file.gsub("#{Dir.pwd}/", '') } + files.map! { |file| file.gsub("#{directory}/", '') } end private diff --git a/lib/guard/listeners/darwin.rb b/lib/guard/listeners/darwin.rb index c6eb055..b82089e 100644 --- a/lib/guard/listeners/darwin.rb +++ b/lib/guard/listeners/darwin.rb @@ -8,7 +8,7 @@ module Guard end def on_change(&callback) - @fsevent.watch Dir.pwd do |modified_dirs| + @fsevent.watch directory do |modified_dirs| files = modified_files(modified_dirs) update_last_event callback.call(files) diff --git a/lib/guard/listeners/linux.rb b/lib/guard/listeners/linux.rb index 2d53b62..c25b86b 100644 --- a/lib/guard/listeners/linux.rb +++ b/lib/guard/listeners/linux.rb @@ -22,7 +22,7 @@ module Guard def on_change(&callback) @callback = callback - inotify.watch(Dir.pwd, :recursive, :modify, :create, :delete, :move) do |event| + inotify.watch(directory, :recursive, :modify, :create, :delete, :move) do |event| unless event.name == "" # Event on root directory @files << event.absolute_name end @@ -61,7 +61,7 @@ module Guard unless files.empty? files.uniq! - files.map! { |file| file.gsub("#{Dir.pwd}/", '') } + files.map! { |file| file.gsub("#{directory}/", '') } callback.call(files) files.clear end diff --git a/lib/guard/listeners/polling.rb b/lib/guard/listeners/polling.rb index bf5a17b..d637de2 100644 --- a/lib/guard/listeners/polling.rb +++ b/lib/guard/listeners/polling.rb @@ -25,7 +25,7 @@ module Guard def watch_change until @stop start = Time.now.to_f - files = modified_files([Dir.pwd + '/'], :all => true) + files = modified_files([directory + '/'], :all => true) update_last_event callback.call(files) unless files.empty? nap_time = latency - (Time.now.to_f - start) diff --git a/lib/guard/listeners/windows.rb b/lib/guard/listeners/windows.rb index e6d888a..f8e7fa4 100644 --- a/lib/guard/listeners/windows.rb +++ b/lib/guard/listeners/windows.rb @@ -8,7 +8,7 @@ module Guard end def on_change(&callback) - @fchange.watch(Dir.pwd, :all_events, :recursive) do |event| + @fchange.watch(directory, :all_events, :recursive) do |event| paths = [File.expand_path(event.watcher.path) + '/'] files = modified_files(paths, {:all => true}) update_last_event