Module: GemContribute::LockfileParser

Defined in:
lib/gem_contribute/lockfile_parser.rb

Overview

Wraps Bundler::LockfileParser. See ADR-0002.

Input: a path to a Gemfile.lock. Output: an Array of LockedGem.

Class Method Summary collapse

Class Method Details

.build_locked_gem(spec) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/gem_contribute/lockfile_parser.rb', line 30

def build_locked_gem(spec)
  LockedGem.new(
    name: spec.name,
    version: spec.version.to_s,
    source_type: classify_source(spec.source),
    source_uri: source_uri(spec.source)
  )
end

.classify_source(source) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/gem_contribute/lockfile_parser.rb', line 39

def classify_source(source)
  case source
  when Bundler::Source::Rubygems then :rubygems
  when Bundler::Source::Git then :git
  when Bundler::Source::Path then :path
  else :unknown
  end
end

.parse(path) ⇒ Array<LockedGem>

Parameters:

  • path (String, Pathname)

    path to a Gemfile.lock

Returns:



15
16
17
18
19
20
21
22
# File 'lib/gem_contribute/lockfile_parser.rb', line 15

def parse(path)
  contents = read_lockfile(path)
  parser = Bundler::LockfileParser.new(contents)

  parser.specs.map { |spec| build_locked_gem(spec) }
rescue Bundler::LockfileError => e
  raise LockfileParseError, "could not parse #{path}: #{e.message}"
end

.read_lockfile(path) ⇒ Object



24
25
26
27
28
# File 'lib/gem_contribute/lockfile_parser.rb', line 24

def read_lockfile(path)
  File.read(path)
rescue Errno::ENOENT
  raise LockfileNotFound, "no Gemfile.lock at #{path}"
end

.source_uri(source) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gem_contribute/lockfile_parser.rb', line 48

def source_uri(source)
  case source
  when Bundler::Source::Rubygems
    # Bundler::Source::Rubygems can have multiple remotes; pick the first.
    # In practice this is rubygems.org for almost every gem.
    source.remotes.first&.to_s
  when Bundler::Source::Git
    source.uri
  when Bundler::Source::Path
    source.path.to_s
  end
end