info: add --bottle option for JSON bottle info

This commit is contained in:
Rylan Polster 2021-06-05 12:27:24 -04:00
parent f8a58c27a0
commit d60f549a48
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
3 changed files with 30 additions and 9 deletions

View File

@ -50,6 +50,9 @@ module Homebrew
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 "\
"JSON output: <https://docs.brew.sh/Querying-Brew>"
switch "--bottle",
depends_on: "--json",
description: "Output information about the bottles for <formula> and its dependencies."
switch "--installed",
depends_on: "--json",
description: "Print JSON of formulae that are currently installed."
@ -66,6 +69,10 @@ 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
@ -184,7 +191,11 @@ module Homebrew
args.named.to_formulae
end
if args.bottle?
formulae.map(&:to_recursive_bottle_hash)
else
formulae.map(&:to_hash)
end
when :v2
formulae, casks = if args.all?
[Formula.sort, Cask::Cask.to_a.sort_by(&:full_name)]
@ -194,10 +205,14 @@ module Homebrew
args.named.to_formulae_to_casks
end
if args.bottle?
{ "formulae" => formulae.map(&:to_recursive_bottle_hash) }
else
{
"formulae" => formulae.map(&:to_hash),
"casks" => casks.map(&:to_h),
}
end
else
raise
end

View File

@ -1910,7 +1910,7 @@ class Formula
# @api private
# Generate a hash to be used to install a formula from a JSON file
def to_bottle_hash(top_level: true)
def to_recursive_bottle_hash(top_level: true)
bottle = bottle_hash
bottles = bottle["files"].map do |tag, file|
@ -1923,9 +1923,14 @@ class Formula
return bottles unless top_level
dependencies = declared_runtime_dependencies.map do |dep|
dep.to_formula.to_recursive_bottle_hash(top_level: false)
end
{
"name" => name,
"bottles" => bottles,
"dependencies" => declared_runtime_dependencies.map { |dep| dep.to_formula.to_bottle_hash(top_level: false) },
"dependencies" => dependencies,
}
end

View File

@ -871,7 +871,7 @@ describe Formula do
expect(h["versions"]["bottle"]).to be_truthy
end
specify "#to_bottle_hash" do
specify "#to_recursive_bottle_hash" do
f1 = formula "foo" do
url "foo-1.0"
@ -881,9 +881,10 @@ describe Formula do
end
end
h = f1.to_bottle_hash
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", "sha256"]
expect(h["bottles"][Utils::Bottles.tag.to_s]["sha256"]).to eq TEST_SHA256