Class: Gem::Guardian::LockfileParser

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of LockfileParser.



8
9
10
# File 'lib/gem/guardian/lockfile_parser.rb', line 8

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

Instance Method Details

#dependenciesObject

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gem/guardian/lockfile_parser.rb', line 12

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

  specs_section = false
  File.readlines(@path, chomp: true).filter_map do |line|
    specs_section = true if line == "  specs:"
    specs_section = false if specs_section && line.match?(/^[A-Z]/)
    next unless specs_section

    match = GEM_LINE.match(line)
    next unless match

    name = match[1]
    version_and_platform = match[2]
    version, platform = split_version_and_platform(version_and_platform)
    Dependency.new(name:, version:, platform:)
  end
end