2011-05-11 05:39:15 -04:00
|
|
|
class Pry
|
2011-05-15 13:31:07 -04:00
|
|
|
module ExtendedCommands
|
2011-05-11 05:39:15 -04:00
|
|
|
|
|
|
|
UserCommandAPI = Pry::CommandSet.new do
|
|
|
|
|
2011-06-16 09:50:19 -04:00
|
|
|
command "define-command", "Define a command in the session, use same syntax as `command` method for command API" do |arg|
|
2011-10-01 19:57:39 -04:00
|
|
|
if arg.nil?
|
|
|
|
raise CommandError, "Provide an arg!"
|
|
|
|
end
|
2011-05-11 05:39:15 -04:00
|
|
|
|
2011-05-29 23:57:06 -04:00
|
|
|
prime_string = "command #{arg_string}\n"
|
2011-08-15 21:02:32 -04:00
|
|
|
command_string = _pry_.r(target, prime_string)
|
2011-05-11 05:39:15 -04:00
|
|
|
|
2011-05-29 23:57:06 -04:00
|
|
|
eval_string.replace <<-HERE
|
2011-05-11 05:39:15 -04:00
|
|
|
_pry_.commands.instance_eval do
|
|
|
|
#{command_string}
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-01-08 04:17:15 -05:00
|
|
|
command_class "reload-command", "Reload a Pry command." do
|
|
|
|
banner <<-BANNER
|
|
|
|
Usage: reload-command command
|
|
|
|
Reload a Pry command.
|
|
|
|
BANNER
|
2011-10-01 19:57:39 -04:00
|
|
|
|
2012-01-08 04:17:15 -05:00
|
|
|
def process
|
|
|
|
command = _pry_.commands.find_command(args.first)
|
2011-08-02 18:29:25 -04:00
|
|
|
|
2012-01-08 04:17:15 -05:00
|
|
|
if command.nil?
|
|
|
|
raise Pry::CommandError, 'No command found.'
|
|
|
|
end
|
|
|
|
|
|
|
|
source_code = command.block.source
|
|
|
|
file, lineno = command.block.source_location
|
|
|
|
|
|
|
|
set = Pry::CommandSet.new do
|
|
|
|
eval(source_code, binding, file, lineno)
|
|
|
|
end
|
2011-08-02 18:29:25 -04:00
|
|
|
|
2012-01-08 04:17:15 -05:00
|
|
|
_pry_.commands.delete(command.name)
|
|
|
|
_pry_.commands.import(set)
|
2011-08-02 18:29:25 -04:00
|
|
|
end
|
2011-08-01 17:30:13 -04:00
|
|
|
end
|
|
|
|
|
2012-01-06 21:39:36 -05:00
|
|
|
command_class "edit-command", "Edit a Pry command." do
|
|
|
|
banner <<-BANNER
|
|
|
|
Usage: edit-command [options] command
|
|
|
|
Edit a Pry command.
|
|
|
|
BANNER
|
|
|
|
|
|
|
|
def initialize env
|
|
|
|
@pry = env[:pry_instance]
|
|
|
|
@command = nil
|
2012-01-06 23:27:39 -05:00
|
|
|
super(env)
|
2011-10-01 19:57:39 -04:00
|
|
|
end
|
|
|
|
|
2012-01-06 21:39:36 -05:00
|
|
|
def options(opt)
|
|
|
|
opt.on :p, :patch, 'Perform a in-memory edit of a command'
|
2011-10-01 19:57:39 -04:00
|
|
|
end
|
2011-08-02 18:29:25 -04:00
|
|
|
|
2012-01-06 21:39:36 -05:00
|
|
|
def process
|
|
|
|
@command = @pry.commands.find_command(args.first)
|
2011-08-02 18:29:25 -04:00
|
|
|
|
2012-01-06 21:39:36 -05:00
|
|
|
if @command.nil?
|
|
|
|
raise Pry::CommandError, 'Command not found.'
|
|
|
|
end
|
|
|
|
|
|
|
|
case
|
|
|
|
when opts.present?(:patch)
|
|
|
|
edit_temporarily
|
|
|
|
else
|
|
|
|
edit_permanently
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_permanently
|
|
|
|
file, lineno = @command.block.source_location
|
|
|
|
invoke_editor(file, lineno)
|
|
|
|
|
|
|
|
command_set = silence_warnings do
|
|
|
|
eval File.read(file), TOPLEVEL_BINDING, file, 1
|
|
|
|
end
|
|
|
|
|
|
|
|
unless command_set.is_a?(Pry::CommandSet)
|
|
|
|
raise Pry::CommandError,
|
|
|
|
"Expected file '#{file}' to return a CommandSet"
|
|
|
|
end
|
|
|
|
|
|
|
|
@pry.commands.delete(@command.name)
|
|
|
|
@pry.commands.import(command_set)
|
|
|
|
set_file_and_dir_locals(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_temporarily
|
2012-01-09 23:12:01 -05:00
|
|
|
source_code = Pry::Method(@command.block).source
|
2012-01-09 23:06:30 -05:00
|
|
|
modified_code = nil
|
2012-01-06 21:39:36 -05:00
|
|
|
|
2012-01-09 23:06:30 -05:00
|
|
|
temp_file do |f|
|
2012-01-06 21:39:36 -05:00
|
|
|
f.write(source_code)
|
|
|
|
f.flush
|
|
|
|
|
|
|
|
invoke_editor(f.path, 1)
|
|
|
|
modified_code = File.read(f.path)
|
2012-01-09 23:06:30 -05:00
|
|
|
end
|
2012-01-06 21:39:36 -05:00
|
|
|
|
2012-01-09 23:06:30 -05:00
|
|
|
command_set = CommandSet.new do
|
|
|
|
silence_warnings do
|
|
|
|
pry = Pry.new :input => StringIO.new(modified_code)
|
|
|
|
pry.rep(binding)
|
2012-01-06 21:39:36 -05:00
|
|
|
end
|
|
|
|
end
|
2012-01-09 23:06:30 -05:00
|
|
|
|
|
|
|
@pry.commands.delete(@command.name)
|
|
|
|
@pry.commands.import(command_set)
|
2011-08-02 18:29:25 -04:00
|
|
|
end
|
2011-08-01 17:30:13 -04:00
|
|
|
end
|
|
|
|
|
2011-05-11 05:39:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|