1require 'rubygems/command'
2require 'rubygems/local_remote_options'
3require 'rubygems/version_option'
4
5class Gem::Commands::FetchCommand < Gem::Command
6
7  include Gem::LocalRemoteOptions
8  include Gem::VersionOption
9
10  def initialize
11    super 'fetch', 'Download a gem and place it in the current directory'
12
13    add_bulk_threshold_option
14    add_proxy_option
15    add_source_option
16    add_clear_sources_option
17
18    add_version_option
19    add_platform_option
20    add_prerelease_option
21  end
22
23  def arguments # :nodoc:
24    'GEMNAME       name of gem to download'
25  end
26
27  def defaults_str # :nodoc:
28    "--version '#{Gem::Requirement.default}'"
29  end
30
31  def usage # :nodoc:
32    "#{program_name} GEMNAME [GEMNAME ...]"
33  end
34
35  def execute
36    version = options[:version] || Gem::Requirement.default
37
38    platform  = Gem.platforms.last
39    gem_names = get_all_gem_names
40
41    gem_names.each do |gem_name|
42      dep = Gem::Dependency.new gem_name, version
43      dep.prerelease = options[:prerelease]
44
45      specs_and_sources, errors = Gem::SpecFetcher.fetcher.spec_for_dependency dep
46
47      if platform then
48        filtered = specs_and_sources.select { |s,| s.platform == platform }
49        specs_and_sources = filtered unless filtered.empty?
50      end
51
52      spec, source = specs_and_sources.max_by { |s,| s.version }
53
54      if spec.nil? then
55        show_lookup_failure gem_name, version, errors, options[:domain]
56        next
57      end
58
59      source.download spec
60
61      say "Downloaded #{spec.full_name}"
62    end
63  end
64
65end
66
67