brew/Library/Homebrew/requirements/macos_requirement.rb
Issy Long 45978435e7
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 15:24:27 +01:00

144 lines
3.9 KiB
Ruby

# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "requirement"
# A requirement on macOS.
class MacOSRequirement < Requirement
fatal true
attr_reader :comparator, :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 = [
:yosemite,
].freeze
DEPRECATED_MACOS_VERSIONS = [].freeze
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 == ">="
end
@comparator = comparator
super(tags.drop(1))
end
def version_specified?
@version.present?
end
satisfy(build_env: false) do
T.bind(self, MacOSRequirement)
next Array(@version).any? { |v| OS::Mac.version.compare(@comparator, v) } if OS.mac? && version_specified?
next true if OS.mac?
next true if @version
false
end
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
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
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 "<="
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
"This cask does not run on macOS versions newer than #{@version.pretty_name}."
end
else
if @version.respond_to?(:to_ary)
*versions, last = @version.map(&:pretty_name)
return "This software does not run on macOS versions other than #{versions.join(", ")} and #{last}."
end
"This software does not run on macOS versions other than #{@version.pretty_name}."
end
end
def ==(other)
super && comparator == other.comparator && version == other.version
end
alias eql? ==
def hash
[super, comparator, version].hash
end
sig { returns(String) }
def inspect
"#<#{self.class.name}: version#{@comparator}#{@version.to_s.inspect} #{tags.inspect}>"
end
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
def to_json(options)
comp = @comparator.to_s
return { comp => @version.map(&:to_s) }.to_json(options) if @version.is_a?(Array)
{ comp => [@version.to_s] }.to_json(options)
end
end