Skip to content

Commit

Permalink
make the setup script generated by the standalone instalation multipl…
Browse files Browse the repository at this point in the history
…atform
  • Loading branch information
gustavothecoder committed May 20, 2023
1 parent 3bd9a1b commit 7d7d385
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
66 changes: 65 additions & 1 deletion bundler/lib/bundler/installer/standalone.rb
Expand Up @@ -14,6 +14,7 @@ def generate
file.puts "require 'rbconfig'"
file.puts define_path_helpers
file.puts reverse_rubygems_kernel_mixin
file.puts add_arch_formatter
paths.each do |path|
if Pathname.new(path).absolute?
file.puts %($:.unshift "#{path}")
Expand All @@ -32,7 +33,7 @@ def paths
Array(spec.require_paths).map do |path|
gem_path(path, spec).
sub(version_dir, '#{RUBY_ENGINE}/#{Gem.ruby_api_version}').
sub(extensions_dir, 'extensions/\k<platform>/#{Gem.extension_api_version}')
sub(extensions_dir, 'extensions/#{local_platform}/#{Gem.extension_api_version}')
# This is a static string intentionally. It's interpolated at a later time.
end
end.flatten.compact
Expand Down Expand Up @@ -99,5 +100,68 @@ def reverse_rubygems_kernel_mixin
end
END
end

def add_arch_formatter
<<~'END'
def local_platform
arch = RbConfig::CONFIG["arch"]
arch = "#{arch}_60" if arch =~ /mswin(?:32|64)$/
arch = arch.split "-"
if arch.length > 2 && arch.last !~ /\d+(\.\d+)?$/ # reassemble x86-linux-{libc}
extra = arch.pop
arch.last << "-#{extra}"
end
cpu = arch.shift
formated_cpu = case cpu
when /i\d86/ then "x86"
else cpu
end
if arch.length == 2 && arch.last =~ /^\d+(\.\d+)?$/ # for command-line
formated_os, formated_version = arch
return
end
os, = arch
if os.nil?
formated_cpu = nil
os = cpu
end # legacy jruby
formated_os, formated_version = case os
when /aix(\d+)?/ then ["aix", $1]
when /cygwin/ then ["cygwin", nil]
when /darwin(\d+)?/ then ["darwin", $1]
when /^macruby$/ then ["macruby", nil]
when /freebsd(\d+)?/ then ["freebsd", $1]
when /^java$/, /^jruby$/ then ["java", nil]
when /^java([\d.]*)/ then ["java", $1]
when /^dalvik(\d+)?$/ then ["dalvik", $1]
when /^dotnet$/ then ["dotnet", nil]
when /^dotnet([\d.]*)/ then ["dotnet", $1]
when /linux-?(\w+)?/ then ["linux", $1]
when /mingw32/ then ["mingw32", nil]
when /mingw-?(\w+)?/ then ["mingw", $1]
when /(mswin\d+)(\_(\d+))?/ then
os = $1
version = $3
formated_cpu = "x86" if formated_cpu.nil? && os =~ /32$/
[os, version]
when /netbsdelf/ then ["netbsdelf", nil]
when /openbsd(\d+\.\d+)?/ then ["openbsd", $1]
when /solaris(\d+\.\d+)?/ then ["solaris", $1]
# test
when /^(\w+_platform)(\d+)?/ then [$1, $2]
else ["unknown", nil]
end
[formated_cpu, formated_os, formated_version].compact.join "-"
end
END
end
end
end
24 changes: 21 additions & 3 deletions bundler/spec/install/gems/standalone_spec.rb
Expand Up @@ -244,16 +244,34 @@

expect(script_content).to include("def self.ruby_api_version")
expect(script_content).to include("def self.extension_api_version")
expect(script_content).to include("def local_platform")
end

it "generates a bundle/bundler/setup.rb with the proper paths" do
expected_path = bundled_app("bundle/bundler/setup.rb")
script_content = File.read(expected_path)
extension_line = script_content.each_line.find {|line| line.include? "/extensions/" }.strip
platform = Gem::Platform.local

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 + '/#{Gem.extension_api_version}/very_simple_binary-1.0")'
expect(
extension_line
).to eq('$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/extensions' \
'/#{local_platform}/#{Gem.extension_api_version}/very_simple_binary-1.0")')
end

# Our CI has steps for several platforms, so each step Gem::Platform.local will return a different
# value and in the end we'll test multiple platforms.
it "generates a bundle/bundler/setup.rb that is multiplatform" do
local_platform = Gem::Platform.local
native_extension_pattern = %r{#{local_platform}/.+/very_simple_binary-1.0}

ruby <<-RUBY
require "./bundle/bundler/setup"
puts $:
RUBY

expect(out).to match(native_extension_pattern)
expect(err).to be_empty
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubygems/platform.rb
Expand Up @@ -69,7 +69,7 @@ def initialize(arch)
case arch
when Array then
@cpu, @os, @version = arch
when String then
when String then # Update Bundler::Standalone#add_arch_formatter whenever this case is changed.
arch = arch.split "-"

if arch.length > 2 && arch.last !~ /\d+(\.\d+)?$/ # reassemble x86-linux-{libc}
Expand Down

0 comments on commit 7d7d385

Please sign in to comment.