Class: Bibliothecary::Parsers::Rubygems

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

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/bibliothecary/parsers/rubygems.rb', line 24

def self.analyse(folder_path, file_list)
  [
    analyse_gemfile(folder_path, file_list),
    analyse_gemspec(folder_path, file_list),
    analyse_gemfile_lock(folder_path, file_list)
  ]
end

.analyse_gemfile(folder_path, file_list) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bibliothecary/parsers/rubygems.rb', line 32

def self.analyse_gemfile(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Gemfile$|^gems\.rb$/) }
  return unless path

  manifest = Gemnasium::Parser.send(:gemfile, File.open(path).read)

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_manifest(manifest)
  }
end

.analyse_gemfile_lock(folder_path, file_list) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bibliothecary/parsers/rubygems.rb', line 58

def self.analyse_gemfile_lock(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Gemfile\.lock$|^gems\.locked$/) }
  return unless path

  manifest = File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_gemfile_lock(manifest)
  }
end

.analyse_gemspec(folder_path, file_list) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bibliothecary/parsers/rubygems.rb', line 45

def self.analyse_gemspec(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^[A-Za-z0-9_-]+\.gemspec$/) }
  return unless path

  manifest = Gemnasium::Parser.send(:gemspec, File.open(path).read)

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_manifest(manifest)
  }
end

.parse(filename, file_contents) ⇒ Object



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

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

.parse_gemfile_lock(manifest) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bibliothecary/parsers/rubygems.rb', line 71

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



85
86
87
88
89
90
91
92
93
# File 'lib/bibliothecary/parsers/rubygems.rb', line 85

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