Pry::CommandSet.new do
create_command "define-command", "Define a Pry command for this session." do
banner " Usage: define-command \"name\", \"my description\" do\n p \"I do something\"\n end\n\n Define a Pry command.\n BANNER\n\n def process\n if args.empty?\n raise Pry::CommandError, \"Provide an arg!\"\n end\n\n prime_string = \"command \#{arg_string}\\n\"\n command_string = _pry_.r(target, prime_string)\n\n eval_string.replace <<-HERE\n _pry_.commands.instance_eval do\n \#{command_string}\n end\n HERE\n end\n end\n\n create_command \"reload-command\", \"Reload a Pry command.\" do\n banner <<-BANNER\n Usage: reload-command command\n Reload a Pry command.\n BANNER\n\n def process\n command = _pry_.commands.find_command(args.first)\n\n if command.nil?\n raise Pry::CommandError, 'No command found.'\n end\n\n source_code = command.block.source\n file, lineno = command.block.source_location\n\n set = Pry::CommandSet.new do\n eval(source_code, binding, file, lineno)\n end\n\n _pry_.commands.delete(command.name)\n _pry_.commands.import(set)\n end\n end\n\n create_command \"edit-command\", \"Edit a Pry command.\" do\n banner <<-BANNER\n Usage: edit-command [options] command\n Edit a Pry command.\n BANNER\n\n def options(opt)\n opt.on :p, :patch, 'Perform a in-memory edit of a command'\n end\n\n def process\n if args.empty?\n raise Pry::CommandError, \"No command given.\"\n end\n\n @command, @_target = find_command_and_target\n\n case\n when opts.present?(:patch)\n edit_temporarily\n else\n edit_permanently\n end\n end\n\n def find_command_and_target\n raw = args.first\n\n if raw.include?('#')\n command, method = raw.split('#', 2)\n command = _pry_.commands.find_command(command)\n target = command.instance_method(method) rescue nil\n else\n command = _pry_.commands.find_command(raw)\n target = command.block rescue nil\n end\n\n if command.nil?\n raise Pry::CommandError, \"No command found.\"\n end\n\n if target.nil?\n raise Pry::CommandError, \"Method '\#{method}' could not be found.\"\n end\n\n [command, target]\n end\n\n def edit_permanently\n file, lineno = @_target.source_location\n invoke_editor(file, lineno, true)\n\n command_set = silence_warnings do\n eval File.read(file), TOPLEVEL_BINDING, file, 1\n end\n\n unless command_set.is_a?(Pry::CommandSet)\n raise Pry::CommandError,\n \"Expected file '\#{file}' to return a CommandSet\"\n end\n\n _pry_.commands.delete(@command.name)\n _pry_.commands.import(command_set)\n set_file_and_dir_locals(file)\n end\n\n def edit_temporarily\n source_code = Pry::Method(@_target).source\n modified_code = nil\n\n temp_file do |f|\n f.write(source_code)\n f.flush\n\n invoke_editor(f.path, 1, true)\n modified_code = File.read(f.path)\n end\n\n command_set = Pry::CommandSet.new do\n silence_warnings do\n pry = Pry.new :input => StringIO.new(modified_code)\n pry.rep(binding)\n end\n end\n\n _pry_.commands.delete(@command.name)\n _pry_.commands.import(command_set)\n end\n end\nend\n"