diff --git a/bin/test/watchexec-ruby-app b/bin/test/watchexec-ruby-app index 70040bd..3b86630 100755 --- a/bin/test/watchexec-ruby-app +++ b/bin/test/watchexec-ruby-app @@ -2,6 +2,7 @@ bundle install bundle exec rake build -cp -R features/support features/watchexec-ruby-app/retest +# cp -R features/support features/watchexec-ruby-app/retest ls -t pkg | head -n1 | xargs -I {} mv pkg/{} features/watchexec-ruby-app/retest.gem -docker compose -f features/watchexec-ruby-app/docker-compose.yml up --build --exit-code-from retest \ No newline at end of file +docker compose -f features/watchexec-ruby-app/docker-compose.yml up --build --exit-code-from retest +# docker compose -f features/watchexec-ruby-app/docker-compose.yml up --exit-code-from retest diff --git a/lib/retest/watcher.rb b/lib/retest/watcher.rb index a782023..3850dbd 100644 --- a/lib/retest/watcher.rb +++ b/lib/retest/watcher.rb @@ -41,48 +41,81 @@ def self.installed? end def self.watch(dir:, extensions:, polling: false) - require 'open3' command = "watchexec --exts #{extensions.join(',')} -w #{dir} --emit-events-to stdio --no-meta --only-emit-events" + files = VersionControl.files(extensions: extensions).zip([]).to_h - # watch_rd, watch_wr = IO.pipe - # pid = Process.spawn(command, out: watch_wr) - # at_exit do - # Process.kill("TERM", pid) if pid - # watch_rd.close - # watch_wr.close - # end + watch_rd, watch_wr = IO.pipe + pid = Process.spawn(command, out: watch_wr) + at_exit do + Process.kill("TERM", pid) if pid + watch_rd.close + watch_wr.close + end Thread.new do - files = VersionControl.files(extensions: extensions).zip([]).to_h - - Open3.popen3(command) do |stdin, stdout, stderr, wait_thr| - loop do - ready = IO.select([stdout]) - readable_connections = ready[0] - readable_connections.each do |conn| - data = conn.readpartial(4096) - change = /^(?:create|remove|rename|modify):(?.*)/.match(data.strip) - if change - modified, added, removed = result = [[], [], []] - path = Pathname(change[:path]).relative_path_from(Dir.pwd).to_s - file_exist = File.exist?(path) - file_cached = files.key?(path) - 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 + 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):(?.*)/.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 + + # require 'open3' + # Thread.new do + # files = VersionControl.files(extensions: extensions).zip([]).to_h + + # Open3.popen3(command) do |stdin, stdout, stderr, wait_thr| + # loop do + # ready = IO.select([stdout]) + # readable_connections = ready[0] + # readable_connections.each do |conn| + # data = conn.readpartial(4096) + # change = /^(?:create|remove|rename|modify):(?.*)/.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 + # end end end end