aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rubygems/indexer/master_index_builder.rb
blob: dbe02370a911aff301d4d4a4a01ed3dc8e45cd2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'rubygems/indexer'

# Construct the master Gem index file.
class Gem::Indexer::MasterIndexBuilder < Gem::Indexer::AbstractIndexBuilder

  def start_index
    super
    @index = Gem::SourceIndex.new
  end

  def end_index
    super
    @file.puts "--- !ruby/object:#{@index.class}"
    @file.puts "gems:"

    gems = @index.sort_by { |name, gemspec| gemspec.sort_obj }
    gems.each do |name, gemspec|
      yaml = gemspec.to_yaml.gsub(/^/, '    ')
      yaml = yaml.sub(/\A    ---/, '') # there's a needed extra ' ' here
      @file.print "  #{gemspec.original_name}:"
      @file.puts yaml
    end
  end

  def cleanup
    super

    index_file_name = File.join @directory, @filename

    compress index_file_name, "Z"
    compressed_file_name = "#{index_file_name}.Z"

    paranoid index_file_name, compressed_file_name

    @files << compressed_file_name
  end

  def add(spec)
    @index.add_spec(spec)
  end

  private

  def paranoid(fn, compressed_fn)
    data = File.open(fn, 'rb') do |fp| fp.read end
    compressed_data = File.open(compressed_fn, 'rb') do |fp| fp.read end

    if data != unzip(compressed_data) then
      fail "Compressed file #{compressed_fn} does not match uncompressed file #{fn}"
    end
  end

end