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.
|
|
|
|
#++
|
|
|
|
|
|
|
|
require 'rubygems/command'
|
|
|
|
require 'rubygems/user_interaction'
|
|
|
|
|
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
|
|
|
|
|
|
|
|
include Gem::UserInteraction
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
BUILTIN_COMMANDS = [ # :nodoc:
|
|
|
|
:build,
|
|
|
|
:cert,
|
|
|
|
:check,
|
|
|
|
:cleanup,
|
|
|
|
:contents,
|
|
|
|
:dependency,
|
|
|
|
:environment,
|
|
|
|
:fetch,
|
|
|
|
:generate_index,
|
|
|
|
:help,
|
|
|
|
:install,
|
|
|
|
:list,
|
|
|
|
:lock,
|
|
|
|
:mirror,
|
|
|
|
:outdated,
|
|
|
|
:owner,
|
|
|
|
:pristine,
|
|
|
|
:push,
|
|
|
|
:query,
|
|
|
|
:rdoc,
|
|
|
|
:search,
|
|
|
|
:server,
|
|
|
|
:sources,
|
|
|
|
:specification,
|
|
|
|
:stale,
|
|
|
|
:uninstall,
|
|
|
|
:unpack,
|
|
|
|
:update,
|
|
|
|
:which,
|
|
|
|
:yank,
|
|
|
|
]
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
|
|
|
# Return the authoritative instance of the command manager.
|
|
|
|
|
|
|
|
def self.instance
|
|
|
|
@command_manager ||= new
|
|
|
|
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
|
|
|
|
@command_manager = nil
|
|
|
|
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
|
|
|
|
@commands.keys.collect {|key| key.to_s}.sort
|
|
|
|
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
|
2014-08-27 16:21:41 +04:00
|
|
|
alert_error "While executing gem ... (#{ex.class})\n #{ex}"
|
2012-11-29 10:52:18 +04:00
|
|
|
ui.backtrace ex
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
terminate_interaction(1)
|
|
|
|
rescue Interrupt
|
|
|
|
alert_error "Interrupted"
|
|
|
|
terminate_interaction(1)
|
|
|
|
end
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def process_args(args, build_args=nil)
|
|
|
|
if args.empty? then
|
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
|
|
|
|
when /^-/ then
|
|
|
|
alert_error "Invalid option: #{args.first}. See 'gem --help'."
|
|
|
|
terminate_interaction 1
|
2009-06-10 01:38:59 +04:00
|
|
|
else
|
|
|
|
cmd_name = args.shift.downcase
|
2012-11-29 10:52:18 +04:00
|
|
|
cmd = find_command cmd_name
|
|
|
|
cmd.invoke_with_build_args 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)
|
|
|
|
possibilities = find_command_possibilities cmd_name
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
if possibilities.size > 1 then
|
2012-11-29 10:52:18 +04:00
|
|
|
raise Gem::CommandLineError,
|
2012-12-06 09:26:46 +04:00
|
|
|
"Ambiguous command #{cmd_name} matches [#{possibilities.join(', ')}]"
|
2012-11-29 10:52:18 +04:00
|
|
|
elsif possibilities.empty? then
|
|
|
|
raise Gem::CommandLineError, "Unknown command #{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
|
|
|
|
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
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
found = command_names.select { |name| cmd_name == name[0, len] }
|
|
|
|
|
|
|
|
exact = found.find { |name| name == cmd_name }
|
|
|
|
|
|
|
|
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
|
|
|
|
rescue Exception => e
|
|
|
|
e = load_error if load_error
|
|
|
|
|
|
|
|
alert_error "Loading command: #{command_name} (#{e.class})\n\t#{e}"
|
|
|
|
ui.backtrace e
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2009-03-06 06:56:38 +03:00
|
|
|
end
|
2009-06-10 01:38:59 +04:00
|
|
|
|