Skip to content

Commit

Permalink
update test command and choose Process spwan instead of Open3
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexB52 committed Dec 5, 2024
1 parent d334295 commit 88ca376
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 36 deletions.
5 changes: 3 additions & 2 deletions bin/test/watchexec-ruby-app
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
101 changes: 67 additions & 34 deletions lib/retest/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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):(?<path>.*)/.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):(?<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

# 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):(?<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
# end
end
end
end
Expand Down

0 comments on commit 88ca376

Please sign in to comment.