Skip to content
This repository
Browse code

added optional support for watching deletions and with that comes fil…

…e moves
  • Loading branch information...
commit 8da8f6a33def410147a5f3eb4f3beb91c4d1faa4 1 parent e795ab2
Darren Pearce authored
2  bin/guard
@@ -3,4 +3,4 @@
3 3
 require 'guard'
4 4
 require 'guard/cli'
5 5
 
6  
-Guard::CLI.start
  6
+Guard::CLI.start
23  lib/guard.rb
@@ -14,10 +14,11 @@ class << self
14 14
     # initialize this singleton
15 15
     def setup(options = {})
16 16
       @options  = options
17  
-      @listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd)
  17
+      @listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd,options)
18 18
       @groups   = [:default]
19 19
       @guards   = []
20 20
 
  21
+      @watch_deletions = options[:deletions]
21 22
       @options[:notify] && ENV["GUARD_NOTIFY"] != 'false' ? Notifier.turn_on : Notifier.turn_off
22 23
 
23 24
       UI.clear if @options[:clear]
@@ -46,10 +47,24 @@ def start(options = {})
46 47
 
47 48
     def run_on_change_for_all_guards(files)
48 49
       guards.each do |guard|
  50
+        #UI.debug "files: #{files.inspect}"
49 51
         paths = Watcher.match_files(guard, files)
50  
-        unless paths.empty?
51  
-          UI.debug "#{guard.class.name}#run_on_change with #{paths.inspect}"
52  
-          supervised_task(guard, :run_on_change, paths)
  52
+        if @watch_deletions
  53
+          unless paths.empty?
  54
+            UI.debug "#{guard.class.name}#run_on_change with #{paths.inspect}"
  55
+            supervised_task(guard, :run_on_change, paths.select {|f| !f.start_with?('!') })
  56
+            deletions = paths.collect { |f| f.slice(1..-1) if f.start_with?('!') }.compact
  57
+          end
  58
+
  59
+          unless deletions.empty?
  60
+            UI.debug "#{guard.class.name}#run_on_deletion with #{deletions.inspect}"
  61
+            supervised_task(guard, :run_on_deletion, deletions)
  62
+          end
  63
+        else
  64
+          unless paths.empty?
  65
+            UI.debug "#{guard.class.name}#run_on_change with #{paths.inspect}"
  66
+            supervised_task(guard, :run_on_change, paths)
  67
+          end
53 68
         end
54 69
       end
55 70
 
1  lib/guard/cli.rb
@@ -11,6 +11,7 @@ class CLI < Thor
11 11
     method_option :group,     :type => :array,   :default => [],    :aliases => '-g', :banner => "Run only the passed groups"
12 12
     method_option :watchdir,  :type => :string,                     :aliases => '-w', :banner => "Specify the directory to watch"
13 13
     method_option :guardfile, :type => :string,                     :aliases => '-G', :banner => "Specify a Guardfile"
  14
+    method_option :deletions, :type => :boolean, :default => false, :aliases => '-D', :banner => "Watch for deleted files"
14 15
 
15 16
     desc "start", "Starts Guard"
16 17
     def start
4  lib/guard/guard.rb
@@ -53,5 +53,9 @@ def run_on_change(paths)
53 53
       true
54 54
     end
55 55
 
  56
+    def run_on_deletion(paths)
  57
+      true
  58
+    end
  59
+
56 60
   end
57 61
 end
23  lib/guard/listener.rb
@@ -29,12 +29,14 @@ def initialize(directory=Dir.pwd, options={})
29 29
       @directory           = directory.to_s
30 30
       @sha1_checksums_hash = {}
31 31
       @relativize_paths    = options.fetch(:relativize_paths, true)
32  
-      @cached_files = all_files
  32
+      @watch_deletions = options.deletions
33 33
       update_last_event
34 34
     end
35 35
 
36 36
     def start
37 37
       watch(@directory)
  38
+      # populate initial sha1 hash to watch for deleted or moved files
  39
+      all_files.each {|path| set_sha1_checksums_hash(path, sha1_checksum(path))} if @watch_deletions
38 40
     end
39 41
 
40 42
     def stop
@@ -49,10 +51,17 @@ def update_last_event
49 51
     end
50 52
 
51 53
     def modified_files(dirs, options={})
52  
-      files = potentially_modified_files(dirs, options).select { |path| file_modified?(path) }
53  
-      deleted_files = @cached_files.collect { |path| "!#{path}" unless File.exists?(path) }.compact
54  
-      files.concat(deleted_files)
55  
-      @cached_files = all_files
  54
+      files = []
  55
+      if @watch_deletions
  56
+        deleted_files = @sha1_checksums_hash.collect do |path, sha1|
  57
+          unless File.exists?(path)
  58
+            @sha1_checksums_hash.delete(path)
  59
+            "!#{path}"
  60
+          end
  61
+        end
  62
+        files.concat(deleted_files.compact)
  63
+      end
  64
+      files.concat(potentially_modified_files(dirs, options).select { |path| file_modified?(path) })
56 65
       update_last_event
57 66
       relativize_paths(files)
58 67
     end
@@ -74,7 +83,7 @@ def all_files
74 83
     def relativize_paths(paths)
75 84
       return paths unless relativize_paths?
76 85
       paths.map do |path|
77  
-        path.gsub(%r{^#{@directory}/}, '')
  86
+        path.gsub(%r{#{@directory}/}, '')
78 87
       end
79 88
     end
80 89
 
@@ -111,6 +120,8 @@ def file_modified?(path)
111 120
       elsif File.mtime(path).to_i > @last_event.to_i
112 121
         set_sha1_checksums_hash(path, sha1_checksum(path))
113 122
         true
  123
+      elsif @watch_deletions
  124
+        file_content_modified?(path, sha1_checksum(path))
114 125
       end
115 126
     rescue
116 127
       false

0 notes on commit 8da8f6a

Please sign in to comment.
Something went wrong with that request. Please try again.