Skip to content

Commit

Permalink
Track Java.Interop.Tools.Maven changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpobst committed Mar 13, 2024
1 parent e52cc13 commit 516fd07
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion external/xamarin-android-tools
30 changes: 15 additions & 15 deletions src/Xamarin.Android.Build.Tasks/Tasks/JavaDependencyVerification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public override bool RunTask ()
return !Log.HasLoggedErrors;
}

static bool TryResolveProject (Artifact artifact, IPomResolver resolver, [NotNullWhen (true)]out ResolvedProject? project)
static bool TryResolveProject (Artifact artifact, IProjectResolver resolver, [NotNullWhen (true)]out ResolvedProject? project)
{
// ResolvedProject.FromArtifact will throw if a POM cannot be resolved, but our MSBuildLoggingPomResolver
// has already logged the failure as an MSBuild error. We don't want to log it again as an unhandled exception.
Expand Down Expand Up @@ -135,7 +135,7 @@ public bool EnsureDependencySatisfied (ResolvedDependency dependency, MicrosoftN
return true;

var suggestion = packages.GetNuGetPackage ($"{dependency.GroupId}:{dependency.ArtifactId}");
var artifact_spec = dependency.ToArtifactString (dependency.Version.HasValue ());
var artifact_spec = dependency.Version.HasValue () ? dependency.VersionedArtifactString : dependency.ArtifactString;

if (suggestion is string nuget)
log.LogCodedError ("XA4242", Properties.Resources.XA4242, artifact_spec, nuget);
Expand All @@ -157,7 +157,7 @@ public void AddAndroidLibraries (ITaskItem []? tasks)

if (version != null && MavenExtensions.TryParseArtifactWithVersion (id, version, log, out var art)) {
log.LogMessage ("Found Java dependency '{0}:{1}' version '{2}' from AndroidLibrary '{3}'", art.GroupId, art.Id, art.Version, task.ItemSpec);
artifacts.Add (art.ToGroupAndArtifactId (), art);
artifacts.Add (art.ArtifactString, art);
}
}
}
Expand All @@ -168,7 +168,7 @@ public void AddPackageReferences (ITaskItem []? tasks)

// See if JavaArtifact/JavaVersion overrides were used
if (task.TryParseJavaArtifactAndJavaVersion ("PackageReference", log, out var explicit_artifact, out var attributes_specified)) {
artifacts.Add (explicit_artifact.ToGroupAndArtifactId (), explicit_artifact);
artifacts.Add (explicit_artifact.ArtifactString, explicit_artifact);
continue;
}

Expand All @@ -181,7 +181,7 @@ public void AddPackageReferences (ITaskItem []? tasks)

if (artifact != null) {
log.LogMessage ("Found Java dependency '{0}:{1}' version '{2}' from PackageReference '{3}'", artifact.GroupId, artifact.Id, artifact.Version, task.ItemSpec);
artifacts.Add (artifact.ToGroupAndArtifactId (), artifact);
artifacts.Add (artifact.ArtifactString, artifact);

continue;
}
Expand All @@ -195,7 +195,7 @@ public void AddProjectReferences (ITaskItem []? tasks)
foreach (var task in tasks.OrEmpty ()) {
// See if JavaArtifact/JavaVersion overrides were used
if (task.TryParseJavaArtifactAndJavaVersion ("ProjectReference", log, out var explicit_artifact, out var attributes_specified)) {
artifacts.Add (explicit_artifact.ToGroupAndArtifactId (), explicit_artifact);
artifacts.Add (explicit_artifact.ArtifactString, explicit_artifact);
continue;
}

Expand All @@ -219,26 +219,26 @@ public void AddIgnoredDependencies (ITaskItem []? tasks)

if (version != null && MavenExtensions.TryParseArtifactWithVersion (id, version, log, out var art)) {
log.LogMessage ("Ignoring Java dependency '{0}:{1}' version '{2}'", art.GroupId, art.Id, art.Version);
artifacts.Add (art.ToGroupAndArtifactId (), art);
artifacts.Add (art.ArtifactString, art);
}
}
}

bool TrySatisfyDependency (ResolvedDependency dependency)
{
if (!dependency.Version.HasValue ())
return artifacts.ContainsKey (dependency.ToGroupAndArtifactId ());
return artifacts.ContainsKey (dependency.ArtifactString);

var dep_versions = MavenVersionRange.Parse (dependency.Version);

if (artifacts.TryGetValue (dependency.ToGroupAndArtifactId (), out var artifact))
if (artifacts.TryGetValue (dependency.ArtifactString, out var artifact))
return dep_versions.Any (r => r.ContainsVersion (MavenVersion.Parse (artifact.Version)));

return false;
}
}

