Skip to content

Commit

Permalink
Address review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpobst committed Feb 22, 2024
1 parent f938d56 commit 6896260
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

namespace Java.Interop.Tools.Maven;

public class DefaultPomResolver : IPomResolver
public class DefaultProjectResolver : IProjectResolver
{
readonly Dictionary<string, Project> poms = new ();

public void Register (Project project)
{
poms.Add (project.ToString (), project);
poms.Add (project.VersionedArtifactString, project);
}

public virtual Project ResolveRawProject (Artifact artifact)
public virtual Project Resolve (Artifact artifact)
{
if (poms.TryGetValue (artifact.ToString (), out var project))
if (poms.TryGetValue (artifact.VersionedArtifactString, out var project))
return project;

throw new InvalidOperationException ($"No POM registered for {artifact}");
Expand Down
72 changes: 0 additions & 72 deletions src/Java.Interop.Tools.Maven/Extensions/MavenNetExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Java.Interop.Tools.Maven;

public interface IPomResolver
public interface IProjectResolver
{
Project ResolveRawProject (Artifact artifact);
Project Resolve (Artifact artifact);
}
8 changes: 6 additions & 2 deletions src/Java.Interop.Tools.Maven/Models/Artifact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public class Artifact

public string Version { get; }

public string ArtifactString => $"{GroupId}:{Id}";

// Format should match Project.ArtifactString for comparisons.
public string VersionedArtifactString => $"{GroupId}:{Id}:{Version}";

public Artifact (string groupId, string artifactId, string version)
{
Id = artifactId;
Expand Down Expand Up @@ -40,6 +45,5 @@ public static bool TryParse (string value, [NotNullWhen (true)]out Artifact? art
return true;
}

// This is a "well-known" format we use, it should not be changed.
public override string ToString () => $"{GroupId}:{Id}:{Version}";
public override string ToString () => VersionedArtifactString;
}
9 changes: 9 additions & 0 deletions src/Java.Interop.Tools.Maven/Models/Dependency.Partial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Java.Interop.Tools.Maven.Extensions;

namespace Java.Interop.Tools.Maven.Models;

public partial class Dependency
{
public Artifact ToArtifact ()
=> new Artifact (GroupId.OrEmpty (), ArtifactId.OrEmpty (), Version.OrEmpty ());
}
37 changes: 15 additions & 22 deletions src/Java.Interop.Tools.Maven/Models/Project.Partial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,22 @@ namespace Java.Interop.Tools.Maven.Models;

public partial class Project
{
public static Project Parse (Stream stream)
{
Project? result = null;

var serializer = new XmlSerializer (typeof (Project));

using (var sr = new StreamReader (stream))
result = (Project) serializer.Deserialize (new XmlTextReader (sr) {
Namespaces = false,
});

return result;
}
static readonly XmlSerializer xml_serializer = new (typeof (Project));

public static Project ParseXml (string xml)
public static Project Load (Stream stream)
{
Project? result = null;
using (var xr = new XmlTextReader (stream) { Namespaces = false })
return Load (xr);
}

var serializer = new XmlSerializer (typeof (Project));
public static Project Load (XmlReader reader)
=> (Project) xml_serializer.Deserialize (reader);

public static Project Parse (string xml)
{
using (var sr = new StringReader (xml))
result = (Project) serializer.Deserialize (new XmlTextReader (sr) {
Namespaces = false,
});

return result;
using (var xr = new XmlTextReader (sr) { Namespaces = false })
return Load (xr);
}

public bool TryGetParentPomArtifact ([NotNullWhen (true)] out Artifact? parent)
Expand All @@ -48,7 +38,7 @@ public bool TryGetParentPomArtifact ([NotNullWhen (true)] out Artifact? parent)
return false;
}

public override string ToString () => $"{GroupId}:{ArtifactId}:{Version}";
public override string ToString () => VersionedArtifactString;

public string ToXml ()
{
Expand All @@ -59,4 +49,7 @@ public string ToXml ()
return sw.ToString ();
}
}

// Format should match Artifact.VersionedArtifactString for comparisons.
public string VersionedArtifactString => $"{GroupId}:{ArtifactId}:{Version}";
}
77 changes: 67 additions & 10 deletions src/Java.Interop.Tools.Maven/Models/ResolvedDependency.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Java.Interop.Tools.Maven.Extensions;

