2014-04-04 10:14:24 -04:00
|
|
|
module Mutant
|
|
|
|
class Zombifier
|
2014-08-07 12:00:31 -04:00
|
|
|
# File containing source being zombified
|
2014-04-04 10:14:24 -04:00
|
|
|
class File
|
2014-06-29 17:25:17 -04:00
|
|
|
include Adamantium::Flat, Concord::Public.new(:path), AST::Sexp
|
2014-04-04 10:14:24 -04:00
|
|
|
|
|
|
|
# Zombify contents of file
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def zombify(namespace)
|
2014-05-27 11:12:36 -04:00
|
|
|
$stderr.puts("Zombifying #{path}")
|
2014-04-04 10:14:24 -04:00
|
|
|
eval(
|
|
|
|
Unparser.unparse(namespaced_node(namespace)),
|
|
|
|
TOPLEVEL_BINDING,
|
|
|
|
path.to_s
|
|
|
|
)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2014-04-04 10:18:45 -04:00
|
|
|
# Find file by logical path
|
|
|
|
#
|
|
|
|
# @param [String] logical_name
|
|
|
|
#
|
|
|
|
# @return [File]
|
|
|
|
# if found
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.find(logical_name)
|
2014-04-04 10:25:16 -04:00
|
|
|
file_name =
|
2014-04-04 10:18:45 -04:00
|
|
|
case ::File.extname(logical_name)
|
|
|
|
when '.so'
|
|
|
|
return
|
|
|
|
when '.rb'
|
|
|
|
logical_name
|
|
|
|
else
|
|
|
|
"#{logical_name}.rb"
|
|
|
|
end
|
|
|
|
|
|
|
|
$LOAD_PATH.each do |path|
|
|
|
|
path = Pathname.new(path).join(file_name)
|
|
|
|
return new(path) if path.file?
|
|
|
|
end
|
|
|
|
|
|
|
|
$stderr.puts "Cannot find file #{file_name} in $LOAD_PATH"
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2014-04-04 10:14:24 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# Return node
|
|
|
|
#
|
|
|
|
# @return [Parser::AST::Node]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def node
|
|
|
|
Parser::CurrentRuby.parse(path.read)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return namespaced root
|
|
|
|
#
|
|
|
|
# @param [Symbol] namespace
|
|
|
|
#
|
|
|
|
# @return [Parser::AST::Node]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def namespaced_node(namespace)
|
|
|
|
s(:module, s(:const, nil, namespace), node)
|
|
|
|
end
|
|
|
|
|
|
|
|
end # File
|
|
|
|
end # Zombifier
|
|
|
|
end # Mutant
|