2016-02-01 15:43:26 +03:00
|
|
|
# frozen_string_literal: true
|
2023-03-17 12:36:42 +03:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
2019-04-22 14:56:16 +03:00
|
|
|
require_relative "command"
|
|
|
|
require_relative "user_interaction"
|
|
|
|
require_relative "text"
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
|
|
|
# The command manager registers and installs all the individual sub-commands
|
|
|
|
# supported by the gem command.
|
|
|
|
#
|
|
|
|
# Extra commands can be provided by writing a rubygems_plugin.rb
|
|
|
|
# file in an installed gem. You should register your command against the
|
|
|
|
# Gem::CommandManager instance, like this:
|
|
|
|
#
|
|
|
|
# # file rubygems_plugin.rb
|
|
|
|
# require 'rubygems/command_manager'
|
|
|
|
#
|
2012-11-29 10:52:18 +04:00
|
|
|
# Gem::CommandManager.instance.register_command :edit
|
|
|
|
#
|
|
|
|
# You should put the implementation of your command in rubygems/commands.
|
|
|
|
#
|
|
|
|
# # file rubygems/commands/edit_command.rb
|
2009-06-10 01:38:59 +04:00
|
|
|
# class Gem::Commands::EditCommand < Gem::Command
|
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# See Gem::Command for instructions on writing gem commands.
|
|
|
|
|
|
|
|
class Gem::CommandManager
|
2019-03-05 06:32:58 +03:00
|
|
|
include Gem::Text
|
2009-06-10 01:38:59 +04:00
|
|
|
include Gem::UserInteraction
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
BUILTIN_COMMANDS = [ # :nodoc:
|
|
|
|
:build,
|
|
|
|
:cert,
|
|
|
|
:check,
|
|
|
|
:cleanup,
|
|
|
|
:contents,
|
|
|
|
:dependency,
|
|
|
|
:environment,
|
2023-01-26 04:08:14 +03:00
|
|
|
:exec,
|
2013-09-14 12:59:02 +04:00
|
|
|
:fetch,
|
|
|
|
:generate_index,
|
|
|
|
:help,
|
2018-05-30 16:01:35 +03:00
|
|
|
:info,
|
2013-09-14 12:59:02 +04:00
|
|
|
:install,
|
|
|
|
:list,
|
|
|
|
:lock,
|
|
|
|
:mirror,
|
2014-09-14 07:30:02 +04:00
|
|
|
:open,
|
2013-09-14 12:59:02 +04:00
|
|
|
:outdated,
|
|
|
|
:owner,
|
|
|
|
:pristine,
|
|
|
|
:push,
|
|
|
|
:query,
|
|
|
|
:rdoc,
|
|
|
|
:search,
|
|
|
|
:server,
|
2017-10-08 04:32:18 +03:00
|
|
|
:signin,
|
|
|
|
:signout,
|
2013-09-14 12:59:02 +04:00
|
|
|
:sources,
|
|
|
|
:specification,
|
|
|
|
:stale,
|
|
|
|
:uninstall,
|
|
|
|
:unpack,
|
|
|
|
:update,
|
|
|
|
:which,
|
|
|
|
:yank,
|
2018-10-22 03:27:02 +03:00
|
|
|
].freeze
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2018-08-27 13:05:04 +03:00
|
|
|
ALIAS_COMMANDS = {
|
2022-09-07 08:13:04 +03:00
|
|
|
"i" => "install",
|
|
|
|
"login" => "signin",
|
2021-12-04 10:25:43 +03:00
|
|
|
"logout" => "signout",
|
2018-10-22 03:27:02 +03:00
|
|
|
}.freeze
|
2018-08-27 13:05:04 +03:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
|
|
|
# Return the authoritative instance of the command manager.
|
|
|
|
|
|
|
|
def self.instance
|
2023-03-24 22:41:46 +03:00
|
|
|
@instance ||= new
|
2009-06-10 01:38:59 +04:00
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
##
|
|
|
|
# Returns self. Allows a CommandManager instance to stand
|
|
|
|
# in for the class itself.
|
|
|
|
|
|
|
|
def instance
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
##
|
|
|
|
# Reset the authoritative instance of the command manager.
|
|
|
|
|
|
|
|
def self.reset
|
2023-03-24 22:41:46 +03:00
|
|
|
@instance = nil
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
|
|
|
# Register all the subcommands supported by the gem command.
|
|
|
|
|
|
|
|
def initialize
|
2011-01-19 03:08:49 +03:00
|
|
|
require "timeout"
|
2009-06-10 01:38:59 +04:00
|
|
|
@commands = {}
|
2013-09-14 12:59:02 +04:00
|
|
|
|
|
|
|
BUILTIN_COMMANDS.each do |name|
|
|
|
|
register_command name
|
|
|
|
end
|
2009-06-10 01:38:59 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
2010-04-22 12:24:42 +04:00
|
|
|
# Register the Symbol +command+ as a gem command.
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def register_command(command, obj=false)
|
|
|
|
@commands[command] = obj
|
2009-06-10 01:38:59 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
##
|
|
|
|
# Unregister the Symbol +command+ as a gem command.
|
|
|
|
|
|
|
|
def unregister_command(command)
|
|
|
|
@commands.delete command
|
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
2012-11-29 10:52:18 +04:00
|
|
|
# Returns a Command instance for +command_name+
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
def [](command_name)
|
|
|
|
command_name = command_name.intern
|
|
|
|
return nil if @commands[command_name].nil?
|
|
|
|
@commands[command_name] ||= load_and_instantiate(command_name)
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
2012-11-29 10:52:18 +04:00
|
|
|
# Return a sorted list of all command names as strings.
|
2009-06-10 01:38:59 +04:00
|
|
|
|
|
|
|
def command_names
|
2023-03-16 06:57:49 +03:00
|
|
|
@commands.keys.collect(&:to_s).sort
|
2009-06-10 01:38:59 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
2012-11-29 10:52:18 +04:00
|
|
|
# Run the command specified by +args+.
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def run(args, build_args=nil)
|
|
|
|
process_args(args, build_args)
|
2009-06-10 01:38:59 +04:00
|
|
|
rescue StandardError, Timeout::Error => ex
|
2022-05-23 12:45:39 +03:00
|
|
|
if ex.respond_to?(:detailed_message)
|
|
|
|
msg = ex.detailed_message(highlight: false).sub(/\A(.*?)(?: \(.+?\))/) { $1 }
|
|
|
|
else
|
|
|
|
msg = ex.message
|
|
|
|
end
|
|
|
|
alert_error clean_text("While executing gem ... (#{ex.class})\n #{msg}")
|
2012-11-29 10:52:18 +04:00
|
|
|
ui.backtrace ex
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
terminate_interaction(1)
|
|
|
|
rescue Interrupt
|
2019-03-05 06:32:58 +03:00
|
|
|
alert_error clean_text("Interrupted")
|
2009-06-10 01:38:59 +04:00
|
|
|
terminate_interaction(1)
|
|
|
|
end
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def process_args(args, build_args=nil)
|
2018-11-21 13:20:47 +03:00
|
|
|
if args.empty?
|
2009-06-10 01:38:59 +04:00
|
|
|
say Gem::Command::HELP
|
2012-11-29 10:52:18 +04:00
|
|
|
terminate_interaction 1
|
2009-06-10 01:38:59 +04:00
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
|
|
|
case args.first
|
|
|
|
when "-h", "--help" then
|
2009-06-10 01:38:59 +04:00
|
|
|
say Gem::Command::HELP
|
2012-11-29 10:52:18 +04:00
|
|
|
terminate_interaction 0
|
|
|
|
when "-v", "--version" then
|
2010-04-22 12:24:42 +04:00
|
|
|
say Gem::VERSION
|
2012-11-29 10:52:18 +04:00
|
|
|
terminate_interaction 0
|
2022-12-22 21:24:34 +03:00
|
|
|
when "-C" then
|
|
|
|
args.shift
|
|
|
|
start_point = args.shift
|
|
|
|
if Dir.exist?(start_point)
|
|
|
|
Dir.chdir(start_point) { invoke_command(args, build_args) }
|
|
|
|
else
|
|
|
|
alert_error clean_text("#{start_point} isn't a directory.")
|
|
|
|
terminate_interaction 1
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
when /^-/ then
|
2019-03-05 06:32:58 +03:00
|
|
|
alert_error clean_text("Invalid option: #{args.first}. See 'gem --help'.")
|
2012-11-29 10:52:18 +04:00
|
|
|
terminate_interaction 1
|
2009-06-10 01:38:59 +04:00
|
|
|
else
|
2022-12-22 21:24:34 +03:00
|
|
|
invoke_command(args, build_args)
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
2009-06-10 01:38:59 +04:00
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
def find_command(cmd_name)
|
2018-08-27 13:05:04 +03:00
|
|
|
cmd_name = find_alias_command cmd_name
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
possibilities = find_command_possibilities cmd_name
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
if possibilities.size > 1
|
2012-11-29 10:52:18 +04:00
|
|
|
raise Gem::CommandLineError,
|
2023-03-16 07:00:54 +03:00
|
|
|
"Ambiguous command #{cmd_name} matches [#{possibilities.join(", ")}]"
|
2018-11-21 13:20:47 +03:00
|
|
|
elsif possibilities.empty?
|
2021-01-04 04:09:05 +03:00
|
|
|
raise Gem::UnknownCommandError.new(cmd_name)
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
self[possibilities.first]
|
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2018-08-27 13:05:04 +03:00
|
|
|
def find_alias_command(cmd_name)
|
|
|
|
alias_name = ALIAS_COMMANDS[cmd_name]
|
|
|
|
alias_name ? alias_name : cmd_name
|
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
def find_command_possibilities(cmd_name)
|
|
|
|
len = cmd_name.length
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2020-06-10 20:46:05 +03:00
|
|
|
found = command_names.select {|name| cmd_name == name[0, len] }
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2020-06-10 20:46:05 +03:00
|
|
|
exact = found.find {|name| name == cmd_name }
|
2012-11-29 10:52:18 +04:00
|
|
|
|
|
|
|
exact ? [exact] : found
|
2009-06-10 01:38:59 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_and_instantiate(command_name)
|
|
|
|
command_name = command_name.to_s
|
2009-12-30 11:49:48 +03:00
|
|
|
const_name = command_name.capitalize.gsub(/_(.)/) { $1.upcase } << "Command"
|
2012-11-29 10:52:18 +04:00
|
|
|
load_error = nil
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
begin
|
2010-04-22 12:24:42 +04:00
|
|
|
begin
|
2009-06-10 01:38:59 +04:00
|
|
|
require "rubygems/commands/#{command_name}_command"
|
2012-11-29 10:52:18 +04:00
|
|
|
rescue LoadError => e
|
|
|
|
load_error = e
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
Gem::Commands.const_get(const_name).new
|
2023-03-22 06:25:40 +03:00
|
|
|
rescue StandardError => e
|
2012-11-29 10:52:18 +04:00
|
|
|
e = load_error if load_error
|
|
|
|
|
2019-03-05 06:32:58 +03:00
|
|
|
alert_error clean_text("Loading command: #{command_name} (#{e.class})\n\t#{e}")
|
2012-11-29 10:52:18 +04:00
|
|
|
ui.backtrace e
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
2022-12-22 21:24:34 +03:00
|
|
|
|
|
|
|
def invoke_command(args, build_args)
|
|
|
|
cmd_name = args.shift.downcase
|
|
|
|
cmd = find_command cmd_name
|
|
|
|
cmd.deprecation_warning if cmd.deprecated?
|
|
|
|
cmd.invoke_with_build_args args, build_args
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
end
|