Class: Bibliothecary::Parsers::Maven

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

Constant Summary collapse

PLATFORM_NAME =
'Maven'

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



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

def self.analyse(folder_path, file_list)
  [
    analyse_pom(folder_path, file_list),
    analyse_ivy(folder_path, file_list),
    analyse_gradle(folder_path, file_list),
  ].flatten
end

.analyse_gradle(folder_path, file_list) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bibliothecary/parsers/maven.rb', line 64

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

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

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

.analyse_ivy(folder_path, file_list) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bibliothecary/parsers/maven.rb', line 47

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

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

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

.analyse_pom(folder_path, file_list) ⇒ Object



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

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

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

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

.extract_pom_dep_info(xml, dependency, name) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/bibliothecary/parsers/maven.rb', line 124

def self.extract_pom_dep_info(xml, dependency, name)
  field = dependency.locate(name).first
  return nil if field.nil?
  value = field.nodes.first
  if match = value.match(/^\$\{(.+)\}/)
    prop_field = xml.properties.locate(match[1]).first
    if prop_field
      return prop_field.nodes.first
    else
      return value
    end
  else
    return value
  end
end

.parse(filename, file_contents) ⇒ Object



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

def self.parse(filename, file_contents)
  if filename.match(/ivy\.xml$/i)
    xml = Ox.parse file_contents
    parse_ivy_manifest(xml)
  elsif filename.match(/pom\.xml$/i)
    xml = Ox.parse file_contents
    parse_pom_manifest(xml)
  elsif filename.match(/build.gradle$/i)
    parse_gradle(file_contents)
  else
    []
  end
end

.parse_gradle(manifest) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bibliothecary/parsers/maven.rb', line 107

def self.parse_gradle(manifest)
  response = Typhoeus.post("https://gradle-parser.herokuapp.com/parse", body: manifest)
  json = JSON.parse(response.body)

  return [] unless json['dependencies'] && json['dependencies']['compile'] && json['dependencies']['compile'].is_a?(Array)
  json['dependencies']['compile'].map do |dependency|
    next unless dependency =~ (/[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+(\.[A-Za-z0-9_-])?\:[A-Za-z0-9_-]+\:/)
    version = dependency.split(':').last
    name = dependency.split(':')[0..-2].join(':')
    {
      name: name,
      version: version,
      type: 'runtime'
    }
  end.compact
end

.parse_ivy_manifest(manifest) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/bibliothecary/parsers/maven.rb', line 81

def self.parse_ivy_manifest(manifest)
  manifest.dependencies.locate('dependency').map do |dependency|
    attrs = dependency.attributes
    {
      name: "#{attrs[:org]}:#{attrs[:name]}",
      requirement: attrs[:rev],
      type: 'runtime'
    }
  end
end

.parse_pom_manifest(manifest) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bibliothecary/parsers/maven.rb', line 92

def self.parse_pom_manifest(manifest)
  if manifest.respond_to?('project')
    xml = manifest.project
  else
    xml = manifest
  end
  xml.dependencies.locate('dependency').map do |dependency|
    {
      name: "#{extract_pom_dep_info(xml, dependency, 'groupId')}:#{extract_pom_dep_info(xml, dependency, 'artifactId')}",
      requirement: extract_pom_dep_info(xml, dependency, 'version'),
      type: extract_pom_dep_info(xml, dependency, 'scope') || 'runtime'
    }
  end
end