namespace Java.Interop.Tools.Maven.Models;
Expand All @@ -13,6 +16,9 @@ public class ResolvedDependency
public string? Type { get; }
public string Version { get; }

public string ArtifactString => $"{GroupId}:{ArtifactId}";
public string VersionedArtifactString => $"{GroupId}:{ArtifactId}:{Version}";

public ResolvedDependency (ResolvedProject project, Dependency dependency)
: this (project, dependency, false)
{ }
Expand All @@ -33,19 +39,19 @@ internal ResolvedDependency (ResolvedProject project, Dependency dependency, boo
// If we're not shallow, fill in any still missing properties with parent values
if (!shallow) {
if (!Classifier.HasValue ())
Classifier = this.GetInheritedProperty (project, d => d.Classifier);
Classifier = GetInheritedProperty (this, project, d => d.Classifier);

if (!Optional.HasValue ())
Optional = this.GetInheritedProperty (project, d => d.Optional);
Optional = GetInheritedProperty (this, project, d => d.Optional);

if (!Scope.HasValue ())
Scope = this.GetInheritedProperty (project, d => d.Scope);
Scope = GetInheritedProperty (this, project, d => d.Scope);

if (!Type.HasValue ())
Type = this.GetInheritedProperty (project, d => d.Type);
Type = GetInheritedProperty (this, project, d => d.Type);

if (!Version.HasValue ())
Version = this.GetInheritedProperty (project, d => d.Version);
Version = GetInheritedProperty (this, project, d => d.Version);
}

// Default scope to "compile" if not specified
Expand All @@ -57,12 +63,63 @@ internal ResolvedDependency (ResolvedProject project, Dependency dependency, boo
Optional = "false";
}

public string ToArtifactString (bool includeVersion = true)
public override string ToString () => $"{VersionedArtifactString} - {Scope}";

static string GetInheritedProperty (ResolvedDependency dependency, ResolvedProject project, Func<ResolvedDependency, string?> property)
{
return includeVersion
? $"{GroupId}:{ArtifactId}:{Version}"
: $"{GroupId}:{ArtifactId}";
// Check our <dependencyManagement> section
if (CheckDependencyManagementSection (project, dependency, property, out var result))
return result;

// Check imported POMs
foreach (var imported in project.ImportedPomProjects) {
var value = GetInheritedProperty (dependency, imported, property);

if (value.HasValue ())
return value;
}

// Check parent POM
if (project.Parent is not null && !project.Parent.IsSuperPom)
return GetInheritedProperty (dependency, project.Parent, property);

return string.Empty;
}

static bool CheckImportedPoms (ResolvedDependency dependency, ResolvedProject project, Func<ResolvedDependency, string?> property, [NotNullWhen (true)] out string? result)
{
result = null;

foreach (var imported in project.ImportedPomProjects) {
var imported_dep = imported.Resolved.DependencyManagement?.Dependencies.FirstOrDefault (x => x.ArtifactId == dependency.ArtifactId && x.GroupId == dependency.GroupId);

if (imported_dep != null) {
result = property (new ResolvedDependency (imported, imported_dep, true));

if (result.HasValue ())
return true;
}

// Recurse, as imported POMs can also import POMs
if (CheckImportedPoms (dependency, imported, property, out result))
return true;
}

return false;
}

public override string ToString () => $"{GroupId}:{ArtifactId}:{Version} - {Scope}";
static bool CheckDependencyManagementSection (ResolvedProject project, ResolvedDependency dependency, Func<ResolvedDependency, string?> property, [NotNullWhen (true)] out string? result)
{
result = null;

// Check <dependencyManagement>
var dep_man = project.Resolved.DependencyManagement?.Dependencies.FirstOrDefault (x => x.ArtifactId == dependency.ArtifactId && x.GroupId == dependency.GroupId);

if (dep_man != null) {
result = property (new ResolvedDependency (project, dep_man, true)) ?? string.Empty;
return result.HasValue ();
}

return false;
}
}
12 changes: 6 additions & 6 deletions src/Java.Interop.Tools.Maven/Models/ResolvedProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Java.Interop.Tools.Maven.Models;
public class ResolvedProject
{
readonly ResolvedProject? parent;
readonly IPomResolver? resolver;
readonly IProjectResolver? resolver;

Project? resolved_project;

Expand All @@ -34,7 +34,7 @@ public ResolvedProject Parent {
}
}

