MacOS::Version: add (and use) architecture.

This commit is contained in:
Mike McQuaid 2020-12-09 11:55:27 +00:00
parent 566ebf2a98
commit 6467fbadee
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70
4 changed files with 73 additions and 23 deletions

View File

@ -36,13 +36,8 @@ module Homebrew
args = dispatch_build_bottle_args.parse args = dispatch_build_bottle_args.parse
# Fixup version for ARM/Apple Silicon # Fixup version for ARM/Apple Silicon
arm_regex = Regexp.union(/^arm64_/, /-arm$/) # TODO: fix label name to be 11-arm64 instead and remove this.
arm_label = if arm_regex.match?(args.macos) args.macos&.gsub!(/^11-arm$/, "11-arm64")
args.macos&.gsub!(arm_regex, "")
true
else
false
end
macos = args.macos&.yield_self do |s| macos = args.macos&.yield_self do |s|
MacOS::Version.from_symbol(s.to_sym) MacOS::Version.from_symbol(s.to_sym)
@ -53,7 +48,8 @@ module Homebrew
raise UsageError, "Must specify --macos option" if macos.blank? raise UsageError, "Must specify --macos option" if macos.blank?
# Fixup label for ARM/Apple Silicon # Fixup label for ARM/Apple Silicon
macos_label = if arm_label macos_label = if macos.arch == :arm64
# TODO: fix label name to be 11-arm64 instead.
"#{macos}-arm" "#{macos}-arm"
else else
macos.to_s macos.to_s

View File

@ -42,7 +42,10 @@ module Utils
return if tag_version.blank? return if tag_version.blank?
keys.find do |key| keys.find do |key|
MacOS::Version.from_symbol(key) <= tag_version key_version = MacOS::Version.from_symbol(key)
next if key_version.arch != tag_version.arch
key_version <= tag_version
rescue MacOSVersionError rescue MacOSVersionError
false false
end end

View File

@ -13,6 +13,8 @@ module OS
class Version < ::Version class Version < ::Version
extend T::Sig extend T::Sig
attr_reader :arch
SYMBOLS = { SYMBOLS = {
big_sur: "11", big_sur: "11",
catalina: "10.15", catalina: "10.15",
@ -25,20 +27,37 @@ module OS
sig { params(sym: Symbol).returns(T.attached_class) } sig { params(sym: Symbol).returns(T.attached_class) }
def self.from_symbol(sym) def self.from_symbol(sym)
@all_archs_regex ||= /^#{Regexp.union(Hardware::CPU::ALL_ARCHS.map(&:to_s))}_/ version, arch = version_arch(sym)
sym_without_arch = sym.to_s version ||= sym
.sub(@all_archs_regex, "") str = SYMBOLS.fetch(version.to_sym) { raise MacOSVersionError, sym }
.to_sym new(str, arch: arch)
str = SYMBOLS.fetch(sym_without_arch) { raise MacOSVersionError, sym }
new(str)
end end
sig { params(value: T.nilable(String)).void } sig { params(value: T.any(String, Symbol)).returns(T::Array[String]) }
def initialize(value) def self.version_arch(value)
raise MacOSVersionError, value unless /\A1\d+(?:\.\d+){0,2}\Z/.match?(value) @all_archs_regex ||= begin
all_archs = Hardware::CPU::ALL_ARCHS.map(&:to_s)
/^((#{Regexp.union(all_archs)})_)?([\w.]+)(-(#{Regexp.union(all_archs)}))?$/
end
match = @all_archs_regex.match(value)
return [] unless match
super(value) version = match[3]
arch = match[2] || match[5]
[version, arch]
end
sig { params(value: T.nilable(String), arch: T.nilable(String)).void }
def initialize(value, arch: nil)
version, arch = Version.version_arch(value) if value.present? && arch.nil?
version ||= value
arch ||= "intel"
raise MacOSVersionError, version unless /\A1\d+(?:\.\d+){0,2}\Z/.match?(version)
super(version)
@arch = arch.to_sym
@comparison_cache = {} @comparison_cache = {}
end end

View File

@ -62,12 +62,44 @@ describe OS::Mac::Version do
described_class.new("1.2") described_class.new("1.2")
}.to raise_error(MacOSVersionError, 'unknown or unsupported macOS version: "1.2"') }.to raise_error(MacOSVersionError, 'unknown or unsupported macOS version: "1.2"')
end end
it "creates a new version from a valid macOS version" do
string_version = described_class.new("11")
expect(string_version).to eq(:big_sur)
expect(string_version.arch).to eq(:intel)
end
it "creates a new version from a valid macOS version with architecture" do
string_version = described_class.new("11-arm64")
expect(string_version).to eq(:big_sur)
expect(string_version.arch).to eq(:arm64)
end
it "creates a new version from a valid macOS version and architecture" do
string_version = described_class.new("11", arch: "arm64")
expect(string_version).to eq(:big_sur)
expect(string_version.arch).to eq(:arm64)
end
end end
specify "#from_symbol" do describe "#from_symbol" do
expect(described_class.from_symbol(:mojave)).to eq(version) it "raises an error if the symbol is not a valid macOS version" do
expect { described_class.from_symbol(:foo) } expect {
.to raise_error(MacOSVersionError, "unknown or unsupported macOS version: :foo") described_class.from_symbol(:foo)
}.to raise_error(MacOSVersionError, "unknown or unsupported macOS version: :foo")
end
it "creates a new version from a valid macOS version" do
symbol_version = described_class.from_symbol(:mojave)
expect(symbol_version).to eq(version)
expect(symbol_version.arch).to eq(:intel)
end
it "creates a new version from a valid macOS version with architecture" do
symbol_version = described_class.from_symbol(:arm64_big_sur)
expect(symbol_version).to eq(:big_sur)
expect(symbol_version.arch).to eq(:arm64)
end
end end
specify "#pretty_name" do specify "#pretty_name" do