Include *flight block source in cask API

This commit is contained in:
Rylan Polster 2023-01-02 14:33:33 -05:00
parent 05188b1ab9
commit 2d5d132713
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
4 changed files with 78 additions and 6 deletions

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "method_source"
require "cask/artifact/abstract_artifact"
module Cask
@ -36,6 +37,10 @@ module Cask
directives.keys.map(&:to_s).join(", ")
end
def to_h
directives.transform_values(&:source)
end
private
def class_for_dsl_key(dsl_key)

View File

@ -288,13 +288,9 @@ module Cask
def artifacts_list
artifacts.map do |artifact|
key, value = if artifact.is_a? Artifact::AbstractFlightBlock
artifact.summarize
else
[artifact.class.dsl_key, to_h_gsubs(artifact.to_args)]
end
next artifact.to_h if artifact.is_a? Artifact::AbstractFlightBlock
{ key => value }
{ artifact.class.dsl_key => to_h_gsubs(artifact.to_args) }
end
end

View File

@ -1,6 +1,8 @@
# typed: false
# frozen_string_literal: true
require "method_source"
describe Cask::Cask, :cask do
let(:cask) { described_class.new("versioned-cask") }
@ -265,6 +267,46 @@ describe Cask::Cask, :cask do
}
JSON
}
let(:expected_flight_variations) {
<<~JSON
{
"arm64_big_sur": {
"artifacts": [
{
"preflight": " preflight do\\n puts \\"preflight on Big Sur\\"\\n end\\n"
},
{
"uninstall_postflight": " uninstall_postflight do\\n puts \\"uninstall_postflight on Big Sur\\"\\n end\\n"
}
]
},
"big_sur": {
"artifacts": [
{
"preflight": " preflight do\\n puts \\"preflight on Big Sur\\"\\n end\\n"
},
{
"uninstall_postflight": " uninstall_postflight do\\n puts \\"uninstall_postflight on Big Sur\\"\\n end\\n"
}
]
},
"catalina": {
"artifacts": [
{
"preflight": " preflight do\\n puts \\"preflight on Catalina or older\\"\\n end\\n"
}
]
},
"mojave": {
"artifacts": [
{
"preflight": " preflight do\\n puts \\"preflight on Catalina or older\\"\\n end\\n"
}
]
}
}
JSON
}
before do
# Use a more limited symbols list to shorten the variations hash
@ -300,5 +342,13 @@ describe Cask::Cask, :cask do
expect(h).to be_a(Hash)
expect(JSON.pretty_generate(h["variations"])).to eq expected_sha256_variations.strip
end
it "returns the correct variations hash for a cask with conditional flight blocks" do
c = Cask::CaskLoader.load("conditional-flight")
h = c.to_hash_with_variations
expect(h).to be_a(Hash)
expect(JSON.pretty_generate(h["variations"])).to eq expected_flight_variations.strip
end
end
end

View File

@ -0,0 +1,21 @@
cask "conditional-flight" do
version "1.2.3"
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
url "file://#{TEST_FIXTURE_DIR}/cask/caffeine/#{platform}/#{version}/#{arch}.zip"
homepage "https://brew.sh/"
on_big_sur do
preflight do
puts "preflight on Big Sur"
end
uninstall_postflight do
puts "uninstall_postflight on Big Sur"
end
end
on_catalina :or_older do
preflight do
puts "preflight on Catalina or older"
end
end
end