/
evented_file_update_checker.rb
168 lines (140 loc) · 4.11 KB
/
evented_file_update_checker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# frozen_string_literal: true
require "set"
require "pathname"
require "concurrent/atomic/atomic_boolean"
require "listen"
require "active_support/fork_tracker"
module ActiveSupport
# Allows you to "listen" to changes in a file system.
# The evented file updater does not hit disk when checking for updates.
# Instead, it uses platform-specific file system events to trigger a change
# in state.
#
# The file checker takes an array of files to watch or a hash specifying directories
# and file extensions to watch. It also takes a block that is called when
# EventedFileUpdateChecker#execute is run or when EventedFileUpdateChecker#execute_if_updated
# is run and there have been changes to the file system.
#
# Example:
#
# checker = ActiveSupport::EventedFileUpdateChecker.new(["/tmp/foo"]) { puts "changed" }
# checker.updated?
# # => false
# checker.execute_if_updated
# # => nil
#
# FileUtils.touch("/tmp/foo")
#
# checker.updated?
# # => true
# checker.execute_if_updated
# # => "changed"
#
class EventedFileUpdateChecker # :nodoc: all
def initialize(files, dirs = {}, &block)
unless block
raise ArgumentError, "A block is required to initialize an EventedFileUpdateChecker"
end
@block = block
@core = Core.new(files, dirs)
ObjectSpace.define_finalizer(self, @core.finalizer)
end
def updated?
if @core.restart?
@core.thread_safely(&:restart)
@core.updated.make_true
end
@core.updated.true?
end
def execute
@core.updated.make_false
@block.call
end
def execute_if_updated
if updated?
yield if block_given?
execute
true
end
end
class Core
attr_reader :updated
def initialize(files, dirs)
@files = files.map { |file| Pathname(file).expand_path }.to_set
@dirs = dirs.each_with_object({}) do |(dir, exts), hash|
hash[Pathname(dir).expand_path] = Array(exts).map { |ext| ext.to_s.sub(/\A\.?/, ".") }.to_set
end
@common_path = common_path(@dirs.keys)
@dtw = directories_to_watch
@missing = []
@updated = Concurrent::AtomicBoolean.new(false)
@mutex = Mutex.new
start
@after_fork = ActiveSupport::ForkTracker.after_fork { start }
end
def finalizer
proc do
stop
ActiveSupport::ForkTracker.unregister(@after_fork)
end
end
def thread_safely
@mutex.synchronize do
yield self
end
end
def start
normalize_dirs!
@dtw, @missing = [*@dtw, *@missing].partition(&:exist?)
@listener = @dtw.any? ? Listen.to(*@dtw, &method(:changed)) : nil
@listener&.start
end
def stop
@listener&.stop
end
def restart
stop
start
end
def restart?
@missing.any?(&:exist?)
end
def normalize_dirs!
@dirs.transform_keys! do |dir|
dir.exist? ? dir.realpath : dir
end
end
def changed(modified, added, removed)
unless @updated.true?
@updated.make_true if (modified + added + removed).any? { |f| watching?(f) }
end
end
def watching?(file)
file = Pathname(file)
if @files.member?(file)
true
elsif file.directory?
false
else
ext = file.extname
file.dirname.ascend do |dir|
matching = @dirs[dir]
if matching && (matching.empty? || matching.include?(ext))
break true
elsif dir == @common_path || dir.root?
break false
end
end
end
end
def directories_to_watch
dtw = @dirs.keys | @files.map(&:dirname)
accounted_for = dtw.to_set + Gem.path.map { |path| Pathname(path) }
dtw.reject { |dir| dir.ascend.drop(1).any? { |parent| accounted_for.include?(parent) } }
end
def common_path(paths)
paths.map { |path| path.ascend.to_a }.reduce(&:&)&.first
end
end
end
end