|
| 1 | +using System; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.Linq; |
| 4 | +using Java.Interop.Tools.Maven.Models; |
| 5 | + |
| 6 | +namespace Java.Interop.Tools.Maven.Extensions; |
| 7 | + |
| 8 | +static class MavenNetExtensions |
| 9 | +{ |
| 10 | + public static bool HasValue ([NotNullWhen (true)] this string? str) => !string.IsNullOrEmpty (str); |
| 11 | + |
| 12 | + public static string OrEmpty (this string? str) => str ?? string.Empty; |
| 13 | + |
| 14 | + public static string GetInheritedProperty (this ResolvedDependency dependency, ResolvedProject project, Func<ResolvedDependency, string?> property) |
| 15 | + { |
| 16 | + // Check our <dependencyManagement> section |
| 17 | + if (CheckDependencyManagementSection (project, dependency, property, out var result)) |
| 18 | + return result; |
| 19 | + |
| 20 | + // Check imported POMs |
| 21 | + foreach (var imported in project.ImportedPomProjects) { |
| 22 | + var value = GetInheritedProperty (dependency, imported, property); |
| 23 | + |
| 24 | + if (value.HasValue ()) |
| 25 | + return value; |
| 26 | + } |
| 27 | + |
| 28 | + // Check parent POM |
| 29 | + if (project.Parent is not null && !project.Parent.IsSuperPom) |
| 30 | + return GetInheritedProperty (dependency, project.Parent, property); |
| 31 | + |
| 32 | + return string.Empty; |
| 33 | + } |
| 34 | + |
| 35 | + static bool CheckImportedPoms (ResolvedDependency dependency, ResolvedProject project, Func<ResolvedDependency, string?> property, [NotNullWhen (true)] out string? result) |
| 36 | + { |
| 37 | + result = null; |
| 38 | + |
| 39 | + foreach (var imported in project.ImportedPomProjects) { |
| 40 | + var imported_dep = imported.Resolved.DependencyManagement?.Dependencies.FirstOrDefault (x => x.ArtifactId == dependency.ArtifactId && x.GroupId == dependency.GroupId); |
| 41 | + |
| 42 | + if (imported_dep != null) { |
| 43 | + result = property (new ResolvedDependency (imported, imported_dep, true)); |
| 44 | + |
| 45 | + if (result.HasValue ()) |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + // Recurse, as imported POMs can also import POMs |
| 50 | + if (CheckImportedPoms (dependency, imported, property, out result)) |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + static bool CheckDependencyManagementSection (ResolvedProject project, ResolvedDependency dependency, Func<ResolvedDependency, string?> property, [NotNullWhen (true)] out string? result) |
| 58 | + { |
| 59 | + result = null; |
| 60 | + |
| 61 | + // Check <dependencyManagement> |
| 62 | + var dep_man = project.Resolved.DependencyManagement?.Dependencies.FirstOrDefault (x => x.ArtifactId == dependency.ArtifactId && x.GroupId == dependency.GroupId); |
| 63 | + |
| 64 | + if (dep_man != null) { |
| 65 | + result = property (new ResolvedDependency (project, dep_man, true)) ?? string.Empty; |
| 66 | + return result.HasValue (); |
| 67 | + } |
| 68 | + |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + public static Artifact ToArtifact (this Dependency dependency) |
| 73 | + { |
| 74 | + return new Artifact (dependency.GroupId.OrEmpty (), dependency.ArtifactId.OrEmpty ().OrEmpty (), dependency.Version.OrEmpty ()); |
| 75 | + } |
| 76 | +} |
0 commit comments