diff options
| author | Jack Nagel | 2015-06-01 19:20:11 -0400 |
|---|---|---|
| committer | Jack Nagel | 2015-06-01 19:24:47 -0400 |
| commit | 45a71898e8f06caf334e3c6baefb974504681744 (patch) | |
| tree | 2f2b8850801b039437be30d30d116b227ab1114e /Library | |
| parent | bd5083f970e26b3978f26493300951f9be0fad75 (diff) | |
| download | brew-45a71898e8f06caf334e3c6baefb974504681744.tar.bz2 | |
Isolate formula class namespaces
Closes Homebrew/homebrew#40151.
Closes Homebrew/homebrew#40203.
Diffstat (limited to 'Library')
| -rw-r--r-- | Library/Homebrew/formula_versions.rb | 4 | ||||
| -rw-r--r-- | Library/Homebrew/formulary.rb | 68 |
2 files changed, 20 insertions, 52 deletions
diff --git a/Library/Homebrew/formula_versions.rb b/Library/Homebrew/formula_versions.rb index fa6446f36..715124406 100644 --- a/Library/Homebrew/formula_versions.rb +++ b/Library/Homebrew/formula_versions.rb @@ -32,8 +32,6 @@ class FormulaVersions path = Pathname.pwd.join("#{name}.rb") path.write file_contents_at_revision(rev) - old_const = Formulary.unload_formula(name) - begin nostdout { yield Formulary.factory(path.to_s) } rescue *IGNORED_EXCEPTIONS => e @@ -42,8 +40,6 @@ class FormulaVersions ohai "#{e} in #{name} at revision #{rev}", e.backtrace if ARGV.debug? rescue FormulaUnavailableError # Suppress this error - ensure - Formulary.restore_formula(name, old_const) end end end diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 398117ab9..65ab0b68f 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -2,45 +2,28 @@ # It is not meant to be used directy from formulae. class Formulary - module Formulae - class << self - if instance_method(:const_defined?).arity == -1 - def formula_const_defined?(name) - const_defined?(name, false) - end - - def formula_const_get(name) - const_get(name, false) - end - else - def formula_const_defined?(name) - const_defined?(name) - end - - def formula_const_get(name) - const_get(name) - end - end - - def remove_formula_const(name) - remove_const(name) - end + FORMULAE = {} - def formula_const_set(name, value) - const_set(name, value) - end - end + def self.formula_class_defined?(path) + FORMULAE.key?(path) end - def self.unload_formula formula_name - Formulae.remove_formula_const(class_s(formula_name)) + def self.formula_class_get(path) + FORMULAE.fetch(path) end - def self.restore_formula formula_name, value - old_verbose, $VERBOSE = $VERBOSE, nil - Formulae.formula_const_set(class_s(formula_name), value) - ensure - $VERBOSE = old_verbose + def self.load_formula(name, path) + mod = Module.new + mod.module_eval(path.read, path) + class_name = class_s(name) + + begin + klass = mod.const_get(class_name) + rescue NameError => e + raise FormulaUnavailableError, name, e.backtrace + else + FORMULAE[path] = klass + end end def self.class_s name @@ -57,13 +40,10 @@ class Formulary attr_reader :name # The formula's ruby file's path or filename attr_reader :path - # The ruby constant name of the formula's class - attr_reader :class_name def initialize(name, path) @name = name @path = path.resolved_path - @class_name = Formulary.class_s(name) end # Gets the formula instance. @@ -72,16 +52,8 @@ class Formulary end def klass - begin - have_klass = Formulae.formula_const_defined?(class_name) - rescue NameError => e - raise unless e.name.to_s == class_name - raise FormulaUnavailableError, name, e.backtrace - end - - load_file unless have_klass - - Formulae.formula_const_get(class_name) + load_file unless Formulary.formula_class_defined?(path) + Formulary.formula_class_get(path) end private @@ -89,7 +61,7 @@ class Formulary def load_file STDERR.puts "#{$0} (#{self.class.name}): loading #{path}" if ARGV.debug? raise FormulaUnavailableError.new(name) unless path.file? - Formulae.module_eval(path.read, path) + Formulary.load_formula(name, path) end end |
