From 6cf84ed16f9f443002f5d07faba7918eb8bbdfe9 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Sat, 9 Mar 2024 17:00:40 -0800 Subject: [PATCH 1/3] Start parsing cask internal JSON v3 This is currently hidden behind the `HOMEBREW_INTERNAL_JSON_V3` environment variable. The new format should be equivalent functionally to the old one but it's just smaller. --- Library/Homebrew/api/cask.rb | 43 ++++-- Library/Homebrew/cask/cask.rb | 13 +- Library/Homebrew/cask/cask_loader.rb | 7 +- Library/Homebrew/tap.rb | 2 + .../test/api/internal_tap_json/cask_spec.rb | 123 ++++++++++++++++++ 5 files changed, 173 insertions(+), 15 deletions(-) create mode 100644 Library/Homebrew/test/api/internal_tap_json/cask_spec.rb diff --git a/Library/Homebrew/api/cask.rb b/Library/Homebrew/api/cask.rb index d70f6da2e17d9..31e4b8ee84c67 100644 --- a/Library/Homebrew/api/cask.rb +++ b/Library/Homebrew/api/cask.rb @@ -42,17 +42,22 @@ def self.source_download(cask) sig { returns(T::Boolean) } def self.download_and_cache_data! - json_casks, updated = Homebrew::API.fetch_json_api_file "cask.jws.json" + if Homebrew::API.internal_json_v3? + json_casks, updated = Homebrew::API.fetch_json_api_file "internal/v3/homebrew-cask.jws.json" + overwrite_cache! T.cast(json_casks, T::Hash[String, T.untyped]) + else + json_casks, updated = Homebrew::API.fetch_json_api_file "cask.jws.json" - cache["renames"] = {} - cache["casks"] = json_casks.to_h do |json_cask| - token = json_cask["token"] + cache["renames"] = {} + cache["casks"] = json_casks.to_h do |json_cask| + token = json_cask["token"] - json_cask.fetch("old_tokens", []).each do |old_token| - cache["renames"][old_token] = token - end + json_cask.fetch("old_tokens", []).each do |old_token| + cache["renames"][old_token] = token + end - [token, json_cask.except("token")] + [token, json_cask.except("token")] + end end updated @@ -79,6 +84,28 @@ def self.all_renames cache.fetch("renames") end + sig { returns(Hash) } + def self.tap_migrations + # Not sure that we need to reload here. + unless cache.key?("tap_migrations") + json_updated = download_and_cache_data! + write_names(regenerate: json_updated) + end + + cache["tap_migrations"] + end + + sig { returns(String) } + def self.tap_git_head + # Note sure we need to reload here. + unless cache.key?("tap_git_head") + json_updated = download_and_cache_data! + write_names(regenerate: json_updated) + end + + cache["tap_git_head"] + end + sig { params(regenerate: T::Boolean).void } def self.write_names(regenerate: false) download_and_cache_data! unless cache.key?("casks") diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index 8c0870c4ecee1..f1fecb4fa8f0b 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -139,7 +139,7 @@ def installed? # The caskfile is needed during installation when there are # `*flight` blocks or the cask has multiple languages def caskfile_only? - languages.any? || artifacts.any?(Artifact::AbstractFlightBlock) + @caskfile_only || languages.any? || artifacts.any?(Artifact::AbstractFlightBlock) end sig { returns(T.nilable(Time)) } @@ -292,15 +292,16 @@ def tap_git_head def populate_from_api!(json_cask) raise ArgumentError, "Expected cask to be loaded from the API" unless loaded_from_api? + @caskfile_only = json_cask[:caskfile_only] @languages = json_cask.fetch(:languages, []) @tap_git_head = json_cask.fetch(:tap_git_head, "HEAD") @ruby_source_path = json_cask[:ruby_source_path] - - # TODO: Clean this up when we deprecate the current JSON API and move to the internal JSON v3. - ruby_source_sha256 = json_cask.dig(:ruby_source_checksum, :sha256) - ruby_source_sha256 ||= json_cask[:ruby_source_sha256] - @ruby_source_checksum = { "sha256" => ruby_source_sha256 } + @ruby_source_checksum = if Homebrew::API.internal_json_v3? + { "sha256" => json_cask.fetch(:ruby_source_sha256) } + else + json_cask[:ruby_source_checksum] + end end def to_s diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index 5f6aeaba377bb..c248ef03abc64 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -299,7 +299,12 @@ def load(config:) json_cask = Homebrew::API.merge_variations(json_cask).deep_symbolize_keys.freeze - cask_options[:tap] = Tap.fetch(json_cask[:tap]) if json_cask[:tap].to_s.include?("/") + # We could probably just default to the core cask tap in all cases without problems. + cask_options[:tap] = if Homebrew::API.internal_json_v3? + CoreCaskTap.instance + elsif json_cask[:tap].to_s.include?("/") + Tap.fetch(json_cask[:tap]) + end user_agent = json_cask.dig(:url_specs, :user_agent) json_cask[:url_specs][:user_agent] = user_agent[1..].to_sym if user_agent && user_agent[0] == ":" diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index 8599a20bfc8cb..233d810fb22a6 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -1380,6 +1380,8 @@ def cask_renames def tap_migrations @tap_migrations ||= if Homebrew::EnvConfig.no_install_from_api? super + elsif Homebrew::API.internal_json_v3? + Homebrew::API::Cask.tap_migrations else migrations, = Homebrew::API.fetch_json_api_file "cask_tap_migrations.jws.json", stale_seconds: TAP_MIGRATIONS_STALE_SECONDS diff --git a/Library/Homebrew/test/api/internal_tap_json/cask_spec.rb b/Library/Homebrew/test/api/internal_tap_json/cask_spec.rb new file mode 100644 index 0000000000000..4171bd930ae30 --- /dev/null +++ b/Library/Homebrew/test/api/internal_tap_json/cask_spec.rb @@ -0,0 +1,123 @@ +# frozen_string_literal: true + +RSpec.describe "Internal Tap JSON -- Cask" do + let(:internal_tap_json) { File.read(TEST_FIXTURE_DIR/"internal_tap_json/homebrew-cask.json").chomp } + let(:tap_git_head) { "b26c1e550a8b7eed2dcd5306ea8f3da3848258b3" } + + context "when generating JSON", :needs_macos do + before do + FileUtils.rm_rf CoreCaskTap.instance.path + cp_r(TEST_FIXTURE_DIR/"internal_tap_json/homebrew-cask", Tap::TAP_DIRECTORY/"homebrew") + allow(Cask::Cask).to receive(:generating_hash?).and_return(true) + end + + it "creates the expected hash" do + api_hash = CoreCaskTap.instance.to_internal_api_hash + api_hash["tap_git_head"] = tap_git_head # tricky to mock + + expect(JSON.pretty_generate(api_hash)).to eq(internal_tap_json) + end + end + + context "when loading JSON" do + before do + ENV["HOMEBREW_INTERNAL_JSON_V3"] = "1" + ENV.delete("HOMEBREW_NO_INSTALL_FROM_API") + + allow(Homebrew::API).to receive(:fetch_json_api_file) + .with("internal/v3/homebrew-cask.jws.json") + .and_return([JSON.parse(internal_tap_json), false]) + + # `Tap.tap_migration_oldnames` looks for renames in every + # tap so `CoreTap.tap_migrations` gets called and tries to + # fetch stuff from the API. This just avoids errors. + allow(Homebrew::API).to receive(:fetch_json_api_file) + .with("internal/v3/homebrew-core.jws.json") + .and_return([{ "tap_migrations" => {}, "formulae" => {}, "aliases" => {} }, false]) + + # To allow `cask_names.txt` to be written to the cache. + (HOMEBREW_CACHE/"api").mkdir + + Homebrew::API::Cask.clear_cache + end + + it "loads cask renames" do + expect(CoreCaskTap.instance.cask_renames).to eq({ + "ankerslicer" => "ankermake", + "autodesk-fusion360" => "autodesk-fusion", + "betterdummy" => "betterdisplay", + "julia-lang" => "julia", + "smlnj-lang" => "smlnj", + }) + end + + it "loads tap migrations" do + expect(CoreCaskTap.instance.tap_migrations).to eq({ + "azure-cli" => "homebrew/core", + "basex" => "homebrew/core", + "borgbackup" => "homebrew/core", + "chronograf" => "homebrew/core", + "consul" => "homebrew/core", + }) + end + + it "loads tap git head" do + expect(Homebrew::API::Cask.tap_git_head) + .to eq(tap_git_head) + end + + context "when loading formulae" do + let(:julia_metadata) do + { + "token" => "julia", + "name" => %w[Julia], + "desc" => "Programming language for technical computing", + "homepage" => "https://julialang.org/", + "version" => "1.10.2", + "ruby_source_path" => "Casks/j/julia.rb", + "ruby_source_checksum" => { + "sha256" => "7fbf6c98c0a3b75ca8636c141f38512a899565a58518fc714e5f73c210e24449", + }, + } + end + + let(:smlnj_metadata) do + { + "token" => "smlnj", + "name" => ["Standard ML of New Jersey"], + "desc" => "Compiler for the Standard ML '97 programming language", + "homepage" => "https://www.smlnj.org/", + "version" => "110.99.4", + "ruby_source_path" => "Casks/s/smlnj.rb", + "ruby_source_checksum" => { + "sha256" => "d47f46a88248272314a501741460d42a8c731030912a83ef58d3c7fd1e90034d", + }, + } + end + + it "loads julia" do + julia = Cask::CaskLoader.load("julia") + expect(julia.to_h).to include(julia_metadata) + expect(julia.sha256).to eq("26b822154ae05f2c2b66d2b1538e1df86f1bb39967cbc9380a7f2271f5a677ce") + expect(julia.url.to_s).to eq("https://julialang-s3.julialang.org/bin/mac/x64/1.10/julia-1.10.2-mac64.dmg") + end + + it "loads julia from rename" do + julia = Cask::CaskLoader.load("julia-lang") + expect(julia.to_h).to include(**julia_metadata) + end + + it "loads smlnj" do + smlnj = Cask::CaskLoader.load("smlnj") + expect(smlnj.to_h).to include(**smlnj_metadata) + expect(smlnj.sha256).to eq("2bf858017b8ba43a70b30527290ed9fbbc81d9eaac1abeba62469d95392019a3") + expect(smlnj.url.to_s).to eq("http://smlnj.cs.uchicago.edu/dist/working/110.99.4/smlnj-amd64-110.99.4.pkg") + end + + it "loads smlnj from rename" do + smlnj = Cask::CaskLoader.load("smlnj-lang") + expect(smlnj.to_h).to include(**smlnj_metadata) + end + end + end +end From e1646a7ced0622eca740de223494af1a3452c22b Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Wed, 13 Mar 2024 22:07:50 -0700 Subject: [PATCH 2/3] cask internal json v3: add + update tests --- Library/Homebrew/cask/cask.rb | 1 - .../test/api/internal_tap_json/cask_spec.rb | 34 ++++----- .../api/internal_tap_json/formula_spec.rb | 16 ++-- .../internal_tap_json/homebrew-cask.json | 76 +++++++++++++++++++ .../homebrew-cask/Casks/f/factor.rb | 20 +++++ .../homebrew-cask/Casks/s/smlnj.rb | 25 ++++++ .../homebrew-cask/cask_renames.json | 7 ++ .../homebrew-cask/tap_migrations.json | 7 ++ 8 files changed, 158 insertions(+), 28 deletions(-) create mode 100644 Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask.json create mode 100644 Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/Casks/f/factor.rb create mode 100644 Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/Casks/s/smlnj.rb create mode 100644 Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/cask_renames.json create mode 100644 Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/tap_migrations.json diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index f1fecb4fa8f0b..2ad6a587f69c4 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -362,7 +362,6 @@ def to_h # @private def to_internal_api_hash api_hash = { - "token" => token, "name" => name, "desc" => desc, "homepage" => homepage, diff --git a/Library/Homebrew/test/api/internal_tap_json/cask_spec.rb b/Library/Homebrew/test/api/internal_tap_json/cask_spec.rb index 4171bd930ae30..d9ff2cfc233b7 100644 --- a/Library/Homebrew/test/api/internal_tap_json/cask_spec.rb +++ b/Library/Homebrew/test/api/internal_tap_json/cask_spec.rb @@ -46,7 +46,7 @@ "ankerslicer" => "ankermake", "autodesk-fusion360" => "autodesk-fusion", "betterdummy" => "betterdisplay", - "julia-lang" => "julia", + "factor-lang" => "factor", "smlnj-lang" => "smlnj", }) end @@ -67,16 +67,16 @@ end context "when loading formulae" do - let(:julia_metadata) do + let(:factor_metadata) do { - "token" => "julia", - "name" => %w[Julia], - "desc" => "Programming language for technical computing", - "homepage" => "https://julialang.org/", - "version" => "1.10.2", - "ruby_source_path" => "Casks/j/julia.rb", + "token" => "factor", + "name" => %w[Factor], + "desc" => "Programming language", + "homepage" => "https://factorcode.org/", + "version" => "0.99", + "ruby_source_path" => "Casks/f/factor.rb", "ruby_source_checksum" => { - "sha256" => "7fbf6c98c0a3b75ca8636c141f38512a899565a58518fc714e5f73c210e24449", + "sha256" => "a0dabe24c67269e5310b47639cf32e74f49959ba1be454b2c072805b1f04c7e5", }, } end @@ -95,16 +95,16 @@ } end - it "loads julia" do - julia = Cask::CaskLoader.load("julia") - expect(julia.to_h).to include(julia_metadata) - expect(julia.sha256).to eq("26b822154ae05f2c2b66d2b1538e1df86f1bb39967cbc9380a7f2271f5a677ce") - expect(julia.url.to_s).to eq("https://julialang-s3.julialang.org/bin/mac/x64/1.10/julia-1.10.2-mac64.dmg") + it "loads factor" do + factor = Cask::CaskLoader.load("factor") + expect(factor.to_h).to include(factor_metadata) + expect(factor.sha256).to eq("8a7968b873b5e87c83b5d0f5ddb4d3d76a2460f5e5c14edac6b18fe5957bd7d6") + expect(factor.url.to_s).to eq("https://downloads.factorcode.org/releases/0.99/factor-macosx-x86-64-0.99.dmg") end - it "loads julia from rename" do - julia = Cask::CaskLoader.load("julia-lang") - expect(julia.to_h).to include(**julia_metadata) + it "loads factor from rename" do + factor = Cask::CaskLoader.load("factor-lang") + expect(factor.to_h).to include(**factor_metadata) end it "loads smlnj" do diff --git a/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb b/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb index 1426677483c95..4dedcd0a2416f 100644 --- a/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb +++ b/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb @@ -9,10 +9,10 @@ cp_r(TEST_FIXTURE_DIR/"internal_tap_json/homebrew-core", Tap::TAP_DIRECTORY/"homebrew") # NOTE: Symlinks can't be copied recursively so we create them manually here. - (Tap::TAP_DIRECTORY/"homebrew/homebrew-core").tap do |core_tap| - mkdir(core_tap/"Aliases") - ln_s(core_tap/"Formula/f/fennel.rb", core_tap/"Aliases/fennel-lang") - ln_s(core_tap/"Formula/p/ponyc.rb", core_tap/"Aliases/ponyc-lang") + CoreTap.instance.path.tap do |core_tap_path| + mkdir(core_tap_path/"Aliases") + ln_s(core_tap_path/"Formula/f/fennel.rb", core_tap_path/"Aliases/fennel-lang") + ln_s(core_tap_path/"Formula/p/ponyc.rb", core_tap_path/"Aliases/ponyc-lang") end end @@ -37,8 +37,8 @@ # tap so `CoreCaskTap.tap_migrations` gets called and tries to # fetch stuff from the API. This just avoids errors. allow(Homebrew::API).to receive(:fetch_json_api_file) - .with("cask_tap_migrations.jws.json", anything) - .and_return([{}, false]) + .with("internal/v3/homebrew-cask.jws.json") + .and_return([{ "tap_migrations" => {}, "casks" => {} }, false]) # To allow `formula_names.txt` to be written to the cache. (HOMEBREW_CACHE/"api").mkdir @@ -46,10 +46,6 @@ Homebrew::API::Formula.clear_cache end - after do - Homebrew::API::Formula.clear_cache - end - it "loads tap aliases" do expect(CoreTap.instance.alias_table).to eq({ "fennel-lang" => "fennel", diff --git a/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask.json b/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask.json new file mode 100644 index 0000000000000..a550e2dd6a73e --- /dev/null +++ b/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask.json @@ -0,0 +1,76 @@ +{ + "tap_git_head": "b26c1e550a8b7eed2dcd5306ea8f3da3848258b3", + "renames": { + "ankerslicer": "ankermake", + "autodesk-fusion360": "autodesk-fusion", + "betterdummy": "betterdisplay", + "factor-lang": "factor", + "smlnj-lang": "smlnj" + }, + "tap_migrations": { + "azure-cli": "homebrew/core", + "basex": "homebrew/core", + "borgbackup": "homebrew/core", + "chronograf": "homebrew/core", + "consul": "homebrew/core" + }, + "casks": { + "factor": { + "name": [ + "Factor" + ], + "desc": "Programming language", + "homepage": "https://factorcode.org/", + "url": "https://downloads.factorcode.org/releases/0.99/factor-macosx-x86-64-0.99.dmg", + "version": "0.99", + "sha256": "8a7968b873b5e87c83b5d0f5ddb4d3d76a2460f5e5c14edac6b18fe5957bd7d6", + "artifacts": [ + { + "suite": [ + "factor" + ] + } + ], + "ruby_source_path": "Casks/f/factor.rb", + "ruby_source_sha256": "a0dabe24c67269e5310b47639cf32e74f49959ba1be454b2c072805b1f04c7e5", + "caveats": "To use factor, you may need to add the $APPDIR/factor directory\nto your PATH environment variable, e.g. (for Bash shell):\n export PATH=$APPDIR/factor:\"$PATH\"\n" + }, + "smlnj": { + "name": [ + "Standard ML of New Jersey" + ], + "desc": "Compiler for the Standard ML '97 programming language", + "homepage": "https://www.smlnj.org/", + "url": "http://smlnj.cs.uchicago.edu/dist/working/110.99.4/smlnj-amd64-110.99.4.pkg", + "version": "110.99.4", + "sha256": "2bf858017b8ba43a70b30527290ed9fbbc81d9eaac1abeba62469d95392019a3", + "artifacts": [ + { + "uninstall": [ + { + "pkgutil": "org.smlnj.amd64.pkg" + } + ] + }, + { + "pkg": [ + "smlnj-amd64-110.99.4.pkg" + ] + }, + { + "zap": [ + { + "delete": "/usr/local/smlnj" + } + ] + } + ], + "ruby_source_path": "Casks/s/smlnj.rb", + "ruby_source_sha256": "d47f46a88248272314a501741460d42a8c731030912a83ef58d3c7fd1e90034d", + "url_specs": { + "verified": "smlnj.cs.uchicago.edu/" + }, + "caveats": "To use smlnj, you may need to add the /usr/local/smlnj/bin directory\nto your PATH environment variable, e.g. (for Bash shell):\n export PATH=/usr/local/smlnj/bin:\"$PATH\"\n" + } + } +} diff --git a/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/Casks/f/factor.rb b/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/Casks/f/factor.rb new file mode 100644 index 0000000000000..03b6ae890651f --- /dev/null +++ b/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/Casks/f/factor.rb @@ -0,0 +1,20 @@ +cask "factor" do + version "0.99" + sha256 "8a7968b873b5e87c83b5d0f5ddb4d3d76a2460f5e5c14edac6b18fe5957bd7d6" + + url "https://downloads.factorcode.org/releases/#{version}/factor-macosx-x86-64-#{version}.dmg" + name "Factor" + desc "Programming language" + homepage "https://factorcode.org/" + + livecheck do + url "https://downloads.factorcode.org/releases/" + regex(%r{href=.*?(\d+(?:\.\d+)+)/}i) + end + + suite "factor" + + caveats do + path_environment_variable "#{appdir}/factor" + end +end diff --git a/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/Casks/s/smlnj.rb b/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/Casks/s/smlnj.rb new file mode 100644 index 0000000000000..aea8544a04494 --- /dev/null +++ b/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/Casks/s/smlnj.rb @@ -0,0 +1,25 @@ +cask "smlnj" do + version "110.99.4" + sha256 "2bf858017b8ba43a70b30527290ed9fbbc81d9eaac1abeba62469d95392019a3" + + url "http://smlnj.cs.uchicago.edu/dist/working/#{version}/smlnj-amd64-#{version}.pkg", + verified: "smlnj.cs.uchicago.edu/" + name "Standard ML of New Jersey" + desc "Compiler for the Standard ML '97 programming language" + homepage "https://www.smlnj.org/" + + livecheck do + url :homepage + regex(%r{href=.*?/smlnj-amd64-(\d+(?:\.\d+)*)\.pkg}i) + end + + pkg "smlnj-amd64-#{version}.pkg" + + uninstall pkgutil: "org.smlnj.amd64.pkg" + + zap delete: "/usr/local/smlnj" + + caveats do + path_environment_variable "/usr/local/smlnj/bin" + end +end diff --git a/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/cask_renames.json b/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/cask_renames.json new file mode 100644 index 0000000000000..75bf3f592e625 --- /dev/null +++ b/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/cask_renames.json @@ -0,0 +1,7 @@ +{ + "ankerslicer": "ankermake", + "autodesk-fusion360": "autodesk-fusion", + "betterdummy": "betterdisplay", + "factor-lang": "factor", + "smlnj-lang": "smlnj" +} diff --git a/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/tap_migrations.json b/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/tap_migrations.json new file mode 100644 index 0000000000000..667b16e8e94ae --- /dev/null +++ b/Library/Homebrew/test/support/fixtures/internal_tap_json/homebrew-cask/tap_migrations.json @@ -0,0 +1,7 @@ +{ + "azure-cli": "homebrew/core", + "basex": "homebrew/core", + "borgbackup": "homebrew/core", + "chronograf": "homebrew/core", + "consul": "homebrew/core" +} From db3ab2ec12e7d91012200df453cc0ed31b8ae088 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Sun, 17 Mar 2024 11:25:11 -0700 Subject: [PATCH 3/3] cask/cask_loader: simplify branching logic for json v2 vs. v3 --- Library/Homebrew/cask/cask.rb | 7 ++----- Library/Homebrew/cask/cask_loader.rb | 8 ++++---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index 2ad6a587f69c4..9fb4a76f5f53b 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -297,11 +297,8 @@ def populate_from_api!(json_cask) @tap_git_head = json_cask.fetch(:tap_git_head, "HEAD") @ruby_source_path = json_cask[:ruby_source_path] - @ruby_source_checksum = if Homebrew::API.internal_json_v3? - { "sha256" => json_cask.fetch(:ruby_source_sha256) } - else - json_cask[:ruby_source_checksum] - end + @ruby_source_checksum = json_cask[:ruby_source_checksum] || # public JSON v2 + json_cask[:ruby_source_sha256]&.then { { "sha256" => _1 } } # internal JSON v3 end def to_s diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index c248ef03abc64..26ecfb55b608f 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -299,11 +299,11 @@ def load(config:) json_cask = Homebrew::API.merge_variations(json_cask).deep_symbolize_keys.freeze - # We could probably just default to the core cask tap in all cases without problems. - cask_options[:tap] = if Homebrew::API.internal_json_v3? + cask_options[:tap] = if json_cask.key?(:tap) # public JSON v2 + tap_value = json_cask.fetch(:tap) + Tap.fetch(tap_value) if tap_value.to_s.include?("/") + else # internal JSON v3 CoreCaskTap.instance - elsif json_cask[:tap].to_s.include?("/") - Tap.fetch(json_cask[:tap]) end user_agent = json_cask.dig(:url_specs, :user_agent)