brew/Library/Homebrew/requirements/macos_requirement.rb

173 lines
4.9 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "requirement"
2020-08-19 06:51:15 +02:00
# A requirement on macOS.
class MacOSRequirement < Requirement
fatal true
sig { returns(String) }
attr_reader :comparator
attr_reader :version
# TODO: when Yosemite is removed here, keep these around as empty arrays so we
# can keep the deprecation/disabling code the same.
DISABLED_MACOS_VERSIONS = T.let([
:yosemite,
].freeze, T::Array[Symbol])
DEPRECATED_MACOS_VERSIONS = T.let([].freeze, T::Array[Symbol])
def initialize(tags = [], comparator: ">=")
@version = begin
if comparator == "==" && tags.first.respond_to?(:map)
tags.first.map { |s| MacOSVersion.from_symbol(s) }
else
MacOSVersion.from_symbol(tags.first) unless tags.empty?
end
rescue MacOSVersion::Error => e
if DISABLED_MACOS_VERSIONS.include?(e.version)
# This odisabled should stick around indefinitely.
odisabled "`depends_on macos: :#{e.version}`"
elsif DEPRECATED_MACOS_VERSIONS.include?(e.version)
# This odeprecated should stick around indefinitely.
odeprecated "`depends_on macos: :#{e.version}`"
else
raise
end
# Array of versions: remove the bad ones and try again.
if tags.first.respond_to?(:reject)
tags = [tags.first.reject { |s| s == e.version }, tags[1..]]
retry
end
# Otherwise fallback to the oldest allowed if comparator is >=.
MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) if comparator == ">="
2019-08-15 10:14:10 +02:00
end
@comparator = T.let(comparator, String)
super(tags.drop(1))
end
sig { returns(T::Boolean) }
def version_specified?
@version.present?
2017-02-25 10:17:25 -08:00
end
satisfy(build_env: false) do
2023-02-14 15:39:32 -08:00
T.bind(self, MacOSRequirement)
next Array(@version).any? { |v| OS::Mac.version.compare(@comparator, v) } if OS.mac? && version_specified?
2017-02-25 10:17:25 -08:00
next true if OS.mac?
next true if @version
2018-09-17 02:45:00 +02:00
2017-02-25 10:17:25 -08:00
false
end
sig { returns(MacOSVersion) }
def minimum_version
return MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) if @comparator == "<=" || !version_specified?
return @version.min if @version.respond_to?(:to_ary)
@version
end
sig { returns(MacOSVersion) }
def maximum_version
return MacOSVersion.new(HOMEBREW_MACOS_NEWEST_UNSUPPORTED) if @comparator == ">=" || !version_specified?
return @version.max if @version.respond_to?(:to_ary)
@version
end
sig { params(other: MacOSVersion).returns(T::Boolean) }
def allows?(other)
return true unless version_specified?
case @comparator
when ">=" then other >= @version
when "<=" then other <= @version
else
return @version.include?(other) if @version.respond_to?(:to_ary)
@version == other
end
end
# Finds the highest supported {MacOSVersion} that meets the requirement, if
# any.
sig { returns(T.nilable(MacOSVersion)) }
def highest_allowed
MacOSVersion::SYMBOLS.each_key do |sym|
candidate_version = MacOSVersion.from_symbol(sym)
return candidate_version if allows?(candidate_version)
end
end
sig { params(type: Symbol).returns(String) }
2019-08-15 10:14:10 +02:00
def message(type: :formula)
return "macOS is required for this software." unless version_specified?
case @comparator
when ">="
"This software does not run on macOS versions older than #{@version.pretty_name}."
when "<="
2019-08-15 10:14:10 +02:00
case type
when :formula
<<~EOS
This formula either does not compile or function as expected on macOS
versions newer than #{@version.pretty_name} due to an upstream incompatibility.
EOS
when :cask
2019-08-30 14:11:13 -04:00
"This cask does not run on macOS versions newer than #{@version.pretty_name}."
else
"This does not run on macOS versions newer than #{@version.pretty_name}."
2019-08-15 10:14:10 +02:00
end
else
2019-08-15 10:14:10 +02:00
if @version.respond_to?(:to_ary)
2019-08-30 14:11:13 -04:00
*versions, last = @version.map(&:pretty_name)
return "This software does not run on macOS versions other than #{versions.join(", ")} and #{last}."
2019-08-15 10:14:10 +02:00
end
"This software does not run on macOS versions other than #{@version.pretty_name}."
end
end
2022-11-09 01:19:46 +00:00
def ==(other)
2024-05-23 17:08:41 +01:00
super && comparator == other.comparator && version == other.version
2022-11-09 01:19:46 +00:00
end
alias eql? ==
sig { returns(Integer) }
2022-11-09 01:19:46 +00:00
def hash
[super, comparator, version].hash
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def inspect
"#<#{self.class.name}: version#{@comparator}#{@version.to_s.inspect} #{tags.inspect}>"
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def display_s
if version_specified?
if @version.respond_to?(:to_ary)
"macOS #{@comparator} #{version.join(" / ")} (or Linux)"
else
"macOS #{@comparator} #{@version} (or Linux)"
end
else
"macOS"
end
end
2019-08-30 14:11:13 -04:00
sig { params(options: T.untyped).returns(String) }
2023-02-14 15:39:32 -08:00
def to_json(options)
2019-08-30 14:11:13 -04:00
comp = @comparator.to_s
2023-02-14 15:39:32 -08:00
return { comp => @version.map(&:to_s) }.to_json(options) if @version.is_a?(Array)
2019-08-30 14:11:13 -04:00
2023-02-14 15:39:32 -08:00
{ comp => [@version.to_s] }.to_json(options)
2019-08-30 14:11:13 -04:00
end
end