# frozen_string_literal: true#--# 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'require'rubygems/text'### 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'## Gem::CommandManager.instance.register_command :edit## You should put the implementation of your command in rubygems/commands.## # file rubygems/commands/edit_command.rb# class Gem::Commands::EditCommand < Gem::Command# # ...# end## See Gem::Command for instructions on writing gem commands.classGem::CommandManagerincludeGem::TextincludeGem::UserInteractionBUILTIN_COMMANDS=[# :nodoc::build,:cert,:check,:cleanup,:contents,:dependency,:environment,:fetch,:generate_index,:help,:info,:install,:list,:lock,:mirror,:open,:outdated,:owner,:pristine,:push,:query,:rdoc,:search,:server,:signin,:signout,:sources,:specification,:stale,:uninstall,:unpack,:update,:which,:yank,].freezeALIAS_COMMANDS={'i'=>'install',}.freeze### Return the authoritative instance of the command manager.defself.instance@command_manager||=newend### Returns self. Allows a CommandManager instance to stand# in for the class itself.definstanceselfend### Reset the authoritative instance of the command manager.defself.reset@command_manager=nilend### Register all the subcommands supported by the gem command.definitializerequire'timeout'@commands={}BUILTIN_COMMANDS.eachdo|name|register_commandnameendend### Register the Symbol +command+ as a gem command.defregister_command(command,obj=false)@commands[command]=objend### Unregister the Symbol +command+ as a gem command.defunregister_command(command)@commands.deletecommandend### Returns a Command instance for +command_name+def[](command_name)command_name=command_name.internreturnnilif@commands[command_name].nil?@commands[command_name]||=load_and_instantiate(command_name)end### Return a sorted list of all command names as strings.defcommand_names@commands.keys.collect{|key|key.to_s}.sortend### Run the command specified by +args+.defrun(args,build_args=nil)process_args(args,build_args)rescueStandardError,Timeout::Error=>exalert_errorclean_text("While executing gem ... (#{ex.class})\n#{ex}")ui.backtraceexterminate_interaction(1)rescueInterruptalert_errorclean_text("Interrupted")terminate_interaction(1)enddefprocess_args(args,build_args=nil)ifargs.empty?sayGem::Command::HELPterminate_interaction1endcaseargs.firstwhen'-h','--help'thensayGem::Command::HELPterminate_interaction0when'-v','--version'thensayGem::VERSIONterminate_interaction0when/^-/thenalert_errorclean_text("Invalid option: #{args.first}. See 'gem --help'.")terminate_interaction1elsecmd_name=args.shift.downcasecmd=find_commandcmd_namecmd.deprecation_warningifcmd.deprecated?cmd.invoke_with_build_argsargs,build_argsendenddeffind_command(cmd_name)cmd_name=find_alias_commandcmd_namepossibilities=find_command_possibilitiescmd_nameifpossibilities.size>1raiseGem::CommandLineError,"Ambiguous command #{cmd_name} matches [#{possibilities.join(', ')}]"elsifpossibilities.empty?raiseGem::CommandLineError,"Unknown command #{cmd_name}"endself[possibilities.first]enddeffind_alias_command(cmd_name)alias_name=ALIAS_COMMANDS[cmd_name]alias_name?alias_name:cmd_nameenddeffind_command_possibilities(cmd_name)len=cmd_name.lengthfound=command_names.select{|name|cmd_name==name[0,len]}exact=found.find{|name|name==cmd_name}exact?[exact]:foundendprivatedefload_and_instantiate(command_name)command_name=command_name.to_sconst_name=command_name.capitalize.gsub(/_(.)/){$1.upcase}<<"Command"load_error=nilbeginbeginrequire"rubygems/commands/#{command_name}_command"rescueLoadError=>eload_error=eendGem::Commands.const_get(const_name).newrescueException=>ee=load_errorifload_erroralert_errorclean_text("Loading command: #{command_name} (#{e.class})\n\t#{e}")ui.backtraceeendendend