Class: Bibliothecary::Parsers::Maven
- Inherits:
-
Object
- Object
- Bibliothecary::Parsers::Maven
- Includes:
- Analyser
- Defined in:
- lib/bibliothecary/parsers/maven.rb
Constant Summary collapse
- MAVEN_DOT_PROJECT_REGEXP =
Matches digraph contents from the Maven dependency tree .dot file format.
/digraph\s+"([^"]+)"\s+{/- MAVEN_DOT_RELATIONSHIP_REGEXP =
/"([^"]+)"\s+->\s+"([^"]+)"/- GRADLE_TYPE_REGEXP =
e.g. “annotationProcessor - Annotation processors and their dependencies for source set ‘main’.”
/^(\w+)/- GRADLE_DEP_REGEXP =
e.g. “| \— com.google.guava:guava:23.5-jre (*)”
/(\+---|\\---){1}/- GRADLE_PROJECT_REGEXP =
Dependencies that are on-disk projects, eg: e.g. “-– project :api:my-internal-project” e.g. “+— my-group:my-alias:1.2.3 -> project :client (*)”
/project :(\S+)?/- GRADLE_LINE_ENDING_REGEXP =
line ending legend: © means a dependency constraint, (n) means not resolved, or (*) means resolved previously, e.g. org.springframework.boot:spring-boot-starter-web:2.1.0.M3 (*) e.g. the “(n)” in “+— my-group:my-name:1.2.3 (n)”
/(\((c|n|\*)\))$/- GRADLE_DEPENDENCY_METHODS =
%w[api compile compileClasspath compileOnly compileOnlyApi implementation runtime runtimeClasspath runtimeOnly testCompile testCompileOnly testImplementation testRuntime testRuntimeOnly].freeze
- GRADLE_VERSION_REGEXP =
Intentionally overly-simplified regexes to scrape deps from build.gradle (Groovy) and build.gradle.kts (Kotlin) files. To be truly useful bibliothecary would need full Groovy / Kotlin parsers that speaks Gradle, because the Groovy and Kotlin DSLs have many dynamic ways of declaring dependencies.
/[\w.-]+/- GRADLE_VAR_INTERPOLATION_REGEXP =
e.g. ‘$myVersion’
/\$\w+/- GRADLE_CODE_INTERPOLATION_REGEXP =
e.g. ‘$"version"’
/\$\{.*\}/- GRADLE_GAV_REGEXP =
e.g. “group:artifactId:1.2.3”
/([\w.-]+):([\w.-]+)(?::(#{GRADLE_VERSION_REGEXP}|#{GRADLE_VAR_INTERPOLATION_REGEXP}|#{GRADLE_CODE_INTERPOLATION_REGEXP}))?/- GRADLE_GROOVY_SIMPLE_REGEXP =
/(#{GRADLE_DEPENDENCY_METHODS.join('|')})\s*\(?\s*['"]#{GRADLE_GAV_REGEXP}['"]/m- GRADLE_KOTLIN_SIMPLE_REGEXP =
/(#{GRADLE_DEPENDENCY_METHODS.join('|')})\s*\(\s*"#{GRADLE_GAV_REGEXP}"/m- MAVEN_PROPERTY_REGEXP =
/\$\{(.+?)\}/- MAX_DEPTH =
5- SBT_TYPE_REGEXP =
e.g. “[info] test:”
/^\[info\]\s+([-\w]+):$/- SBT_DEP_REGEXP =
e.g. “[info] org.typelevel:spire-util_2.12”
/^\[info\]\s+(.+)$/- SBT_VERSION_REGEXP =
e.g. “[info] - 1.7.5”
/^\[info\]\s+-\s+(.+)$/- SBT_FIELD_REGEXP =
e.g. “[info] homepage: www.slf4j.org”
/^\[info\]\s+([^:]+):\s+(.+)$/- SBT_IGNORE_REGEXP =
e.g. “[info] ”
/^\[info\]\s*$/- ANSI_MATCHER =
Copied from the “strings-ansi” gem, because it seems abandoned: github.com/piotrmurach/strings-ansi/pull/2 From: github.com/piotrmurach/strings-ansi/blob/35d0c9430cf0a8022dc12bdab005bce296cb9f00/lib/strings/ansi.rb#L14-L29 License: MIT The regex to match ANSI codes
%r{ (?>\033( \[[\[?>!]?\d*(;\d+)*[ ]?[a-zA-Z~@$^\]_\{\\] # graphics | \#?\d # cursor modes | [)(%+\-*/. ](\d|[a-zA-Z@=%]|) # character sets | O[p-xA-Z] # special keys | [a-zA-Z=><~\}|] # cursor movement | \]8;[^;]*;.*?(\033\\|\07) # hyperlink )) }x
Class Method Summary collapse
-
.extract_pom_dep_info(xml, dependency, name, parent_properties = {}) ⇒ Object
TODO: it might be worth renaming parent_properties to parent_elements so that more can be inherited from the parent pom than just <properties> here (see maven.apache.org/pom.html#inheritance).
- .extract_pom_info(xml, location, parent_properties = {}) ⇒ Object
- .extract_property(xml, property_name, value, parent_properties = {}, depth = 0) ⇒ Object
- .gradle_dependency_name(group, name) ⇒ Object
- .ivy_report?(file_contents) ⇒ Boolean
- .mapping ⇒ Object
- .parse_gradle(file_contents, options: {}) ⇒ Object
- .parse_gradle_kts(file_contents, options: {}) ⇒ Object
- .parse_gradle_resolved(file_contents, options: {}) ⇒ Object
- .parse_ivy_manifest(file_contents, options: {}) ⇒ Object
- .parse_ivy_report(file_contents, options: {}) ⇒ Object
- .parse_maven_resolved(file_contents, options: {}) ⇒ Object
- .parse_maven_tree(file_contents, options: {}) ⇒ Object
-
.parse_maven_tree_dependency(item) ⇒ Object
split “org.yaml:snakeyaml:jar:2.2:compile” into [“org.yaml:snakeyaml”, “2.2”, “compile”].
- .parse_maven_tree_dot(file_contents, options: {}) ⇒ Object
-
.parse_maven_tree_items_with_depths(file_contents) ⇒ Object
Return each item in the ascii art tree with a depth of that item, like [[0, “groupId:artifactId:jar:version:scope”], [1, “…”], …] The depth-0 items are the (sub)project names These are in the original order, with no de-duplication.
- .parse_pom_manifest(file_contents, parent_properties = {}, options: {}) ⇒ Object
-
.parse_pom_manifests(files, merged_properties, source = nil) ⇒ Object
pom.xml bodies.
- .parse_resolved_dep_line(line, options: {}) ⇒ Object
- .parse_sbt_deps(type, lines) ⇒ Object
- .parse_sbt_update_full(file_contents, options: {}) ⇒ Object
- .parse_sbt_version(type, name, version, lines) ⇒ Object
- .parse_sbt_versions(type, name, lines) ⇒ Object
- .parse_standalone_pom_manifest(file_contents, options: {}) ⇒ Object
- .property_value(xml, property_name, parent_properties) ⇒ Object
- .replace_value_with_prop(original_value, property_value, property_name) ⇒ Object
Methods included from Analyser
create_analysis, create_error_analysis, included
Class Method Details
.extract_pom_dep_info(xml, dependency, name, parent_properties = {}) ⇒ Object
TODO: it might be worth renaming parent_properties to parent_elements so that more can be inherited from the parent pom than just <properties> here (see maven.apache.org/pom.html#inheritance)
553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/bibliothecary/parsers/maven.rb', line 553 def self.extract_pom_dep_info(xml, dependency, name, parent_properties = {}) field = dependency.locate(name).first return nil if field.nil? value = field.nodes.first value = value.value if value.is_a?(Ox::CData) # whitespace in dependency tags should be ignored value = value&.strip match = value&.match(MAVEN_PROPERTY_REGEXP) return extract_property(xml, match[1], value, parent_properties) if match value end |
.extract_pom_info(xml, location, parent_properties = {}) ⇒ Object
546 547 548 |
# File 'lib/bibliothecary/parsers/maven.rb', line 546 def self.extract_pom_info(xml, location, parent_properties = {}) extract_pom_dep_info(xml, xml, location, parent_properties) end |
.extract_property(xml, property_name, value, parent_properties = {}, depth = 0) ⇒ Object
571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
# File 'lib/bibliothecary/parsers/maven.rb', line 571 def self.extract_property(xml, property_name, value, parent_properties = {}, depth = 0) prop_value = property_value(xml, property_name, parent_properties) return value unless prop_value # don't resolve more than 5 levels deep to avoid potential circular references resolved_value = replace_value_with_prop(value, prop_value, property_name) # check to see if we just resolved to another property name match = resolved_value.match(MAVEN_PROPERTY_REGEXP) return resolved_value unless match && depth < MAX_DEPTH depth += 1 extract_property(xml, match[1], resolved_value, parent_properties, depth) end |
.gradle_dependency_name(group, name) ⇒ Object
532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/bibliothecary/parsers/maven.rb', line 532 def self.gradle_dependency_name(group, name) if group.empty? && name.include?(":") group, name = name.split(":", 2) end # Strip comments, and single/doublequotes [group, name].map do |part| part .gsub(/\s*\/\/.*$/, "") # Comments .gsub(/^["']/, "") # Beginning single/doublequotes .gsub(/["']$/, "") # Ending single/doublequotes end.join(":") end |
.ivy_report?(file_contents) ⇒ Boolean
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/bibliothecary/parsers/maven.rb', line 155 def self.ivy_report?(file_contents) doc = Ox.parse file_contents root = doc&.locate("ivy-report")&.first !root.nil? rescue Exception # rubocop:disable Lint/RescueException # We rescue exception here since native libs can throw a non-StandardError # We don't want to throw errors during the matching phase, only during # parsing after we match. false end |
.mapping ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/bibliothecary/parsers/maven.rb', line 85 def self.mapping { match_filename("ivy.xml", case_insensitive: true) => { kind: "manifest", parser: :parse_ivy_manifest, }, match_filename("pom.xml", case_insensitive: true) => { kind: "manifest", parser: :parse_standalone_pom_manifest, }, match_filename("build.gradle", case_insensitive: true) => { kind: "manifest", parser: :parse_gradle, }, match_filename("build.gradle.kts", case_insensitive: true) => { kind: "manifest", parser: :parse_gradle_kts, }, match_extension(".xml", case_insensitive: true) => { content_matcher: :ivy_report?, kind: "lockfile", parser: :parse_ivy_report, }, match_filename("gradle-dependencies-q.txt", case_insensitive: true) => { kind: "lockfile", parser: :parse_gradle_resolved, }, match_filename("maven-resolved-dependencies.txt", case_insensitive: true) => { kind: "lockfile", parser: :parse_maven_resolved, }, match_filename("sbt-update-full.txt", case_insensitive: true) => { kind: "lockfile", parser: :parse_sbt_update_full, }, # maven-dependency-tree.txt is the output of `mvn dependency:tree` as a single command. # The tree lines contain "[INFO]" prefix and uses 2-space indentation. match_filename("maven-dependency-tree.txt", case_insensitive: true) => { kind: "lockfile", parser: :parse_maven_tree, }, # maven-dependency-tree.dot is the output of this command: # `mvn dependency:tree -DoutputType=dot -DoutputFile=maven-dependency-tree.dot` # It doesn't have the "[INFO]" prefix, and is in graphviz .dot format. match_filename("maven-dependency-tree.dot", case_insensitive: true) => { kind: "lockfile", parser: :parse_maven_tree_dot, }, } end |
.parse_gradle(file_contents, options: {}) ⇒ Object
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/bibliothecary/parsers/maven.rb', line 500 def self.parse_gradle(file_contents, options: {}) dependencies = file_contents .scan(GRADLE_GROOVY_SIMPLE_REGEXP) # match 'implementation "group:artifactId:version"' .reject { |(_type, group, artifact_id, _version)| group.nil? || artifact_id.nil? } # remove any matches with missing group/artifactId .map do |(type, group, artifact_id, version)| Dependency.new( name: [group, artifact_id].join(":"), requirement: version, type: type, source: .fetch(:filename, nil), platform: platform_name ) end ParserResult.new(dependencies: dependencies) end |
.parse_gradle_kts(file_contents, options: {}) ⇒ Object
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/bibliothecary/parsers/maven.rb', line 516 def self.parse_gradle_kts(file_contents, options: {}) dependencies = file_contents .scan(GRADLE_KOTLIN_SIMPLE_REGEXP) # match 'implementation("group:artifactId:version")' .reject { |(_type, group, artifact_id, _version)| group.nil? || artifact_id.nil? } # remove any matches with missing group/artifactId .map do |(type, group, artifact_id, version)| Dependency.new( name: [group, artifact_id].join(":"), requirement: version, type: type, source: .fetch(:filename, nil), platform: platform_name ) end ParserResult.new(dependencies: dependencies) end |
.parse_gradle_resolved(file_contents, options: {}) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/bibliothecary/parsers/maven.rb', line 196 def self.parse_gradle_resolved(file_contents, options: {}) current_type = nil dependencies = file_contents.split("\n").map do |line| current_type_match = GRADLE_TYPE_REGEXP.match(line) current_type = current_type_match.captures[0] if current_type_match gradle_dep_match = GRADLE_DEP_REGEXP.match(line) next unless gradle_dep_match split = gradle_dep_match.captures[0] # gradle can import on-disk projects and deps will be listed under them, e.g. `+--- project :test:integration`, # so we treat these projects as "internal" deps with requirement of "1.0.0" if (project_match = line.match(GRADLE_PROJECT_REGEXP)) # an empty project name is self-referential (i.e. a cycle), and we don't need to track the manifest's project itself, e.g. "+--- project :" next if project_match[1].nil? # project names can have colons (e.g. for gradle projects in subfolders), which breaks maven artifact naming assumptions, so just replace them with hyphens. project_name = project_match[1].gsub(":", "-") line = line.sub(GRADLE_PROJECT_REGEXP, "internal:#{project_name}:1.0.0") end dep = line .split(split)[1] .sub(GRADLE_LINE_ENDING_REGEXP, "") .sub(/ FAILED$/, "") # dependency could not be resolved (but still may have a version) .sub(" -> ", ":") # handle version arrow syntax .strip .split(":") # A testImplementation line can look like this so just skip those # \--- org.springframework.security:spring-security-test (n) next unless dep.length >= 3 if dep.count == 6 # get name from renamed package resolution "org:name:version -> renamed_org:name:version" Dependency.new( original_name: dep[0, 2].join(":"), original_requirement: dep[2], name: dep[-3..-2].join(":"), requirement: dep[-1], type: current_type, source: .fetch(:filename, nil), platform: platform_name ) elsif dep.count == 5 # get name from renamed package resolution "org:name -> renamed_org:name:version" Dependency.new( original_name: dep[0, 2].join(":"), original_requirement: "*", name: dep[-3..-2].join(":"), requirement: dep[-1], type: current_type, source: .fetch(:filename, nil), platform: platform_name ) else # get name from version conflict resolution ("org:name:version -> version") and no-resolution ("org:name:version") Dependency.new( name: dep[0..1].join(":"), requirement: dep[-1], type: current_type, source: .fetch(:filename, nil), platform: platform_name ) end end .compact .uniq { |item| [item.name, item.requirement, item.type, item.original_name, item.original_requirement] } ParserResult.new(dependencies: dependencies) end |
.parse_ivy_manifest(file_contents, options: {}) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/bibliothecary/parsers/maven.rb', line 140 def self.parse_ivy_manifest(file_contents, options: {}) manifest = Ox.parse file_contents dependencies = manifest.dependencies.locate("dependency").map do |dependency| attrs = dependency.attributes Dependency.new( name: "#{attrs[:org]}:#{attrs[:name]}", requirement: attrs[:rev], type: "runtime", source: .fetch(:filename, nil), platform: platform_name ) end ParserResult.new(dependencies: dependencies) end |
.parse_ivy_report(file_contents, options: {}) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/bibliothecary/parsers/maven.rb', line 166 def self.parse_ivy_report(file_contents, options: {}) doc = Ox.parse file_contents root = doc.locate("ivy-report").first raise "ivy-report document does not have ivy-report at the root" if root.nil? info = doc.locate("ivy-report/info").first raise "ivy-report document lacks <info> element" if info.nil? type = info.attributes[:conf] type = "unknown" if type.nil? modules = doc.locate("ivy-report/dependencies/module") dependencies = modules.map do |mod| attrs = mod.attributes org = attrs[:organisation] name = attrs[:name] version = mod.locate("revision").first&.attributes&.[](:name) next nil if org.nil? || name.nil? || version.nil? Dependency.new( name: "#{org}:#{name}", requirement: version, type: type, source: .fetch(:filename, nil), platform: platform_name ) end.compact ParserResult.new(dependencies: dependencies) end |
.parse_maven_resolved(file_contents, options: {}) ⇒ Object
269 270 271 272 273 274 275 276 277 |
# File 'lib/bibliothecary/parsers/maven.rb', line 269 def self.parse_maven_resolved(file_contents, options: {}) dependencies = file_contents .gsub(ANSI_MATCHER, "") .split("\n") .map { |line| parse_resolved_dep_line(line, options: ) } .compact .uniq ParserResult.new(dependencies: dependencies) end |
.parse_maven_tree(file_contents, options: {}) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/bibliothecary/parsers/maven.rb', line 321 def self.parse_maven_tree(file_contents, options: {}) keep_subprojects = .fetch(:keep_subprojects_in_maven_tree, false) items = parse_maven_tree_items_with_depths(file_contents) raise "found no lines with deps in maven-dependency-tree.txt" if items.empty? projects_to_exclude = {} (root_name, root_version, _root_type) = parse_maven_tree_dependency(items[0][1]) # traditional behavior: we only exclude the root project, and only if we parsed multiple lines if keep_subprojects && !items.empty? items.shift projects_to_exclude[root_name] = Set.new projects_to_exclude[root_name].add(root_version) end unique_items = items.map do |(depth, item)| # new behavior: we exclude root and subprojects (depth 0 items) (name, version, type) = parse_maven_tree_dependency(item) if depth == 0 && !keep_subprojects # record and then remove the depth 0 projects_to_exclude[name] ||= Set.new projects_to_exclude[name].add(version) nil else [name, version, type] end end.compact.uniq dependencies = unique_items # drop the projects and subprojects .reject { |(name, version, _type)| projects_to_exclude[name]&.include?(version) } .map do |(name, version, type)| Bibliothecary::Dependency.new( name: name, requirement: version, type: type, source: .fetch(:filename, nil), platform: platform_name ) end ParserResult.new( project_name: root_name, dependencies: dependencies ) end |
.parse_maven_tree_dependency(item) ⇒ Object
split “org.yaml:snakeyaml:jar:2.2:compile” into
- “org.yaml:snakeyaml”, “2.2”, “compile”
306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/bibliothecary/parsers/maven.rb', line 306 def self.parse_maven_tree_dependency(item) parts = item.split(":") case parts.count when 4 version = parts[-1] type = parts[-2] when 5..6 version, type = parts[-2..] end name = parts[0..1].join(":") [name, version, type] end |
.parse_maven_tree_dot(file_contents, options: {}) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/bibliothecary/parsers/maven.rb', line 371 def self.parse_maven_tree_dot(file_contents, options: {}) # Project could be either the root project or a sub-module. project = file_contents.match(MAVEN_DOT_PROJECT_REGEXP)[1] project_name, _project_version, _project_type = parse_maven_tree_dependency(project) relationships = file_contents.scan(MAVEN_DOT_RELATIONSHIP_REGEXP) direct_names_to_versions = relationships.each.with_object({}) do |(parent, child), obj| next unless parent == project name, version, _type = parse_maven_tree_dependency(child) obj[name] ||= Set.new obj[name].add(version) end deps = relationships.map do |(_parent, child)| child_name, child_version, child_type = parse_maven_tree_dependency(child) Dependency.new( name: child_name, requirement: child_version, platform: platform_name, type: child_type, direct: direct_names_to_versions[child_name]&.include?(child_version) || false, source: .fetch(:filename, nil) ) end.uniq # TODO: should we change every method to return this, or just allow both to be # returned to an analysis? (I'm leaning towards former) ParserResult.new( dependencies: deps, project_name: project_name ) end |
.parse_maven_tree_items_with_depths(file_contents) ⇒ Object
Return each item in the ascii art tree with a depth of that item, like [[0, “groupId:artifactId:jar:version:scope”], [1, “…”], …] The depth-0 items are the (sub)project names These are in the original order, with no de-duplication.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/bibliothecary/parsers/maven.rb', line 283 def self.parse_maven_tree_items_with_depths(file_contents) file_contents .gsub(ANSI_MATCHER, "") .encode(universal_newline: true) # capture two groups; one is the ASCII art telling us the tree depth, # and two is the actual dependency .scan(/^\[INFO\]\s((?:[-+|\\]|\s)*)((?:[\w.-]+:)+[\w.\-${}]+)/) # lines that start with "-" aren't part of the tree, example: "[INFO] --- dependency:3.8.1:tree" .reject { |(tree_ascii_art, _dep_info)| tree_ascii_art.start_with?("-") } .map do |(tree_ascii_art, dep_info)| child_marker_index = tree_ascii_art.index(/(\+-)|(\\-)/) depth = if child_marker_index.nil? 0 else # There are three characters present in the line for each level of depth (child_marker_index / 3) + 1 end [depth, dep_info] end end |
.parse_pom_manifest(file_contents, parent_properties = {}, options: {}) ⇒ Object
429 430 431 |
# File 'lib/bibliothecary/parsers/maven.rb', line 429 def self.parse_pom_manifest(file_contents, parent_properties = {}, options: {}) parse_pom_manifests([file_contents], parent_properties, .fetch(:filename, nil)) end |
.parse_pom_manifests(files, merged_properties, source = nil) ⇒ Object
pom.xml bodies. The first element should be the child file.
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/bibliothecary/parsers/maven.rb', line 436 def self.parse_pom_manifests(files, merged_properties, source = nil) documents = files.map do |file| doc = Ox.parse(file) doc.respond_to?("project") ? doc.project : doc end merged_dependency_managements = {} documents.each do |document| document.locate("dependencyManagement/dependencies/dependency").each do |dep| group_id = extract_pom_dep_info(document, dep, "groupId", merged_properties) artifact_id = extract_pom_dep_info(document, dep, "artifactId", merged_properties) key = "#{group_id}:#{artifact_id}" merged_dependency_managements[key] ||= { groupId: group_id, artifactId: artifact_id, version: extract_pom_dep_info(document, dep, "version", merged_properties), scope: extract_pom_dep_info(document, dep, "scope", merged_properties), } end end dep_hashes = {} documents.each do |document| document.locate("dependencies/dependency").each do |dep| group_id = extract_pom_dep_info(document, dep, "groupId", merged_properties) artifact_id = extract_pom_dep_info(document, dep, "artifactId", merged_properties) key = "#{group_id}:#{artifact_id}" unless dep_hashes.key?(key) dep_hashes[key] = { name: key, platform: platform_name, requirement: nil, type: nil, optional: nil, } end dep_hash = dep_hashes[key] dep_hash[:requirement] ||= extract_pom_dep_info(document, dep, "version", merged_properties) dep_hash[:type] ||= extract_pom_dep_info(document, dep, "scope", merged_properties) # optional field is, itself, optional, and will be either "true" or "false" optional = extract_pom_dep_info(document, dep, "optional", merged_properties) if dep_hash[:optional].nil? && !optional.nil? dep_hash[:optional] = optional == "true" end end end # Anything that wasn't covered by a dependency version, get from the # dependencyManagements dep_hashes.each do |key, dep_hash| if (dependency_management = merged_dependency_managements[key]) dep_hash[:requirement] ||= dependency_management[:version] dep_hash[:type] ||= dependency_management[:scope] end dep_hash[:source] = source end dep_hashes.map { |_key, dep_hash| Dependency.new(**dep_hash) } end |
.parse_resolved_dep_line(line, options: {}) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/bibliothecary/parsers/maven.rb', line 406 def self.parse_resolved_dep_line(line, options: {}) # filter out anything that doesn't look like a # resolved dep line return unless line[/ .*:[^-]+-- /] dep_parts = line.strip.split(":") return unless dep_parts.length == 5 # org.springframework.boot:spring-boot-starter-web:jar:2.0.3.RELEASE:compile[36m -- module spring.boot.starter.web[0;1m [auto][m Dependency.new( name: dep_parts[0, 2].join(":"), requirement: dep_parts[3], type: dep_parts[4].split("--").first.strip, source: .fetch(:filename, nil), platform: platform_name ) end |
.parse_sbt_deps(type, lines) ⇒ Object
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
# File 'lib/bibliothecary/parsers/maven.rb', line 655 def self.parse_sbt_deps(type, lines) deps = [] while lines.any? && !SBT_TYPE_REGEXP.match(lines[0]) line = lines.shift next if SBT_IGNORE_REGEXP.match(line) dep_match = SBT_DEP_REGEXP.match(line) if dep_match versions = parse_sbt_versions(type, dep_match.captures[0], lines) deps.concat(versions) else lines.unshift(line) break end end deps end |
.parse_sbt_update_full(file_contents, options: {}) ⇒ Object
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
# File 'lib/bibliothecary/parsers/maven.rb', line 611 def self.parse_sbt_update_full(file_contents, options: {}) all_deps = [] lines = file_contents.split("\n") while lines.any? line = lines.shift type_match = SBT_TYPE_REGEXP.match(line) next unless type_match type = type_match.captures[0] deps = parse_sbt_deps(type, lines) all_deps.concat(deps) end # strip out evicted dependencies all_deps.reject! do |dep| dep[:fields]["evicted"] == "true" end # in the future, we could use "callers" in the fields to # decide which deps are direct root deps and which are # pulled in by another dep. The direct deps have the sbt # project name as a caller. # clean out any duplicates (I'm pretty sure sbt will have done this for # us so this is paranoia, basically) squished = all_deps.compact.uniq { |item| [item[:name], item[:requirement], item[:type]] } # get rid of the fields squished.each do |dep| dep.delete(:fields) end dependencies = squished.map do |dep_kvs| Dependency.new( **dep_kvs, source: .fetch(:filename, nil), platform: platform_name ) end ParserResult.new(dependencies: dependencies) end |
.parse_sbt_version(type, name, version, lines) ⇒ Object
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
# File 'lib/bibliothecary/parsers/maven.rb', line 692 def self.parse_sbt_version(type, name, version, lines) fields = {} while lines.any? && !SBT_TYPE_REGEXP.match(lines[0]) line = lines.shift field_match = SBT_FIELD_REGEXP.match(line) if field_match fields[field_match.captures[0]] = field_match.captures[1] else lines.unshift(line) break end end { name: name, requirement: version, type: type, # we post-process using some of these fields and then delete them again fields: fields, } end |
.parse_sbt_versions(type, name, lines) ⇒ Object
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 |
# File 'lib/bibliothecary/parsers/maven.rb', line 675 def self.parse_sbt_versions(type, name, lines) versions = [] while lines.any? && !SBT_TYPE_REGEXP.match(lines[0]) line = lines.shift version_match = SBT_VERSION_REGEXP.match(line) if version_match versions.push(parse_sbt_version(type, name, version_match.captures[0], lines)) else lines.unshift(line) break end end versions end |
.parse_standalone_pom_manifest(file_contents, options: {}) ⇒ Object
424 425 426 427 |
# File 'lib/bibliothecary/parsers/maven.rb', line 424 def self.parse_standalone_pom_manifest(file_contents, options: {}) dependencies = parse_pom_manifest(file_contents, {}, options:) ParserResult.new(dependencies: dependencies) end |
.property_value(xml, property_name, parent_properties) ⇒ Object
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/bibliothecary/parsers/maven.rb', line 586 def self.property_value(xml, property_name, parent_properties) # the xml root is <project> so lookup the non property name in the xml # this converts ${project/group.id} -> ${group/id} non_prop_name = property_name.gsub(".", "/").gsub("project/", "") prop_field = xml.properties.locate(property_name).first if xml.respond_to?("properties") parent_prop = parent_properties[property_name] || # e.g. "${foo}" parent_properties[property_name.sub(/^project\./, "")] || # e.g. "${project.foo}" parent_properties[property_name.sub(/^project\.parent\./, "")] # e.g. "${project.parent.foo}" if prop_field prop_field.nodes.first elsif parent_prop parent_prop elsif xml.locate(non_prop_name).first # see if the value to look up is a field under the project # examples are ${project.groupId} or ${project.version} xml.locate(non_prop_name).first.nodes.first elsif xml.locate("parent/#{non_prop_name}").first # see if the value to look up is a field under the project parent # examples are ${project.groupId} or ${project.version} xml.locate("parent/#{non_prop_name}").first.nodes.first end end |
.replace_value_with_prop(original_value, property_value, property_name) ⇒ Object
567 568 569 |
# File 'lib/bibliothecary/parsers/maven.rb', line 567 def self.replace_value_with_prop(original_value, property_value, property_name) original_value.gsub("${#{property_name}}", property_value) end |