Add arch cask DSL

This commit is contained in:
Rylan Polster 2022-08-05 15:51:02 -04:00
parent 336335abe5
commit dd72f1ac95
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
8 changed files with 109 additions and 7 deletions

View File

@ -243,6 +243,24 @@ module Cask
end
end
# @api public
def arch(arm: nil, intel: nil)
should_return = arm.blank? && intel.blank?
set_unique_stanza(:arch, should_return) do
@on_system_blocks_exist = true
# If arm and intel are arrays and one isn't specified, default to an array that is the same size as the other
empty_value = Array.new(arm&.count || intel&.count) if arm.is_a?(Array) || intel.is_a?(Array)
if OnSystem.arch_condition_met? :arm
arm || empty_value
elsif OnSystem.arch_condition_met? :intel
intel || empty_value
end
end
end
# `depends_on` uses a load method so that multiple stanzas can be merged.
# @api public
def depends_on(*args)

View File

@ -143,7 +143,8 @@ class Livecheck
end
delegate version: :@package_or_resource
private :version
delegate arch: :@package_or_resource
private :version, :arch
# Returns a `Hash` of all instance variable values.
# @return [Hash]

View File

@ -6,7 +6,7 @@ module RuboCop
# Constants available globally for use in all cask cops.
module Constants
STANZA_GROUPS = [
[:version, :sha256],
[:version, :sha256, :arch],
[:language],
[:url, :appcast, :name, :desc, :homepage],
[:livecheck],

View File

@ -305,6 +305,62 @@ describe Cask::DSL, :cask do
end
end
describe "arch stanza" do
let(:token) { "invalid/invalid-two-arch" }
it "prevents defining multiple arches" do
expect { cask }.to raise_error(Cask::CaskInvalidError, /'arch' stanza may only appear once/)
end
context "when only one string value is passed for intel" do
let(:token) { "one-string-arch" }
context "when running on arm" do
before do
allow(Hardware::CPU).to receive(:type).and_return(:arm)
end
it "returns the value" do
expect(cask.url.to_s).to eq "file://#{TEST_FIXTURE_DIR}/cask/caffeine-arm.zip"
end
end
context "when running on intel" do
before do
allow(Hardware::CPU).to receive(:type).and_return(:intel)
end
it "defaults to `nil` for the other when no arrays are passed" do
expect(cask.url.to_s).to eq "file://#{TEST_FIXTURE_DIR}/cask/caffeine.zip"
end
end
end
context "when only one array arch value is passed for intel" do
let(:token) { "one-array-arch" }
context "when running on arm" do
before do
allow(Hardware::CPU).to receive(:type).and_return(:arm)
end
it "defaults to an array of the same size as the other when an array is passed" do
expect(cask.url.to_s).to eq "file://#{TEST_FIXTURE_DIR}/cask/caffeine.zip"
end
end
context "when running on intel" do
before do
allow(Hardware::CPU).to receive(:type).and_return(:intel)
end
it "returns the value" do
expect(cask.url.to_s).to eq "file://#{TEST_FIXTURE_DIR}/cask/intel-caffeine-x86_64.zip"
end
end
end
end
describe "appcast stanza" do
let(:token) { "with-appcast" }

View File

@ -0,0 +1,11 @@
cask "invalid-two-arch" do
version "1.2.3"
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
arch arm: "arm", intel: "intel"
arch arm: "amd64", intel: "x86_64"
url "file://#{TEST_FIXTURE_DIR}/cask/caffeine.zip"
homepage "https://brew.sh/"
app "Caffeine.app"
end

View File

@ -1,11 +1,7 @@
cask "multiple-versions" do
arch = "arm"
version "1.2.3"
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
on_intel do
arch = "intel"
end
arch arm: "arm", intel: "intel"
on_big_sur do
version "1.2.0"

View File

@ -0,0 +1,10 @@
cask "one-array-arch" do
version "1.2.3"
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
arch intel: ["intel-", "-x86_64"]
url "file://#{TEST_FIXTURE_DIR}/cask/#{arch.first}caffeine#{arch.last}.zip"
homepage "https://brew.sh/"
app "Caffeine.app"
end

View File

@ -0,0 +1,10 @@
cask "one-string-arch" do
version "1.2.3"
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
arch arm: "-arm"
url "file://#{TEST_FIXTURE_DIR}/cask/caffeine#{arch}.zip"
homepage "https://brew.sh/"
app "Caffeine.app"
end