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
|
# File 'lib/retest/watcher.rb', line 33
def self.watch(dir:, extensions:, polling: false)
executable = File.expand_path("../../scripts/listen", __FILE__)
command = "#{executable} --exts #{extensions.join(',')} -w #{dir} --polling #{polling}"
watch_rd, watch_wr = IO.pipe
pid = Process.spawn(command, out: watch_wr, pgroup: true)
thread = Thread.new do
loop do
ready = IO.select([watch_rd])
readable_connections = ready[0]
readable_connections.each do |conn|
data = conn.readpartial(4096)
change = /^(?<action>create|remove|modify):(?<path>.*)/.match(data.strip)
next unless change
modified, added, removed = result = [[], [], []]
case change[:action]
when 'modify' then modified << change[:path]
when 'create' then added << change[:path]
when 'remove' then removed << change[:path]
end
yield result
end
end
end
at_exit do
thread.exit
Process.kill("TERM", pid) if pid
watch_rd.close
watch_wr.close
end
end
|