Class: Bibliothecary::Parsers::Nuget

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

Constant Summary collapse

PLATFORM_NAME =
'nuget'

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/bibliothecary/parsers/nuget.rb', line 29

def self.analyse(folder_path, file_list)
  [analyse_project_json(folder_path, file_list),
  analyse_project_lock_json(folder_path, file_list),
  analyse_packages_config(folder_path, file_list),
  analyse_nuspec(folder_path, file_list),
  analyse_paket_lock(folder_path, file_list)].flatten
end

.analyse_nuspec(folder_path, file_list) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bibliothecary/parsers/nuget.rb', line 88

def self.analyse_nuspec(folder_path, file_list)
  paths = file_list.select{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^[A-Za-z0-9_-]+\.nuspec$/) }
  return unless paths.any?

  paths.map do |path|
    manifest = Ox.parse File.open(path).read

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

.analyse_packages_config(folder_path, file_list) ⇒ Object



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

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

  paths.map do |path|
    manifest = Ox.parse File.open(path).read

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

.analyse_paket_lock(folder_path, file_list) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bibliothecary/parsers/nuget.rb', line 105

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

  paths.map do |path|
    lines = File.readlines(path)
    {
      platform: PLATFORM_NAME,
      path: path,
      dependencies: parse_paket_lock(lines)
    }
  end
rescue
  []
end

.analyse_project_json(folder_path, file_list) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bibliothecary/parsers/nuget.rb', line 37

def self.analyse_project_json(folder_path, file_list)
  paths = file_list.select{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/Project\.json$/i) }
  return unless paths.any?

  paths.map do |path|
    manifest = JSON.parse File.open(path).read

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

.analyse_project_lock_json(folder_path, file_list) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bibliothecary/parsers/nuget.rb', line 54

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

  paths.map do |path|
    manifest = JSON.parse File.open(path).read

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

.parse(filename, file_contents) ⇒ Object



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

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

.parse_nuspec(manifest) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/bibliothecary/parsers/nuget.rb', line 152

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



142
143
144
145
146
147
148
149
150
# File 'lib/bibliothecary/parsers/nuget.rb', line 142

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



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/bibliothecary/parsers/nuget.rb', line 162

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



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

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



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

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