added optional support for watching deletions and with that comes file moves
This commit is contained in:
parent
e795ab29f5
commit
8da8f6a33d
@ -3,4 +3,4 @@
|
|||||||
require 'guard'
|
require 'guard'
|
||||||
require 'guard/cli'
|
require 'guard/cli'
|
||||||
|
|
||||||
Guard::CLI.start
|
Guard::CLI.start
|
||||||
|
23
lib/guard.rb
23
lib/guard.rb
@ -14,10 +14,11 @@ module Guard
|
|||||||
# initialize this singleton
|
# initialize this singleton
|
||||||
def setup(options = {})
|
def setup(options = {})
|
||||||
@options = options
|
@options = options
|
||||||
@listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd)
|
@listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd,options)
|
||||||
@groups = [:default]
|
@groups = [:default]
|
||||||
@guards = []
|
@guards = []
|
||||||
|
|
||||||
|
@watch_deletions = options[:deletions]
|
||||||
@options[:notify] && ENV["GUARD_NOTIFY"] != 'false' ? Notifier.turn_on : Notifier.turn_off
|
@options[:notify] && ENV["GUARD_NOTIFY"] != 'false' ? Notifier.turn_on : Notifier.turn_off
|
||||||
|
|
||||||
UI.clear if @options[:clear]
|
UI.clear if @options[:clear]
|
||||||
@ -46,10 +47,24 @@ module Guard
|
|||||||
|
|
||||||
def run_on_change_for_all_guards(files)
|
def run_on_change_for_all_guards(files)
|
||||||
guards.each do |guard|
|
guards.each do |guard|
|
||||||
|
#UI.debug "files: #{files.inspect}"
|
||||||
paths = Watcher.match_files(guard, files)
|
paths = Watcher.match_files(guard, files)
|
||||||
unless paths.empty?
|
if @watch_deletions
|
||||||
UI.debug "#{guard.class.name}#run_on_change with #{paths.inspect}"
|
unless paths.empty?
|
||||||
supervised_task(guard, :run_on_change, paths)
|
UI.debug "#{guard.class.name}#run_on_change with #{paths.inspect}"
|
||||||
|
supervised_task(guard, :run_on_change, paths.select {|f| !f.start_with?('!') })
|
||||||
|
deletions = paths.collect { |f| f.slice(1..-1) if f.start_with?('!') }.compact
|
||||||
|
end
|
||||||
|
|
||||||
|
unless deletions.empty?
|
||||||
|
UI.debug "#{guard.class.name}#run_on_deletion with #{deletions.inspect}"
|
||||||
|
supervised_task(guard, :run_on_deletion, deletions)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
unless paths.empty?
|
||||||
|
UI.debug "#{guard.class.name}#run_on_change with #{paths.inspect}"
|
||||||
|
supervised_task(guard, :run_on_change, paths)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ module Guard
|
|||||||
method_option :group, :type => :array, :default => [], :aliases => '-g', :banner => "Run only the passed groups"
|
method_option :group, :type => :array, :default => [], :aliases => '-g', :banner => "Run only the passed groups"
|
||||||
method_option :watchdir, :type => :string, :aliases => '-w', :banner => "Specify the directory to watch"
|
method_option :watchdir, :type => :string, :aliases => '-w', :banner => "Specify the directory to watch"
|
||||||
method_option :guardfile, :type => :string, :aliases => '-G', :banner => "Specify a Guardfile"
|
method_option :guardfile, :type => :string, :aliases => '-G', :banner => "Specify a Guardfile"
|
||||||
|
method_option :deletions, :type => :boolean, :default => false, :aliases => '-D', :banner => "Watch for deleted files"
|
||||||
|
|
||||||
desc "start", "Starts Guard"
|
desc "start", "Starts Guard"
|
||||||
def start
|
def start
|
||||||
|
@ -53,5 +53,9 @@ module Guard
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def run_on_deletion(paths)
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -29,12 +29,14 @@ module Guard
|
|||||||
@directory = directory.to_s
|
@directory = directory.to_s
|
||||||
@sha1_checksums_hash = {}
|
@sha1_checksums_hash = {}
|
||||||
@relativize_paths = options.fetch(:relativize_paths, true)
|
@relativize_paths = options.fetch(:relativize_paths, true)
|
||||||
@cached_files = all_files
|
@watch_deletions = options.deletions
|
||||||
update_last_event
|
update_last_event
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
watch(@directory)
|
watch(@directory)
|
||||||
|
# populate initial sha1 hash to watch for deleted or moved files
|
||||||
|
all_files.each {|path| set_sha1_checksums_hash(path, sha1_checksum(path))} if @watch_deletions
|
||||||
end
|
end
|
||||||
|
|
||||||
def stop
|
def stop
|
||||||
@ -49,10 +51,17 @@ module Guard
|
|||||||
end
|
end
|
||||||
|
|
||||||
def modified_files(dirs, options={})
|
def modified_files(dirs, options={})
|
||||||
files = potentially_modified_files(dirs, options).select { |path| file_modified?(path) }
|
files = []
|
||||||
deleted_files = @cached_files.collect { |path| "!#{path}" unless File.exists?(path) }.compact
|
if @watch_deletions
|
||||||
files.concat(deleted_files)
|
deleted_files = @sha1_checksums_hash.collect do |path, sha1|
|
||||||
@cached_files = all_files
|
unless File.exists?(path)
|
||||||
|
@sha1_checksums_hash.delete(path)
|
||||||
|
"!#{path}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
files.concat(deleted_files.compact)
|
||||||
|
end
|
||||||
|
files.concat(potentially_modified_files(dirs, options).select { |path| file_modified?(path) })
|
||||||
update_last_event
|
update_last_event
|
||||||
relativize_paths(files)
|
relativize_paths(files)
|
||||||
end
|
end
|
||||||
@ -74,7 +83,7 @@ module Guard
|
|||||||
def relativize_paths(paths)
|
def relativize_paths(paths)
|
||||||
return paths unless relativize_paths?
|
return paths unless relativize_paths?
|
||||||
paths.map do |path|
|
paths.map do |path|
|
||||||
path.gsub(%r{^#{@directory}/}, '')
|
path.gsub(%r{#{@directory}/}, '')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -111,6 +120,8 @@ module Guard
|
|||||||
elsif File.mtime(path).to_i > @last_event.to_i
|
elsif File.mtime(path).to_i > @last_event.to_i
|
||||||
set_sha1_checksums_hash(path, sha1_checksum(path))
|
set_sha1_checksums_hash(path, sha1_checksum(path))
|
||||||
true
|
true
|
||||||
|
elsif @watch_deletions
|
||||||
|
file_content_modified?(path, sha1_checksum(path))
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
false
|
false
|
||||||
|
Loading…
Reference in New Issue
Block a user