2012-08-08 00:08:30 -04:00
|
|
|
require 'delegate'
|
2019-03-09 18:35:36 -05:00
|
|
|
require 'shellwords'
|
2012-08-13 23:54:07 -04:00
|
|
|
|
2011-12-31 06:50:04 -05:00
|
|
|
class Pry
|
2011-12-31 07:40:01 -05:00
|
|
|
# The super-class of all commands, new commands should be created by calling
|
2019-03-03 10:37:58 -05:00
|
|
|
# {Pry::CommandSet#command} which creates a BlockCommand or
|
|
|
|
# {Pry::CommandSet#create_command} which creates a ClassCommand. Please don't
|
|
|
|
# use this class directly.
|
2011-12-31 06:50:04 -05:00
|
|
|
class Command
|
2012-08-13 23:54:07 -04:00
|
|
|
extend Helpers::DocumentationHelpers
|
2012-12-25 07:47:33 -05:00
|
|
|
extend CodeObject::Helpers
|
2011-12-31 06:50:04 -05:00
|
|
|
|
2019-04-05 19:52:19 -04:00
|
|
|
include Pry::Helpers::BaseHelpers
|
|
|
|
include Pry::Helpers::CommandHelpers
|
|
|
|
include Pry::Helpers::Text
|
|
|
|
|
2011-12-31 06:50:04 -05:00
|
|
|
# represents a void return value for a command
|
|
|
|
VOID_VALUE = Object.new
|
|
|
|
|
|
|
|
# give it a nice inspect
|
2019-03-02 08:35:35 -05:00
|
|
|
def VOID_VALUE.inspect
|
|
|
|
"void"
|
|
|
|
end
|
2011-12-31 06:50:04 -05:00
|
|
|
|
2011-12-31 07:40:01 -05:00
|
|
|
# Properties of the command itself (as passed as arguments to
|
2012-01-21 18:17:43 -05:00
|
|
|
# {CommandSet#command} or {CommandSet#create_command}).
|
2011-12-31 06:50:04 -05:00
|
|
|
class << self
|
2012-04-01 19:20:35 -04:00
|
|
|
attr_writer :block
|
2012-01-15 15:21:28 -05:00
|
|
|
attr_writer :description
|
|
|
|
attr_writer :command_options
|
2012-04-01 19:00:59 -04:00
|
|
|
attr_writer :match
|
|
|
|
|
2018-11-04 04:34:24 -05:00
|
|
|
def match(arg = nil)
|
2013-01-03 20:44:57 -05:00
|
|
|
if arg
|
|
|
|
@command_options ||= default_options(arg)
|
|
|
|
@command_options[:listing] = arg.is_a?(String) ? arg : arg.inspect
|
|
|
|
@match = arg
|
|
|
|
end
|
2013-03-02 19:01:55 -05:00
|
|
|
@match ||= nil
|
2012-04-01 19:00:59 -04:00
|
|
|
end
|
2012-01-08 15:48:54 -05:00
|
|
|
|
|
|
|
# Define or get the command's description
|
2018-11-04 04:34:24 -05:00
|
|
|
def description(arg = nil)
|
2012-01-08 15:48:54 -05:00
|
|
|
@description = arg if arg
|
2013-03-02 19:01:55 -05:00
|
|
|
@description ||= nil
|
2012-01-08 15:48:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Define or get the command's options
|
2018-11-04 04:34:24 -05:00
|
|
|
def command_options(arg = nil)
|
2012-12-27 17:18:03 -05:00
|
|
|
@command_options ||= default_options(match)
|
2012-01-14 23:54:30 -05:00
|
|
|
@command_options.merge!(arg) if arg
|
2012-01-08 15:48:54 -05:00
|
|
|
@command_options
|
|
|
|
end
|
|
|
|
# backward compatibility
|
2019-02-26 18:00:16 -05:00
|
|
|
alias options command_options
|
|
|
|
alias options= command_options=
|
2012-01-08 15:48:54 -05:00
|
|
|
|
|
|
|
# Define or get the command's banner
|
2018-11-04 04:34:24 -05:00
|
|
|
def banner(arg = nil)
|
2012-01-08 15:48:54 -05:00
|
|
|
@banner = arg if arg
|
2015-01-22 16:52:20 -05:00
|
|
|
@banner ||= description
|
2012-01-08 15:48:54 -05:00
|
|
|
end
|
2012-04-01 19:20:35 -04:00
|
|
|
|
|
|
|
def block
|
2012-12-24 21:28:13 -05:00
|
|
|
@block || instance_method(:process)
|
2012-04-01 19:20:35 -04:00
|
|
|
end
|
2012-08-13 23:54:07 -04:00
|
|
|
|
|
|
|
def source
|
2012-12-20 14:14:11 -05:00
|
|
|
file, line = block.source_location
|
|
|
|
strip_leading_whitespace(Pry::Code.from_file(file).expression_at(line))
|
2012-08-13 23:54:07 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 10:32:47 -05:00
|
|
|
def doc
|
|
|
|
new.help
|
|
|
|
end
|
|
|
|
|
2012-12-01 21:57:17 -05:00
|
|
|
def source_file
|
|
|
|
Array(block.source_location).first
|
|
|
|
end
|
2019-02-26 18:00:16 -05:00
|
|
|
alias file source_file
|
2012-12-01 21:57:17 -05:00
|
|
|
|
|
|
|
def source_line
|
|
|
|
Array(block.source_location).last
|
|
|
|
end
|
2019-02-26 18:00:16 -05:00
|
|
|
alias line source_line
|
2012-12-27 17:18:03 -05:00
|
|
|
|
|
|
|
def default_options(match)
|
|
|
|
{
|
2018-10-12 15:09:29 -04:00
|
|
|
keep_retval: false,
|
|
|
|
argument_required: false,
|
|
|
|
interpolate: true,
|
|
|
|
shellwords: true,
|
2019-03-24 08:30:00 -04:00
|
|
|
listing: (match.is_a?(String) ? match : match.inspect),
|
2018-10-12 15:09:29 -04:00
|
|
|
use_prefix: true,
|
|
|
|
takes_block: false
|
2012-12-27 17:18:03 -05:00
|
|
|
}
|
|
|
|
end
|
2018-10-14 02:23:34 -04:00
|
|
|
|
2012-04-01 19:00:59 -04:00
|
|
|
def name
|
|
|
|
super.to_s == "" ? "#<class(Pry::Command #{match.inspect})>" : super
|
|
|
|
end
|
2012-08-03 09:29:21 -04:00
|
|
|
|
2011-12-31 07:40:01 -05:00
|
|
|
def inspect
|
2012-04-01 19:00:59 -04:00
|
|
|
name
|
2011-12-31 07:40:01 -05:00
|
|
|
end
|
|
|
|
|
2012-12-01 21:57:17 -05:00
|
|
|
def command_name
|
2019-03-02 06:32:33 -05:00
|
|
|
options[:listing]
|
2012-12-01 21:57:17 -05:00
|
|
|
end
|
|
|
|
|
2011-12-31 07:40:01 -05:00
|
|
|
# Create a new command with the given properties.
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String, Regex] match The thing that triggers this command
|
|
|
|
# @param [String] description The description to appear in `help`
|
|
|
|
# @param [Hash] options Behavioral options (see {Pry::CommandSet#command})
|
|
|
|
# @param [Module] helpers A module of helper functions to be included.
|
|
|
|
# @yield optional, used for BlockCommands
|
|
|
|
# @return [Class] (a subclass of {Pry::Command})
|
2012-04-01 19:00:59 -04:00
|
|
|
def subclass(match, description, options, helpers, &block)
|
2011-12-31 07:40:01 -05:00
|
|
|
klass = Class.new(self)
|
|
|
|
klass.send(:include, helpers)
|
2012-04-01 19:00:59 -04:00
|
|
|
klass.match = match
|
2011-12-31 07:40:01 -05:00
|
|
|
klass.description = description
|
2012-01-08 15:48:54 -05:00
|
|
|
klass.command_options = options
|
2011-12-31 07:40:01 -05:00
|
|
|
klass.block = block
|
|
|
|
klass
|
|
|
|
end
|
|
|
|
|
2012-01-02 19:04:47 -05:00
|
|
|
# Should this command be called for the given line?
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String] val A line input at the REPL
|
|
|
|
# @return [Boolean]
|
2012-01-02 19:04:47 -05:00
|
|
|
def matches?(val)
|
|
|
|
command_regex =~ val
|
|
|
|
end
|
|
|
|
|
2012-02-11 18:15:22 -05:00
|
|
|
# How well does this command match the given line?
|
|
|
|
#
|
|
|
|
# Higher scores are better because they imply that this command matches
|
|
|
|
# the line more closely.
|
|
|
|
#
|
|
|
|
# The score is calculated by taking the number of characters at the start
|
|
|
|
# of the string that are used only to identify the command, not as part of
|
|
|
|
# the arguments.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# /\.(.*)/.match_score(".foo") #=> 1
|
|
|
|
# /\.*(.*)/.match_score("...foo") #=> 3
|
|
|
|
# 'hi'.match_score("hi there") #=> 2
|
|
|
|
#
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String] val A line input at the REPL
|
|
|
|
# @return [Fixnum]
|
2012-02-11 18:15:22 -05:00
|
|
|
def match_score(val)
|
|
|
|
if command_regex =~ val
|
2019-03-03 10:37:58 -05:00
|
|
|
if Regexp.last_match.size > 1
|
|
|
|
Regexp.last_match.begin(1)
|
|
|
|
else
|
|
|
|
Regexp.last_match.end(0)
|
|
|
|
end
|
2012-02-11 18:15:22 -05:00
|
|
|
else
|
|
|
|
-1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-02 19:04:47 -05:00
|
|
|
def command_regex
|
2019-04-08 19:58:34 -04:00
|
|
|
prefix = convert_to_regex(Pry.config.command_prefix)
|
2012-01-02 19:04:47 -05:00
|
|
|
prefix = "(?:#{prefix})?" unless options[:use_prefix]
|
|
|
|
|
2012-04-01 19:00:59 -04:00
|
|
|
/^#{prefix}#{convert_to_regex(match)}(?!\S)/
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_regex(obj)
|
|
|
|
case obj
|
|
|
|
when String
|
|
|
|
Regexp.escape(obj)
|
|
|
|
else
|
|
|
|
obj
|
|
|
|
end
|
|
|
|
end
|
2012-02-20 02:08:16 -05:00
|
|
|
|
|
|
|
# The group in which the command should be displayed in "help" output.
|
|
|
|
# This is usually auto-generated from directory naming, but it can be
|
|
|
|
# manually overridden if necessary.
|
2013-12-21 06:19:25 -05:00
|
|
|
# Group should not be changed once it is initialized.
|
2018-11-04 04:34:24 -05:00
|
|
|
def group(name = nil)
|
2019-03-02 09:42:29 -05:00
|
|
|
@group ||= begin
|
|
|
|
name || case Pry::Method(block).source_file
|
|
|
|
when %r{/pry/.*_commands/(.*).rb}
|
|
|
|
Regexp.last_match(1).capitalize.tr('_', " ")
|
|
|
|
when /(pry-\w+)-([\d\.]+([\w\.]+)?)/
|
|
|
|
name = Regexp.last_match(1)
|
|
|
|
version = Regexp.last_match(2)
|
|
|
|
"#{name} (v#{version})"
|
|
|
|
when /pryrc/
|
|
|
|
"pryrc"
|
|
|
|
else
|
|
|
|
"(other)"
|
|
|
|
end
|
|
|
|
end
|
2012-02-20 02:08:16 -05:00
|
|
|
end
|
2019-04-29 12:21:54 -04:00
|
|
|
|
|
|
|
def state
|
|
|
|
Pry::CommandState.default.state_for(match)
|
|
|
|
end
|
2011-12-31 07:40:01 -05:00
|
|
|
end
|
|
|
|
|
2012-01-02 19:04:47 -05:00
|
|
|
# Properties of one execution of a command (passed by {Pry#run_command} as a hash of
|
2012-06-27 01:30:00 -04:00
|
|
|
# context and expanded in `#initialize`
|
2011-12-31 06:50:04 -05:00
|
|
|
attr_accessor :output
|
|
|
|
attr_accessor :target
|
|
|
|
attr_accessor :captures
|
|
|
|
attr_accessor :eval_string
|
|
|
|
attr_accessor :arg_string
|
2011-12-31 07:40:01 -05:00
|
|
|
attr_accessor :context
|
2011-12-31 06:50:04 -05:00
|
|
|
attr_accessor :command_set
|
2015-03-11 10:17:10 -04:00
|
|
|
attr_accessor :hooks
|
2019-03-22 18:44:56 -04:00
|
|
|
attr_accessor :pry_instance
|
|
|
|
alias _pry_= pry_instance=
|
2011-12-31 06:50:04 -05:00
|
|
|
|
2012-02-26 07:05:19 -05:00
|
|
|
# The block we pass *into* a command so long as `:takes_block` is
|
|
|
|
# not equal to `false`
|
2012-02-23 09:30:48 -05:00
|
|
|
# @example
|
2012-02-26 07:05:19 -05:00
|
|
|
# my-command | do
|
2012-02-23 09:30:48 -05:00
|
|
|
# puts "block content"
|
|
|
|
# end
|
|
|
|
attr_accessor :command_block
|
|
|
|
|
2019-04-05 19:52:19 -04:00
|
|
|
# Instantiate a command, in preparation for calling it.
|
|
|
|
# @param [Hash] context The runtime context to use with this command.
|
|
|
|
def initialize(context = {})
|
|
|
|
self.context = context
|
|
|
|
self.target = context[:target]
|
|
|
|
self.output = context[:output]
|
|
|
|
self.eval_string = context[:eval_string]
|
|
|
|
self.command_set = context[:command_set]
|
|
|
|
self.hooks = context[:hooks]
|
|
|
|
self.pry_instance = context[:pry_instance]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Make those properties accessible to instances
|
|
|
|
def name
|
|
|
|
self.class.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def match
|
|
|
|
self.class.match
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
self.class.description
|
|
|
|
end
|
|
|
|
|
|
|
|
def block
|
|
|
|
self.class.block
|
|
|
|
end
|
|
|
|
|
|
|
|
def command_options
|
|
|
|
self.class.options
|
|
|
|
end
|
|
|
|
|
|
|
|
def command_name
|
|
|
|
self.class.command_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def source
|
|
|
|
self.class.source
|
|
|
|
end
|
|
|
|
|
2011-12-31 06:50:04 -05:00
|
|
|
# Run a command from another command.
|
|
|
|
# @param [String] command_string The string that invokes the command
|
|
|
|
# @param [Array] args Further arguments to pass to the command
|
|
|
|
# @example
|
|
|
|
# run "show-input"
|
|
|
|
# @example
|
|
|
|
# run ".ls"
|
|
|
|
# @example
|
|
|
|
# run "amend-line", "5", 'puts "hello world"'
|
|
|
|
def run(command_string, *args)
|
2019-03-22 18:44:56 -04:00
|
|
|
command_string = pry_instance.config.command_prefix.to_s + command_string
|
2019-03-02 09:02:43 -05:00
|
|
|
complete_string = "#{command_string} #{args.join(' ')}".rstrip
|
2012-01-02 19:04:47 -05:00
|
|
|
command_set.process_line(complete_string, context)
|
2011-12-31 06:50:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def commands
|
2014-02-03 23:12:28 -05:00
|
|
|
command_set.to_hash
|
2011-12-31 06:50:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def void
|
|
|
|
VOID_VALUE
|
|
|
|
end
|
|
|
|
|
2019-04-05 19:52:19 -04:00
|
|
|
def _pry_
|
2019-05-04 10:15:14 -04:00
|
|
|
Pry::Warning.warn('_pry_ is deprecated, use pry_instance instead')
|
2019-04-05 19:52:19 -04:00
|
|
|
pry_instance
|
2011-12-31 06:50:04 -05:00
|
|
|
end
|
|
|
|
|
2012-06-15 08:36:03 -04:00
|
|
|
# @return [Object] The value of `self` inside the `target` binding.
|
2019-03-02 08:35:35 -05:00
|
|
|
def target_self
|
|
|
|
target.eval('self')
|
|
|
|
end
|
2011-12-31 09:13:59 -05:00
|
|
|
|
2012-06-15 08:36:03 -04:00
|
|
|
# @return [Hash] Pry commands can store arbitrary state
|
|
|
|
# here. This state persists between subsequent command invocations.
|
|
|
|
# All state saved here is unique to the command, it does not
|
|
|
|
# need to be namespaced.
|
|
|
|
# @example
|
|
|
|
# state.my_state = "my state" # this will not conflict with any
|
|
|
|
# # `state.my_state` used in another command.
|
|
|
|
def state
|
2019-04-29 12:21:54 -04:00
|
|
|
self.class.state
|
2012-06-15 08:36:03 -04:00
|
|
|
end
|
|
|
|
|
2012-01-02 19:04:47 -05:00
|
|
|
# Revaluate the string (str) and perform interpolation.
|
|
|
|
# @param [String] str The string to reevaluate with interpolation.
|
|
|
|
#
|
|
|
|
# @return [String] The reevaluated string with interpolations
|
|
|
|
# applied (if any).
|
|
|
|
def interpolate_string(str)
|
|
|
|
dumped_str = str.dump
|
|
|
|
if dumped_str.gsub!(/\\\#\{/, '#{')
|
|
|
|
target.eval(dumped_str)
|
|
|
|
else
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Display a warning if a command collides with a local/method in
|
|
|
|
# the current scope.
|
2012-04-01 19:00:59 -04:00
|
|
|
def check_for_command_collision(command_match, arg_string)
|
|
|
|
collision_type = target.eval("defined?(#{command_match})")
|
2019-02-26 17:46:58 -05:00
|
|
|
collision_type ||= 'local-variable' if arg_string =~ %r{\A\s*[-+*/%&|^]*=}
|
2012-01-28 22:39:31 -05:00
|
|
|
|
|
|
|
if collision_type
|
2018-11-08 01:44:36 -05:00
|
|
|
output.puts(
|
|
|
|
"#{Helpers::Text.bold('WARNING:')} Calling Pry command '#{command_match}', " \
|
|
|
|
"which conflicts with a #{collision_type}.\n\n"
|
|
|
|
)
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
2019-03-03 17:05:33 -05:00
|
|
|
rescue Pry::RescuableException # rubocop:disable Lint/HandleExceptions
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
2012-06-27 01:30:00 -04:00
|
|
|
# Extract necessary information from a line that Command.matches? this
|
|
|
|
# command.
|
|
|
|
#
|
|
|
|
# Returns an array of four elements:
|
2012-01-02 19:04:47 -05:00
|
|
|
#
|
2012-06-27 01:30:00 -04:00
|
|
|
# ```
|
|
|
|
# [String] the portion of the line that matched with the Command match
|
|
|
|
# [String] a string of all the arguments (i.e. everything but the match)
|
|
|
|
# [Array] the captures caught by the command_regex
|
|
|
|
# [Array] the arguments obtained by splitting the arg_string
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# @param [String] val The line of input
|
|
|
|
# @return [Array]
|
2012-01-02 19:04:47 -05:00
|
|
|
def tokenize(val)
|
|
|
|
val.replace(interpolate_string(val)) if command_options[:interpolate]
|
|
|
|
|
|
|
|
self.class.command_regex =~ val
|
|
|
|
|
|
|
|
# please call Command.matches? before Command#call_safely
|
2019-03-03 10:37:58 -05:00
|
|
|
unless Regexp.last_match
|
|
|
|
raise CommandError, "fatal: called a command which didn't match?!"
|
|
|
|
end
|
2018-10-14 09:44:58 -04:00
|
|
|
|
2012-01-02 19:04:47 -05:00
|
|
|
captures = Regexp.last_match.captures
|
|
|
|
pos = Regexp.last_match.end(0)
|
|
|
|
|
|
|
|
arg_string = val[pos..-1]
|
|
|
|
|
|
|
|
# remove the one leading space if it exists
|
|
|
|
arg_string.slice!(0) if arg_string.start_with?(" ")
|
|
|
|
|
2012-02-26 07:05:19 -05:00
|
|
|
# process and pass a block if one is found
|
|
|
|
pass_block(arg_string) if command_options[:takes_block]
|
|
|
|
|
2019-02-28 18:24:54 -05:00
|
|
|
args =
|
|
|
|
if arg_string
|
2019-03-03 10:37:58 -05:00
|
|
|
if command_options[:shellwords]
|
|
|
|
Shellwords.shellwords(arg_string)
|
|
|
|
else
|
|
|
|
arg_string.split(" ")
|
|
|
|
end
|
2019-02-28 18:24:54 -05:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
|
|
|
[val[0..pos].rstrip, arg_string, captures, args]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Process a line that Command.matches? this command.
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String] line The line to process
|
|
|
|
# @return [Object, Command::VOID_VALUE]
|
2012-01-02 19:04:47 -05:00
|
|
|
def process_line(line)
|
2012-04-01 19:00:59 -04:00
|
|
|
command_match, arg_string, captures, args = tokenize(line)
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-03-03 10:37:58 -05:00
|
|
|
if Pry.config.collision_warning
|
|
|
|
check_for_command_collision(command_match, arg_string)
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
|
|
|
self.arg_string = arg_string
|
|
|
|
self.captures = captures
|
|
|
|
|
|
|
|
call_safely(*(captures + args))
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
# Generate completions for this command
|
|
|
|
#
|
|
|
|
# @param [String] _search The line typed so far
|
|
|
|
# @return [Array<String>] Completion words
|
|
|
|
def complete(_search)
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-06-27 01:30:00 -04:00
|
|
|
# Run the command with the given `args`.
|
2011-12-31 07:40:01 -05:00
|
|
|
#
|
2012-06-27 01:30:00 -04:00
|
|
|
# This is a public wrapper around `#call` which ensures all preconditions
|
|
|
|
# are met.
|
2011-12-31 07:40:01 -05:00
|
|
|
#
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [Array<String>] args The arguments to pass to this command.
|
|
|
|
# @return [Object] The return value of the `#call` method, or
|
|
|
|
# {Command::VOID_VALUE}.
|
2011-12-31 06:50:04 -05:00
|
|
|
def call_safely(*args)
|
2019-03-03 10:37:58 -05:00
|
|
|
if command_options[:argument_required] && args.empty?
|
|
|
|
raise CommandError, "The command '#{command_name}' requires an argument."
|
|
|
|
end
|
2011-12-31 07:40:01 -05:00
|
|
|
|
2015-06-30 13:47:38 -04:00
|
|
|
ret = use_unpatched_symbol do
|
|
|
|
call_with_hooks(*args)
|
|
|
|
end
|
2011-12-31 07:40:01 -05:00
|
|
|
command_options[:keep_retval] ? ret : void
|
|
|
|
end
|
|
|
|
|
2015-06-30 13:47:38 -04:00
|
|
|
def use_unpatched_symbol
|
|
|
|
call_method = Symbol.method_defined?(:call) && Symbol.instance_method(:call)
|
|
|
|
Symbol.class_eval { undef :call } if call_method
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Symbol.instance_eval { define_method(:call, call_method) } if call_method
|
|
|
|
end
|
|
|
|
|
2019-03-23 14:18:02 -04:00
|
|
|
# Pass a block argument to a command.
|
|
|
|
# @param [String] arg_string The arguments (as a string) passed to the command.
|
|
|
|
# We inspect these for a '| do' or a '| {' and if we find it we use it
|
|
|
|
# to start a block input sequence. Once we have a complete
|
|
|
|
# block, we save it to an accessor that can be retrieved from the command context.
|
|
|
|
# Note that if we find the '| do' or '| {' we delete this and the
|
|
|
|
# elements following it from `arg_string`.
|
|
|
|
def pass_block(arg_string)
|
|
|
|
# Workaround for weird JRuby bug where rindex in this case can return nil
|
|
|
|
# even when there's a match.
|
|
|
|
arg_string.scan(/\| *(?:do|\{)/)
|
|
|
|
block_index = $LAST_MATCH_INFO && $LAST_MATCH_INFO.offset(0)[0]
|
|
|
|
|
|
|
|
return unless block_index
|
|
|
|
|
|
|
|
block_init_string = arg_string.slice!(block_index..-1)[1..-1]
|
|
|
|
prime_string = "proc #{block_init_string}\n"
|
|
|
|
|
|
|
|
block_string =
|
|
|
|
if !Pry::Code.complete_expression?(prime_string)
|
|
|
|
pry_instance.r(target, prime_string)
|
|
|
|
else
|
|
|
|
prime_string
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
self.command_block = target.eval(block_string)
|
|
|
|
rescue Pry::RescuableException
|
|
|
|
raise CommandError, "Incomplete block definition."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-11 10:17:10 -04:00
|
|
|
def find_hooks(event)
|
|
|
|
event_name = "#{event}_#{command_name}"
|
2019-04-07 17:44:38 -04:00
|
|
|
(hooks || Pry.hooks || self.class.hooks).get_hooks(event_name).values
|
2015-03-11 10:17:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def before_hooks
|
|
|
|
find_hooks('before')
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_hooks
|
|
|
|
find_hooks('after')
|
|
|
|
end
|
|
|
|
|
2012-06-27 01:30:00 -04:00
|
|
|
# Run the `#call` method and all the registered hooks.
|
|
|
|
# @param [Array<String>] args The arguments to `#call`
|
|
|
|
# @return [Object] The return value from `#call`
|
2011-12-31 06:50:04 -05:00
|
|
|
def call_with_hooks(*args)
|
2015-03-11 10:17:10 -04:00
|
|
|
before_hooks.each { |block| instance_exec(*args, &block) }
|
2011-12-31 06:50:04 -05:00
|
|
|
|
2012-01-15 15:21:28 -05:00
|
|
|
ret = call(*args)
|
2011-12-31 06:50:04 -05:00
|
|
|
|
2015-03-11 10:17:10 -04:00
|
|
|
after_hooks.each do |block|
|
2011-12-31 06:50:04 -05:00
|
|
|
ret = instance_exec(*args, &block)
|
|
|
|
end
|
|
|
|
|
2011-12-31 07:40:01 -05:00
|
|
|
ret
|
2011-12-31 06:50:04 -05:00
|
|
|
end
|
2012-01-15 18:51:04 -05:00
|
|
|
|
2019-04-06 07:01:03 -04:00
|
|
|
# Normalize method arguments according to its arity.
|
|
|
|
#
|
|
|
|
# @param [Integer] method
|
|
|
|
# @param [Array] args
|
|
|
|
# @return [Array] a (possibly shorter) array of the arguments to pass
|
|
|
|
def normalize_method_args(method, args)
|
|
|
|
case method.arity
|
|
|
|
when -1
|
2012-01-15 18:51:04 -05:00
|
|
|
args
|
2019-04-06 07:01:03 -04:00
|
|
|
when 0
|
2012-01-15 18:51:04 -05:00
|
|
|
[]
|
2019-04-06 07:01:03 -04:00
|
|
|
else
|
|
|
|
args.values_at(*(0..(method.arity - 1)).to_a)
|
2012-01-15 18:51:04 -05:00
|
|
|
end
|
|
|
|
end
|
2011-12-31 06:50:04 -05:00
|
|
|
end
|
|
|
|
end
|