MacOSVersion: enable strong typing

This commit is contained in:
Sam Ford 2025-04-05 15:03:41 -04:00
parent 6301c2d31f
commit 00249bda44
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D
4 changed files with 19 additions and 13 deletions

View File

@ -52,4 +52,4 @@ FORMULA_COMPONENT_PRECEDENCE_LIST = T.let([
[{ name: :caveats, type: :method_definition }], [{ name: :caveats, type: :method_definition }],
[{ name: :plist_options, type: :method_call }, { name: :plist, type: :method_definition }], [{ name: :plist_options, type: :method_call }, { name: :plist, type: :method_definition }],
[{ name: :test, type: :block_call }], [{ name: :test, type: :block_call }],
].freeze, T::Array[[{ name: Symbol, type: Symbol }]]) ].freeze, T::Array[T::Array[{ name: Symbol, type: Symbol }]])

View File

@ -486,7 +486,7 @@ module Cask
def add_implicit_macos_dependency def add_implicit_macos_dependency
return if (cask_depends_on = @depends_on).present? && cask_depends_on.macos.present? return if (cask_depends_on = @depends_on).present? && cask_depends_on.macos.present?
depends_on macos: ">= :#{MacOSVersion::SYMBOLS.key MacOSVersion::SYMBOLS.values.min}" depends_on macos: ">= #{MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED).to_sym.inspect}"
end end
# Declare conflicts that keep a cask from installing or working correctly. # Declare conflicts that keep a cask from installing or working correctly.

View File

@ -52,16 +52,17 @@ module Cask
raise "Only a single 'depends_on macos' is allowed." if defined?(@macos) raise "Only a single 'depends_on macos' is allowed." if defined?(@macos)
# workaround for https://github.com/sorbet/sorbet/issues/6860 # workaround for https://github.com/sorbet/sorbet/issues/6860
first_arg = args.first&.to_s first_arg = args.first
first_arg_s = first_arg&.to_s
begin begin
@macos = if args.count > 1 @macos = if args.count > 1
MacOSRequirement.new([args], comparator: "==") MacOSRequirement.new([args], comparator: "==")
elsif MacOSVersion::SYMBOLS.key?(args.first) elsif first_arg.is_a?(Symbol) && MacOSVersion::SYMBOLS.key?(first_arg)
MacOSRequirement.new([args.first], comparator: "==") MacOSRequirement.new([args.first], comparator: "==")
elsif (md = /^\s*(?<comparator><|>|[=<>]=)\s*:(?<version>\S+)\s*$/.match(first_arg)) elsif (md = /^\s*(?<comparator><|>|[=<>]=)\s*:(?<version>\S+)\s*$/.match(first_arg_s))
MacOSRequirement.new([T.must(md[:version]).to_sym], comparator: md[:comparator]) MacOSRequirement.new([T.must(md[:version]).to_sym], comparator: md[:comparator])
elsif (md = /^\s*(?<comparator><|>|[=<>]=)\s*(?<version>\S+)\s*$/.match(first_arg)) elsif (md = /^\s*(?<comparator><|>|[=<>]=)\s*(?<version>\S+)\s*$/.match(first_arg_s))
MacOSRequirement.new([md[:version]], comparator: md[:comparator]) MacOSRequirement.new([md[:version]], comparator: md[:comparator])
# This is not duplicate of the first case: see `args.first` and a different comparator. # This is not duplicate of the first case: see `args.first` and a different comparator.
else # rubocop:disable Lint/DuplicateBranch else # rubocop:disable Lint/DuplicateBranch

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strong
# frozen_string_literal: true # frozen_string_literal: true
require "version" require "version"
@ -10,6 +10,7 @@ class MacOSVersion < Version
sig { returns(T.nilable(T.any(String, Symbol))) } sig { returns(T.nilable(T.any(String, Symbol))) }
attr_reader :version attr_reader :version
sig { params(version: T.nilable(T.any(String, Symbol))).void }
def initialize(version) def initialize(version)
@version = version @version = version
super "unknown or unsupported macOS version: #{version.inspect}" super "unknown or unsupported macOS version: #{version.inspect}"
@ -18,7 +19,7 @@ class MacOSVersion < Version
# NOTE: When removing symbols here, ensure that they are added # NOTE: When removing symbols here, ensure that they are added
# to `DEPRECATED_MACOS_VERSIONS` in `MacOSRequirement`. # to `DEPRECATED_MACOS_VERSIONS` in `MacOSRequirement`.
SYMBOLS = { SYMBOLS = T.let({
tahoe: "26", tahoe: "26",
sequoia: "15", sequoia: "15",
sonoma: "14", sonoma: "14",
@ -30,7 +31,7 @@ class MacOSVersion < Version
high_sierra: "10.13", high_sierra: "10.13",
sierra: "10.12", sierra: "10.12",
el_capitan: "10.11", el_capitan: "10.11",
}.freeze }.freeze, T::Hash[Symbol, String])
sig { params(macos_version: MacOSVersion).returns(Version) } sig { params(macos_version: MacOSVersion).returns(Version) }
def self.kernel_major_version(macos_version) def self.kernel_major_version(macos_version)
@ -57,7 +58,9 @@ class MacOSVersion < Version
super(T.must(version)) super(T.must(version))
@comparison_cache = {} @comparison_cache = T.let({}, T::Hash[T.untyped, T.nilable(Integer)])
@pretty_name = T.let(nil, T.nilable(String))
@sym = T.let(nil, T.nilable(Symbol))
end end
sig { override.params(other: T.untyped).returns(T.nilable(Integer)) } sig { override.params(other: T.untyped).returns(T.nilable(Integer)) }
@ -95,7 +98,7 @@ class MacOSVersion < Version
sig { returns(Symbol) } sig { returns(Symbol) }
def to_sym def to_sym
return @sym if defined?(@sym) return @sym if @sym
sym = SYMBOLS.invert.fetch(strip_patch.to_s, :dunno) sym = SYMBOLS.invert.fetch(strip_patch.to_s, :dunno)
@ -106,7 +109,7 @@ class MacOSVersion < Version
sig { returns(String) } sig { returns(String) }
def pretty_name def pretty_name
return @pretty_name if defined?(@pretty_name) return @pretty_name if @pretty_name
pretty_name = to_sym.to_s.split("_").map(&:capitalize).join(" ").freeze pretty_name = to_sym.to_s.split("_").map(&:capitalize).join(" ").freeze
@ -154,5 +157,7 @@ class MacOSVersion < Version
# Represents the absence of a version. # Represents the absence of a version.
# #
# NOTE: Constructor needs to called with an arbitrary macOS-like version which is then set to `nil`. # NOTE: Constructor needs to called with an arbitrary macOS-like version which is then set to `nil`.
NULL = MacOSVersion.new("10.0").tap { |v| v.instance_variable_set(:@version, nil) }.freeze NULL = T.let(MacOSVersion.new("10.0").tap do |v|
T.let(v, MacOSVersion).instance_variable_set(:@version, nil)
end.freeze, MacOSVersion)
end end