Class: Bibliothecary::Parsers::Cargo

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

Constant Summary collapse

PLATFORM_NAME =
'cargo'

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



19
20
21
22
# File 'lib/bibliothecary/parsers/cargo.rb', line 19

def self.analyse(folder_path, file_list)
  [analyse_cargo_toml(folder_path, file_list),
    analyse_cargo_lock(folder_path, file_list)]
end

.analyse_cargo_lock(folder_path, file_list) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bibliothecary/parsers/cargo.rb', line 47

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

  manifest = TOML.load_file(path)

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

.analyse_cargo_toml(folder_path, file_list) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bibliothecary/parsers/cargo.rb', line 24

def self.analyse_cargo_toml(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Cargo\.toml$/) }
  return unless path

  manifest = TOML.load_file(path)

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

.parse(filename, file_contents) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/bibliothecary/parsers/cargo.rb', line 8

def self.parse(filename, file_contents)
  toml = TOML.parse(file_contents)
  if filename.match(/^Cargo\.toml$/)
    parse_manifest(toml)
  elsif filename.match(/^Cargo\.lock$/)
    parse_lockfile(toml)
  else
    []
  end
end

.parse_lockfile(manifest) ⇒ Object



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

def self.parse_lockfile(manifest)
  manifest.fetch('package',[]).map do |dependency|
    {
      name: dependency['name'],
      requirement: dependency['version'],
      type: 'runtime'
    }
  end
end

.parse_manifest(manifest) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/bibliothecary/parsers/cargo.rb', line 37

def self.parse_manifest(manifest)
  manifest.fetch('dependencies', []).map do |name, requirement|
    {
      name: name,
      requirement: requirement,
      type: 'runtime'
    }
  end
end