1require 'rubygems/command'
2require 'rubygems/indexer'
3
4##
5# Generates a index files for use as a gem server.
6#
7# See `gem help generate_index`
8
9class Gem::Commands::GenerateIndexCommand < Gem::Command
10
11  def initialize
12    super 'generate_index',
13          'Generates the index files for a gem server directory',
14          :directory => '.', :build_modern => true
15
16    add_option '-d', '--directory=DIRNAME',
17               'repository base dir containing gems subdir' do |dir, options|
18      options[:directory] = File.expand_path dir
19    end
20
21    add_option '--[no-]modern',
22               'Generate indexes for RubyGems',
23               '(always true)' do |value, options|
24      options[:build_modern] = value
25    end
26
27    add_option '--update',
28               'Update modern indexes with gems added',
29               'since the last update' do |value, options|
30      options[:update] = value
31    end
32  end
33
34  def defaults_str # :nodoc:
35    "--directory . --modern"
36  end
37
38  def description # :nodoc:
39    <<-EOF
40The generate_index command creates a set of indexes for serving gems
41statically.  The command expects a 'gems' directory under the path given to
42the --directory option.  The given directory will be the directory you serve
43as the gem repository.
44
45For `gem generate_index --directory /path/to/repo`, expose /path/to/repo via
46your HTTP server configuration (not /path/to/repo/gems).
47
48When done, it will generate a set of files like this:
49
50  gems/*.gem                                   # .gem files you want to
51                                               # index
52
53  specs.<version>.gz                           # specs index
54  latest_specs.<version>.gz                    # latest specs index
55  prerelease_specs.<version>.gz                # prerelease specs index
56  quick/Marshal.<version>/<gemname>.gemspec.rz # Marshal quick index file
57
58The .rz extension files are compressed with the inflate algorithm.
59The Marshal version number comes from ruby's Marshal::MAJOR_VERSION and
60Marshal::MINOR_VERSION constants.  It is used to ensure compatibility.
61    EOF
62  end
63
64  def execute
65    # This is always true becasue it's the only way now.
66    options[:build_modern] = true
67
68    if not File.exist?(options[:directory]) or
69       not File.directory?(options[:directory]) then
70      alert_error "unknown directory name #{directory}."
71      terminate_interaction 1
72    else
73      indexer = Gem::Indexer.new options.delete(:directory), options
74
75      if options[:update] then
76        indexer.update_index
77      else
78        indexer.generate_index
79      end
80    end
81  end
82
83end
84
85