summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Giddins <segiddins@segiddins.me>2025-04-17 10:54:40 +0900
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2025-05-19 11:15:29 +0900
commit4be199e435da2c0e814e570f1b638b2f08ee2527 (patch)
tree73a709b2064adbae54eb14a8e957ad7f825395ae
parent8f61e1755616f40192e7bff566709759b893e5fa (diff)
[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
-rw-r--r--lib/rubygems/platform.rb64
-rw-r--r--test/rubygems/test_gem_platform.rb4
2 files changed, 26 insertions, 42 deletions
diff --git a/lib/rubygems/platform.rb b/lib/rubygems/platform.rb
index 6d328740d5..67302dc85d 100644
--- a/lib/rubygems/platform.rb
+++ b/lib/rubygems/platform.rb
@@ -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
##
diff --git a/test/rubygems/test_gem_platform.rb b/test/rubygems/test_gem_platform.rb
index a0a961284f..455ee45c3f 100644
--- a/test/rubygems/test_gem_platform.rb
+++ b/test/rubygems/test_gem_platform.rb
@@ -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|