Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formulary: map formula names to local bottle paths #11534

Merged
merged 5 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ def self.factory(
)
raise ArgumentError, "Formulae must have a ref!" unless ref

ref = @ref_mappings[ref] if @ref_mappings.present? && @ref_mappings.key?(ref)

cache_key = "#{ref}-#{spec}-#{alias_path}-#{from}"
if factory_cached? && cache[:formulary_factory] &&
cache[:formulary_factory][cache_key]
Expand All @@ -411,6 +413,19 @@ def self.factory(
formula
end

# Register a reference mapping. This mapping will be used by {Formulary::factory}
# to allow certain references to be substituted for another string before
# being retrived. For example, to map `foo` to the `bar` formula:
# <pre>Formulary.map "foo", to: "bar"
# Formulary.factory "bar" # returns the bar formula
# </pre>
# @param ref the string to map.
# @param :to the target reference to which `ref` should be mapped.
def self.map(ref, to:)
@ref_mappings ||= {}
@ref_mappings[ref] = to
end

# Return a {Formula} instance for the given rack.
#
# @param spec when nil, will auto resolve the formula's spec.
Expand Down
17 changes: 17 additions & 0 deletions Library/Homebrew/test/formulary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,23 @@ class Wrong#{described_class.class_s(formula_name)} < Formula
end
end

describe "::map" do
before do
formula_path.dirname.mkpath
formula_path.write formula_content
end

it "maps a reference to a new Formula" do
expect {
described_class.factory("formula-to-map")
}.to raise_error(FormulaUnavailableError)

described_class.map "formula-to-map", to: formula_name

expect(described_class.factory("formula-to-map")).to be_kind_of(Formula)
end
end

specify "::from_contents" do
expect(described_class.from_contents(formula_name, formula_path, formula_content)).to be_kind_of(Formula)
end
Expand Down