Class: Bibliothecary::Parsers::Cargo

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

Class Method Summary collapse

Methods included from Analyser

included

Class Method Details

.analyse_cargo_lock(folder_path, file_list) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bibliothecary/parsers/cargo.rb', line 32

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

  paths.map do |path|
    manifest = TOML.load_file(path)

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

.parse(filename, path) ⇒ Object



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

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

.parse_lockfile(manifest) ⇒ Object



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

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



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

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