2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2001-04-30 21:38:21 +04:00
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# irb/extend-command.rb - irb extend command
|
2005-04-13 19:27:09 +04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2001-04-30 21:38:21 +04:00
|
|
|
#
|
2023-01-11 07:54:09 +03:00
|
|
|
|
2012-12-13 09:22:30 +04:00
|
|
|
module IRB # :nodoc:
|
2012-12-21 09:45:50 +04:00
|
|
|
# Installs the default irb extensions command bundle.
|
2002-07-09 15:17:17 +04:00
|
|
|
module ExtendCommandBundle
|
2012-12-21 09:45:50 +04:00
|
|
|
EXCB = ExtendCommandBundle # :nodoc:
|
2001-04-30 21:38:21 +04:00
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# See #install_alias_method.
|
2002-07-09 15:17:17 +04:00
|
|
|
NO_OVERRIDE = 0
|
2012-12-21 09:45:50 +04:00
|
|
|
# See #install_alias_method.
|
2002-07-09 15:17:17 +04:00
|
|
|
OVERRIDE_PRIVATE_ONLY = 0x01
|
2012-12-21 09:45:50 +04:00
|
|
|
# See #install_alias_method.
|
2002-07-09 15:17:17 +04:00
|
|
|
OVERRIDE_ALL = 0x02
|
2001-04-30 21:38:21 +04:00
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Quits the current irb context
|
|
|
|
#
|
|
|
|
# +ret+ is the optional signal or message to send to Context#exit
|
|
|
|
#
|
|
|
|
# Same as <code>IRB.CurrentContext.exit</code>.
|
2002-07-09 15:17:17 +04:00
|
|
|
def irb_exit(ret = 0)
|
2023-10-12 02:22:20 +03:00
|
|
|
irb_context&.exit(ret)
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
|
2012-12-13 09:22:30 +04:00
|
|
|
# Displays current configuration.
|
|
|
|
#
|
2019-12-20 06:19:45 +03:00
|
|
|
# Modifying the configuration is achieved by sending a message to IRB.conf.
|
2001-04-30 21:38:21 +04:00
|
|
|
def irb_context
|
2002-07-09 15:17:17 +04:00
|
|
|
IRB.CurrentContext
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
|
2002-07-09 15:17:17 +04:00
|
|
|
@ALIASES = [
|
|
|
|
[:context, :irb_context, NO_OVERRIDE],
|
|
|
|
[:conf, :irb_context, NO_OVERRIDE],
|
|
|
|
[:irb_quit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
|
|
|
|
[:exit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
|
|
|
|
[:quit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
|
|
|
|
]
|
|
|
|
|
2022-12-08 22:10:19 +03:00
|
|
|
|
2002-07-09 15:17:17 +04:00
|
|
|
@EXTEND_COMMANDS = [
|
2019-08-27 19:07:27 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_current_working_workspace, :CurrentWorkingWorkspace, "cmd/chws",
|
2022-12-08 22:10:19 +03:00
|
|
|
[:cwws, NO_OVERRIDE],
|
|
|
|
[:pwws, NO_OVERRIDE],
|
2019-08-27 19:07:27 +03:00
|
|
|
[:irb_print_working_workspace, OVERRIDE_ALL],
|
|
|
|
[:irb_cwws, OVERRIDE_ALL],
|
|
|
|
[:irb_pwws, OVERRIDE_ALL],
|
|
|
|
[:irb_current_working_binding, OVERRIDE_ALL],
|
|
|
|
[:irb_print_working_binding, OVERRIDE_ALL],
|
|
|
|
[:irb_cwb, OVERRIDE_ALL],
|
|
|
|
[:irb_pwb, OVERRIDE_ALL],
|
|
|
|
],
|
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_change_workspace, :ChangeWorkspace, "cmd/chws",
|
2019-08-27 19:07:27 +03:00
|
|
|
[:chws, NO_OVERRIDE],
|
|
|
|
[:cws, NO_OVERRIDE],
|
2022-12-08 22:10:19 +03:00
|
|
|
[:irb_chws, OVERRIDE_ALL],
|
|
|
|
[:irb_cws, OVERRIDE_ALL],
|
2019-08-27 19:07:27 +03:00
|
|
|
[:irb_change_binding, OVERRIDE_ALL],
|
|
|
|
[:irb_cb, OVERRIDE_ALL],
|
|
|
|
[:cb, NO_OVERRIDE],
|
|
|
|
],
|
2002-07-09 15:17:17 +04:00
|
|
|
|
2019-08-27 19:07:27 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_workspaces, :Workspaces, "cmd/pushws",
|
2019-08-27 19:07:27 +03:00
|
|
|
[:workspaces, NO_OVERRIDE],
|
|
|
|
[:irb_bindings, OVERRIDE_ALL],
|
|
|
|
[:bindings, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_push_workspace, :PushWorkspace, "cmd/pushws",
|
2019-08-27 19:07:27 +03:00
|
|
|
[:pushws, NO_OVERRIDE],
|
2022-12-08 22:10:19 +03:00
|
|
|
[:irb_pushws, OVERRIDE_ALL],
|
2019-08-27 19:07:27 +03:00
|
|
|
[:irb_push_binding, OVERRIDE_ALL],
|
|
|
|
[:irb_pushb, OVERRIDE_ALL],
|
|
|
|
[:pushb, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_pop_workspace, :PopWorkspace, "cmd/pushws",
|
2019-08-27 19:07:27 +03:00
|
|
|
[:popws, NO_OVERRIDE],
|
2022-12-08 22:10:19 +03:00
|
|
|
[:irb_popws, OVERRIDE_ALL],
|
2019-08-27 19:07:27 +03:00
|
|
|
[:irb_pop_binding, OVERRIDE_ALL],
|
|
|
|
[:irb_popb, OVERRIDE_ALL],
|
|
|
|
[:popb, NO_OVERRIDE],
|
|
|
|
],
|
2002-07-09 15:17:17 +04:00
|
|
|
|
2019-08-27 19:07:27 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_load, :Load, "cmd/load"],
|
2019-08-27 19:07:27 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_require, :Require, "cmd/load"],
|
2019-08-27 19:07:27 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_source, :Source, "cmd/load",
|
2019-08-27 19:07:27 +03:00
|
|
|
[:source, NO_OVERRIDE],
|
|
|
|
],
|
2002-07-09 15:17:17 +04:00
|
|
|
|
2019-08-27 19:07:27 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb, :IrbCommand, "cmd/subirb"],
|
2019-08-27 19:07:27 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_jobs, :Jobs, "cmd/subirb",
|
2019-08-27 19:07:27 +03:00
|
|
|
[:jobs, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_fg, :Foreground, "cmd/subirb",
|
2019-08-27 19:07:27 +03:00
|
|
|
[:fg, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_kill, :Kill, "cmd/subirb",
|
2019-08-27 19:07:27 +03:00
|
|
|
[:kill, OVERRIDE_PRIVATE_ONLY],
|
|
|
|
],
|
2004-02-06 03:16:50 +03:00
|
|
|
|
2022-11-18 12:11:19 +03:00
|
|
|
[
|
|
|
|
:irb_debug, :Debug, "cmd/debug",
|
|
|
|
[:debug, NO_OVERRIDE],
|
|
|
|
],
|
2022-11-20 07:47:51 +03:00
|
|
|
[
|
|
|
|
:irb_edit, :Edit, "cmd/edit",
|
|
|
|
[:edit, NO_OVERRIDE],
|
|
|
|
],
|
2022-11-21 11:46:22 +03:00
|
|
|
[
|
|
|
|
:irb_break, :Break, "cmd/break",
|
|
|
|
],
|
|
|
|
[
|
|
|
|
:irb_catch, :Catch, "cmd/catch",
|
|
|
|
],
|
|
|
|
[
|
2022-12-08 22:10:19 +03:00
|
|
|
:irb_next, :Next, "cmd/next"
|
2022-11-21 11:46:22 +03:00
|
|
|
],
|
|
|
|
[
|
|
|
|
:irb_delete, :Delete, "cmd/delete",
|
|
|
|
[:delete, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
:irb_step, :Step, "cmd/step",
|
|
|
|
[:step, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
:irb_continue, :Continue, "cmd/continue",
|
|
|
|
[:continue, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
:irb_finish, :Finish, "cmd/finish",
|
|
|
|
[:finish, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
:irb_backtrace, :Backtrace, "cmd/backtrace",
|
|
|
|
[:backtrace, NO_OVERRIDE],
|
|
|
|
[:bt, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
:irb_debug_info, :Info, "cmd/info",
|
|
|
|
[:info, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
|
2019-08-27 19:07:27 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_help, :Help, "cmd/help",
|
2019-08-27 19:07:27 +03:00
|
|
|
[:help, NO_OVERRIDE],
|
|
|
|
],
|
2005-04-13 19:27:09 +04:00
|
|
|
|
2023-05-18 06:28:27 +03:00
|
|
|
[
|
|
|
|
:irb_show_doc, :ShowDoc, "cmd/show_doc",
|
|
|
|
[:show_doc, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
|
2020-04-27 12:27:19 +03:00
|
|
|
[
|
2022-11-21 11:46:22 +03:00
|
|
|
:irb_info, :IrbInfo, "cmd/irb_info"
|
2020-04-27 12:27:19 +03:00
|
|
|
],
|
|
|
|
|
2021-03-18 10:28:04 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_ls, :Ls, "cmd/ls",
|
2021-03-18 10:28:04 +03:00
|
|
|
[:ls, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
|
2020-08-15 00:36:24 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_measure, :Measure, "cmd/measure",
|
2021-03-18 13:06:22 +03:00
|
|
|
[:measure, NO_OVERRIDE],
|
2021-03-18 11:18:42 +03:00
|
|
|
],
|
|
|
|
|
2021-04-01 08:49:51 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_show_source, :ShowSource, "cmd/show_source",
|
2021-04-01 08:49:51 +03:00
|
|
|
[:show_source, NO_OVERRIDE],
|
|
|
|
],
|
|
|
|
|
2021-03-18 11:18:42 +03:00
|
|
|
[
|
2022-01-17 14:45:16 +03:00
|
|
|
:irb_whereami, :Whereami, "cmd/whereami",
|
2021-03-18 11:18:42 +03:00
|
|
|
[:whereami, NO_OVERRIDE],
|
2020-08-15 00:36:24 +03:00
|
|
|
],
|
2022-12-08 22:10:19 +03:00
|
|
|
[
|
|
|
|
:irb_show_cmds, :ShowCmds, "cmd/show_cmds",
|
|
|
|
[:show_cmds, NO_OVERRIDE],
|
|
|
|
]
|
2002-07-09 15:17:17 +04:00
|
|
|
]
|
|
|
|
|
2022-12-08 22:10:19 +03:00
|
|
|
|
|
|
|
@@commands = []
|
|
|
|
|
|
|
|
def self.all_commands_info
|
|
|
|
return @@commands unless @@commands.empty?
|
|
|
|
user_aliases = IRB.CurrentContext.command_aliases.each_with_object({}) do |(alias_name, target), result|
|
|
|
|
result[target] ||= []
|
|
|
|
result[target] << alias_name
|
|
|
|
end
|
|
|
|
|
|
|
|
@EXTEND_COMMANDS.each do |cmd_name, cmd_class, load_file, *aliases|
|
|
|
|
if !defined?(ExtendCommand) || !ExtendCommand.const_defined?(cmd_class, false)
|
|
|
|
require_relative load_file
|
|
|
|
end
|
|
|
|
|
|
|
|
klass = ExtendCommand.const_get(cmd_class, false)
|
|
|
|
aliases = aliases.map { |a| a.first }
|
|
|
|
|
|
|
|
if additional_aliases = user_aliases[cmd_name]
|
|
|
|
aliases += additional_aliases
|
|
|
|
end
|
|
|
|
|
|
|
|
display_name = aliases.shift || cmd_name
|
|
|
|
@@commands << { display_name: display_name, description: klass.description, category: klass.category }
|
|
|
|
end
|
|
|
|
|
|
|
|
@@commands
|
|
|
|
end
|
|
|
|
|
2022-11-07 20:29:24 +03:00
|
|
|
# Convert a command name to its implementation class if such command exists
|
|
|
|
def self.load_command(command)
|
|
|
|
command = command.to_sym
|
|
|
|
@EXTEND_COMMANDS.each do |cmd_name, cmd_class, load_file, *aliases|
|
|
|
|
next if cmd_name != command && aliases.all? { |alias_name, _| alias_name != command }
|
|
|
|
|
|
|
|
if !defined?(ExtendCommand) || !ExtendCommand.const_defined?(cmd_class, false)
|
|
|
|
require_relative load_file
|
|
|
|
end
|
|
|
|
return ExtendCommand.const_get(cmd_class, false)
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2022-11-20 01:14:40 +03:00
|
|
|
# Installs the default irb commands.
|
2006-08-08 19:17:06 +04:00
|
|
|
def self.install_extend_commands
|
2002-07-09 15:17:17 +04:00
|
|
|
for args in @EXTEND_COMMANDS
|
2014-08-09 05:36:49 +04:00
|
|
|
def_extend_command(*args)
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Evaluate the given +cmd_name+ on the given +cmd_class+ Class.
|
|
|
|
#
|
|
|
|
# Will also define any given +aliases+ for the method.
|
|
|
|
#
|
|
|
|
# The optional +load_file+ parameter will be required within the method
|
|
|
|
# definition.
|
2023-04-08 13:11:46 +03:00
|
|
|
def self.def_extend_command(cmd_name, cmd_class, load_file, *aliases)
|
2002-07-09 15:17:17 +04:00
|
|
|
case cmd_class
|
|
|
|
when Symbol
|
2014-08-09 05:36:49 +04:00
|
|
|
cmd_class = cmd_class.id2name
|
2002-07-09 15:17:17 +04:00
|
|
|
when String
|
|
|
|
when Class
|
2014-08-09 05:36:49 +04:00
|
|
|
cmd_class = cmd_class.name
|
2002-07-09 15:17:17 +04:00
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
|
2023-04-08 13:11:46 +03:00
|
|
|
line = __LINE__; eval %[
|
|
|
|
def #{cmd_name}(*opts, **kwargs, &b)
|
|
|
|
Kernel.require_relative "#{load_file}"
|
|
|
|
::IRB::ExtendCommand::#{cmd_class}.execute(irb_context, *opts, **kwargs, &b)
|
|
|
|
end
|
|
|
|
], nil, __FILE__, line
|
2001-04-30 21:38:21 +04:00
|
|
|
|
2002-07-09 15:17:17 +04:00
|
|
|
for ali, flag in aliases
|
2014-08-09 05:36:49 +04:00
|
|
|
@ALIASES.push [ali, cmd_name, flag]
|
2002-07-09 15:17:17 +04:00
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Installs alias methods for the default irb commands, see
|
|
|
|
# ::install_extend_commands.
|
2002-07-09 15:17:17 +04:00
|
|
|
def install_alias_method(to, from, override = NO_OVERRIDE)
|
2001-04-30 21:38:21 +04:00
|
|
|
to = to.id2name unless to.kind_of?(String)
|
|
|
|
from = from.id2name unless from.kind_of?(String)
|
|
|
|
|
|
|
|
if override == OVERRIDE_ALL or
|
2014-08-09 05:36:49 +04:00
|
|
|
(override == OVERRIDE_PRIVATE_ONLY) && !respond_to?(to) or
|
|
|
|
(override == NO_OVERRIDE) && !respond_to?(to, true)
|
|
|
|
target = self
|
|
|
|
(class << self; self; end).instance_eval{
|
|
|
|
if target.respond_to?(to, true) &&
|
|
|
|
!target.respond_to?(EXCB.irb_original_method_name(to), true)
|
|
|
|
alias_method(EXCB.irb_original_method_name(to), to)
|
|
|
|
end
|
|
|
|
alias_method to, from
|
|
|
|
}
|
2001-04-30 21:38:21 +04:00
|
|
|
else
|
2023-06-10 18:01:41 +03:00
|
|
|
Kernel.warn "irb: warn: can't alias #{to} from #{from}.\n"
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
def self.irb_original_method_name(method_name) # :nodoc:
|
2001-04-30 21:38:21 +04:00
|
|
|
"irb_" + method_name + "_org"
|
|
|
|
end
|
2002-07-09 15:17:17 +04:00
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Installs alias methods for the default irb commands on the given object
|
|
|
|
# using #install_alias_method.
|
2006-08-08 19:17:06 +04:00
|
|
|
def self.extend_object(obj)
|
2009-12-05 04:18:28 +03:00
|
|
|
unless (class << obj; ancestors; end).include?(EXCB)
|
2014-08-09 05:36:49 +04:00
|
|
|
super
|
|
|
|
for ali, com, flg in @ALIASES
|
|
|
|
obj.install_alias_method(ali, com, flg)
|
|
|
|
end
|
2002-07-09 15:17:17 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
install_extend_commands
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Extends methods for the Context module
|
2002-07-09 15:17:17 +04:00
|
|
|
module ContextExtender
|
2012-12-21 09:45:50 +04:00
|
|
|
CE = ContextExtender # :nodoc:
|
2002-07-09 15:17:17 +04:00
|
|
|
|
|
|
|
@EXTEND_COMMANDS = [
|
2023-07-04 18:17:36 +03:00
|
|
|
[:eval_history=, "ext/eval_history.rb"],
|
2022-01-17 14:45:16 +03:00
|
|
|
[:use_tracer=, "ext/tracer.rb"],
|
|
|
|
[:use_loader=, "ext/use-loader.rb"],
|
2002-07-09 15:17:17 +04:00
|
|
|
]
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Installs the default context extensions as irb commands:
|
|
|
|
#
|
|
|
|
# Context#eval_history=:: +irb/ext/history.rb+
|
|
|
|
# Context#use_tracer=:: +irb/ext/tracer.rb+
|
|
|
|
# Context#use_loader=:: +irb/ext/use-loader.rb+
|
2006-08-08 19:17:06 +04:00
|
|
|
def self.install_extend_commands
|
2002-07-09 15:17:17 +04:00
|
|
|
for args in @EXTEND_COMMANDS
|
2014-08-09 05:36:49 +04:00
|
|
|
def_extend_command(*args)
|
2002-07-09 15:17:17 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Evaluate the given +command+ from the given +load_file+ on the Context
|
|
|
|
# module.
|
|
|
|
#
|
|
|
|
# Will also define any given +aliases+ for the method.
|
2006-08-08 19:17:06 +04:00
|
|
|
def self.def_extend_command(cmd_name, load_file, *aliases)
|
2009-12-05 04:18:28 +03:00
|
|
|
line = __LINE__; Context.module_eval %[
|
2002-07-09 15:17:17 +04:00
|
|
|
def #{cmd_name}(*opts, &b)
|
2014-08-09 05:36:49 +04:00
|
|
|
Context.module_eval {remove_method(:#{cmd_name})}
|
2022-01-17 14:45:16 +03:00
|
|
|
require_relative "#{load_file}"
|
2020-10-27 07:42:52 +03:00
|
|
|
__send__ :#{cmd_name}, *opts, &b
|
2014-08-09 05:36:49 +04:00
|
|
|
end
|
|
|
|
for ali in aliases
|
|
|
|
alias_method ali, cmd_name
|
|
|
|
end
|
2009-12-05 04:18:28 +03:00
|
|
|
], __FILE__, line
|
2002-07-09 15:17:17 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
CE.install_extend_commands
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
end
|