[rubygems/rubygems] Simplify Gem::Platform#initialize

Based on PR feedback

Signed-off-by: Samuel Giddins <segiddins@segiddins.me>

https://github.com/rubygems/rubygems/commit/562d7aa087
This commit is contained in:
Samuel Giddins 2025-04-17 10:54:40 +09:00 committed by Hiroshi SHIBATA
parent 8f61e17556
commit 4be199e435
2 changed files with 26 additions and 42 deletions

View File

@ -88,18 +88,7 @@ class Gem::Platform
when Array then
@cpu, @os, @version = arch
when String then
arch_str = arch
arch = arch.split "-"
if arch.length > 2 && !arch.last.match?(/^\d+(\.\d+)?$/) # reassemble x86-linux-{libc}
extra = arch.pop
arch.last << "-#{extra}"
end
cpu = arch.shift
if cpu.nil? || cpu == ""
raise ArgumentError, "empty cpu in platform #{arch_str.inspect}"
end
cpu, os = arch.sub(/-+$/, "").split("-", 2)
@cpu = if cpu.match?(/i\d86/)
"x86"
@ -107,43 +96,37 @@ class Gem::Platform
cpu
end
if arch.length == 2 && arch.last.match?(/^\d+(\.\d+)?$/) # for command-line
@os, @version = arch
return
end
# discard the version element, it didn't match the version pattern (\d+(\.\d+)?)
os, = arch
if os.nil?
@cpu = nil
os = cpu
end # legacy jruby
@os, @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+(?:\.\d+)*)?/ then ["java", $1]
when /^dalvik(\d+)?$/ then ["dalvik", $1]
when /^dotnet$/ then ["dotnet", nil]
when /^dotnet(\d+(?:\.\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
when /aix-?(\d+)?/ then ["aix", $1]
when /cygwin/ then ["cygwin", nil]
when /darwin-?(\d+)?/ then ["darwin", $1]
when "macruby" then ["macruby", nil]
when /^macruby-?(\d+(?:\.\d+)*)?/ then ["macruby", $1]
when /freebsd-?(\d+)?/ then ["freebsd", $1]
when "java", "jruby" then ["java", nil]
when /^java-?(\d+(?:\.\d+)*)?/ then ["java", $1]
when /^dalvik-?(\d+)?$/ then ["dalvik", $1]
when /^dotnet$/ then ["dotnet", nil]
when /^dotnet-?(\d+(?:\.\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 = $2
@cpu = "x86" if @cpu.nil? && os.end_with?("32")
[os, version]
when /netbsdelf/ then ["netbsdelf", nil]
when /openbsd(\d+\.\d+)?/ then ["openbsd", $1]
when /solaris(\d+\.\d+)?/ then ["solaris", $1]
when /wasi/ then ["wasi", nil]
when /netbsdelf/ then ["netbsdelf", nil]
when /openbsd-?(\d+\.\d+)?/ then ["openbsd", $1]
when /solaris-?(\d+\.\d+)?/ then ["solaris", $1]
when /wasi/ then ["wasi", nil]
# test
when /^(\w+_platform)(\d+)?/ then [$1, $2]
when /^(\w+_platform)(\d+)?/ then [$1, $2]
else ["unknown", nil]
end
when Gem::Platform then
@ -160,10 +143,7 @@ class Gem::Platform
end
def to_s
if @cpu.nil? && @os && @version
return "#{@os}#{@version}"
end
to_a.compact.join "-"
to_a.compact.join(@cpu.nil? ? "" : "-")
end
##

View File

@ -158,6 +158,10 @@ class TestGemPlatform < Gem::TestCase
"darwin0---" => [nil, "darwin", "0"],
"x86-linux-x8611.0l" => ["x86", "linux", "x8611"],
"0-x86linuxx86---" => ["0", "linux", "x86"],
"x86_64-macruby-x86" => ["x86_64", "macruby", nil],
"x86_64-dotnetx86" => ["x86_64", "dotnet", nil],
"x86_64-dalvik0" => ["x86_64", "dalvik", "0"],
"x86_64-dotnet1." => ["x86_64", "dotnet", "1"],
}
test_cases.each do |arch, expected|