Module: Bootprint::Collectors::Gems

Defined in:
lib/bootprint/collectors/gems.rb

Class Method Summary collapse

Class Method Details

.captureObject



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
# File 'lib/bootprint/collectors/gems.rb', line 13

def capture
  lockfile = File.file?("Gemfile.lock") ? "Gemfile.lock" : nil
  checksums = parse_checksums(lockfile)
  specs = resolved_specs
   = parse_lockfile(lockfile)

  {
    "lockfile_sha256" => lockfile && Digest::SHA256.file(lockfile).hexdigest,
    "platforms" => ["platforms"],
    "ruby_version" => ["ruby_version"],
    "bundled_with" => ["bundled_with"],
    "git_sources" => ["git_sources"],
    "path_sources" => ["path_sources"],
    "resolved" => specs.sort_by(&:name).to_h do |spec|
      [spec.name, {
        "version" => spec.version.to_s,
        "platform" => spec.platform.to_s,
        "checksum" => checksums["#{spec.name} (#{spec.version})"],
        "native_extensions" => !spec.extensions.empty?,
        "missing_extensions" => spec.respond_to?(:missing_extensions?) && spec.missing_extensions?,
        "prerelease" => spec.version.prerelease?
      }.compact]
    end
  }
end

.keyObject



11
# File 'lib/bootprint/collectors/gems.rb', line 11

def key = "gems"

.parse_checksums(lockfile) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bootprint/collectors/gems.rb', line 47

def parse_checksums(lockfile)
  return {} unless lockfile

  lines = File.readlines(lockfile, chomp: true)
  start = lines.index("CHECKSUMS")
  return {} unless start

  lines.drop(start + 1).take_while { |line| line.start_with?("  ") }.to_h do |line|
    name, checksum = line.strip.split(" sha256=", 2)
    [name, checksum]
  end
end

.parse_lockfile(lockfile) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bootprint/collectors/gems.rb', line 60

def parse_lockfile(lockfile)
  empty = { "platforms" => [], "git_sources" => [], "path_sources" => [] }
  return empty unless lockfile

  lines = File.readlines(lockfile, chomp: true)
  empty.merge(
    "platforms" => section_values(lines, "PLATFORMS"),
    "ruby_version" => single_section_value(lines, "RUBY VERSION")&.sub(/\Aruby\s+/, ""),
    "bundled_with" => single_section_value(lines, "BUNDLED WITH"),
    "git_sources" => source_values(lines, "GIT", "remote:"),
    "path_sources" => source_values(lines, "PATH", "remote:")
  )
end

.resolved_specsObject



39
40
41
42
43
44
45
# File 'lib/bootprint/collectors/gems.rb', line 39

def resolved_specs
  return Bundler.load.specs.to_a if defined?(Bundler) && Bundler.respond_to?(:load)

  Gem.loaded_specs.values
rescue StandardError
  Gem.loaded_specs.values
end

.section_values(lines, heading) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/bootprint/collectors/gems.rb', line 74

def section_values(lines, heading)
  start = lines.index(heading)
  return [] unless start

  section = lines.drop(start + 1).take_while { |line| line.empty? || line.start_with?("  ") }
  section.filter_map { |line| line.strip unless line.strip.empty? }
end

.single_section_value(lines, heading) ⇒ Object



82
83
84
# File 'lib/bootprint/collectors/gems.rb', line 82

def single_section_value(lines, heading)
  section_values(lines, heading).first
end

.source_values(lines, heading, prefix) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/bootprint/collectors/gems.rb', line 86

def source_values(lines, heading, prefix)
  indexes = lines.each_index.select { |index| lines[index] == heading }
  indexes.filter_map do |index|
    block = lines.drop(index + 1).take_while { |line| line.empty? || line.start_with?("  ") }
    remote = block.find { |line| line.strip.start_with?(prefix) }
    remote&.strip&.delete_prefix(prefix)&.strip
  end
end