Skip to content

Commit 317110f

Browse files
authored
Merge pull request #20042 from Homebrew/refactor-bottle-tag-handling
Refactor `OnSystem` and `SimulateSystem` bottle tag handling
2 parents 21e3621 + c03f70f commit 317110f

File tree

8 files changed

+42
-20
lines changed

8 files changed

+42
-20
lines changed

Library/Homebrew/api.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ def self.fetch_json_api_file(endpoint, target: HOMEBREW_CACHE_API/endpoint,
126126
def self.merge_variations(json, bottle_tag: nil)
127127
return json unless json.key?("variations")
128128

129-
bottle_tag ||= ::Utils::Bottles::Tag.new(system: Homebrew::SimulateSystem.current_os,
130-
arch: Homebrew::SimulateSystem.current_arch)
129+
bottle_tag ||= Homebrew::SimulateSystem.current_tag
131130

132131
if (variation = json.dig("variations", bottle_tag.to_s).presence) ||
133132
(variation = json.dig("variations", bottle_tag.to_sym).presence)

Library/Homebrew/cask/cask.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,14 @@ def to_hash_with_variations
416416

417417
if @dsl.on_system_blocks_exist?
418418
begin
419-
OnSystem::ALL_OS_ARCH_COMBINATIONS.each do |os, arch|
420-
bottle_tag = ::Utils::Bottles::Tag.new(system: os, arch:)
421-
next unless bottle_tag.valid_combination?
419+
OnSystem::VALID_OS_ARCH_TAGS.each do |bottle_tag|
422420
next if bottle_tag.linux? && @dsl.os.nil?
423421
next if bottle_tag.macos? &&
424422
depends_on.macos &&
425423
!@dsl.depends_on_set_in_block? &&
426424
!depends_on.macos.allows?(bottle_tag.to_macos_version)
427425

428-
Homebrew::SimulateSystem.with(os:, arch:) do
426+
Homebrew::SimulateSystem.with_tag(bottle_tag) do
429427
refresh
430428

431429
to_h.each do |key, value|

Library/Homebrew/dev-cmd/generate-cask-api.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ def run
7070
canonical_json = JSON.pretty_generate(tap.cask_renames)
7171
File.write("_data/cask_canonical.json", "#{canonical_json}\n") unless args.dry_run?
7272

73-
OnSystem::ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch|
74-
bottle_tag = Utils::Bottles::Tag.new(system: os, arch:)
75-
next unless bottle_tag.valid_combination?
76-
73+
OnSystem::VALID_OS_ARCH_TAGS.each do |bottle_tag|
7774
variation_casks = all_casks.transform_values do |cask|
7875
Homebrew::API.merge_variations(cask, bottle_tag:)
7976
end

Library/Homebrew/dev-cmd/generate-formula-api.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ def run
6868
canonical_json = JSON.pretty_generate(tap.formula_renames.merge(tap.alias_table))
6969
File.write("_data/formula_canonical.json", "#{canonical_json}\n") unless args.dry_run?
7070

71-
OnSystem::ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch|
72-
bottle_tag = Utils::Bottles::Tag.new(system: os, arch:)
73-
next unless bottle_tag.valid_combination?
74-
71+
OnSystem::VALID_OS_ARCH_TAGS.each do |bottle_tag|
7572
variation_formulae = all_formulae.transform_values do |formula|
7673
Homebrew::API.merge_variations(formula, bottle_tag:)
7774
end

Library/Homebrew/extend/on_system.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ module OnSystem
99
ALL_OS_OPTIONS = [*MacOSVersion::SYMBOLS.keys, :linux].freeze
1010
ALL_OS_ARCH_COMBINATIONS = ALL_OS_OPTIONS.product(ARCH_OPTIONS).freeze
1111

12+
VALID_OS_ARCH_TAGS = ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch|
13+
tag = Utils::Bottles::Tag.new(system: os, arch:)
14+
next unless tag.valid_combination?
15+
16+
tag
17+
end.freeze
18+
1219
sig { params(arch: Symbol).returns(T::Boolean) }
1320
def self.arch_condition_met?(arch)
1421
raise ArgumentError, "Invalid arch condition: #{arch.inspect}" if ARCH_OPTIONS.exclude?(arch)

Library/Homebrew/formula.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,11 +2593,8 @@ def to_hash_with_variations
25932593

25942594
if path.exist? && on_system_blocks_exist?
25952595
formula_contents = path.read
2596-
OnSystem::ALL_OS_ARCH_COMBINATIONS.each do |os, arch|
2597-
bottle_tag = Utils::Bottles::Tag.new(system: os, arch:)
2598-
next unless bottle_tag.valid_combination?
2599-
2600-
Homebrew::SimulateSystem.with(os:, arch:) do
2596+
OnSystem::VALID_OS_ARCH_TAGS.each do |bottle_tag|
2597+
Homebrew::SimulateSystem.with_tag(bottle_tag) do
26012598
variations_namespace = Formulary.class_s("Variations#{bottle_tag.to_sym.capitalize}")
26022599
variations_formula_class = Formulary.load_formula(name, path, formula_contents, variations_namespace,
26032600
flags: self.class.build_flags, ignore_errors: true)

Library/Homebrew/simulate_system.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# frozen_string_literal: true
33

44
require "macos_version"
5+
require "utils/bottles"
56

67
module Homebrew
78
# Helper module for simulating different system configurations.
@@ -33,6 +34,18 @@ def with(os: T.unsafe(nil), arch: T.unsafe(nil), &_block)
3334
end
3435
end
3536

37+
sig {
38+
type_parameters(:U).params(
39+
tag: Utils::Bottles::Tag,
40+
block: T.proc.returns(T.type_parameter(:U)),
41+
).returns(T.type_parameter(:U))
42+
}
43+
def with_tag(tag, &block)
44+
raise ArgumentError, "Invalid tag: #{tag}" unless tag.valid_combination?
45+
46+
with(os: tag.system, arch: tag.arch, &block)
47+
end
48+
3649
sig { params(new_os: Symbol).void }
3750
def os=(new_os)
3851
os_options = [:macos, :linux, *MacOSVersion::SYMBOLS.keys]
@@ -72,6 +85,14 @@ def current_arch
7285
def current_os
7386
os || :generic
7487
end
88+
89+
sig { returns(Utils::Bottles::Tag) }
90+
def current_tag
91+
Utils::Bottles::Tag.new(
92+
system: current_os,
93+
arch: current_arch,
94+
)
95+
end
7596
end
7697
end
7798
end

Library/Homebrew/test/formula_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,13 @@ class FooVariations < Formula
10461046
before do
10471047
# Use a more limited os list to shorten the variations hash
10481048
os_list = [:monterey, :big_sur, :catalina, :mojave, :linux]
1049-
stub_const("OnSystem::ALL_OS_ARCH_COMBINATIONS", os_list.product(OnSystem::ARCH_OPTIONS))
1049+
valid_tags = os_list.product(OnSystem::ARCH_OPTIONS).filter_map do |os, arch|
1050+
tag = Utils::Bottles::Tag.new(system: os, arch:)
1051+
next unless tag.valid_combination?
1052+
1053+
tag
1054+
end
1055+
stub_const("OnSystem::VALID_OS_ARCH_TAGS", valid_tags)
10501056

10511057
# For consistency, always run on Monterey and ARM
10521058
allow(MacOS).to receive(:version).and_return(MacOSVersion.new("12"))

0 commit comments

Comments
 (0)