Skip to content

Commit

Permalink
Merge pull request #1369 from xamarin/parent-pom
Browse files Browse the repository at this point in the history
[binderator] Add support for dependency versions declared in parent POMs.
  • Loading branch information
moljac authored May 9, 2022
2 parents 2c42b7f + 346b3fe commit 043b855
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
68 changes: 55 additions & 13 deletions Util/Xamarin.AndroidBinderator/Xamarin.AndroidBinderator/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,11 @@ static List<BindingProjectModel> BuildProjectModels(BindingConfig config, Dictio
// Gather maven dependencies to try and map out nuget dependencies
foreach (var mavenDep in mavenProject.Dependencies)
{
FixDependency(config, mavenArtifact, mavenDep, mavenProject);

if (!ShouldIncludeDependency(config, mavenArtifact, mavenDep, exceptions))
continue;

mavenDep.Version = FixVersion(mavenDep.Version, mavenProject);

mavenDep.GroupId = mavenDep.GroupId.Replace ("${project.groupId}", mavenProject.GroupId);
mavenDep.Version = mavenDep.Version?.Replace ("${project.version}", mavenProject.Version);

Expand Down Expand Up @@ -485,14 +485,40 @@ static Dictionary<string, string> MergeValues(Dictionary<string, string> dest, D
return dest;
}

// VersionRange.Parse cannot handle single number versions that we sometimes see in Maven, like "1".
// Fix them to be "1.0".
// https://github.com/NuGet/Home/issues/10342
static string FixVersion(string version, Project project)
static void FixDependency(BindingConfig config, MavenArtifactConfig mavenArtifact, Dependency dependency, Project project)
{
// Handle Parent POM
if ((string.IsNullOrEmpty(dependency.Version) || string.IsNullOrEmpty(dependency.Scope)) && project.Parent != null) {
var parent_pom = GetParentPom(config, mavenArtifact, project.Parent);
var parent_dependency = parent_pom.FindParentDependency(dependency);

// Try to fish a version out of the parent POM
if (string.IsNullOrEmpty(dependency.Version))
dependency.Version = ReplaceVersionProperties(parent_pom, parent_dependency?.Version);

// Try to fish a scope out of the parent POM
if (string.IsNullOrEmpty(dependency.Scope))
dependency.Scope = parent_dependency?.Scope;
}

var version = dependency.Version;

if (string.IsNullOrWhiteSpace(version))
return version;
return;

version = ReplaceVersionProperties(project, version);

// VersionRange.Parse cannot handle single number versions that we sometimes see in Maven, like "1".
// Fix them to be "1.0".
// https://github.com/NuGet/Home/issues/10342
if (!version.Contains("."))
version += ".0";

dependency.Version = version;
}

static string ReplaceVersionProperties(Project project, string version)
{
// Handle versions with Properties, like:
// <properties>
// <java.version>1.8</java.version>
Expand All @@ -505,15 +531,31 @@ static string FixVersion(string version, Project project)
// <version>${gson.version}</version>
// </dependency>
// </dependencies>
if (project?.Properties != null) {
foreach (var prop in project.Properties.Any)
version = version.Replace($"${{{prop.Name.LocalName}}}", prop.Value);
}
if (string.IsNullOrWhiteSpace(version) || project?.Properties == null)
return version;

if (!version.Contains("."))
version += ".0";
foreach (var prop in project.Properties.Any)
version = version.Replace ($"${{{prop.Name.LocalName}}}", prop.Value);

return version;
}

static Dictionary<string, Project> parent_poms = new Dictionary<string, Project>();

static Project GetParentPom(BindingConfig config, MavenArtifactConfig mavenArtifact, Parent parent)
{
var key = $"{parent.GroupId}.{parent.ArtifactId}-{parent.Version}";

if (parent_poms.TryGetValue(key, out var cached_pom))
return cached_pom;

var maven = MavenFactory.GetMavenRepository(config, mavenArtifact);
maven.Populate(parent.GroupId, parent.ArtifactId).Wait();
var pom = maven.GetProjectAsync(parent.GroupId, parent.ArtifactId, parent.Version).Result;

parent_poms.Add(key, pom);

return pom;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MavenNet.Models;

Expand All @@ -14,5 +15,11 @@ public static class Extensions
public static bool IsCompileDependency (this Dependency dependency) => string.IsNullOrWhiteSpace (dependency.Scope) || dependency.Scope.ToLowerInvariant ().Equals ("compile");

public static bool IsRuntimeDependency (this Dependency dependency) => dependency?.Scope != null && dependency.Scope.ToLowerInvariant ().Equals ("runtime");

public static Dependency FindParentDependency (this Project project, Dependency dependency)
{
return project.DependencyManagement?.Dependencies?.FirstOrDefault (
d => d.GroupAndArtifactId () == dependency.GroupAndArtifactId () && d.Classifier != "sources");
}
}
}

0 comments on commit 043b855

Please sign in to comment.