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'
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
'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
|
|
|
|
@commands.keys.collect {|key| key.to_s}.sort
|
|
|
|
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
|
2014-08-27 08:21:41 -04:00
|
|
|
alert_error "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
|
|
|
|
alert_error "Interrupted"
|
|
|
|
terminate_interaction(1)
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def process_args(args, build_args=nil)
|
|
|
|
if args.empty? then
|
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
|
2017-10-07 21:32:18 -04:00
|
|
|
alert_error "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
|
|
|
|
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
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
if possibilities.size > 1 then
|
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(', ')}]"
|
2012-11-29 01:52:18 -05:00
|
|
|
elsif possibilities.empty? then
|
|
|
|
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
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
found = command_names.select { |name| cmd_name == name[0, len] }
|
|
|
|
|
|
|
|
exact = found.find { |name| name == cmd_name }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
alert_error "Loading command: #{command_name} (#{e.class})\n\t#{e}"
|
|
|
|
ui.backtrace e
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|