public ResolvedProject (Project project, ResolvedProject parent, IPomResolver resolver)
public ResolvedProject (Project project, ResolvedProject parent, IProjectResolver resolver)
{
Raw = project;
this.parent = parent;
Expand All @@ -43,17 +43,17 @@ public ResolvedProject (Project project, ResolvedProject parent, IPomResolver re

public void Resolve () => ResolveCore (new PropertyStack ());

public static ResolvedProject FromArtifact (Artifact artifact, IPomResolver resolver)
public static ResolvedProject FromArtifact (Artifact artifact, IProjectResolver resolver)
{
var project = FromArtifactCore (artifact, resolver);
project.Resolve ();

return project;
}

static ResolvedProject FromArtifactCore (Artifact artifact, IPomResolver resolver)
static ResolvedProject FromArtifactCore (Artifact artifact, IProjectResolver resolver)
{
var raw = resolver.ResolveRawProject (artifact);
var raw = resolver.Resolve (artifact);

// POM has a parent, resolve it
if (raw.TryGetParentPomArtifact (out var parentArtifact)) {
Expand All @@ -79,7 +79,7 @@ void ResolveCore (PropertyStack properties)
var xml = Raw.ToXml ();
xml = ReplaceProperties (xml, this, properties);

resolved_project = Project.ParseXml (xml);
resolved_project = Project.Parse (xml);

properties.Push (Raw.Properties);
parent?.ResolveCore (properties);
Expand Down
7 changes: 1 addition & 6 deletions src/Java.Interop.Tools.Maven/Repositories/MavenRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Java.Interop.Tools.Maven.Repositories;
public class MavenRepository : IMavenRepository
{
readonly string base_url;
static readonly HttpClient client;
static readonly HttpClient client = new HttpClient ();

public string Name { get; }

Expand All @@ -18,11 +18,6 @@ public MavenRepository (string baseUrl, string name)
base_url = baseUrl.TrimEnd ('/');
}

static MavenRepository ()
{
client = new HttpClient ();
}

public bool TryGetFile (Artifact artifact, string filename, [NotNullWhen (true)] out Stream? stream)
{
// ex: https://repo1.maven.org/maven2/dev/chrisbanes/snapper/snapper/0.3.0/{filename}
Expand Down
4 changes: 2 additions & 2 deletions tests/Java.Interop.Tools.Maven-Tests/DependenciesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public class DependenciesTests
[TestCase ("com.facebook.android:facebook-android-sdk:14.1.1", "com.facebook.android:facebook-applinks:14.1.1 - compile;com.facebook.android:facebook-common:14.1.1 - compile;com.facebook.android:facebook-core:14.1.1 - compile;com.facebook.android:facebook-gamingservices:14.1.1 - compile;com.facebook.android:facebook-login:14.1.1 - compile;com.facebook.android:facebook-messenger:14.1.1 - compile;com.facebook.android:facebook-share:14.1.1 - compile;org.jetbrains.kotlin:kotlin-stdlib:1.5.10 - compile")]
[TestCase ("com.airbnb.android:lottie:5.2.0", "androidx.appcompat:appcompat:1.3.1 - runtime;com.squareup.okio:okio:1.17.4 - runtime")]
public void TestMavenCentralResolvedDependencies (string artifact, string expected)
=> TestResolvedDependencies (MavenPomResolver.Central, artifact, expected);
=> TestResolvedDependencies (MavenProjectResolver.Central, artifact, expected);

void TestResolvedDependencies (MavenPomResolver resolver, string artifact, string expected)
void TestResolvedDependencies (MavenProjectResolver resolver, string artifact, string expected)
{
var art = Artifact.Parse (artifact);
var project = ResolvedProject.FromArtifact (art, resolver);
Expand Down
Loading

0 comments on commit 6896260

Please sign in to comment.