2013-06-28 17:23:05 -04:00
|
|
|
module Mutant
|
|
|
|
# Zombifier namespace
|
2014-04-04 10:14:24 -04:00
|
|
|
class Zombifier
|
2015-06-06 18:29:47 -04:00
|
|
|
include Anima.new(
|
|
|
|
:includes,
|
|
|
|
:load_path,
|
|
|
|
:kernel,
|
2015-11-15 15:13:48 -05:00
|
|
|
:namespace,
|
|
|
|
:pathname,
|
2015-06-06 18:29:47 -04:00
|
|
|
:require_highjack,
|
2015-11-15 15:13:48 -05:00
|
|
|
:root_require
|
2015-06-06 18:29:47 -04:00
|
|
|
)
|
2015-11-15 15:13:48 -05:00
|
|
|
|
2015-10-30 23:59:29 -04:00
|
|
|
private(*anima.attribute_names)
|
2014-04-04 10:14:24 -04:00
|
|
|
|
2015-06-06 18:29:47 -04:00
|
|
|
include AST::Sexp
|
2014-04-04 11:46:55 -04:00
|
|
|
|
2015-06-06 18:29:47 -04:00
|
|
|
LoadError = Class.new(::LoadError)
|
2014-04-04 10:14:24 -04:00
|
|
|
|
|
|
|
# Initialize object
|
|
|
|
#
|
|
|
|
# @param [Symbol] namespace
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
2015-06-06 18:29:47 -04:00
|
|
|
def initialize(*)
|
|
|
|
super
|
|
|
|
@includes = %r{\A#{Regexp.union(includes)}(?:/.*)?\z}
|
2014-04-04 11:46:55 -04:00
|
|
|
@zombified = Set.new
|
2014-04-04 10:14:24 -04:00
|
|
|
end
|
2013-06-28 17:23:05 -04:00
|
|
|
|
2015-07-01 23:35:54 -04:00
|
|
|
# Call zombifier
|
|
|
|
#
|
|
|
|
# @return [self]
|
2015-06-06 18:29:47 -04:00
|
|
|
def self.call(*args)
|
|
|
|
new(*args).__send__(:call)
|
2014-06-28 16:52:47 -04:00
|
|
|
self
|
2014-04-04 10:14:24 -04:00
|
|
|
end
|
2013-06-28 17:23:05 -04:00
|
|
|
|
2015-06-06 18:29:47 -04:00
|
|
|
private
|
|
|
|
|
2014-04-04 10:14:24 -04:00
|
|
|
# Run zombifier
|
2013-06-28 17:23:05 -04:00
|
|
|
#
|
2014-04-04 10:14:24 -04:00
|
|
|
# @return [undefined]
|
2015-06-06 18:29:47 -04:00
|
|
|
def call
|
|
|
|
@original = require_highjack.call(method(:require))
|
|
|
|
require(root_require)
|
2013-06-28 17:23:05 -04:00
|
|
|
end
|
|
|
|
|
2014-04-04 11:46:55 -04:00
|
|
|
# Test if logical name is subjected to zombification
|
|
|
|
#
|
|
|
|
# @param [String]
|
|
|
|
def include?(logical_name)
|
2015-10-30 23:59:29 -04:00
|
|
|
!@zombified.include?(logical_name) && includes =~ logical_name
|
2014-04-04 11:46:55 -04:00
|
|
|
end
|
|
|
|
|
2014-04-04 10:14:24 -04:00
|
|
|
# Require file in zombie namespace
|
2013-06-28 17:23:05 -04:00
|
|
|
#
|
2014-06-15 11:50:38 -04:00
|
|
|
# @param [#to_s] logical_name
|
2013-06-28 17:23:05 -04:00
|
|
|
#
|
2015-06-06 18:29:47 -04:00
|
|
|
# @return [undefined]
|
2014-04-04 10:14:24 -04:00
|
|
|
def require(logical_name)
|
2014-06-15 11:50:38 -04:00
|
|
|
logical_name = logical_name.to_s
|
2015-06-06 18:29:47 -04:00
|
|
|
@original.call(logical_name)
|
2014-04-04 11:46:55 -04:00
|
|
|
return unless include?(logical_name)
|
2014-04-04 10:14:24 -04:00
|
|
|
@zombified << logical_name
|
2015-06-06 18:29:47 -04:00
|
|
|
zombify(find(logical_name))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Find file by logical path
|
|
|
|
#
|
|
|
|
# @param [String] logical_name
|
|
|
|
#
|
|
|
|
# @return [File]
|
|
|
|
#
|
|
|
|
# @raise [LoadError]
|
|
|
|
# otherwise
|
|
|
|
def find(logical_name)
|
|
|
|
file_name = "#{logical_name}.rb"
|
|
|
|
|
|
|
|
load_path.each do |path|
|
|
|
|
path = pathname.new(path).join(file_name)
|
|
|
|
return path if path.file?
|
|
|
|
end
|
|
|
|
|
|
|
|
fail LoadError, "Cannot find file #{file_name.inspect} in load path"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Zombify contents of file
|
|
|
|
#
|
|
|
|
# Probably the 2nd valid use of eval ever. (First one is inserting mutants!).
|
|
|
|
#
|
|
|
|
# @param [Pathname] source_path
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
def zombify(source_path)
|
|
|
|
kernel.eval(
|
|
|
|
Unparser.unparse(namespaced_node(source_path)),
|
|
|
|
TOPLEVEL_BINDING,
|
|
|
|
source_path.to_s
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Namespaced root node
|
2015-06-06 18:29:47 -04:00
|
|
|
#
|
2015-07-28 14:50:54 -04:00
|
|
|
# @param [Pathname] source_path
|
2015-06-06 18:29:47 -04:00
|
|
|
#
|
|
|
|
# @return [Parser::AST::Node]
|
|
|
|
def namespaced_node(source_path)
|
2015-11-12 23:47:47 -05:00
|
|
|
s(:module, s(:const, nil, namespace), ::Parser::CurrentRuby.parse(source_path.read))
|
2013-06-28 17:23:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end # Zombifier
|
|
|
|
end # Mutant
|