diff --git a/lib/guard/listener.rb b/lib/guard/listener.rb index 8144cff..8d14c1a 100644 --- a/lib/guard/listener.rb +++ b/lib/guard/listener.rb @@ -46,7 +46,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("#{directory}/", '') } + relativate_paths files end def worker @@ -58,6 +58,18 @@ module Guard raise NotImplementedError, "do whatever you want here, given the directory as only argument" end + def all_files + potentially_modified_files [directory + '/'], :all => true + end + + # scopes all given paths to the current #directory + def relativate_paths(paths) + paths.map do |path| + path.gsub(%r~^#{directory}/~, '') + end + end + + private def potentially_modified_files(dirs, options = {}) diff --git a/lib/guard/listeners/linux.rb b/lib/guard/listeners/linux.rb index 7bae611..242b0a7 100644 --- a/lib/guard/listeners/linux.rb +++ b/lib/guard/listeners/linux.rb @@ -66,8 +66,7 @@ module Guard unless files.empty? files.uniq! - files.map! { |file| file.gsub("#{directory}/", '') } - callback.call(files) + callback.call( relativate_paths(files) ) files.clear end end diff --git a/lib/guard/listeners/polling.rb b/lib/guard/listeners/polling.rb index fa1697e..af140a9 100644 --- a/lib/guard/listeners/polling.rb +++ b/lib/guard/listeners/polling.rb @@ -1,6 +1,6 @@ module Guard class Polling < Listener - attr_reader :callback, :latency + attr_reader :callback, :latency, :existing def initialize(*) super @@ -23,17 +23,28 @@ module Guard def watch_change until @stop start = Time.now.to_f - files = modified_files([directory + '/'], :all => true) + current = all_files + changed = [] + # removed files + changed += existing - current + # added files + changed += current - existing + # modified + changed += existing.select { |path| File.file?(path) && file_modified?(path) && file_content_modified?(path) } update_last_event - callback.call(files) unless files.empty? + + changed.uniq! + + callback.call( relativate_paths(changed) ) unless changed.empty? + @existing = current nap_time = latency - (Time.now.to_f - start) sleep(nap_time) if nap_time > 0 end end - # we have no real worker here # FIXME: cannot watch muliple directories, but is not needed in guard (yet?) def watch(directory) + @existing = all_files end end diff --git a/spec/guard/listener_spec.rb b/spec/guard/listener_spec.rb index d527e2b..90f5da2 100644 --- a/spec/guard/listener_spec.rb +++ b/spec/guard/listener_spec.rb @@ -29,6 +29,22 @@ describe Guard::Listener do end end + describe "#all_files" do + subject { described_class.new(@fixture_path) } + + it "should return all files" do + subject.all_files.should =~ Dir.glob("#{@fixture_path}/**/*") + end + end + + describe "#relativate_paths" do + subject { described_class.new } + it "should relavate paths to the configured directory" do + subject.stub!(:directory).and_return('/tmp') + subject.relativate_paths(%w( /tmp/a /tmp/a/b /tmp/a.b/c.d )).should =~ %w( a a/b a.b/c.d ) + end + end + describe "#update_last_event" do subject { described_class.new }