| Class | Gem::CommandManager |
| In: |
lib/rubygems/command_manager.rb
|
| Parent: | Object |
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'
class Gem::Commands::EditCommand < Gem::Command
# ...
end
Gem::CommandManager.instance.register_command :edit
See Gem::Command for instructions on writing gem commands.
Register all the subcommands supported by the gem command.
# File lib/rubygems/command_manager.rb, line 44
44: def initialize
45: @commands = {}
46: register_command :build
47: register_command :cert
48: register_command :check
49: register_command :cleanup
50: register_command :contents
51: register_command :dependency
52: register_command :environment
53: register_command :fetch
54: register_command :generate_index
55: register_command :help
56: register_command :install
57: register_command :list
58: register_command :lock
59: register_command :mirror
60: register_command :outdated
61: register_command :pristine
62: register_command :query
63: register_command :rdoc
64: register_command :search
65: register_command :server
66: register_command :sources
67: register_command :specification
68: register_command :stale
69: register_command :uninstall
70: register_command :unpack
71: register_command :update
72: register_command :which
73: end
Return the registered command from the command name.
# File lib/rubygems/command_manager.rb, line 85
85: def [](command_name)
86: command_name = command_name.intern
87: return nil if @commands[command_name].nil?
88: @commands[command_name] ||= load_and_instantiate(command_name)
89: end
Return a sorted list of all command names (as strings).
# File lib/rubygems/command_manager.rb, line 94
94: def command_names
95: @commands.keys.collect {|key| key.to_s}.sort
96: end
# File lib/rubygems/command_manager.rb, line 136
136: def find_command(cmd_name)
137: possibilities = find_command_possibilities cmd_name
138: if possibilities.size > 1 then
139: raise "Ambiguous command #{cmd_name} matches [#{possibilities.join(', ')}]"
140: elsif possibilities.size < 1 then
141: raise "Unknown command #{cmd_name}"
142: end
143:
144: self[possibilities.first]
145: end
# File lib/rubygems/command_manager.rb, line 147
147: def find_command_possibilities(cmd_name)
148: len = cmd_name.length
149:
150: command_names.select { |n| cmd_name == n[0, len] }
151: end
# File lib/rubygems/command_manager.rb, line 113
113: def process_args(args)
114: args = args.to_str.split(/\s+/) if args.respond_to?(:to_str)
115: if args.size == 0
116: say Gem::Command::HELP
117: terminate_interaction(1)
118: end
119: case args[0]
120: when '-h', '--help'
121: say Gem::Command::HELP
122: terminate_interaction(0)
123: when '-v', '--version'
124: say Gem::RubyGemsVersion
125: terminate_interaction(0)
126: when /^-/
127: alert_error "Invalid option: #{args[0]}. See 'gem --help'."
128: terminate_interaction(1)
129: else
130: cmd_name = args.shift.downcase
131: cmd = find_command(cmd_name)
132: cmd.invoke(*args)
133: end
134: end
Register the command object.
# File lib/rubygems/command_manager.rb, line 78
78: def register_command(command_obj)
79: @commands[command_obj] = false
80: end
Run the config specified by args.
# File lib/rubygems/command_manager.rb, line 101
101: def run(args)
102: process_args(args)
103: rescue StandardError, Timeout::Error => ex
104: alert_error "While executing gem ... (#{ex.class})\n #{ex.to_s}"
105: ui.errs.puts "\t#{ex.backtrace.join "\n\t"}" if
106: Gem.configuration.backtrace
107: terminate_interaction(1)
108: rescue Interrupt
109: alert_error "Interrupted"
110: terminate_interaction(1)
111: end