aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Nagel2014-04-05 22:03:33 -0500
committerJack Nagel2014-04-05 22:03:39 -0500
commitca3688e33e1b98e67242e87a6b067b696f05effb (patch)
tree58d684db28b4a53e1300080d853d605e8f16b248
parent2616127a281b9cf7f15c3fb2cd8536741d0f1b6c (diff)
downloadbrew-ca3688e33e1b98e67242e87a6b067b696f05effb.tar.bz2
Eliminate repeated work in Formulary.factory
Much of the name resolution done in Formula.canonical_name is repeated Formulary.factory. Here we eliminate the repeated work by duplicating the code from canonical_name. Later we will refactor it so that both methods can share the bulk of the logic.
-rw-r--r--Library/Homebrew/formulary.rb61
1 files changed, 45 insertions, 16 deletions
diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb
index 34e40fa23..5e0fd4d3f 100644
--- a/Library/Homebrew/formulary.rb
+++ b/Library/Homebrew/formulary.rb
@@ -165,25 +165,54 @@ class Formulary
# * a formula URL
# * a local bottle reference
def self.factory ref
- # If a URL is passed, download to the cache and install
- if ref =~ %r[(https?|ftp)://]
- f = FromUrlLoader.new(ref)
- elsif ref =~ Pathname::BOTTLE_EXTNAME_RX
- f = BottleLoader.new(ref)
- else
- name_or_path = Formula.canonical_name(ref)
- if name_or_path =~ HOMEBREW_TAP_FORMULA_REGEX
- # name appears to be a tapped formula, so we don't munge it
- # in order to provide a useful error message when require fails.
- f = TapLoader.new(name_or_path)
- elsif name_or_path.include?("/") || File.extname(name_or_path) == ".rb"
- # If name was a path or mapped to a cached formula
- f = FromPathLoader.new(name_or_path)
+ loader_for(ref).get_formula
+ end
+
+ def self.loader_for(ref)
+ case ref
+ when %r[(https?|ftp)://]
+ return FromUrlLoader.new(ref)
+ when Pathname::BOTTLE_EXTNAME_RX
+ return BottleLoader.new(ref)
+ end
+
+ if ref =~ HOMEBREW_TAP_FORMULA_REGEX
+ tap_name = "#$1-#$2".downcase
+ tapd = Pathname.new("#{HOMEBREW_LIBRARY}/Taps/#{tap_name}")
+
+ if tapd.directory?
+ tapd.find_formula do |relative_pathname|
+ path = "#{tapd}/#{relative_pathname}"
+ if relative_pathname.stem.to_s == $3
+ return FromPathLoader.new(path)
+ end
+ end
else
- f = StandardLoader.new(name_or_path)
+ return TapLoader.new(ref)
end
end
- f.get_formula
+ if ref.include?("/") || File.extname(ref) == ".rb"
+ return FromPathLoader.new(ref)
+ end
+
+ formula_with_that_name = Formula.path(ref)
+ if formula_with_that_name.file? and formula_with_that_name.readable?
+ return StandardLoader.new(ref)
+ end
+
+ # test if the name is a formula alias
+ possible_alias = Pathname.new("#{HOMEBREW_LIBRARY}/Aliases/#{ref}")
+ if possible_alias.file?
+ name = possible_alias.resolved_path.basename(".rb").to_s
+ return StandardLoader.new(name)
+ end
+
+ possible_cached_formula = Pathname.new("#{HOMEBREW_CACHE_FORMULA}/#{ref}.rb")
+ if possible_cached_formula.file?
+ return FromPathLoader.new(possible_cached_formula.to_s)
+ end
+
+ return StandardLoader.new(ref)
end
end