evented_file_update_checker.rb 4.1 KB
Newer Older
1 2
require 'set'
require 'pathname'
3
require 'concurrent/atomic/atomic_boolean'
4 5

module ActiveSupport
6
  class EventedFileUpdateChecker #:nodoc: all
X
Xavier Noria 已提交
7
    def initialize(files, dirs = {}, &block)
8
      @ph    = PathHelper.new
X
Xavier Noria 已提交
9
      @files = files.map { |f| @ph.xpath(f) }.to_set
10 11 12

      @dirs = {}
      dirs.each do |dir, exts|
X
Xavier Noria 已提交
13
        @dirs[@ph.xpath(dir)] = Array(exts).map { |ext| @ph.normalize_extension(ext) }
14
      end
15

X
Xavier Noria 已提交
16
      @block   = block
17
      @updated = Concurrent::AtomicBoolean.new(false)
X
Xavier Noria 已提交
18
      @lcsp    = @ph.longest_common_subpath(@dirs.keys)
19

20
      if (dtw = directories_to_watch).any?
X
Xavier Noria 已提交
21 22 23
        # Loading listen triggers warnings. These are originated by a legit
        # usage of attr_* macros for private attributes, but adds a lot of noise
        # to our test suite. Thus, we lazy load it and disable warnings locally.
24 25 26 27 28 29 30
        silence_warnings do
          begin
            require 'listen'
          rescue LoadError => e
            raise LoadError, "Could not load the 'listen' gem. Add `gem 'listen'` to the development group of your Gemfile", e.backtrace
          end
        end
31
        Listen.to(*dtw, &method(:changed)).start
32
      end
33 34 35
    end

    def updated?
36
      @updated.true?
37 38 39
    end

    def execute
40
      @updated.make_false
41
      @block.call
42 43 44 45
    end

    def execute_if_updated
      if updated?
46
        yield if block_given?
47 48 49 50 51 52 53
        execute
        true
      end
    end

    private

54
      def changed(modified, added, removed)
X
Xavier Noria 已提交
55 56
        unless updated?
          @updated.make_true if (modified + added + removed).any? { |f| watching?(f) }
57
        end
58 59
      end

60 61
      def watching?(file)
        file = @ph.xpath(file)
62

63 64 65 66 67 68
        if @files.member?(file)
          true
        elsif file.directory?
          false
        else
          ext = @ph.normalize_extension(file.extname)
69

70 71 72 73 74
          file.dirname.ascend do |dir|
            if @dirs.fetch(dir, []).include?(ext)
              break true
            elsif dir == @lcsp || dir.root?
              break false
75 76
            end
          end
77 78 79
        end
      end

80
      def directories_to_watch
X
Xavier Noria 已提交
81
        dtw = (@files + @dirs.keys).map { |f| @ph.existing_parent(f) }
X
Xavier Noria 已提交
82 83
        dtw.compact!
        dtw.uniq!
84

X
Xavier Noria 已提交
85
        @ph.filter_out_descendants(dtw)
86
      end
87

88
    class PathHelper
89 90 91
      using Module.new {
        refine Pathname do
          def ascendant_of?(other)
92 93
            self != other && other.ascend do |ascendant|
              break true if self == ascendant
94
            end
95 96 97 98
          end
        end
      }

99 100 101
      def xpath(path)
        Pathname.new(path).expand_path
      end
102

103 104 105 106 107 108 109 110 111
      def normalize_extension(ext)
        ext.to_s.sub(/\A\./, '')
      end

      # Given a collection of Pathname objects returns the longest subpath
      # common to all of them, or +nil+ if there is none.
      def longest_common_subpath(paths)
        return if paths.empty?

112
        lcsp = Pathname.new(paths[0])
113 114

        paths[1..-1].each do |path|
X
Xavier Noria 已提交
115
          until lcsp.ascendant_of?(path)
116 117 118 119
            if lcsp.root?
              # If we get here a root directory is not an ascendant of path.
              # This may happen if there are paths in different drives on
              # Windows.
120 121
              return
            else
122
              lcsp = lcsp.parent
123 124 125 126
            end
          end
        end

127
        lcsp
128 129 130 131
      end

      # Returns the deepest existing ascendant, which could be the argument itself.
      def existing_parent(dir)
132 133
        dir.ascend do |ascendant|
          break ascendant if ascendant.directory?
134 135
        end
      end
136 137

      # Filters out directories which are descendants of others in the collection (stable).
138 139
      def filter_out_descendants(dirs)
        return dirs if dirs.length < 2
140

141
        dirs_sorted_by_nparts = dirs.sort_by { |dir| dir.each_filename.to_a.length }
142 143
        descendants = []

144 145
        until dirs_sorted_by_nparts.empty?
          dir = dirs_sorted_by_nparts.shift
146

147 148 149
          dirs_sorted_by_nparts.reject! do |possible_descendant|
            dir.ascendant_of?(possible_descendant) && descendants << possible_descendant
          end
150 151
        end

152
        # Array#- preserves order.
153
        dirs - descendants
154
      end
155 156 157
    end
  end
end