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
|
# File 'lib/retest/watcher.rb', line 80
def self.watch(dir:, extensions:, polling: false)
command = "watchexec --exts #{extensions.join(',')} -w #{dir} --emit-events-to stdio --no-meta --only-emit-events"
watch_rd, watch_wr = IO.pipe
pid = Process.spawn(command, out: watch_wr, pgroup: true)
thread = Thread.new do
files = VersionControl.files(extensions: extensions).zip([]).to_h
loop do
ready = IO.select([watch_rd])
readable_connections = ready[0]
readable_connections.each do |conn|
data = conn.readpartial(4096)
change = /^(?:create|remove|rename|modify):(?<path>.*)/.match(data.strip)
next unless change
path = Pathname(change[:path]).relative_path_from(Dir.pwd).to_s
file_exist = File.exist?(path)
file_cached = files.key?(path)
modified, added, removed = result = [[], [], []]
if file_exist && file_cached
modified << path
elsif file_exist && !file_cached
added << path
files[path] = nil
elsif !file_exist && file_cached
removed << path
files.delete(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
|