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



20
21
22
23
# File 'lib/bibliothecary/parsers/maven.rb', line 20

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

.analyse_ivy(folder_path, file_list) ⇒ Object



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

def self.analyse_ivy(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^ivy.xml$/i) }
  return unless path
  p path
  manifest = Ox.parse File.open(path).read

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

.analyse_pom(folder_path, file_list) ⇒ Object



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

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

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

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

.extract_pom_dep_info(manifest, dependency, name) ⇒ Object



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

def self.extract_pom_dep_info(manifest, dependency, name)
  field = dependency.locate(name).first
  return nil if field.nil?
  value = field.nodes.first
  if match = value.match(/^\$\{(.+)\}/)
    prop_field = manifest.project.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
# 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)
  else
    []
  end
end

.parse_ivy_manifest(manifest) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/bibliothecary/parsers/maven.rb', line 51

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



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

def self.parse_pom_manifest(manifest)
  manifest.project.dependencies.locate('dependency').map do |dependency|

    {
      name: "#{extract_pom_dep_info(manifest, dependency, 'groupId')}:#{extract_pom_dep_info(manifest, dependency, 'artifactId')}",
      requirement: extract_pom_dep_info(manifest, dependency, 'version'),
      type: extract_pom_dep_info(manifest, dependency, 'scope') || 'runtime'
    }
  end
end