Skip to content

Commit 4be199e

Browse files
segiddinshsbt
authored andcommitted
[rubygems/rubygems] Simplify Gem::Platform#initialize
Based on PR feedback Signed-off-by: Samuel Giddins <[email protected]> ruby/rubygems@562d7aa087
1 parent 8f61e17 commit 4be199e

File tree

2 files changed

+26
-42
lines changed

2 files changed

+26
-42
lines changed

lib/rubygems/platform.rb

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -88,62 +88,45 @@ def initialize(arch)
8888
when Array then
8989
@cpu, @os, @version = arch
9090
when String then
91-
arch_str = arch
92-
arch = arch.split "-"
93-
94-
if arch.length > 2 && !arch.last.match?(/^\d+(\.\d+)?$/) # reassemble x86-linux-{libc}
95-
extra = arch.pop
96-
arch.last << "-#{extra}"
97-
end
98-
99-
cpu = arch.shift
100-
if cpu.nil? || cpu == ""
101-
raise ArgumentError, "empty cpu in platform #{arch_str.inspect}"
102-
end
91+
cpu, os = arch.sub(/-+$/, "").split("-", 2)
10392

10493
@cpu = if cpu.match?(/i\d86/)
10594
"x86"
10695
else
10796
cpu
10897
end
10998

110-
if arch.length == 2 && arch.last.match?(/^\d+(\.\d+)?$/) # for command-line
111-
@os, @version = arch
112-
return
113-
end
114-
115-
# discard the version element, it didn't match the version pattern (\d+(\.\d+)?)
116-
os, = arch
11799
if os.nil?
118100
@cpu = nil
119101
os = cpu
120102
end # legacy jruby
121103

122104
@os, @version = case os
123-
when /aix(\d+)?/ then ["aix", $1]
124-
when /cygwin/ then ["cygwin", nil]
125-
when /darwin(\d+)?/ then ["darwin", $1]
126-
when /^macruby$/ then ["macruby", nil]
127-
when /freebsd(\d+)?/ then ["freebsd", $1]
128-
when /^java$/, /^jruby$/ then ["java", nil]
129-
when /^java(\d+(?:\.\d+)*)?/ then ["java", $1]
130-
when /^dalvik(\d+)?$/ then ["dalvik", $1]
131-
when /^dotnet$/ then ["dotnet", nil]
132-
when /^dotnet(\d+(?:\.\d+)*)?/ then ["dotnet", $1]
133-
when /linux-?(\w+)?/ then ["linux", $1]
134-
when /mingw32/ then ["mingw32", nil]
135-
when /mingw-?(\w+)?/ then ["mingw", $1]
136-
when /(mswin\d+)(?:\_(\d+))?/ then
105+
when /aix-?(\d+)?/ then ["aix", $1]
106+
when /cygwin/ then ["cygwin", nil]
107+
when /darwin-?(\d+)?/ then ["darwin", $1]
108+
when "macruby" then ["macruby", nil]
109+
when /^macruby-?(\d+(?:\.\d+)*)?/ then ["macruby", $1]
110+
when /freebsd-?(\d+)?/ then ["freebsd", $1]
111+
when "java", "jruby" then ["java", nil]
112+
when /^java-?(\d+(?:\.\d+)*)?/ then ["java", $1]
113+
when /^dalvik-?(\d+)?$/ then ["dalvik", $1]
114+
when /^dotnet$/ then ["dotnet", nil]
115+
when /^dotnet-?(\d+(?:\.\d+)*)?/ then ["dotnet", $1]
116+
when /linux-?(\w+)?/ then ["linux", $1]
117+
when /mingw32/ then ["mingw32", nil]
118+
when /mingw-?(\w+)?/ then ["mingw", $1]
119+
when /(mswin\d+)(?:[_-](\d+))?/ then
137120
os = $1
138121
version = $2
139122
@cpu = "x86" if @cpu.nil? && os.end_with?("32")
140123
[os, version]
141-
when /netbsdelf/ then ["netbsdelf", nil]
142-
when /openbsd(\d+\.\d+)?/ then ["openbsd", $1]
143-
when /solaris(\d+\.\d+)?/ then ["solaris", $1]
144-
when /wasi/ then ["wasi", nil]
124+
when /netbsdelf/ then ["netbsdelf", nil]
125+
when /openbsd-?(\d+\.\d+)?/ then ["openbsd", $1]
126+
when /solaris-?(\d+\.\d+)?/ then ["solaris", $1]
127+
when /wasi/ then ["wasi", nil]
145128
# test
146-
when /^(\w+_platform)(\d+)?/ then [$1, $2]
129+
when /^(\w+_platform)(\d+)?/ then [$1, $2]
147130
else ["unknown", nil]
148131
end
149132
when Gem::Platform then
@@ -160,10 +143,7 @@ def to_a
160143
end
161144

162145
def to_s
163-
if @cpu.nil? && @os && @version
164-
return "#{@os}#{@version}"
165-
end
166-
to_a.compact.join "-"
146+
to_a.compact.join(@cpu.nil? ? "" : "-")
167147
end
168148

169149
##

test/rubygems/test_gem_platform.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ def test_initialize
158158
"darwin0---" => [nil, "darwin", "0"],
159159
"x86-linux-x8611.0l" => ["x86", "linux", "x8611"],
160160
"0-x86linuxx86---" => ["0", "linux", "x86"],
161+
"x86_64-macruby-x86" => ["x86_64", "macruby", nil],
162+
"x86_64-dotnetx86" => ["x86_64", "dotnet", nil],
163+
"x86_64-dalvik0" => ["x86_64", "dalvik", "0"],
164+
"x86_64-dotnet1." => ["x86_64", "dotnet", "1"],
161165
}
162166

163167
test_cases.each do |arch, expected|

0 commit comments

Comments
 (0)