2013-01-05 14:31:20 -05:00
|
|
|
class Pry
|
|
|
|
class Command::ReloadCode < Pry::ClassCommand
|
|
|
|
match 'reload-code'
|
|
|
|
group 'Misc'
|
2013-01-09 15:23:19 -05:00
|
|
|
description 'Reload the source file that contains the specified code object.'
|
|
|
|
|
|
|
|
banner <<-'BANNER'
|
|
|
|
Reload the source file that contains the specified code object.
|
2013-01-29 10:36:37 -05:00
|
|
|
|
|
|
|
e.g reload-code MyClass#my_method #=> reload a method
|
|
|
|
reload-code MyClass #=> reload a class
|
|
|
|
reload-code my-command #=> reload a pry command
|
|
|
|
reload-code self #=> reload the 'current' object
|
|
|
|
reload-code #=> identical to reload-code self
|
2013-01-09 15:23:19 -05:00
|
|
|
BANNER
|
2013-01-05 14:31:20 -05:00
|
|
|
|
|
|
|
def process
|
|
|
|
|
2013-05-09 20:46:22 -04:00
|
|
|
if obj_name.empty?
|
|
|
|
# if no parameters were provided then try to reload the
|
|
|
|
# current file (i.e target.eval("__FILE__"))
|
|
|
|
reload_current_file
|
|
|
|
else
|
|
|
|
code_object = Pry::CodeObject.lookup(obj_name, _pry_)
|
|
|
|
reload_code_object(code_object)
|
|
|
|
end
|
2013-01-05 14:31:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-05-09 20:46:22 -04:00
|
|
|
def current_file
|
|
|
|
File.expand_path target.eval("__FILE__")
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload_current_file
|
|
|
|
if !File.exists?(current_file)
|
|
|
|
raise CommandError, "Current file: #{current_file} cannot be found on disk!"
|
|
|
|
end
|
|
|
|
|
|
|
|
load current_file
|
|
|
|
output.puts "The current file: #{current_file} was reloaded!"
|
|
|
|
end
|
|
|
|
|
2013-01-05 14:31:20 -05:00
|
|
|
def reload_code_object(code_object)
|
2013-05-09 20:46:22 -04:00
|
|
|
check_for_reloadability(code_object)
|
2013-01-05 14:31:20 -05:00
|
|
|
load code_object.source_file
|
2013-01-05 14:37:37 -05:00
|
|
|
output.puts "#{obj_name} was reloaded!"
|
2013-01-05 14:31:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def obj_name
|
2013-05-09 20:46:22 -04:00
|
|
|
@obj_name ||= args.join(" ")
|
2013-01-05 14:31:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_for_reloadability(code_object)
|
2013-05-09 20:46:22 -04:00
|
|
|
if !code_object || !code_object.source_file
|
2013-01-05 14:31:20 -05:00
|
|
|
raise CommandError, "Cannot locate #{obj_name}!"
|
|
|
|
elsif !File.exists?(code_object.source_file)
|
2013-01-05 14:37:37 -05:00
|
|
|
raise CommandError, "Cannot reload #{obj_name} as it has no associated file on disk. File found was: #{code_object.source_file}"
|
2013-01-05 14:31:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Pry::Commands.add_command(Pry::Command::ReloadCode)
|
2013-01-28 16:51:05 -05:00
|
|
|
Pry::Commands.alias_command 'reload-method', 'reload-code'
|
2013-01-05 14:31:20 -05:00
|
|
|
end
|