diff --git a/Library/Homebrew/api.rb b/Library/Homebrew/api.rb index 52e22a1410..6003c7dd5e 100644 --- a/Library/Homebrew/api.rb +++ b/Library/Homebrew/api.rb @@ -108,5 +108,16 @@ module Homebrew cache[endpoint] = output.stdout end + + sig { params(json: Hash).returns(Hash) } + def merge_variations(json) + if (bottle_tag = ::Utils::Bottles.tag.to_s.presence) && + (variations = json["variations"].presence) && + (variation = variations[bottle_tag].presence) + json = json.merge(variation) + end + + json.except("variations") + end end end diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index 8d37c14cf2..d4b17d94a2 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -231,6 +231,11 @@ module Cask alias == eql? def to_h + if loaded_from_api && Homebrew::EnvConfig.install_from_api? + json_cask = Homebrew::API::Cask.all_casks[token] + return Homebrew::API.merge_variations(json_cask) + end + { "token" => token, "full_token" => full_name, @@ -257,6 +262,8 @@ module Cask end def to_hash_with_variations + return Homebrew::API::Cask.all_casks[token] if loaded_from_api && Homebrew::EnvConfig.install_from_api? + hash = to_h variations = {} diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index 16ceeb23af..7dba19805c 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -228,13 +228,7 @@ module Cask json_cask = @from_json || Homebrew::API::Cask.all_casks[token] cask_source = JSON.pretty_generate(json_cask) - if (bottle_tag = ::Utils::Bottles.tag.to_s.presence) && - (variations = json_cask["variations"].presence) && - (variation = variations[bottle_tag].presence) - json_cask = json_cask.merge(variation) - end - - json_cask.deep_symbolize_keys! + json_cask = Homebrew::API.merge_variations(json_cask).deep_symbolize_keys # Use the cask-source API if there are any `*flight` blocks or the cask has multiple languages if json_cask[:artifacts].any? { |artifact| FLIGHT_STANZAS.include?(artifact.keys.first) } || diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index 866d369833..d1d92be9b2 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -51,10 +51,6 @@ module Homebrew description: "Print a JSON representation. Currently the default value for is `v1` for " \ ". For and use `v2`. See the docs for examples of using the " \ "JSON output: " - switch "--bottle", - depends_on: "--json", - description: "Output information about the bottles for and its dependencies.", - hidden: true switch "--installed", depends_on: "--json", description: "Print JSON of formulae that are currently installed." @@ -79,10 +75,6 @@ module Homebrew conflicts "--installed", "--all" conflicts "--formula", "--cask" - %w[--cask --analytics --github].each do |conflict| - conflicts "--bottle", conflict - end - named_args [:formula, :cask] end end @@ -215,24 +207,12 @@ module Homebrew args.named.to_formulae end - if args.bottle? - formulae.map(&:to_recursive_bottle_hash) - elsif args.variations? + if args.variations? formulae.map(&:to_hash_with_variations) else formulae.map(&:to_hash) end when :v2 - # Cannot generate cask API JSON data from the cask JSON API - if EnvConfig.install_from_api? - ENV["HOMEBREW_NO_INSTALL_FROM_API"] = "1" - core_untapped = !CoreTap.instance.installed? - cask_untapped = !Tap.fetch("Homebrew/homebrew-cask").installed? - if core_untapped || cask_untapped - raise UsageError, "Tap homebrew/core and/or homebrew/cask to use `--json=v2`." - end - end - formulae, casks = if all [Formula.all.sort, Cask::Cask.all.sort_by(&:full_name)] elsif args.installed? @@ -241,9 +221,7 @@ module Homebrew args.named.to_formulae_to_casks end - if args.bottle? - { "formulae" => formulae.map(&:to_recursive_bottle_hash) } - elsif args.variations? + if args.variations? { "formulae" => formulae.map(&:to_hash_with_variations), "casks" => casks.map(&:to_hash_with_variations), diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 666f36eafe..23e2f8128c 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -2074,6 +2074,11 @@ class Formula # @private def to_hash + if self.class.loaded_from_api && Homebrew::EnvConfig.install_from_api? + json_formula = Homebrew::API::Formula.all_formulae[name] + return Homebrew::API.merge_variations(json_formula) + end + dependencies = deps hsh = { @@ -2185,6 +2190,10 @@ class Formula # @private def to_hash_with_variations + if self.class.loaded_from_api && Homebrew::EnvConfig.install_from_api? + return Homebrew::API::Formula.all_formulae[name] + end + hash = to_hash variations = {} @@ -2222,32 +2231,6 @@ class Formula hash end - # @api private - # Generate a hash to be used to install a formula from a JSON file - def to_recursive_bottle_hash(top_level: true) - bottle = bottle_hash - - bottles = bottle["files"].to_h do |tag, file| - info = { "url" => file["url"] } - info["sha256"] = file["sha256"] if tap.name != "homebrew/core" - [tag.to_s, info] - end - - hash = { - "name" => name, - "pkg_version" => pkg_version, - "rebuild" => bottle["rebuild"], - "bottles" => bottles, - } - - return hash unless top_level - - hash["dependencies"] = declared_runtime_dependencies.map do |dep| - dep.to_formula.to_recursive_bottle_hash(top_level: false) - end - hash - end - # Returns the bottle information for a formula def bottle_hash bottle_spec = stable.bottle_specification diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index f26508069e..e779d15ace 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -135,12 +135,7 @@ module Formulary class_s = Formulary.class_s(name) json_formula = Homebrew::API::Formula.all_formulae[name] - - if (bottle_tag = Utils::Bottles.tag.to_s.presence) && - (variations = json_formula["variations"].presence) && - (variation = variations[bottle_tag].presence) - json_formula = json_formula.merge(variation) - end + json_formula = Homebrew::API.merge_variations(json_formula) uses_from_macos_names = json_formula["uses_from_macos"].map do |dep| next dep unless dep.is_a? Hash diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 6248ca8a9d..87d92614ef 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -997,25 +997,6 @@ describe Formula do end end - specify "#to_recursive_bottle_hash" do - f1 = formula "foo" do - url "foo-1.0" - - bottle do - sha256 cellar: :any, Utils::Bottles.tag.to_sym => TEST_SHA256 - sha256 cellar: :any, foo: TEST_SHA256 - end - end - - h = f1.to_recursive_bottle_hash - - expect(h).to be_a(Hash) - expect(h["name"]).to eq "foo" - expect(h["bottles"].keys).to eq [Utils::Bottles.tag.to_s, "x86_64_foo"] - expect(h["bottles"][Utils::Bottles.tag.to_s].keys).to eq ["url"] - expect(h["dependencies"]).to eq [] - end - describe "#eligible_kegs_for_cleanup" do it "returns Kegs eligible for cleanup" do f1 = Class.new(Testball) do diff --git a/completions/zsh/_brew b/completions/zsh/_brew index 2a90f78d7f..f61957df29 100644 --- a/completions/zsh/_brew +++ b/completions/zsh/_brew @@ -375,12 +375,12 @@ _brew__s() { # brew abv _brew_abv() { _arguments \ - '(--bottle)--analytics[List global Homebrew analytics data or, if specified, installation and build error data for formula (provided neither `HOMEBREW_NO_ANALYTICS` nor `HOMEBREW_NO_GITHUB_API` are set)]' \ + '--analytics[List global Homebrew analytics data or, if specified, installation and build error data for formula (provided neither `HOMEBREW_NO_ANALYTICS` nor `HOMEBREW_NO_GITHUB_API` are set)]' \ '--category[Which type of analytics data to retrieve. The value for category must be `install`, `install-on-request` or `build-error`; `cask-install` or `os-version` may be specified if formula is not. The default is `install`]' \ '--days[How many days of analytics data to retrieve. The value for days must be `30`, `90` or `365`. The default is `30`]' \ '--debug[Display any debugging information]' \ '(--installed)--eval-all[Evaluate all available formulae and casks, whether installed or not, to print their JSON. Implied if `HOMEBREW_EVAL_ALL` is set]' \ - '(--bottle)--github[Open the GitHub source page for formula and cask in a browser. To view the history locally: `brew log -p` formula or cask]' \ + '--github[Open the GitHub source page for formula and cask in a browser. To view the history locally: `brew log -p` formula or cask]' \ '--help[Show this message]' \ '(--eval-all --all)--installed[Print JSON of formulae that are currently installed]' \ '--json[Print a JSON representation. Currently the default value for version is `v1` for formula. For formula and cask use `v2`. See the docs for examples of using the JSON output: https://docs.brew.sh/Querying-Brew]' \ @@ -391,7 +391,7 @@ _brew_abv() { '(--cask)--formula[Treat all named arguments as formulae]' \ '*::formula:__brew_formulae' \ - cask \ - '(--formula --bottle)--cask[Treat all named arguments as casks]' \ + '(--formula)--cask[Treat all named arguments as casks]' \ '*::cask:__brew_casks' } @@ -950,12 +950,12 @@ _brew_homepage() { # brew info _brew_info() { _arguments \ - '(--bottle)--analytics[List global Homebrew analytics data or, if specified, installation and build error data for formula (provided neither `HOMEBREW_NO_ANALYTICS` nor `HOMEBREW_NO_GITHUB_API` are set)]' \ + '--analytics[List global Homebrew analytics data or, if specified, installation and build error data for formula (provided neither `HOMEBREW_NO_ANALYTICS` nor `HOMEBREW_NO_GITHUB_API` are set)]' \ '--category[Which type of analytics data to retrieve. The value for category must be `install`, `install-on-request` or `build-error`; `cask-install` or `os-version` may be specified if formula is not. The default is `install`]' \ '--days[How many days of analytics data to retrieve. The value for days must be `30`, `90` or `365`. The default is `30`]' \ '--debug[Display any debugging information]' \ '(--installed)--eval-all[Evaluate all available formulae and casks, whether installed or not, to print their JSON. Implied if `HOMEBREW_EVAL_ALL` is set]' \ - '(--bottle)--github[Open the GitHub source page for formula and cask in a browser. To view the history locally: `brew log -p` formula or cask]' \ + '--github[Open the GitHub source page for formula and cask in a browser. To view the history locally: `brew log -p` formula or cask]' \ '--help[Show this message]' \ '(--eval-all --all)--installed[Print JSON of formulae that are currently installed]' \ '--json[Print a JSON representation. Currently the default value for version is `v1` for formula. For formula and cask use `v2`. See the docs for examples of using the JSON output: https://docs.brew.sh/Querying-Brew]' \ @@ -966,7 +966,7 @@ _brew_info() { '(--cask)--formula[Treat all named arguments as formulae]' \ '*::formula:__brew_formulae' \ - cask \ - '(--formula --bottle)--cask[Treat all named arguments as casks]' \ + '(--formula)--cask[Treat all named arguments as casks]' \ '*::cask:__brew_casks' }