class MSBuildLoggingPomResolver : IPomResolver
class MSBuildLoggingPomResolver : IProjectResolver
{
readonly TaskLoggingHelper logger;
readonly Dictionary<string, Project> poms = new ();
Expand Down Expand Up @@ -272,8 +272,8 @@ public MSBuildLoggingPomResolver (TaskLoggingHelper logger)

try {
using (var file = File.OpenRead (filename)) {
var project = Project.Parse (file);
var registered_artifact = Artifact.Parse (project.ToString ());
var project = Project.Load (file);
var registered_artifact = Artifact.Parse (project.VersionedArtifactString);

// Return the registered artifact, preferring any overrides specified in the task item
var final_artifact = new Artifact (
Expand All @@ -283,7 +283,7 @@ public MSBuildLoggingPomResolver (TaskLoggingHelper logger)
);

// Use index instead of Add to handle duplicates
poms [final_artifact.ToString ()] = project;
poms [final_artifact.VersionedArtifactString] = project;

logger.LogDebugMessage ("Registered POM for artifact '{0}' from '{1}'", final_artifact, filename);

Expand All @@ -295,9 +295,9 @@ public MSBuildLoggingPomResolver (TaskLoggingHelper logger)
}
}

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

logger.LogCodedError ("XA4247", Properties.Resources.XA4247, artifact);
Expand Down
12 changes: 6 additions & 6 deletions src/Xamarin.Android.Build.Tasks/Tasks/MavenDownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ public async override System.Threading.Tasks.Task RunTaskAsync ()
var project = ResolvedProject.FromArtifact (artifact, resolver);

// Set the POM file path for _this_ artifact
var primary_pom = resolver.ResolvedPoms [artifact.ToString ()];
var primary_pom = resolver.ResolvedPoms [artifact.VersionedArtifactString];
result.SetMetadata ("Manifest", primary_pom);

Log.LogMessage ("Found POM file '{0}' for Java artifact '{1}'.", primary_pom, artifact);

// Create TaskItems for any other POMs we resolved
foreach (var kv in resolver.ResolvedPoms.Where (k => k.Key != artifact.ToString ())) {
foreach (var kv in resolver.ResolvedPoms.Where (k => k.Key != artifact.VersionedArtifactString)) {

var pom_item = new TaskItem (kv.Value);
var pom_artifact = Artifact.Parse (kv.Key);
Expand Down Expand Up @@ -163,7 +163,7 @@ public async override System.Threading.Tasks.Task RunTaskAsync ()

// This wrapper around CachedMavenRepository is used to log the POMs that are resolved.
// We need these on-disk file locations so we can pass them as <AndroidAdditionalJavaManifest> items.
class LoggingPomResolver : IPomResolver
class LoggingPomResolver : IProjectResolver
{
readonly CachedMavenRepository repository;

Expand All @@ -174,14 +174,14 @@ public LoggingPomResolver (CachedMavenRepository repository)
this.repository = repository;
}

public Project ResolveRawProject (Artifact artifact)
public Project Resolve (Artifact artifact)
{
if (repository.TryGetFilePath (artifact, $"{artifact.Id}-{artifact.Version}.pom", out var path)) {
using (var stream = File.OpenRead (path)) {
var pom = Project.Parse (stream) ?? throw new InvalidOperationException ($"Could not deserialize POM for {artifact}");
var pom = Project.Load (stream) ?? throw new InvalidOperationException ($"Could not deserialize POM for {artifact}");

// Use index instead of Add to handle duplicates
ResolvedPoms [artifact.ToString ()] = path;
ResolvedPoms [artifact.VersionedArtifactString] = path;

return pom;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Xml;
using Java.Interop.Tools.Maven.Models;
Expand Down Expand Up @@ -386,7 +387,7 @@ public PomBuilder (string groupId, string artifactId, string? version)

public string Build ()
{
using var sw = new StringWriter ();
using var sw = new Utf8StringWriter ();
using var xw = XmlWriter.Create (sw);

xw.WriteStartDocument ();
Expand Down Expand Up @@ -487,4 +488,10 @@ public PomBuilder WithParent (string groupId, string artifactId, string? version
}

public TemporaryFile BuildTemporary () => new TemporaryFile (Build ());

// Trying to write XML to a StringWriter defaults to UTF-16, but we want UTF-8
class Utf8StringWriter : StringWriter
{
public override Encoding Encoding => Encoding.UTF8;
}
}
4 changes: 0 additions & 4 deletions src/Xamarin.Android.Build.Tasks/Utilities/MavenExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,4 @@ public static bool TryParseJavaArtifactAndJavaVersion (this ITaskItem task, stri
public static bool IsRuntimeDependency (this ResolvedDependency dependency) => dependency?.Scope != null && dependency.Scope.IndexOf ("runtime", StringComparison.OrdinalIgnoreCase) != -1;

public static bool IsOptional (this ResolvedDependency dependency) => dependency?.Optional != null && dependency.Optional.IndexOf ("true", StringComparison.OrdinalIgnoreCase) != -1;

public static string ToGroupAndArtifactId (this Artifact artifact) => $"{artifact.GroupId}:{artifact.Id}";

public static string ToGroupAndArtifactId (this ResolvedDependency dependency) => $"{dependency.GroupId}:{dependency.ArtifactId}";
}

0 comments on commit 516fd07

Please sign in to comment.