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



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

def self.extract_pom_dep_info(xml, dependency, name)
  field = dependency.locate(name).first
  return nil if field.nil?
  value = field.nodes.first
  match = value.match(/^\$\{(.+)\}/)
  if 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

.mappingObject



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.mapping
  {
    /ivy\.xml$/i => {
      kind: 'manifest',
      parser: :parse_ivy_manifest
    },
    /pom\.xml$/i => {
      kind: 'manifest',
      parser: :parse_pom_manifest
    },
    /build.gradle$/i => {
      kind: 'manifest',
      parser: :parse_gradle
    }
  }
end

.parse_gradle(manifest) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bibliothecary/parsers/maven.rb', line 58

def self.parse_gradle(manifest)
  response = Typhoeus.post("#{Bibliothecary.configuration.gradle_parser_host}/parse", body: manifest)
  json = JSON.parse(response.body)
  return [] unless json['dependencies']
  json['dependencies'].map do |dependency|
    name = [dependency["group"], dependency["name"]].join(':')
    next unless name =~ (/[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+(\.[A-Za-z0-9_-])?\:[A-Za-z0-9_-]/)
    {
      name: name,
      requirement: dependency["version"],
      type: dependency["type"]
    }
  end.compact
end

.parse_ivy_manifest(file_contents) ⇒ Object



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

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

.parse_pom_manifest(file_contents) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bibliothecary/parsers/maven.rb', line 39

def self.parse_pom_manifest(file_contents)
  manifest = Ox.parse file_contents
  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
rescue
  []
end