Merge pull request #20042 from Homebrew/refactor-bottle-tag-handling

Refactor `OnSystem` and `SimulateSystem` bottle tag handling
This commit is contained in:
Rylan Polster 2025-06-03 16:22:53 +00:00 committed by GitHub
commit 317110f6f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 42 additions and 20 deletions

View File

@ -126,8 +126,7 @@ module Homebrew
def self.merge_variations(json, bottle_tag: nil)
return json unless json.key?("variations")
bottle_tag ||= ::Utils::Bottles::Tag.new(system: Homebrew::SimulateSystem.current_os,
arch: Homebrew::SimulateSystem.current_arch)
bottle_tag ||= Homebrew::SimulateSystem.current_tag
if (variation = json.dig("variations", bottle_tag.to_s).presence) ||
(variation = json.dig("variations", bottle_tag.to_sym).presence)

View File

@ -416,16 +416,14 @@ module Cask
if @dsl.on_system_blocks_exist?
begin
OnSystem::ALL_OS_ARCH_COMBINATIONS.each do |os, arch|
bottle_tag = ::Utils::Bottles::Tag.new(system: os, arch:)
next unless bottle_tag.valid_combination?
OnSystem::VALID_OS_ARCH_TAGS.each do |bottle_tag|
next if bottle_tag.linux? && @dsl.os.nil?
next if bottle_tag.macos? &&
depends_on.macos &&
!@dsl.depends_on_set_in_block? &&
!depends_on.macos.allows?(bottle_tag.to_macos_version)
Homebrew::SimulateSystem.with(os:, arch:) do
Homebrew::SimulateSystem.with_tag(bottle_tag) do
refresh
to_h.each do |key, value|

View File

@ -70,10 +70,7 @@ module Homebrew
canonical_json = JSON.pretty_generate(tap.cask_renames)
File.write("_data/cask_canonical.json", "#{canonical_json}\n") unless args.dry_run?
OnSystem::ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch|
bottle_tag = Utils::Bottles::Tag.new(system: os, arch:)
next unless bottle_tag.valid_combination?
OnSystem::VALID_OS_ARCH_TAGS.each do |bottle_tag|
variation_casks = all_casks.transform_values do |cask|
Homebrew::API.merge_variations(cask, bottle_tag:)
end

View File

@ -68,10 +68,7 @@ module Homebrew
canonical_json = JSON.pretty_generate(tap.formula_renames.merge(tap.alias_table))
File.write("_data/formula_canonical.json", "#{canonical_json}\n") unless args.dry_run?
OnSystem::ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch|
bottle_tag = Utils::Bottles::Tag.new(system: os, arch:)
next unless bottle_tag.valid_combination?
OnSystem::VALID_OS_ARCH_TAGS.each do |bottle_tag|
variation_formulae = all_formulae.transform_values do |formula|
Homebrew::API.merge_variations(formula, bottle_tag:)
end

View File

@ -9,6 +9,13 @@ module OnSystem
ALL_OS_OPTIONS = [*MacOSVersion::SYMBOLS.keys, :linux].freeze
ALL_OS_ARCH_COMBINATIONS = ALL_OS_OPTIONS.product(ARCH_OPTIONS).freeze
VALID_OS_ARCH_TAGS = ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch|
tag = Utils::Bottles::Tag.new(system: os, arch:)
next unless tag.valid_combination?
tag
end.freeze
sig { params(arch: Symbol).returns(T::Boolean) }
def self.arch_condition_met?(arch)
raise ArgumentError, "Invalid arch condition: #{arch.inspect}" if ARCH_OPTIONS.exclude?(arch)

View File

@ -2593,11 +2593,8 @@ class Formula
if path.exist? && on_system_blocks_exist?
formula_contents = path.read
OnSystem::ALL_OS_ARCH_COMBINATIONS.each do |os, arch|
bottle_tag = Utils::Bottles::Tag.new(system: os, arch:)
next unless bottle_tag.valid_combination?
Homebrew::SimulateSystem.with(os:, arch:) do
OnSystem::VALID_OS_ARCH_TAGS.each do |bottle_tag|
Homebrew::SimulateSystem.with_tag(bottle_tag) do
variations_namespace = Formulary.class_s("Variations#{bottle_tag.to_sym.capitalize}")
variations_formula_class = Formulary.load_formula(name, path, formula_contents, variations_namespace,
flags: self.class.build_flags, ignore_errors: true)

View File

@ -2,6 +2,7 @@
# frozen_string_literal: true
require "macos_version"
require "utils/bottles"
module Homebrew
# Helper module for simulating different system configurations.
@ -33,6 +34,18 @@ module Homebrew
end
end
sig {
type_parameters(:U).params(
tag: Utils::Bottles::Tag,
block: T.proc.returns(T.type_parameter(:U)),
).returns(T.type_parameter(:U))
}
def with_tag(tag, &block)
raise ArgumentError, "Invalid tag: #{tag}" unless tag.valid_combination?
with(os: tag.system, arch: tag.arch, &block)
end
sig { params(new_os: Symbol).void }
def os=(new_os)
os_options = [:macos, :linux, *MacOSVersion::SYMBOLS.keys]
@ -72,6 +85,14 @@ module Homebrew
def current_os
os || :generic
end
sig { returns(Utils::Bottles::Tag) }
def current_tag
Utils::Bottles::Tag.new(
system: current_os,
arch: current_arch,
)
end
end
end
end

View File

@ -1046,7 +1046,13 @@ RSpec.describe Formula do
before do
# Use a more limited os list to shorten the variations hash
os_list = [:monterey, :big_sur, :catalina, :mojave, :linux]
stub_const("OnSystem::ALL_OS_ARCH_COMBINATIONS", os_list.product(OnSystem::ARCH_OPTIONS))
valid_tags = os_list.product(OnSystem::ARCH_OPTIONS).filter_map do |os, arch|
tag = Utils::Bottles::Tag.new(system: os, arch:)
next unless tag.valid_combination?
tag
end
stub_const("OnSystem::VALID_OS_ARCH_TAGS", valid_tags)
# For consistency, always run on Monterey and ARM
allow(MacOS).to receive(:version).and_return(MacOSVersion.new("12"))