Remove need for class name cache by only computing it once per formula
This commit is contained in:
parent
1207ba4f8c
commit
3bad664c39
@ -5,12 +5,12 @@ class Formulary
|
|||||||
Object.send(:remove_const, class_s(formula_name))
|
Object.send(:remove_const, class_s(formula_name))
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.formula_class_defined? formula_name
|
def self.formula_class_defined? class_name
|
||||||
Object.const_defined?(class_s(formula_name))
|
Object.const_defined?(class_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get_formula_class formula_name
|
def self.get_formula_class class_name
|
||||||
Object.const_get(class_s(formula_name))
|
Object.const_get(class_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.restore_formula formula_name, value
|
def self.restore_formula formula_name, value
|
||||||
@ -21,12 +21,10 @@ class Formulary
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.class_s name
|
def self.class_s name
|
||||||
(@class_s ||= {}).fetch(name) do
|
|
||||||
class_name = name.capitalize
|
class_name = name.capitalize
|
||||||
class_name.gsub!(/[-_.\s]([a-zA-Z0-9])/) { $1.upcase }
|
class_name.gsub!(/[-_.\s]([a-zA-Z0-9])/) { $1.upcase }
|
||||||
class_name.gsub!('+', 'x')
|
class_name.gsub!('+', 'x')
|
||||||
@class_s[name] = class_name
|
class_name
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# A FormulaLoader returns instances of formulae.
|
# A FormulaLoader returns instances of formulae.
|
||||||
@ -36,6 +34,14 @@ class Formulary
|
|||||||
attr_reader :name
|
attr_reader :name
|
||||||
# The formula's ruby file's path or filename
|
# The formula's ruby file's path or filename
|
||||||
attr_reader :path
|
attr_reader :path
|
||||||
|
# The ruby constant name of the formula's class
|
||||||
|
attr_reader :class_name
|
||||||
|
|
||||||
|
def initialize(name, path)
|
||||||
|
@name = name
|
||||||
|
@path = path
|
||||||
|
@class_name = Formulary.class_s(name)
|
||||||
|
end
|
||||||
|
|
||||||
# Gets the formula instance.
|
# Gets the formula instance.
|
||||||
def get_formula
|
def get_formula
|
||||||
@ -46,7 +52,7 @@ class Formulary
|
|||||||
# it has not been parsed before.
|
# it has not been parsed before.
|
||||||
def klass
|
def klass
|
||||||
begin
|
begin
|
||||||
have_klass = Formulary.formula_class_defined? name
|
have_klass = Formulary.formula_class_defined? class_name
|
||||||
rescue NameError
|
rescue NameError
|
||||||
raise FormulaUnavailableError.new(name)
|
raise FormulaUnavailableError.new(name)
|
||||||
end
|
end
|
||||||
@ -65,7 +71,7 @@ class Formulary
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
klass = Formulary.get_formula_class(name)
|
klass = Formulary.get_formula_class(class_name)
|
||||||
if klass == Formula || !(klass < Formula)
|
if klass == Formula || !(klass < Formula)
|
||||||
raise FormulaUnavailableError.new(name)
|
raise FormulaUnavailableError.new(name)
|
||||||
end
|
end
|
||||||
@ -82,11 +88,12 @@ class Formulary
|
|||||||
if ARGV.homebrew_developer?
|
if ARGV.homebrew_developer?
|
||||||
opoo "Add a new regex to bottle_version.rb to parse this filename."
|
opoo "Add a new regex to bottle_version.rb to parse this filename."
|
||||||
end
|
end
|
||||||
@name = bottle_name
|
name = bottle_name
|
||||||
else
|
else
|
||||||
@name = name_without_version
|
name = name_without_version
|
||||||
end
|
end
|
||||||
@path = Formula.path(@name)
|
|
||||||
|
super name, Formula.path(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_formula
|
def get_formula
|
||||||
@ -99,8 +106,7 @@ class Formulary
|
|||||||
# Loads formulae from Homebrew's provided Library
|
# Loads formulae from Homebrew's provided Library
|
||||||
class StandardLoader < FormulaLoader
|
class StandardLoader < FormulaLoader
|
||||||
def initialize name
|
def initialize name
|
||||||
@name = name
|
super name, Formula.path(name)
|
||||||
@path = Formula.path(name)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -110,9 +116,8 @@ class Formulary
|
|||||||
# require allows filenames to drop the .rb extension, but everything else
|
# require allows filenames to drop the .rb extension, but everything else
|
||||||
# in our codebase will require an exact and fullpath.
|
# in our codebase will require an exact and fullpath.
|
||||||
path = "#{path}.rb" unless path =~ /\.rb$/
|
path = "#{path}.rb" unless path =~ /\.rb$/
|
||||||
|
path = Pathname.new(path).expand_path
|
||||||
@path = Pathname.new(path).expand_path
|
super path.stem, path
|
||||||
@name = @path.stem
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -122,13 +127,12 @@ class Formulary
|
|||||||
|
|
||||||
def initialize url
|
def initialize url
|
||||||
@url = url
|
@url = url
|
||||||
@path = HOMEBREW_CACHE_FORMULA/File.basename(url)
|
super File.basename(url, ".rb"), HOMEBREW_CACHE_FORMULA/File.basename(url)
|
||||||
@name = File.basename(url, '.rb')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Downloads the formula's .rb file
|
# Downloads the formula's .rb file
|
||||||
def fetch
|
def fetch
|
||||||
unless Formulary.formula_class_defined? name
|
unless Formulary.formula_class_defined? class_name
|
||||||
HOMEBREW_CACHE_FORMULA.mkpath
|
HOMEBREW_CACHE_FORMULA.mkpath
|
||||||
FileUtils.rm path.to_s, :force => true
|
FileUtils.rm path.to_s, :force => true
|
||||||
curl url, '-o', path.to_s
|
curl url, '-o', path.to_s
|
||||||
@ -144,8 +148,7 @@ class Formulary
|
|||||||
# Loads tapped formulae.
|
# Loads tapped formulae.
|
||||||
class TapLoader < FormulaLoader
|
class TapLoader < FormulaLoader
|
||||||
def initialize tapped_name
|
def initialize tapped_name
|
||||||
@name = tapped_name
|
super tapped_name, Pathname.new(tapped_name)
|
||||||
@path = Pathname.new(tapped_name)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_formula
|
def get_formula
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user