Class: GemGuard::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_guard/parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(lockfile_path) ⇒ Object



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
# File 'lib/gem_guard/parser.rb', line 5

def parse(lockfile_path)
  content = File.read(lockfile_path)
  begin
    lockfile = Bundler::LockfileParser.new(content)
  rescue => e
    # Wrap any parsing errors from Bundler with a clearer custom error
    raise GemGuard::InvalidLockfileError, "Invalid Gemfile.lock at #{lockfile_path}: #{e.message}"
  end

  # Basic structural validation to catch truncated files quickly
  unless content.include?("\nBUNDLED WITH") || content.end_with?("BUNDLED WITH\n")
    raise GemGuard::InvalidLockfileError, "Invalid Gemfile.lock at #{lockfile_path}: missing 'BUNDLED WITH' section"
  end

  dependencies = []

  lockfile.specs.each do |spec|
    dependencies << Dependency.new(
      name: spec.name,
      version: spec.version.to_s,
      source: extract_source(spec),
      dependencies: spec.dependencies.map(&:name)
    )
  end

  # Validate DEPENDENCIES section formatting and presence in specs
  validate_dependencies_section!(content, dependencies.map(&:name), lockfile_path)

  # Deduplicate dependencies by name to handle platform-specific gems
  # (e.g., nokogiri-arm64-darwin, nokogiri-x86_64-darwin, etc.)
  dependencies.uniq { |dep| dep.name }
end