Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions Library/Homebrew/cask/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ def refresh
raise CaskInvalidError.new(token, e.message), e.backtrace
end

# Refresh the cask as evaluated on `tag` and yield. Returns `nil` instead of
# raising when the cask has `on_system` blocks that omit the tag.
sig {
type_parameters(:U)
.params(tag: ::Utils::Bottles::Tag, _block: T.proc.returns(T.type_parameter(:U)))
.returns(T.nilable(T.type_parameter(:U)))
}
def refresh_for_tag(tag, &_block)
Homebrew::SimulateSystem.with(os: tag.system, arch: tag.arch) do
refresh
yield
end
rescue CaskInvalidError, CaskUnreadableError
raise unless on_system_blocks_exist?

nil
end

def_delegators :@dsl, *::Cask::DSL::DSL_METHODS

sig { returns(DSL::Caveats) }
Expand Down Expand Up @@ -592,9 +610,7 @@ def to_hash_with_variations
!dsl!.depends_on_set_in_block? &&
macos_requirements.any? { |requirement| !requirement.allows?(bottle_tag.to_macos_version) }

Homebrew::SimulateSystem.with_tag(bottle_tag) do
refresh

refresh_for_tag(bottle_tag) do
to_h.each do |key, value|
next if HASH_KEYS_TO_SKIP.include? key
next if value.to_s == hash[key].to_s
Expand Down
14 changes: 4 additions & 10 deletions Library/Homebrew/dev-cmd/bump-cask-pr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,11 @@ def replace_version_and_checksum(cask, new_hash, new_version, contents)
cask_sourcefile_path = cask.sourcefile_path
raise "unexpected nil cask.sourcefile_path" unless cask_sourcefile_path

old_cask = Cask::CaskLoader.load(cask_sourcefile_path)
generate_system_options(cask, new_version).each do |os, arch|
SimulateSystem.with(os:, arch:) do
# Handle the cask being invalid for specific os/arch combinations
old_cask = begin
Cask::CaskLoader.load(cask_sourcefile_path)
rescue Cask::CaskInvalidError, Cask::CaskUnreadableError
raise unless cask.on_system_blocks_exist?
end
next if old_cask.nil?

# Skip archs excluded by the reloaded cask's `depends_on arch:`.
tag = Utils::Bottles::Tag.new(system: os, arch:)
old_cask.refresh_for_tag(tag) do
# Skip archs excluded by the cask's `depends_on arch:`.
reloaded_archs = old_cask.depends_on.arch&.filter_map { |a| a[:type] }&.uniq
next if reloaded_archs.present? && reloaded_archs.exclude?(arch)

Expand Down
8 changes: 2 additions & 6 deletions Library/Homebrew/dev-cmd/generate-cask-ci-matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,15 @@ def runner_arch_pairs(runners:, multi_os:)
def architectures(cask:, os:)
architectures = T.let([], T::Array[Symbol])
[:arm, :intel].each do |arch|
tag = Utils::Bottles::Tag.new(system: os, arch: arch)
Homebrew::SimulateSystem.with_tag(tag) do
cask.refresh

tag = Utils::Bottles::Tag.new(system: os, arch:)
cask.refresh_for_tag(tag) do
if cask.depends_on.arch.blank?
architectures = RUNNERS.keys.map { |r| r.fetch(:arch).to_sym }.uniq.sort
next
end

architectures = cask.depends_on.arch.map { |arch| arch[:type] }
end
rescue ::Cask::CaskInvalidError
# Can't read cask for this system-arch combination.
end

architectures
Expand Down
22 changes: 22 additions & 0 deletions Library/Homebrew/test/cask/cask_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,20 @@ def write_auto_updates_cask(path, version:, artifacts:, token: "auto-updates-bun
end
end

describe "#refresh_for_tag" do
let(:cask) { Cask::CaskLoader.load("on-linux-asymmetric") }

it "yields with the cask refreshed for a supported tag" do
tag = Utils::Bottles::Tag.new(system: :sonoma, arch: :intel)
expect(cask.refresh_for_tag(tag) { cask.url.to_s }).to include("caffeine-intel-darwin")
end

it "returns nil for a tag the cask does not support" do
tag = Utils::Bottles::Tag.new(system: :linux, arch: :arm)
expect(cask.refresh_for_tag(tag) { cask.url }).to be_nil
end
end

describe "#to_hash_with_variations" do
let!(:original_macos_version) { MacOS.full_version.to_s }
let(:expected_versions_variations) do
Expand Down Expand Up @@ -744,6 +758,14 @@ def write_auto_updates_cask(path, version:, artifacts:, token: "auto-updates-bun
expect(JSON.pretty_generate(h["variations"])).to eq expected_sha256_variations_os.strip
end

it "omits tags a cask intentionally doesn't define in on_system blocks" do
c = Cask::CaskLoader.load("on-linux-asymmetric")
h = c.to_hash_with_variations

expect(h["variations"]).to include(:x86_64_linux)
expect(h["variations"]).not_to include(:arm64_linux)
end

# NOTE: The calls to `Cask.generating_hash!` and `Cask.generated_hash!`
# are not idempotent so they can only be used in one test.
it "returns the correct hash placeholders" do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# typed: false

cask "on-linux-asymmetric" do
arch arm: "arm", intel: "intel"
os macos: "darwin", linux: "linux"

version "1.2.3"

url "file://#{TEST_FIXTURE_DIR}/cask/caffeine-#{arch}-#{os}.zip"
homepage "https://brew.sh/"

on_macos do
sha256 arm: "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94",
intel: "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
end

on_linux do
sha256 x86_64_linux: "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"

depends_on arch: :x86_64
end

app "Caffeine.app"
end
Loading