Class: Licensure::DependencyResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/licensure/dependency_resolver.rb

Overview

Parses Gemfile.lock and resolves dependency names/versions.

Instance Method Summary collapse

Constructor Details

#initialize(lockfile_path: "Gemfile.lock", recursive: false) ⇒ DependencyResolver

Returns a new instance of DependencyResolver.

Parameters:

  • lockfile_path (String) (defaults to: "Gemfile.lock")
  • recursive (Boolean) (defaults to: false)


10
11
12
13
# File 'lib/licensure/dependency_resolver.rb', line 10

def initialize(lockfile_path: "Gemfile.lock", recursive: false)
  @lockfile_path = lockfile_path
  @recursive = recursive
end

Instance Method Details

#resolve ⇒ Array<Hash{Symbol => String}>

Returns:

  • (Array<Hash{Symbol => String}>)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/licensure/dependency_resolver.rb', line 16

def resolve
  raise DependencyResolutionError, "Gemfile.lock not found: #{@lockfile_path}" unless File.exist?(@lockfile_path)

  parser = Bundler::LockfileParser.new(File.read(@lockfile_path))
  specs_by_name = parser.specs.to_h { |spec| [spec.name, spec] }

  names = if @recursive
    parser.specs.map(&:name)
  else
    parser.dependencies.keys
  end

  names.uniq
       .reject { |name| name == "bundler" }
       .filter_map do |name|
         spec = specs_by_name[name]
         next unless spec

         { name: name, version: spec.version.to_s }
       end
       .sort_by { |dependency| dependency[:name] }
rescue Bundler::LockfileError => e
  raise DependencyResolutionError, "Failed to parse #{@lockfile_path}: #{e.message}"
end