Class: Bibliothecary::Parsers::Maven

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

Class Method Summary collapse

Methods included from Analyser

included

Class Method Details

.extract_pom_dep_info(xml, dependency, name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bibliothecary/parsers/maven.rb', line 69

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, path) ⇒ Object



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

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

.parse_gradle(manifest) ⇒ Object



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

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



25
26
27
28
29
30
31
32
33
34
# File 'lib/bibliothecary/parsers/maven.rb', line 25

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



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

def self.parse_pom_manifest(manifest)
  if manifest.respond_to?('project')
    xml = manifest.project
  else
    xml = manifest
  end
  return [] unless xml.respond_to?('dependencies')
  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