Skip to content

Commit

Permalink
Use Process.spawn, it is enough to implement ChildProcess
Browse files Browse the repository at this point in the history
* Process.spawn requires only very minimal variants between platforms.
* Remove all other backends, Process.spawn is enough.
  • Loading branch information
eregon committed Feb 21, 2023
1 parent b85a644 commit f7471c8
Show file tree
Hide file tree
Showing 26 changed files with 146 additions and 2,044 deletions.
63 changes: 11 additions & 52 deletions lib/childprocess.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'childprocess/errors'
require 'childprocess/abstract_process'
require 'childprocess/abstract_io'
require 'childprocess/process_spawn_process'
require "fcntl"
require 'logger'

Expand All @@ -15,13 +16,7 @@ class << self
def new(*args)
case os
when :macosx, :linux, :solaris, :bsd, :cygwin, :aix
if posix_spawn?
Unix::PosixSpawnProcess.new(args)
elsif jruby?
JRuby::Process.new(args)
else
Unix::ForkExecProcess.new(args)
end
Unix::Process.new(args)
when :windows
Windows::Process.new(args)
else
Expand All @@ -40,13 +35,7 @@ def logger
end

def platform
if RUBY_PLATFORM == "java"
:jruby
elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ironruby"
:ironruby
else
os
end
os
end

def platform_name
Expand All @@ -62,35 +51,14 @@ def linux?
end

def jruby?
platform == :jruby
RUBY_ENGINE == 'jruby'
end

def windows?
os == :windows
end

def posix_spawn?
enabled = @posix_spawn || %w[1 true].include?(ENV['CHILDPROCESS_POSIX_SPAWN'])
return false unless enabled

begin
require 'ffi'
rescue LoadError
raise ChildProcess::MissingFFIError
end

begin
require "childprocess/unix/platform/#{ChildProcess.platform_name}"
rescue LoadError
raise ChildProcess::MissingPlatformError
end

require "childprocess/unix/lib"
require 'childprocess/unix/posix_spawn_process'

true
rescue ChildProcess::MissingPlatformError => ex
warn_once ex.message
false
end

Expand All @@ -103,6 +71,8 @@ def posix_spawn=(bool)
end

def os
return :windows if ENV['FAKE_WINDOWS'] == 'true'

@os ||= (
require "rbconfig"
host_os = RbConfig::CONFIG['host_os'].downcase
Expand Down Expand Up @@ -158,17 +128,6 @@ def arch
def close_on_exec(file)
if file.respond_to?(:close_on_exec=)
file.close_on_exec = true
elsif file.respond_to?(:fcntl) && defined?(Fcntl::FD_CLOEXEC)
file.fcntl Fcntl::F_SETFD, Fcntl::FD_CLOEXEC

if jruby? && posix_spawn?
# on JRuby, the fcntl call above apparently isn't enough when
# we're launching the process through posix_spawn.
fileno = JRuby.posix_fileno_for(file)
Unix::Lib.fcntl fileno, Fcntl::F_SETFD, Fcntl::FD_CLOEXEC
end
elsif windows?
Windows::Lib.dont_inherit file
else
raise Error, "not sure how to set close-on-exec for #{file.inspect} on #{platform_name.inspect}"
end
Expand Down Expand Up @@ -203,8 +162,8 @@ def is_64_bit?
end # class << self
end # ChildProcess

require 'jruby' if ChildProcess.jruby?

require 'childprocess/unix' if ChildProcess.unix?
require 'childprocess/windows' if ChildProcess.windows?
require 'childprocess/jruby' if ChildProcess.jruby?
if ChildProcess.windows?
require 'childprocess/windows'
else
require 'childprocess/unix'
end
56 changes: 0 additions & 56 deletions lib/childprocess/jruby.rb

This file was deleted.

16 changes: 0 additions & 16 deletions lib/childprocess/jruby/io.rb

This file was deleted.

184 changes: 0 additions & 184 deletions lib/childprocess/jruby/process.rb

This file was deleted.

Loading

0 comments on commit f7471c8

Please sign in to comment.