refactor Polling Listener to catch deleted and moved files
This commit is contained in:
parent
7b95eeb275
commit
2f0870abfc
@ -46,7 +46,7 @@ module Guard
|
|||||||
|
|
||||||
def modified_files(dirs, options = {})
|
def modified_files(dirs, options = {})
|
||||||
files = potentially_modified_files(dirs, options).select { |path| File.file?(path) && file_modified?(path) && file_content_modified?(path) }
|
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
|
end
|
||||||
|
|
||||||
def worker
|
def worker
|
||||||
@ -58,6 +58,18 @@ module Guard
|
|||||||
raise NotImplementedError, "do whatever you want here, given the directory as only argument"
|
raise NotImplementedError, "do whatever you want here, given the directory as only argument"
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def potentially_modified_files(dirs, options = {})
|
def potentially_modified_files(dirs, options = {})
|
||||||
|
@ -66,8 +66,7 @@ module Guard
|
|||||||
|
|
||||||
unless files.empty?
|
unless files.empty?
|
||||||
files.uniq!
|
files.uniq!
|
||||||
files.map! { |file| file.gsub("#{directory}/", '') }
|
callback.call( relativate_paths(files) )
|
||||||
callback.call(files)
|
|
||||||
files.clear
|
files.clear
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
module Guard
|
module Guard
|
||||||
class Polling < Listener
|
class Polling < Listener
|
||||||
attr_reader :callback, :latency
|
attr_reader :callback, :latency, :existing
|
||||||
|
|
||||||
def initialize(*)
|
def initialize(*)
|
||||||
super
|
super
|
||||||
@ -23,17 +23,28 @@ module Guard
|
|||||||
def watch_change
|
def watch_change
|
||||||
until @stop
|
until @stop
|
||||||
start = Time.now.to_f
|
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
|
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)
|
nap_time = latency - (Time.now.to_f - start)
|
||||||
sleep(nap_time) if nap_time > 0
|
sleep(nap_time) if nap_time > 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# we have no real worker here
|
|
||||||
# FIXME: cannot watch muliple directories, but is not needed in guard (yet?)
|
# FIXME: cannot watch muliple directories, but is not needed in guard (yet?)
|
||||||
def watch(directory)
|
def watch(directory)
|
||||||
|
@existing = all_files
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -29,6 +29,22 @@ describe Guard::Listener do
|
|||||||
end
|
end
|
||||||
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
|
describe "#update_last_event" do
|
||||||
subject { described_class.new }
|
subject { described_class.new }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user