Class: Bibliothecary::Parsers::Maven
- Inherits:
-
Object
- Object
- Bibliothecary::Parsers::Maven
- Includes:
- Analyser
- Defined in:
- lib/bibliothecary/parsers/maven.rb
Class Method Summary collapse
- .extract_pom_dep_info(xml, dependency, name) ⇒ Object
- .mapping ⇒ Object
- .parse_gradle(manifest) ⇒ Object
- .parse_ivy_manifest(file_contents) ⇒ Object
- .parse_pom_manifest(file_contents) ⇒ Object
Methods included from Analyser
Class Method Details
.extract_pom_dep_info(xml, dependency, name) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/bibliothecary/parsers/maven.rb', line 71 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 |
.mapping ⇒ 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.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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bibliothecary/parsers/maven.rb', line 54 def self.parse_gradle(manifest) response = Typhoeus.post("https://gradle-parser.libraries.io/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(file_contents) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# 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 end |
.parse_pom_manifest(file_contents) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bibliothecary/parsers/maven.rb', line 37 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 end |