Merge pull request #11488 from Rylan12/formula-to-bottle-hash

formula: add `to_recursive_bottle_hash` method
This commit is contained in:
Rylan Polster 2021-06-07 16:11:46 -04:00 committed by GitHub
commit 07d098bc76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 5 deletions

View File

@ -50,6 +50,9 @@ 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."
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."
@ -66,6 +69,10 @@ 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
@ -184,7 +191,11 @@ module Homebrew
args.named.to_formulae args.named.to_formulae
end end
formulae.map(&:to_hash) if args.bottle?
formulae.map(&:to_recursive_bottle_hash)
else
formulae.map(&:to_hash)
end
when :v2 when :v2
formulae, casks = if args.all? formulae, casks = if args.all?
[Formula.sort, Cask::Cask.to_a.sort_by(&:full_name)] [Formula.sort, Cask::Cask.to_a.sort_by(&:full_name)]
@ -194,10 +205,14 @@ module Homebrew
args.named.to_formulae_to_casks args.named.to_formulae_to_casks
end end
{ if args.bottle?
"formulae" => formulae.map(&:to_hash), { "formulae" => formulae.map(&:to_recursive_bottle_hash) }
"casks" => casks.map(&:to_h), else
} {
"formulae" => formulae.map(&:to_hash),
"casks" => casks.map(&:to_h),
}
end
else else
raise raise
end end

View File

@ -1908,6 +1908,30 @@ class Formula
hsh hsh
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"].map do |tag, file|
info = { "url" => file["url"] }
info["sha256"] = file["sha256"] if tap.name != "homebrew/core"
[tag.to_s, info]
end.to_h
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" => dependencies,
}
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

@ -871,6 +871,25 @@ describe Formula do
expect(h["versions"]["bottle"]).to be_truthy expect(h["versions"]["bottle"]).to be_truthy
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