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

create_analysis, create_error_analysis, included

Class Method Details

.mappingObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bibliothecary/parsers/nuget.rb', line 9

def self.mapping
  {
    match_filename("Project.json") => {
      kind: 'manifest',
      parser: :parse_json_runtime_manifest
    },
    match_filename("Project.lock.json") => {
      kind: 'lockfile',
      parser: :parse_project_lock_json
    },
    match_filename("packages.lock.json") => {
      kind: 'lockfile',
      parser: :parse_packages_lock_json
    },
    match_filename("packages.config") => {
      kind: 'manifest',
      parser: :parse_packages_config
    },
    match_extension(".nuspec") => {
      kind: 'manifest',
      parser: :parse_nuspec
    },
    match_extension(".csproj") => {
      kind: 'manifest',
      parser: :parse_csproj
    },
    match_filename("paket.lock") => {
      kind: 'lockfile',
      parser: :parse_paket_lock
    },
    match_filename("project.assets.json") => {
      kind: 'lockfile',
      parser: :parse_project_assets_json
    },
  }
end

.parse_csproj(file_contents) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bibliothecary/parsers/nuget.rb', line 98

def self.parse_csproj(file_contents)
  manifest = Ox.parse file_contents

  packages = manifest.locate('ItemGroup/PackageReference').map do |dependency|
    requirement = (dependency.Version if dependency.respond_to? "Version") || "*"
    if requirement.is_a?(Ox::Element)
      requirement = dependency.nodes.detect{ |n| n.value == "Version" }&.text
    end

    {
      name: dependency.Include,
      requirement: requirement,
      type: 'runtime'
    }
  end
  packages.uniq {|package| package[:name] }
rescue
  []
end

.parse_nuspec(file_contents) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/bibliothecary/parsers/nuget.rb', line 118

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

.parse_packages_config(file_contents) ⇒ Object



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

def self.parse_packages_config(file_contents)
  manifest = Ox.parse file_contents
  manifest.packages.locate('package').map do |dependency|
    {
      name: dependency.id,
      requirement: (dependency.version if dependency.respond_to? "version") || "*",
      type: 'runtime'
    }
  end
rescue
  []
end

.parse_packages_lock_json(file_contents) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bibliothecary/parsers/nuget.rb', line 58

def self.parse_packages_lock_json(file_contents)
  manifest = JSON.parse file_contents

  frameworks = {}
  manifest.fetch('dependencies',[]).each do |framework, deps|
    frameworks[framework] = deps.map do |name, details|
      {
        name: name,
        # 'resolved' has been set in all examples so far
        # so fallback to requested is pure paranoia
        requirement: details.fetch('resolved', details.fetch('requested', '*')),
        type: 'runtime'
      }
    end
  end

  if frameworks.size > 0
    # we should really return multiple manifests, but bibliothecary doesn't
    # do that yet so at least pick deterministically.

    # Note, frameworks can be empty, so remove empty ones and then return the last sorted item if any
    frameworks = frameworks.delete_if { |k, v| v.empty? }
    return frameworks[frameworks.keys.sort.last] unless frameworks.empty?
  end
  []
end

.parse_paket_lock(file_contents) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bibliothecary/parsers/nuget.rb', line 131

def self.parse_paket_lock(file_contents)
  lines = file_contents.split("\n")
  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,
      requirement: 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_assets_json(file_contents) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/bibliothecary/parsers/nuget.rb', line 145

def self.parse_project_assets_json(file_contents)
  manifest = JSON.parse file_contents

  frameworks = {}
  manifest.fetch("targets",[]).each do |framework, deps|
    frameworks[framework] = deps
                              .select { |name, details| details["type"] == "package" }
                              .map do |name, details|
      name_split = name.split("/")
      {
        name: name_split[0],
        requirement: name_split[1],
        type: "runtime"
      }
    end
  end

  if frameworks.size > 0
    # we should really return multiple manifests, but bibliothecary doesn't
    # do that yet so at least pick deterministically.

    # Note, frameworks can be empty, so remove empty ones and then return the last sorted item if any
    frameworks = frameworks.delete_if { |k, v| v.empty? }
    return frameworks[frameworks.keys.sort.last] unless frameworks.empty?
  end
  []
end

.parse_project_lock_json(file_contents) ⇒ Object



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

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