2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2005-04-13 11:27:09 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# loader.rb -
|
2009-07-07 07:36:20 -04:00
|
|
|
# $Release Version: 0.9.6$
|
2005-04-13 11:27:09 -04:00
|
|
|
# $Revision$
|
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2005-04-13 11:27:09 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
|
2012-12-13 00:22:30 -05:00
|
|
|
module IRB # :nodoc:
|
|
|
|
# Raised in the event of an exception in a file loaded from an Irb session
|
2002-07-09 07:17:17 -04:00
|
|
|
class LoadAbort < Exception;end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Provides a few commands for loading files within an irb session.
|
|
|
|
#
|
|
|
|
# See ExtendCommandBundle for more information.
|
2002-07-09 07:17:17 -04:00
|
|
|
module IrbLoader
|
2005-04-14 05:58:18 -04:00
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
alias ruby_load load
|
|
|
|
alias ruby_require require
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Loads the given file similarly to Kernel#load
|
2002-07-09 07:17:17 -04:00
|
|
|
def irb_load(fn, priv = nil)
|
|
|
|
path = search_file_from_ruby_path(fn)
|
|
|
|
raise LoadError, "No such file to load -- #{fn}" unless path
|
|
|
|
|
|
|
|
load_file(path, priv)
|
|
|
|
end
|
|
|
|
|
2021-01-28 18:53:45 -05:00
|
|
|
if File.respond_to?(:absolute_path?)
|
|
|
|
def absolute_path?(path)
|
|
|
|
File.absolute_path?(path)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
separator =
|
|
|
|
if File::ALT_SEPARATOR
|
2021-02-11 07:03:11 -05:00
|
|
|
"[#{Regexp.quote(File::SEPARATOR + File::ALT_SEPARATOR)}]"
|
2021-01-28 18:53:45 -05:00
|
|
|
else
|
2021-02-11 07:03:11 -05:00
|
|
|
File::SEPARATOR
|
2021-01-28 18:53:45 -05:00
|
|
|
end
|
|
|
|
ABSOLUTE_PATH_PATTERN = # :nodoc:
|
|
|
|
case Dir.pwd
|
|
|
|
when /\A\w:/, /\A#{separator}{2}/
|
|
|
|
/\A(?:\w:|#{separator})#{separator}/
|
|
|
|
else
|
|
|
|
/\A#{separator}/
|
|
|
|
end
|
|
|
|
def absolute_path?(path)
|
|
|
|
ABSOLUTE_PATH_PATTERN =~ path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
def search_file_from_ruby_path(fn) # :nodoc:
|
2021-01-28 18:53:45 -05:00
|
|
|
if absolute_path?(fn)
|
2014-08-08 21:36:49 -04:00
|
|
|
return fn if File.exist?(fn)
|
|
|
|
return nil
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
for path in $:
|
2014-08-08 21:36:49 -04:00
|
|
|
if File.exist?(f = File.join(path, fn))
|
|
|
|
return f
|
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Loads a given file in the current session and displays the source lines
|
|
|
|
#
|
|
|
|
# See Irb#suspend_input_method for more information.
|
2002-07-09 07:17:17 -04:00
|
|
|
def source_file(path)
|
|
|
|
irb.suspend_name(path, File.basename(path)) do
|
2021-01-22 12:12:39 -05:00
|
|
|
FileInputMethod.open(path) do |io|
|
|
|
|
irb.suspend_input_method(io) do
|
|
|
|
|back_io|
|
|
|
|
irb.signal_status(:IN_LOAD) do
|
|
|
|
if back_io.kind_of?(FileInputMethod)
|
2014-08-08 21:36:49 -04:00
|
|
|
irb.eval_input
|
2021-01-22 12:12:39 -05:00
|
|
|
else
|
|
|
|
begin
|
|
|
|
irb.eval_input
|
|
|
|
rescue LoadAbort
|
|
|
|
print "load abort!!\n"
|
|
|
|
end
|
2014-08-08 21:36:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Loads the given file in the current session's context and evaluates it.
|
|
|
|
#
|
|
|
|
# See Irb#suspend_input_method for more information.
|
2002-07-09 07:17:17 -04:00
|
|
|
def load_file(path, priv = nil)
|
|
|
|
irb.suspend_name(path, File.basename(path)) do
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
if priv
|
|
|
|
ws = WorkSpace.new(Module.new)
|
|
|
|
else
|
|
|
|
ws = WorkSpace.new
|
|
|
|
end
|
|
|
|
irb.suspend_workspace(ws) do
|
2021-01-22 12:12:39 -05:00
|
|
|
FileInputMethod.open(path) do |io|
|
|
|
|
irb.suspend_input_method(io) do
|
|
|
|
|back_io|
|
|
|
|
irb.signal_status(:IN_LOAD) do
|
|
|
|
if back_io.kind_of?(FileInputMethod)
|
2014-08-08 21:36:49 -04:00
|
|
|
irb.eval_input
|
2021-01-22 12:12:39 -05:00
|
|
|
else
|
|
|
|
begin
|
|
|
|
irb.eval_input
|
|
|
|
rescue LoadAbort
|
|
|
|
print "load abort!!\n"
|
|
|
|
end
|
2014-08-08 21:36:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
def old # :nodoc:
|
2002-07-09 07:17:17 -04:00
|
|
|
back_io = @io
|
|
|
|
back_path = @irb_path
|
|
|
|
back_name = @irb_name
|
|
|
|
back_scanner = @irb.scanner
|
|
|
|
begin
|
2014-08-08 21:36:49 -04:00
|
|
|
@io = FileInputMethod.new(path)
|
|
|
|
@irb_name = File.basename(path)
|
|
|
|
@irb_path = path
|
|
|
|
@irb.signal_status(:IN_LOAD) do
|
|
|
|
if back_io.kind_of?(FileInputMethod)
|
|
|
|
@irb.eval_input
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
@irb.eval_input
|
|
|
|
rescue LoadAbort
|
|
|
|
print "load abort!!\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
ensure
|
2014-08-08 21:36:49 -04:00
|
|
|
@io = back_io
|
|
|
|
@irb_name = back_name
|
|
|
|
@irb_path = back_path
|
|
|
|
@irb.scanner = back_scanner
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|