summaryrefslogtreecommitdiff
path: root/lib/bundler/compact_index_client/parser.rb
blob: a227bc2cfd85543b3a461e768b783a03bc9b9c0b (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

module Bundler
  class CompactIndexClient
    class Parser
      # `compact_index` - an object responding to #names, #versions, #info(name, checksum),
      #                   returning the file contents as a string
      def initialize(compact_index)
        @compact_index = compact_index
        @info_checksums = nil
        @versions_by_name = nil
        @available = nil
      end

      def names
        lines(@compact_index.names)
      end

      def versions
        @versions_by_name ||= Hash.new {|hash, key| hash[key] = [] }
        @info_checksums = {}

        lines(@compact_index.versions).each do |line|
          name, versions_string, checksum = line.split(" ", 3)
          @info_checksums[name] = checksum || ""
          versions_string.split(",") do |version|
            delete = version.delete_prefix!("-")
            version = version.split("-", 2).unshift(name)
            if delete
              @versions_by_name[name].delete(version)
            else
              @versions_by_name[name] << version
            end
          end
        end

        @versions_by_name
      end

      def info(name)
        data = @compact_index.info(name, info_checksums[name])
        lines(data).map {|line| gem_parser.parse(line).unshift(name) }
      end

      def available?
        return @available unless @available.nil?
        @available = !info_checksums.empty?
      end

      private

      def info_checksums
        @info_checksums ||= lines(@compact_index.versions).each_with_object({}) do |line, checksums|
          parse_version_checksum(line, checksums)
        end
      end

      def lines(data)
        return [] if data.nil? || data.empty?
        lines = data.split("\n")
        header = lines.index("---")
        header ? lines[header + 1..-1] : lines
      end

      def gem_parser
        @gem_parser ||= GemParser.new
      end

      # This is mostly the same as `split(" ", 3)` but it avoids allocating extra objects.
      # This method gets called at least once for every gem when parsing versions.
      def parse_version_checksum(line, checksums)
        line.freeze # allows slicing into the string to not allocate a copy of the line
        name_end = line.index(" ")
        checksum_start = line.index(" ", name_end + 1) + 1
        checksum_end = line.size - checksum_start
        # freeze name since it is used as a hash key
        # pre-freezing means a frozen copy isn't created
        name = line[0, name_end].freeze
        checksum = line[checksum_start, checksum_end]
        checksums[name] = checksum
      end
    end
  end
end