Class: Bibliothecary::Parsers::Rubygems

Inherits:
Object
  • Object
show all
Includes:
Analyser
Defined in:
lib/bibliothecary/parsers/rubygems.rb

Constant Summary collapse

NAME_VERSION =
'(?! )(.*?)(?: \(([^-]*)(?:-(.*))?\))?'.freeze
NAME_VERSION_4 =
/^ {4}#{NAME_VERSION}$/

Class Method Summary collapse

Methods included from Analyser

included

Class Method Details

.parse(filename, path) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bibliothecary/parsers/rubygems.rb', line 11

def self.parse(filename, path)
  if filename.match(/^Gemfile$|^gems\.rb$/)
    file_contents = File.open(path).read
    manifest = Gemnasium::Parser.send(:gemfile, file_contents)
    parse_manifest(manifest)
  elsif filename.match(/[A-Za-z0-9_-]+\.gemspec$/)
    file_contents = File.open(path).read
    manifest = Gemnasium::Parser.send(:gemspec, file_contents)
    parse_manifest(manifest)
  elsif filename.match(/^Gemfile\.lock$|^gems\.locked$/)
    file_contents = File.open(path).read
    parse_gemfile_lock(file_contents)
  else
    []
  end
end

.parse_gemfile_lock(manifest) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bibliothecary/parsers/rubygems.rb', line 28

def self.parse_gemfile_lock(manifest)
  manifest.split("\n").map do |line|
    match = line.match(NAME_VERSION_4)
    next unless match
    name = match[1]
    version = match[2].gsub(/\(|\)/,'')
    {
      name: name,
      requirement: version,
      type: 'runtime'
    }
  end.compact
end

.parse_manifest(manifest) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/bibliothecary/parsers/rubygems.rb', line 42

def self.parse_manifest(manifest)
  manifest.dependencies.inject([]) do |deps, dep|
    deps.push({
      name: dep.name,
      requirement: dep.requirement.to_s,
      type: dep.type
    })
  end.uniq
end