Allow brew info --json=v2 without taps with JSON API

This commit is contained in:
Rylan Polster 2023-02-10 12:07:36 -05:00 committed by Mike McQuaid
parent f4f22bc937
commit f7fa5937d8
No known key found for this signature in database
GPG Key ID: 3338A31AFDB1D829
8 changed files with 37 additions and 88 deletions

View File

@ -108,5 +108,16 @@ module Homebrew
cache[endpoint] = output.stdout cache[endpoint] = output.stdout
end 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
end end

View File

@ -231,6 +231,11 @@ module Cask
alias == eql? alias == eql?
def to_h 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, "token" => token,
"full_token" => full_name, "full_token" => full_name,
@ -257,6 +262,8 @@ module Cask
end end
def to_hash_with_variations def to_hash_with_variations
return Homebrew::API::Cask.all_casks[token] if loaded_from_api && Homebrew::EnvConfig.install_from_api?
hash = to_h hash = to_h
variations = {} variations = {}

View File

@ -228,13 +228,7 @@ module Cask
json_cask = @from_json || Homebrew::API::Cask.all_casks[token] json_cask = @from_json || Homebrew::API::Cask.all_casks[token]
cask_source = JSON.pretty_generate(json_cask) cask_source = JSON.pretty_generate(json_cask)
if (bottle_tag = ::Utils::Bottles.tag.to_s.presence) && json_cask = Homebrew::API.merge_variations(json_cask).deep_symbolize_keys
(variations = json_cask["variations"].presence) &&
(variation = variations[bottle_tag].presence)
json_cask = json_cask.merge(variation)
end
json_cask.deep_symbolize_keys!
# Use the cask-source API if there are any `*flight` blocks or the cask has multiple languages # 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) } || if json_cask[:artifacts].any? { |artifact| FLIGHT_STANZAS.include?(artifact.keys.first) } ||

View File

@ -51,10 +51,6 @@ module Homebrew
description: "Print a JSON representation. Currently the default value for <version> is `v1` for " \ description: "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 " \ "<formula>. For <formula> and <cask> use `v2`. See the docs for examples of using the " \
"JSON output: <https://docs.brew.sh/Querying-Brew>" "JSON output: <https://docs.brew.sh/Querying-Brew>"
switch "--bottle",
depends_on: "--json",
description: "Output information about the bottles for <formula> and its dependencies.",
hidden: true
switch "--installed", switch "--installed",
depends_on: "--json", depends_on: "--json",
description: "Print JSON of formulae that are currently installed." description: "Print JSON of formulae that are currently installed."
@ -79,10 +75,6 @@ module Homebrew
conflicts "--installed", "--all" conflicts "--installed", "--all"
conflicts "--formula", "--cask" conflicts "--formula", "--cask"
%w[--cask --analytics --github].each do |conflict|
conflicts "--bottle", conflict
end
named_args [:formula, :cask] named_args [:formula, :cask]
end end
end end
@ -215,24 +207,12 @@ module Homebrew
args.named.to_formulae args.named.to_formulae
end end
if args.bottle? if args.variations?
formulae.map(&:to_recursive_bottle_hash)
elsif args.variations?
formulae.map(&:to_hash_with_variations) formulae.map(&:to_hash_with_variations)
else else
formulae.map(&:to_hash) formulae.map(&:to_hash)
end end
when :v2 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 formulae, casks = if all
[Formula.all.sort, Cask::Cask.all.sort_by(&:full_name)] [Formula.all.sort, Cask::Cask.all.sort_by(&:full_name)]
elsif args.installed? elsif args.installed?
@ -241,9 +221,7 @@ module Homebrew
args.named.to_formulae_to_casks args.named.to_formulae_to_casks
end end
if args.bottle? if args.variations?
{ "formulae" => formulae.map(&:to_recursive_bottle_hash) }
elsif args.variations?
{ {
"formulae" => formulae.map(&:to_hash_with_variations), "formulae" => formulae.map(&:to_hash_with_variations),
"casks" => casks.map(&:to_hash_with_variations), "casks" => casks.map(&:to_hash_with_variations),

View File

@ -2074,6 +2074,11 @@ class Formula
# @private # @private
def to_hash 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 dependencies = deps
hsh = { hsh = {
@ -2185,6 +2190,10 @@ class Formula
# @private # @private
def to_hash_with_variations 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 hash = to_hash
variations = {} variations = {}
@ -2222,32 +2231,6 @@ class Formula
hash hash
end 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 # Returns the bottle information for a formula
def bottle_hash def bottle_hash
bottle_spec = stable.bottle_specification bottle_spec = stable.bottle_specification

View File

@ -135,12 +135,7 @@ module Formulary
class_s = Formulary.class_s(name) class_s = Formulary.class_s(name)
json_formula = Homebrew::API::Formula.all_formulae[name] json_formula = Homebrew::API::Formula.all_formulae[name]
json_formula = Homebrew::API.merge_variations(json_formula)
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
uses_from_macos_names = json_formula["uses_from_macos"].map do |dep| uses_from_macos_names = json_formula["uses_from_macos"].map do |dep|
next dep unless dep.is_a? Hash next dep unless dep.is_a? Hash

View File

@ -997,25 +997,6 @@ describe Formula do
end end
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 describe "#eligible_kegs_for_cleanup" do
it "returns Kegs eligible for cleanup" do it "returns Kegs eligible for cleanup" do
f1 = Class.new(Testball) do f1 = Class.new(Testball) do

View File

@ -375,12 +375,12 @@ _brew__s() {
# brew abv # brew abv
_brew_abv() { _brew_abv() {
_arguments \ _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`]' \ '--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`]' \ '--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]' \ '--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]' \ '(--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]' \ '--help[Show this message]' \
'(--eval-all --all)--installed[Print JSON of formulae that are currently installed]' \ '(--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]' \ '--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]' \ '(--cask)--formula[Treat all named arguments as formulae]' \
'*::formula:__brew_formulae' \ '*::formula:__brew_formulae' \
- cask \ - cask \
'(--formula --bottle)--cask[Treat all named arguments as casks]' \ '(--formula)--cask[Treat all named arguments as casks]' \
'*::cask:__brew_casks' '*::cask:__brew_casks'
} }
@ -950,12 +950,12 @@ _brew_homepage() {
# brew info # brew info
_brew_info() { _brew_info() {
_arguments \ _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`]' \ '--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`]' \ '--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]' \ '--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]' \ '(--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]' \ '--help[Show this message]' \
'(--eval-all --all)--installed[Print JSON of formulae that are currently installed]' \ '(--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]' \ '--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]' \ '(--cask)--formula[Treat all named arguments as formulae]' \
'*::formula:__brew_formulae' \ '*::formula:__brew_formulae' \
- cask \ - cask \
'(--formula --bottle)--cask[Treat all named arguments as casks]' \ '(--formula)--cask[Treat all named arguments as casks]' \
'*::cask:__brew_casks' '*::cask:__brew_casks'
} }