From 9694bb8cac12969300692dac5a1cf7aa4e3a46cd Mon Sep 17 00:00:00 2001 From: drbrain Date: Thu, 29 Nov 2012 06:52:18 +0000 Subject: * lib/rubygems*: Updated to RubyGems 2.0 * test/rubygems*: ditto. * common.mk (prelude): Updated for RubyGems 2.0 source rearrangement. * tool/change_maker.rb: Allow invalid UTF-8 characters in source files. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37976 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/rubygems/command_manager.rb | 107 +++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 40 deletions(-) (limited to 'lib/rubygems/command_manager.rb') diff --git a/lib/rubygems/command_manager.rb b/lib/rubygems/command_manager.rb index 9edd550136..e39c509ef9 100644 --- a/lib/rubygems/command_manager.rb +++ b/lib/rubygems/command_manager.rb @@ -18,12 +18,15 @@ require 'rubygems/user_interaction' # # 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 # -# Gem::CommandManager.instance.register_command :edit -# # See Gem::Command for instructions on writing gem commands. class Gem::CommandManager @@ -37,6 +40,14 @@ class Gem::CommandManager @command_manager ||= new end + ## + # Returns self. Allows a CommandManager instance to stand + # in for the class itself. + + def instance + self + end + ## # Reset the authoritative instance of the command manager. @@ -63,6 +74,7 @@ class Gem::CommandManager register_command :install register_command :list register_command :lock + register_command :mirror register_command :outdated register_command :owner register_command :pristine @@ -78,13 +90,14 @@ class Gem::CommandManager register_command :unpack register_command :update register_command :which + register_command :yank end ## # Register the Symbol +command+ as a gem command. - def register_command(command) - @commands[command] = false + def register_command(command, obj=false) + @commands[command] = obj end ## @@ -95,7 +108,7 @@ class Gem::CommandManager end ## - # Return the registered command from the command name. + # Returns a Command instance for +command_name+ def [](command_name) command_name = command_name.intern @@ -104,56 +117,69 @@ class Gem::CommandManager end ## - # Return a sorted list of all command names (as strings). + # Return a sorted list of all command names as strings. def command_names @commands.keys.collect {|key| key.to_s}.sort end ## - # Run the config specified by +args+. + # Run the command specified by +args+. - def run(args) - process_args(args) + def run(args, build_args=nil) + process_args(args, build_args) rescue StandardError, Timeout::Error => ex alert_error "While executing gem ... (#{ex.class})\n #{ex.to_s}" - ui.errs.puts "\t#{ex.backtrace.join "\n\t"}" if - Gem.configuration.backtrace + ui.backtrace ex + + if Gem.configuration.really_verbose and \ + ex.kind_of?(Gem::Exception) and ex.source_exception + e = ex.source_exception + + ui.errs.puts "Because of: (#{e.class})\n #{e.to_s}" + ui.backtrace e + end + terminate_interaction(1) rescue Interrupt alert_error "Interrupted" terminate_interaction(1) end - def process_args(args) + def process_args(args, build_args=nil) args = args.to_str.split(/\s+/) if args.respond_to?(:to_str) - if args.size == 0 + + if args.empty? then say Gem::Command::HELP - terminate_interaction(1) + terminate_interaction 1 end - case args[0] - when '-h', '--help' + + case args.first + when '-h', '--help' then say Gem::Command::HELP - terminate_interaction(0) - when '-v', '--version' + terminate_interaction 0 + when '-v', '--version' then say Gem::VERSION - terminate_interaction(0) - when /^-/ - alert_error "Invalid option: #{args[0]}. See 'gem --help'." - terminate_interaction(1) + terminate_interaction 0 + when /^-/ then + alert_error "Invalid option: #{args.first}. See 'gem --help'." + terminate_interaction 1 else cmd_name = args.shift.downcase - cmd = find_command(cmd_name) - cmd.invoke(*args) + cmd = find_command cmd_name + cmd.invoke_with_build_args args, build_args end end def find_command(cmd_name) possibilities = find_command_possibilities cmd_name + if possibilities.size > 1 then - raise "Ambiguous command #{cmd_name} matches [#{possibilities.join(', ')}]" - elsif possibilities.size < 1 then - raise "Unknown command #{cmd_name}" + raise Gem::CommandLineError, + "Ambiguous command #{cmd_name} " \ + "matches [#{possibilities.join(', ')}]" + elsif possibilities.empty? then + raise Gem::CommandLineError, "Unknown command #{cmd_name}" end self[possibilities.first] @@ -162,7 +188,11 @@ class Gem::CommandManager def find_command_possibilities(cmd_name) len = cmd_name.length - command_names.select { |n| cmd_name == n[0, len] } + found = command_names.select { |name| cmd_name == name[0, len] } + + exact = found.find { |name| name == cmd_name } + + exact ? [exact] : found end private @@ -170,23 +200,20 @@ class Gem::CommandManager def load_and_instantiate(command_name) command_name = command_name.to_s const_name = command_name.capitalize.gsub(/_(.)/) { $1.upcase } << "Command" - commands = Gem::Commands - retried = false + load_error = nil begin - commands.const_get(const_name).new - rescue NameError - raise if retried - - retried = true begin require "rubygems/commands/#{command_name}_command" - rescue Exception => e - alert_error "Loading command: #{command_name} (#{e.class})\n #{e}" - ui.errs.puts "\t#{e.backtrace.join "\n\t"}" if - Gem.configuration.backtrace + rescue LoadError => e + load_error = e end - retry + 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 end end -- cgit v1.2.3