1#--
2# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
3# All rights reserved.
4# See LICENSE.txt for permissions.
5#++
6
7require 'rubygems'
8require 'rubygems/command_manager'
9require 'rubygems/config_file'
10
11##
12# Load additional plugins from $LOAD_PATH
13
14Gem.load_env_plugins rescue nil
15
16##
17# Run an instance of the gem program.
18#
19# Gem::GemRunner is only intended for internal use by RubyGems itself.  It
20# does not form any public API and may change at any time for any reason.
21#
22# If you would like to duplicate functionality of `gem` commands, use the
23# classes they call directly.
24
25class Gem::GemRunner
26
27  def initialize(options={})
28    # TODO: nuke these options
29    @command_manager_class = options[:command_manager] || Gem::CommandManager
30    @config_file_class = options[:config_file] || Gem::ConfigFile
31  end
32
33  ##
34  # Run the gem command with the following arguments.
35
36  def run(args)
37    if args.include?('--')
38      # We need to preserve the original ARGV to use for passing gem options
39      # to source gems.  If there is a -- in the line, strip all options after
40      # it...its for the source building process.
41      # TODO use slice!
42      build_args = args[args.index("--") + 1...args.length]
43      args = args[0...args.index("--")]
44    end
45
46    do_configuration args
47    cmd = @command_manager_class.instance
48
49    cmd.command_names.each do |command_name|
50      config_args = Gem.configuration[command_name]
51      config_args = case config_args
52                    when String
53                      config_args.split ' '
54                    else
55                      Array(config_args)
56                    end
57      Gem::Command.add_specific_extra_args command_name, config_args
58    end
59
60    cmd.run Gem.configuration.args, build_args
61  end
62
63  private
64
65  def do_configuration(args)
66    Gem.configuration = @config_file_class.new(args)
67    Gem.use_paths Gem.configuration[:gemhome], Gem.configuration[:gempath]
68    Gem::Command.extra_args = Gem.configuration[:gem]
69  end
70
71end
72
73Gem.load_plugins
74