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