diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index f3fcc3ac1e..a902e92820 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -395,6 +395,8 @@ module Formulary ) 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] @@ -411,6 +413,19 @@ module Formulary 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: + #
Formulary.map "foo", to: "bar"
+  # Formulary.factory "bar" # returns the bar formula
+  # 
+ # @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. diff --git a/Library/Homebrew/test/formulary_spec.rb b/Library/Homebrew/test/formulary_spec.rb index dfd1c8311a..454b064014 100644 --- a/Library/Homebrew/test/formulary_spec.rb +++ b/Library/Homebrew/test/formulary_spec.rb @@ -205,6 +205,23 @@ describe Formulary do 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("foo") + }.to raise_error(FormulaUnavailableError) + + described_class.map "foo", to: formula_name + + expect(described_class.factory("foo")).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