Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

pry your_file.rb at command line now runs your_file through repl loop

sprinkle make-interactive into your code to make it interactive, and make-non-iteractive inside the repl to return to non-interactive.
  • Loading branch information...
commit 5396811b727414c285839bc30c7c44077b35d497 1 parent 2bdaee2
@banister banister authored
Showing with 50 additions and 38 deletions.
  1. +12 −1 lib/pry/cli.rb
  2. +38 −37 lib/pry/pry_class.rb
View
13 lib/pry/cli.rb
@@ -14,6 +14,10 @@ class << self
# @return [Array] The Procs that process the parsed options.
attr_accessor :option_processors
+ # @return [Array<String>] The input array of strings to process
+ # as CLI options.
+ attr_accessor :input_args
+
# Add another set of CLI options (a Slop block)
def add_options(&block)
if options
@@ -55,6 +59,8 @@ def reset
def parse_options(args=ARGV.dup)
raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options." if !options
+ self.input_args = args
+
opts = Slop.parse(args, :help => true, :multiple_switches => false, &options)
option_processors.each { |processor| processor.call(opts) } if option_processors # option processors are optional
@@ -134,13 +140,18 @@ def parse_options(args=ARGV.dup)
:default => "Pry.toplevel_binding"
)
end.process_options do |opts|
+
# invoked via cli
Pry.cli = true
# create the actual context
context = Pry.binding_for(eval(opts[:context]))
- if opts[:exec]
+ if Pry::CLI.input_args.any?
+ full_name = File.expand_path(Pry::CLI.input_args.first)
+ Pry.load_file_through_repl(full_name)
+ exit
+ elsif opts[:exec]
exec_string = opts[:exec] + "\n"
else
exec_string = ""
View
75 lib/pry/pry_class.rb
@@ -62,7 +62,7 @@ def self.load_rc
files = RC_FILES.collect { |file_name| File.expand_path(file_name) }.uniq
files.each do |file_name|
begin
- load(file_name) if File.exists?(file_name)
+ toplevel_binding.eval(File.read(file_name)) if File.exists?(file_name)
rescue RescuableException => e
puts "Error loading #{file_name}: #{e}"
end
@@ -91,7 +91,7 @@ def self.initial_session_setup
# note these have to be loaded here rather than in pry_instance as
# we only want them loaded once per entire Pry lifetime.
- # load_rc if Pry.config.should_load_rc
+ load_rc if Pry.config.should_load_rc
load_plugins if Pry.config.should_load_plugins
load_requires if Pry.config.should_load_requires
load_history if Pry.config.history.should_load
@@ -141,22 +141,6 @@ def self.start(target=toplevel_binding, options={})
puts defined?(Win32::Console) ? "\e[0F" : "\e[0A\e[0G"
end
- # rcs = RC_FILES.collect do |file_name|
- # full_name = File.expand_path(file_name)
- # StringIO.new(File.read(full_name)) if File.exists?(full_name)
- # end.uniq.compact
-
- # old_input = pry_instance.input
- # pry_instance.input = rcs.shift
- # pry_instance.input_stack.push(*rcs)
- # pry_instance.input_stack.push(old_input)
- # pry_instance.print = proc {}
- # pry_instance.hooks.add_hook(:input_object_changed, :hi) do |old, new, _pry|
- # _pry.print = Pry::DEFAULT_PRINT
- # end
-
- prepare_rc_file_load(pry_instance)
-
# Enter the matrix
pry_instance.repl(head)
end
@@ -168,32 +152,49 @@ def self.rc_files
end.uniq.compact
end
- def self.prepare_rc_file_load(pry_instance)
- rcs = rc_files.map { |v| StringIO.new(File.read(v)) }
- return if rcs.empty?
+ def self.load_file_through_repl(file_name)
+ full_name = File.expand_path(file_name)
+ raise RuntimeError, "No such file: #{full_name}" if !File.exists?(full_name)
- original_input = pry_instance.input
- pry_instance.input = rcs.shift
- pry_instance.input_stack.push(*rcs) if !rcs.empty?
- pry_instance.input_stack.push(original_input)
+ content = StringIO.new(File.read(full_name))
- old_print = pry_instance.print
- pry_instance.print = proc {}
+ initial_session_setup
+
+ original_input = Pry.config.input
+ original_print = Pry.config.print
+ original_exception_handler = Pry.config.exception_handler
- old_exception_handler = pry_instance.exception_handler
- pry_instance.exception_handler = proc do |output, ex, _pry_|
- _pry_.run_command "cat --ex"
- output.puts "...exception encountered, going interactive!"
+ restore_originals = proc do |_pry_|
_pry_.input = original_input
+ _pry_.print = original_print
+ _pry_.exception_handler = original_exception_handler
end
- pry_instance.hooks.add_hook(:input_object_changed, :restore_print) do |old_input, new_input, _pry_|
- if new_input == original_input
- _pry_.print = old_print
- _pry_.exception_handler = old_exception_handler
- _pry_.hooks.delete_hook(:input_object_changed, :restore_print)
- end
+ Pry::Commands.command "make-interactive", "Make the session interactive" do
+ self._pry_.input_stack.push self._pry_.input
+ self._pry_.input = original_input
end
+
+ Pry::Commands.command "make-non-interactive", "Make the session non-interactive" do
+ self._pry_.input = _pry_.input_stack.pop
+ end
+
+ Pry::Commands.command "load-file", "Load another file through the repl" do |file_name|
+ file_input = StringIO.new(File.expand_path(file_name))
+
+ _pry_.input_stack.push(_pry_.input)
+ _pry_.input = file_input
+ end
+
+ Pry.start(Pry.toplevel_binding,
+ :input => content,
+ :input_stack => [StringIO.new("exit-all\n")],
+ :print => proc {},
+ :exception_handler => (proc do |o, e, _pry_|
+ _pry_.run_command "cat --ex"
+ o.puts "...exception encountered, going interactive!"
+ restore_originals.call(_pry_)
+ end))
end
# An inspector that clips the output to `max_length` chars.
Please sign in to comment.
Something went wrong with that request. Please try again.