Isolate formula class namespaces
Closes Homebrew/homebrew#40151. Closes Homebrew/homebrew#40203.
This commit is contained in:
parent
bd5083f970
commit
45a71898e8
@ -32,8 +32,6 @@ class FormulaVersions
|
|||||||
path = Pathname.pwd.join("#{name}.rb")
|
path = Pathname.pwd.join("#{name}.rb")
|
||||||
path.write file_contents_at_revision(rev)
|
path.write file_contents_at_revision(rev)
|
||||||
|
|
||||||
old_const = Formulary.unload_formula(name)
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
nostdout { yield Formulary.factory(path.to_s) }
|
nostdout { yield Formulary.factory(path.to_s) }
|
||||||
rescue *IGNORED_EXCEPTIONS => e
|
rescue *IGNORED_EXCEPTIONS => e
|
||||||
@ -42,8 +40,6 @@ class FormulaVersions
|
|||||||
ohai "#{e} in #{name} at revision #{rev}", e.backtrace if ARGV.debug?
|
ohai "#{e} in #{name} at revision #{rev}", e.backtrace if ARGV.debug?
|
||||||
rescue FormulaUnavailableError
|
rescue FormulaUnavailableError
|
||||||
# Suppress this error
|
# Suppress this error
|
||||||
ensure
|
|
||||||
Formulary.restore_formula(name, old_const)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,45 +2,28 @@
|
|||||||
# It is not meant to be used directy from formulae.
|
# It is not meant to be used directy from formulae.
|
||||||
|
|
||||||
class Formulary
|
class Formulary
|
||||||
module Formulae
|
FORMULAE = {}
|
||||||
class << self
|
|
||||||
if instance_method(:const_defined?).arity == -1
|
def self.formula_class_defined?(path)
|
||||||
def formula_const_defined?(name)
|
FORMULAE.key?(path)
|
||||||
const_defined?(name, false)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def formula_const_get(name)
|
def self.formula_class_get(path)
|
||||||
const_get(name, false)
|
FORMULAE.fetch(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
else
|
||||||
def formula_const_defined?(name)
|
FORMULAE[path] = klass
|
||||||
const_defined?(name)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def formula_const_get(name)
|
|
||||||
const_get(name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def remove_formula_const(name)
|
|
||||||
remove_const(name)
|
|
||||||
end
|
|
||||||
|
|
||||||
def formula_const_set(name, value)
|
|
||||||
const_set(name, value)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.unload_formula formula_name
|
|
||||||
Formulae.remove_formula_const(class_s(formula_name))
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.class_s name
|
def self.class_s name
|
||||||
@ -57,13 +40,10 @@ 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)
|
def initialize(name, path)
|
||||||
@name = name
|
@name = name
|
||||||
@path = path.resolved_path
|
@path = path.resolved_path
|
||||||
@class_name = Formulary.class_s(name)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Gets the formula instance.
|
# Gets the formula instance.
|
||||||
@ -72,16 +52,8 @@ class Formulary
|
|||||||
end
|
end
|
||||||
|
|
||||||
def klass
|
def klass
|
||||||
begin
|
load_file unless Formulary.formula_class_defined?(path)
|
||||||
have_klass = Formulae.formula_const_defined?(class_name)
|
Formulary.formula_class_get(path)
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@ -89,7 +61,7 @@ class Formulary
|
|||||||
def load_file
|
def load_file
|
||||||
STDERR.puts "#{$0} (#{self.class.name}): loading #{path}" if ARGV.debug?
|
STDERR.puts "#{$0} (#{self.class.name}): loading #{path}" if ARGV.debug?
|
||||||
raise FormulaUnavailableError.new(name) unless path.file?
|
raise FormulaUnavailableError.new(name) unless path.file?
|
||||||
Formulae.module_eval(path.read, path)
|
Formulary.load_formula(name, path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user