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



32
33
34
35
36
37
# File 'lib/bibliothecary/parsers/nuget.rb', line 32

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)]
end

.analyse_nuspec(folder_path, file_list) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bibliothecary/parsers/nuget.rb', line 78

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

  manifest = Ox.parse File.open(path).read

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

.analyse_packages_config(folder_path, file_list) ⇒ Object



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

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

  manifest = Ox.parse File.open(path).read

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

.analyse_project_json(folder_path, file_list) ⇒ Object



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

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

  manifest = JSON.parse File.open(path).read

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

.analyse_project_lock_json(folder_path, file_list) ⇒ Object



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

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

  manifest = JSON.parse File.open(path).read

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

.parse(filename, file_contents) ⇒ Object



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

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)
  else
    []
  end
end

.parse_nuspec(manifest) ⇒ Object



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

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



112
113
114
115
116
117
118
119
120
# File 'lib/bibliothecary/parsers/nuget.rb', line 112

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

.parse_project_json(manifest) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/bibliothecary/parsers/nuget.rb', line 91

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



101
102
103
104
105
106
107
108
109
110
# File 'lib/bibliothecary/parsers/nuget.rb', line 101

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