brew/Library/Homebrew/requirements/macos_requirement.rb

126 lines
3.5 KiB
Ruby
Raw Normal View History

2023-02-14 15:39:32 -08:00
# typed: true
# frozen_string_literal: true
require "requirement"
2020-08-19 06:51:15 +02:00
# A requirement on macOS.
#
# @api private
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 == ">="
2019-08-15 10:14:10 +02:00
end
@comparator = comparator
super(tags.drop(1))
end
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
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}."
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)
super(other) && comparator == other.comparator && version == other.version
end
alias eql? ==
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
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