Class: Gem::Guardian::LockfileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gem/guardian/lockfile_parser.rb

Overview

Parses Gemfile.lock and exposes dependencies and checksum data.

Defined Under Namespace

Classes: LockfileData

Constant Summary collapse

GEM_LINE =

Matches dependency lines in the specs section.

/^ {4}([A-Za-z0-9_.-]+) \(([^)]+)\)/
CHECKSUM_LINE =

Matches checksum lines in the CHECKSUMS section.

/^ {2}([A-Za-z0-9_.-]+) \(([^)]+)\) (.+)$/

Instance Method Summary collapse

Constructor Details

#initialize(path = "Gemfile.lock") ⇒ LockfileParser

Returns a new instance of LockfileParser.



37
38
39
# File 'lib/gem/guardian/lockfile_parser.rb', line 37

def initialize(path = "Gemfile.lock")
  @path = path
end

Instance Method Details

#checksumsObject

Returns the raw checksum map extracted from the lockfile.



64
65
66
# File 'lib/gem/guardian/lockfile_parser.rb', line 64

def checksums
  parse.checksums
end

#dependenciesObject

Returns the dependencies listed in the lockfile.



59
60
61
# File 'lib/gem/guardian/lockfile_parser.rb', line 59

def dependencies
  parse.dependencies
end

#parseObject

Parses the lockfile into dependencies and checksum metadata.

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gem/guardian/lockfile_parser.rb', line 42

def parse
  raise LockfileError, "Lockfile not found: #{@path}" unless File.file?(@path)

  dependencies = []
  checksums = {}
  section = nil

  File.readlines(@path, chomp: true).each do |line|
    section = section_for(line, section)
    parse_specs_line(line, dependencies) if section == :specs
    parse_checksums_line(line, checksums) if section == :checksums
  end

  LockfileData.new(dependencies, checksums, checksums.any?)
end