Class: Bibliothecary::Parsers::Nuget

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

Class Method Summary collapse

Methods included from Analyser

included

Class Method Details

.parse(filename, path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bibliothecary/parsers/nuget.rb', line 9

def self.parse(filename, path)
  if filename.match(/Project\.json$/)
    file_contents = File.open(path).read
    json = JSON.parse file_contents
    parse_project_json(json)
  elsif filename.match(/Project\.lock\.json$/)
    file_contents = File.open(path).read
    json = JSON.parse file_contents
    parse_project_lock_json(json)
  elsif filename.match(/packages\.config$/)
    file_contents = File.open(path).read
    xml = Ox.parse file_contents
    parse_packages_config(xml)
  elsif filename.match(/^[A-Za-z0-9_-]+\.nuspec$/)
    file_contents = File.open(path).read
    xml = Ox.parse file_contents
    parse_nuspec(xml)
  elsif filename.match(/paket\.lock$/)
    file_contents = File.open(path).read
    parse_paket_lock(file_contents.split("\n"))
  else
    []
  end
end

.parse_nuspec(manifest) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/bibliothecary/parsers/nuget.rb', line 65

def self.parse_nuspec(manifest)
  manifest.package..dependencies.locate('dependency').map do |dependency|
    {
      name: dependency.id,
      version: dependency.attributes[:version] || '*',
      type: 'runtime'
    }
  end
end

.parse_packages_config(manifest) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/bibliothecary/parsers/nuget.rb', line 55

def self.parse_packages_config(manifest)
  manifest.packages.locate('package').map do |dependency|
    {
      name: dependency.id,
      version: dependency.version,
      type: 'runtime'
    }
  end
end

.parse_paket_lock(lines) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bibliothecary/parsers/nuget.rb', line 75

def self.parse_paket_lock(lines)
  package_version_re = /\s+(?<name>\S+)\s\((?<version>\d+\.\d+[\.\d+[\.\d+]*]*)\)/
  packages = lines.select { |line| package_version_re.match(line) }.map { |line| package_version_re.match(line) }.map do |match|
    {
      name: match[:name].strip,
      version: match[:version],
      type: 'runtime'
    }
  end
  # we only have to enforce uniqueness by name because paket ensures that there is only the single version globally in the project
  packages.uniq {|package| package[:name] }
end

.parse_project_json(manifest) ⇒ Object



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

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

.parse_project_lock_json(manifest) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/bibliothecary/parsers/nuget.rb', line 44

def self.parse_project_lock_json(manifest)
  manifest.fetch('libraries',[]).map do |name, requirement|
    dep = name.split('/')
    {
      name: dep[0],
      requirement: dep[1],
      type: 'runtime'
    }
  end
end