[rubygems/rubygems] Fix standalone script generation for statically linked dev ruby

https://github.com/rubygems/rubygems/commit/4d0d7b3c97
This commit is contained in:
David Rodríguez 2022-06-21 18:45:45 +02:00 committed by git
parent 4f5eb48dea
commit 6f229da2c0
2 changed files with 48 additions and 9 deletions

View File

@ -12,6 +12,7 @@ module Bundler
end end
File.open File.join(bundler_path, "setup.rb"), "w" do |file| File.open File.join(bundler_path, "setup.rb"), "w" do |file|
file.puts "require 'rbconfig'" file.puts "require 'rbconfig'"
file.puts define_path_helpers
file.puts reverse_rubygems_kernel_mixin file.puts reverse_rubygems_kernel_mixin
paths.each do |path| paths.each do |path|
if Pathname.new(path).absolute? if Pathname.new(path).absolute?
@ -30,19 +31,19 @@ module Bundler
next if spec.name == "bundler" next if spec.name == "bundler"
Array(spec.require_paths).map do |path| Array(spec.require_paths).map do |path|
gem_path(path, spec). gem_path(path, spec).
sub(version_dir, '#{RUBY_ENGINE}/#{RbConfig::CONFIG["ruby_version"]}'). sub(version_dir, '#{RUBY_ENGINE}/#{Gem.ruby_api_version}').
sub(extensions_dir, 'extensions/\k<platform>/#{RbConfig::CONFIG["ruby_version"]}') sub(extensions_dir, 'extensions/\k<platform>/#{Gem.extension_api_version}')
# This is a static string intentionally. It's interpolated at a later time. # This is a static string intentionally. It's interpolated at a later time.
end end
end.flatten.compact end.flatten.compact
end end
def version_dir def version_dir
"#{RUBY_ENGINE}/#{RbConfig::CONFIG["ruby_version"]}" "#{RUBY_ENGINE}/#{Gem.ruby_api_version}"
end end
def extensions_dir def extensions_dir
%r{extensions/(?<platform>[^/]+)/#{RbConfig::CONFIG["ruby_version"]}} %r{extensions/(?<platform>[^/]+)/#{Regexp.escape(Gem.extension_api_version)}}
end end
def bundler_path def bundler_path
@ -61,6 +62,26 @@ module Bundler
raise Gem::InvalidSpecificationException.new(error_message) raise Gem::InvalidSpecificationException.new(error_message)
end end
def define_path_helpers
<<~'END'
unless defined?(Gem)
module Gem
def self.ruby_api_version
RbConfig::CONFIG["ruby_version"]
end
def self.extension_api_version
if 'no' == RbConfig::CONFIG['ENABLE_SHARED']
"#{ruby_api_version}-static"
else
ruby_api_version
end
end
end
end
END
end
def reverse_rubygems_kernel_mixin def reverse_rubygems_kernel_mixin
<<~END <<~END
kernel = (class << ::Kernel; self; end) kernel = (class << ::Kernel; self; end)

View File

@ -32,6 +32,21 @@ RSpec.shared_examples "bundle install --standalone" do
expect(out).to eq(expected_gems.values.join("\n")) expect(out).to eq(expected_gems.values.join("\n"))
end end
it "makes the gems available without bundler nor rubygems" do
testrb = String.new <<-RUBY
$:.unshift File.expand_path("bundle")
require "bundler/setup"
RUBY
expected_gems.each do |k, _|
testrb << "\nrequire \"#{k}\""
testrb << "\nputs #{k.upcase}"
end
sys_exec %(#{Gem.ruby} --disable-gems -w -e #{testrb.shellescape})
expect(out).to eq(expected_gems.values.join("\n"))
end
it "makes the gems available without bundler via Kernel.require" do it "makes the gems available without bundler via Kernel.require" do
testrb = String.new <<-RUBY testrb = String.new <<-RUBY
$:.unshift File.expand_path("bundle") $:.unshift File.expand_path("bundle")
@ -154,8 +169,8 @@ RSpec.shared_examples "bundle install --standalone" do
load_path_lines = bundled_app("bundle/bundler/setup.rb").read.split("\n").select {|line| line.start_with?("$:.unshift") } load_path_lines = bundled_app("bundle/bundler/setup.rb").read.split("\n").select {|line| line.start_with?("$:.unshift") }
expect(load_path_lines).to eq [ expect(load_path_lines).to eq [
'$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{RbConfig::CONFIG["ruby_version"]}/gems/bar-1.0.0/lib")', '$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/bar-1.0.0/lib")',
'$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{RbConfig::CONFIG["ruby_version"]}/gems/foo-1.0.0/lib")', '$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/foo-1.0.0/lib")',
] ]
end end
end end
@ -201,10 +216,13 @@ RSpec.shared_examples "bundle install --standalone" do
it "generates a bundle/bundler/setup.rb with the proper paths" do it "generates a bundle/bundler/setup.rb with the proper paths" do
expected_path = bundled_app("bundle/bundler/setup.rb") expected_path = bundled_app("bundle/bundler/setup.rb")
extension_line = File.read(expected_path).each_line.find {|line| line.include? "/extensions/" }.strip script_content = File.read(expected_path)
expect(script_content).to include("def self.ruby_api_version")
expect(script_content).to include("def self.extension_api_version")
extension_line = script_content.each_line.find {|line| line.include? "/extensions/" }.strip
platform = Gem::Platform.local platform = Gem::Platform.local
expect(extension_line).to start_with '$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{RbConfig::CONFIG["ruby_version"]}/extensions/' expect(extension_line).to start_with '$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/extensions/'
expect(extension_line).to end_with platform.to_s + '/#{RbConfig::CONFIG["ruby_version"]}/very_simple_binary-1.0")' expect(extension_line).to end_with platform.to_s + '/#{Gem.extension_api_version}/very_simple_binary-1.0")'
end end
end end