Refactor OnSystem and SimulateSystem bottle tag handling

This commit is contained in:
Rylan Polster 2025-06-03 11:57:17 -04:00
parent 21e3621132
commit c03f70f1dc
No known key found for this signature in database
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) def self.merge_variations(json, bottle_tag: nil)
return json unless json.key?("variations") return json unless json.key?("variations")
bottle_tag ||= ::Utils::Bottles::Tag.new(system: Homebrew::SimulateSystem.current_os, bottle_tag ||= Homebrew::SimulateSystem.current_tag
arch: Homebrew::SimulateSystem.current_arch)
if (variation = json.dig("variations", bottle_tag.to_s).presence) || if (variation = json.dig("variations", bottle_tag.to_s).presence) ||
(variation = json.dig("variations", bottle_tag.to_sym).presence) (variation = json.dig("variations", bottle_tag.to_sym).presence)

View File

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

View File

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

View File

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

View File

@ -9,6 +9,13 @@ module OnSystem
ALL_OS_OPTIONS = [*MacOSVersion::SYMBOLS.keys, :linux].freeze ALL_OS_OPTIONS = [*MacOSVersion::SYMBOLS.keys, :linux].freeze
ALL_OS_ARCH_COMBINATIONS = ALL_OS_OPTIONS.product(ARCH_OPTIONS).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) } sig { params(arch: Symbol).returns(T::Boolean) }
def self.arch_condition_met?(arch) def self.arch_condition_met?(arch)
raise ArgumentError, "Invalid arch condition: #{arch.inspect}" if ARCH_OPTIONS.exclude?(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? if path.exist? && on_system_blocks_exist?
formula_contents = path.read formula_contents = path.read
OnSystem::ALL_OS_ARCH_COMBINATIONS.each do |os, arch| OnSystem::VALID_OS_ARCH_TAGS.each do |bottle_tag|
bottle_tag = Utils::Bottles::Tag.new(system: os, arch:) Homebrew::SimulateSystem.with_tag(bottle_tag) do
next unless bottle_tag.valid_combination?
Homebrew::SimulateSystem.with(os:, arch:) do
variations_namespace = Formulary.class_s("Variations#{bottle_tag.to_sym.capitalize}") variations_namespace = Formulary.class_s("Variations#{bottle_tag.to_sym.capitalize}")
variations_formula_class = Formulary.load_formula(name, path, formula_contents, variations_namespace, variations_formula_class = Formulary.load_formula(name, path, formula_contents, variations_namespace,
flags: self.class.build_flags, ignore_errors: true) flags: self.class.build_flags, ignore_errors: true)

View File

@ -2,6 +2,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require "macos_version" require "macos_version"
require "utils/bottles"
module Homebrew module Homebrew
# Helper module for simulating different system configurations. # Helper module for simulating different system configurations.
@ -33,6 +34,18 @@ module Homebrew
end end
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 } sig { params(new_os: Symbol).void }
def os=(new_os) def os=(new_os)
os_options = [:macos, :linux, *MacOSVersion::SYMBOLS.keys] os_options = [:macos, :linux, *MacOSVersion::SYMBOLS.keys]
@ -72,6 +85,14 @@ module Homebrew
def current_os def current_os
os || :generic os || :generic
end end
sig { returns(Utils::Bottles::Tag) }
def current_tag
Utils::Bottles::Tag.new(
system: current_os,
arch: current_arch,
)
end
end end
end end
end end

View File

@ -1046,7 +1046,13 @@ RSpec.describe Formula do
before do before do
# Use a more limited os list to shorten the variations hash # Use a more limited os list to shorten the variations hash
os_list = [:monterey, :big_sur, :catalina, :mojave, :linux] 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 # For consistency, always run on Monterey and ARM
allow(MacOS).to receive(:version).and_return(MacOSVersion.new("12")) allow(MacOS).to receive(:version).and_return(MacOSVersion.new("12"))