2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2007-11-10 02:48:56 -05:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
|
|
|
require 'rubygems/command'
|
|
|
|
require 'rubygems/user_interaction'
|
2019-03-04 22:32:58 -05:00
|
|
|
require 'rubygems/text'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17: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 01:52:18 -05: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-09 17: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-04 22:32:58 -05:00
|
|
|
include Gem::Text
|
2009-06-09 17:38:59 -04:00
|
|
|
include Gem::UserInteraction
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
BUILTIN_COMMANDS = [ # :nodoc:
|
|
|
|
:build,
|
|
|
|
:cert,
|
|
|
|
:check,
|
|
|
|
:cleanup,
|
|
|
|
:contents,
|
|
|
|
:dependency,
|
|
|
|
:environment,
|
|
|
|
:fetch,
|
|
|
|
:generate_index,
|
|
|
|
:help,
|
2018-05-30 09:01:35 -04:00
|
|
|
:info,
|
2013-09-14 04:59:02 -04:00
|
|
|
:install,
|
|
|
|
:list,
|
|
|
|
:lock,
|
|
|
|
:mirror,
|
2014-09-13 23:30:02 -04:00
|
|
|
:open,
|
2013-09-14 04:59:02 -04:00
|
|
|
:outdated,
|
|
|
|
:owner,
|
|
|
|
:pristine,
|
|
|
|
:push,
|
|
|
|
:query,
|
|
|
|
:rdoc,
|
|
|
|
:search,
|
|
|
|
:server,
|
2017-10-07 21:32:18 -04:00
|
|
|
:signin,
|
|
|
|
:signout,
|
2013-09-14 04:59:02 -04:00
|
|
|
:sources,
|
|
|
|
:specification,
|
|
|
|
:stale,
|
|
|
|
:uninstall,
|
|
|
|
:unpack,
|
|
|
|
:update,
|
|
|
|
:which,
|
|
|
|
:yank,
|
2018-10-21 20:27:02 -04:00
|
|
|
].freeze
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
ALIAS_COMMANDS = {
|
2020-12-08 02:33:39 -05:00
|
|
|
'i' => 'install',
|
2018-10-21 20:27:02 -04:00
|
|
|
}.freeze
|
2018-08-27 06:05:04 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Return the authoritative instance of the command manager.
|
|
|
|
|
|
|
|
def self.instance
|
|
|
|
@command_manager ||= new
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
##
|
|
|
|
# Returns self. Allows a CommandManager instance to stand
|
|
|
|
# in for the class itself.
|
|
|
|
|
|
|
|
def instance
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
##
|
|
|
|
# Reset the authoritative instance of the command manager.
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@command_manager = nil
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Register all the subcommands supported by the gem command.
|
|
|
|
|
|
|
|
def initialize
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'timeout'
|
2009-06-09 17:38:59 -04:00
|
|
|
@commands = {}
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
BUILTIN_COMMANDS.each do |name|
|
|
|
|
register_command name
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2010-04-22 04:24:42 -04:00
|
|
|
# Register the Symbol +command+ as a gem command.
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def register_command(command, obj=false)
|
|
|
|
@commands[command] = obj
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
##
|
|
|
|
# Unregister the Symbol +command+ as a gem command.
|
|
|
|
|
|
|
|
def unregister_command(command)
|
|
|
|
@commands.delete command
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2012-11-29 01:52:18 -05:00
|
|
|
# Returns a Command instance for +command_name+
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2009-06-09 17: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-05 22:56:38 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2012-11-29 01:52:18 -05:00
|
|
|
# Return a sorted list of all command names as strings.
|
2009-06-09 17:38:59 -04:00
|
|
|
|
|
|
|
def command_names
|
2020-06-10 13:46:05 -04:00
|
|
|
@commands.keys.collect {|key| key.to_s }.sort
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2012-11-29 01:52:18 -05:00
|
|
|
# Run the command specified by +args+.
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def run(args, build_args=nil)
|
|
|
|
process_args(args, build_args)
|
2009-06-09 17:38:59 -04:00
|
|
|
rescue StandardError, Timeout::Error => ex
|
2019-03-04 22:32:58 -05:00
|
|
|
alert_error clean_text("While executing gem ... (#{ex.class})\n #{ex}")
|
2012-11-29 01:52:18 -05:00
|
|
|
ui.backtrace ex
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
terminate_interaction(1)
|
|
|
|
rescue Interrupt
|
2019-03-04 22:32:58 -05:00
|
|
|
alert_error clean_text("Interrupted")
|
2009-06-09 17:38:59 -04:00
|
|
|
terminate_interaction(1)
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def process_args(args, build_args=nil)
|
2018-11-21 05:20:47 -05:00
|
|
|
if args.empty?
|
2009-06-09 17:38:59 -04:00
|
|
|
say Gem::Command::HELP
|
2012-11-29 01:52:18 -05:00
|
|
|
terminate_interaction 1
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
case args.first
|
|
|
|
when '-h', '--help' then
|
2009-06-09 17:38:59 -04:00
|
|
|
say Gem::Command::HELP
|
2012-11-29 01:52:18 -05:00
|
|
|
terminate_interaction 0
|
|
|
|
when '-v', '--version' then
|
2010-04-22 04:24:42 -04:00
|
|
|
say Gem::VERSION
|
2012-11-29 01:52:18 -05:00
|
|
|
terminate_interaction 0
|
|
|
|
when /^-/ then
|
2019-03-04 22:32:58 -05:00
|
|
|
alert_error clean_text("Invalid option: #{args.first}. See 'gem --help'.")
|
2012-11-29 01:52:18 -05:00
|
|
|
terminate_interaction 1
|
2009-06-09 17:38:59 -04:00
|
|
|
else
|
|
|
|
cmd_name = args.shift.downcase
|
2012-11-29 01:52:18 -05:00
|
|
|
cmd = find_command cmd_name
|
2020-01-31 21:14:04 -05:00
|
|
|
cmd.deprecation_warning if cmd.deprecated?
|
2020-12-08 02:33:39 -05:00
|
|
|
cmd.invoke_with_build_args args, build_args
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def find_command(cmd_name)
|
2018-08-27 06:05:04 -04:00
|
|
|
cmd_name = find_alias_command cmd_name
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
possibilities = find_command_possibilities cmd_name
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if possibilities.size > 1
|
2012-11-29 01:52:18 -05:00
|
|
|
raise Gem::CommandLineError,
|
2012-12-06 00:26:46 -05:00
|
|
|
"Ambiguous command #{cmd_name} matches [#{possibilities.join(', ')}]"
|
2018-11-21 05:20:47 -05:00
|
|
|
elsif possibilities.empty?
|
2012-11-29 01:52:18 -05:00
|
|
|
raise Gem::CommandLineError, "Unknown command #{cmd_name}"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
self[possibilities.first]
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
def find_alias_command(cmd_name)
|
|
|
|
alias_name = ALIAS_COMMANDS[cmd_name]
|
|
|
|
alias_name ? alias_name : cmd_name
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def find_command_possibilities(cmd_name)
|
|
|
|
len = cmd_name.length
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
found = command_names.select {|name| cmd_name == name[0, len] }
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
exact = found.find {|name| name == cmd_name }
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
exact ? [exact] : found
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_and_instantiate(command_name)
|
|
|
|
command_name = command_name.to_s
|
2009-12-30 03:49:48 -05:00
|
|
|
const_name = command_name.capitalize.gsub(/_(.)/) { $1.upcase } << "Command"
|
2012-11-29 01:52:18 -05:00
|
|
|
load_error = nil
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
begin
|
2010-04-22 04:24:42 -04:00
|
|
|
begin
|
2009-06-09 17:38:59 -04:00
|
|
|
require "rubygems/commands/#{command_name}_command"
|
2012-11-29 01:52:18 -05:00
|
|
|
rescue LoadError => e
|
|
|
|
load_error = e
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
Gem::Commands.const_get(const_name).new
|
|
|
|
rescue Exception => e
|
|
|
|
e = load_error if load_error
|
|
|
|
|
2019-03-04 22:32:58 -05:00
|
|
|
alert_error clean_text("Loading command: #{command_name} (#{e.class})\n\t#{e}")
|
2012-11-29 01:52:18 -05:00
|
|
|
ui.backtrace e
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|