From 8b0f7e7ada254c8b2a046776174b6ff1b80a5499 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 3 Jun 2021 12:59:42 -0400 Subject: [PATCH 1/5] formula: add `to_bottle_hash` method --- Library/Homebrew/formula.rb | 21 +++++++++++++++++++++ Library/Homebrew/test/formula_spec.rb | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 35b5d565ff..aef4a16498 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1908,6 +1908,27 @@ class Formula hsh end + # @api private + # Generate a hash to be used to install a formula from a JSON file + def to_bottle_hash(top_level: true) + bottle = bottle_hash + + bottles = bottle["files"].map do |tag, file| + info = { + "url" => file["url"], + "sha256" => file["sha256"], + } + [tag.to_s, info] + end.to_h + + return bottles unless top_level + + { + "bottles" => bottles, + "dependencies" => deps.map { |dep| dep.to_formula.to_bottle_hash(top_level: false) }, + } + end + # Returns the bottle information for a formula def bottle_hash bottle_spec = stable.bottle_specification diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 2f13cd771e..fa39085558 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -871,6 +871,25 @@ describe Formula do expect(h["versions"]["bottle"]).to be_truthy end + specify "#to_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_bottle_hash + + expect(h).to be_a(Hash) + 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 + expect(h["dependencies"]).to eq [] + end + describe "#eligible_kegs_for_cleanup" do it "returns Kegs eligible for cleanup" do f1 = Class.new(Testball) do From f8a58c27a01e416de244074369d794d871e26a9f Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 3 Jun 2021 13:33:26 -0400 Subject: [PATCH 2/5] formula: use `declared_runtime_dependencies` in `to_bottle_hash` --- Library/Homebrew/formula.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index aef4a16498..f0029880c4 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1925,7 +1925,7 @@ class Formula { "bottles" => bottles, - "dependencies" => deps.map { |dep| dep.to_formula.to_bottle_hash(top_level: false) }, + "dependencies" => declared_runtime_dependencies.map { |dep| dep.to_formula.to_bottle_hash(top_level: false) }, } end From d60f549a483a1a0d1f7061f8b8f1a68fc39ad8c4 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Sat, 5 Jun 2021 12:27:24 -0400 Subject: [PATCH 3/5] info: add `--bottle` option for JSON bottle info --- Library/Homebrew/cmd/info.rb | 25 ++++++++++++++++++++----- Library/Homebrew/formula.rb | 9 +++++++-- Library/Homebrew/test/formula_spec.rb | 5 +++-- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index 9da3a45d7d..fb61f2c546 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -50,6 +50,9 @@ 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." 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 - formulae.map(&:to_hash) + 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 - { - "formulae" => formulae.map(&:to_hash), - "casks" => casks.map(&:to_h), - } + 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 diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index f0029880c4..c4320a2167 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -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 diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index fa39085558..a48cf21185 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -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 From aebaa7f8dfa3f2a63313e119dc62c7aacd6beba6 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Sat, 5 Jun 2021 12:31:55 -0400 Subject: [PATCH 4/5] formula: only include `sha256` for core formulae in bottle JSON --- Library/Homebrew/formula.rb | 6 ++---- Library/Homebrew/test/formula_spec.rb | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index c4320a2167..176077eb01 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1914,10 +1914,8 @@ class Formula bottle = bottle_hash bottles = bottle["files"].map do |tag, file| - info = { - "url" => file["url"], - "sha256" => file["sha256"], - } + info = { "url" => file["url"] } + info["sha256"] = file["sha256"] unless tap.name == "homebrew/core" [tag.to_s, info] end.to_h diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index a48cf21185..63140706bd 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -886,8 +886,7 @@ describe Formula do 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 + expect(h["bottles"][Utils::Bottles.tag.to_s].keys).to eq ["url"] expect(h["dependencies"]).to eq [] end From a2a92ba37500a7159979a7c1099bbaa5a0b6ba96 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 7 Jun 2021 10:19:14 -0400 Subject: [PATCH 5/5] Update Library/Homebrew/formula.rb Co-authored-by: Mike McQuaid --- Library/Homebrew/formula.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 176077eb01..b22336f8ac 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1915,7 +1915,7 @@ class Formula bottles = bottle["files"].map do |tag, file| info = { "url" => file["url"] } - info["sha256"] = file["sha256"] unless tap.name == "homebrew/core" + info["sha256"] = file["sha256"] if tap.name != "homebrew/core" [tag.to_s, info] end.to_h