Skip to content

Commit

Permalink
[XABT] Add ArtifactFilename metadata for AndroidMavenLibrary item.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpobst committed Oct 31, 2024
1 parent 4266c2b commit dda9800
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions src/Xamarin.Android.Build.Tasks/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -985,15 +985,11 @@ To use a custom JDK path for a command line build, set the 'JavaSdkDirectory' MS
</data>
<data name="XA4236" xml:space="preserve">
<value>Cannot download Maven artifact '{0}:{1}'.
- {2}: {3}
- {4}: {5}</value>
{2}</value>
<comment>The following are literal names and should not be translated: Maven
{0} - Maven artifact group id
{1} - Maven artifact id
{2} - The .jar filename we tried to download
{3} - The HttpClient reported download exception message
{4} - The .aar filename we tried to download
{5} - The HttpClient provided download exception message</comment>
{2} - The filenames we tried to download and the HttpClient reported download exception messages</comment>
</data>
<data name="XA4237" xml:space="preserve">
<value>Cannot download POM file for Maven artifact '{0}'.
Expand Down
5 changes: 4 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/MavenDownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ public async override System.Threading.Tasks.Task RunTaskAsync ()
if (repository is null)
return null;

// Allow user to override the Maven filename of the artifact
var maven_override_filename = item.GetMetadataOrDefault ("ArtifactFilename", null);

// Download artifact
var artifact_file = await MavenExtensions.DownloadPayload (repository, artifact, MavenCacheDirectory, Log, CancellationToken);
var artifact_file = await MavenExtensions.DownloadPayload (repository, artifact, MavenCacheDirectory, maven_override_filename, Log, CancellationToken);

if (artifact_file is null)
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Android.Build.Tasks;
Expand All @@ -23,7 +24,8 @@ public static IEnumerable<XElement> ToXElements (this ICollection<ITaskItem> ite
}
}

public static string GetMetadataOrDefault (this ITaskItem item, string name, string defaultValue)
[return: NotNullIfNotNull (nameof (defaultValue))]
public static string? GetMetadataOrDefault (this ITaskItem item, string name, string? defaultValue)
{
var value = item.GetMetadata (name);

Expand Down
49 changes: 31 additions & 18 deletions src/Xamarin.Android.Build.Tasks/Utilities/MavenExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Java.Interop.Tools.Maven.Models;
Expand Down Expand Up @@ -77,7 +78,7 @@ public static bool TryParseArtifacts (string id, TaskLoggingHelper log, out List
return result;
}

public static bool TryParseJavaArtifact (this ITaskItem task, string type, TaskLoggingHelper log, [NotNullWhen (true)]out Artifact? artifact, out bool attributesSpecified)
public static bool TryParseJavaArtifact (this ITaskItem task, string type, TaskLoggingHelper log, [NotNullWhen (true)] out Artifact? artifact, out bool attributesSpecified)
{
var result = TryParseJavaArtifacts (task, type, log, out var artifacts, out attributesSpecified);

Expand Down Expand Up @@ -130,45 +131,57 @@ public static bool TryParseJavaArtifacts (this ITaskItem task, string type, Task
}

// Returns artifact output path
public static async Task<string?> DownloadPayload (CachedMavenRepository repository, Artifact artifact, string cacheDir, TaskLoggingHelper log, CancellationToken cancellationToken)
public static async Task<string?> DownloadPayload (CachedMavenRepository repository, Artifact artifact, string cacheDir, string? mavenOverrideFilename, TaskLoggingHelper log, CancellationToken cancellationToken)
{
var output_directory = Path.Combine (cacheDir, repository.Name, artifact.GroupId, artifact.Id, artifact.Version);

Directory.CreateDirectory (output_directory);

var filename = $"{artifact.Id}-{artifact.Version}";
var jar_filename = Path.Combine (output_directory, Path.Combine ($"{filename}.jar"));
var aar_filename = Path.Combine (output_directory, Path.Combine ($"{filename}.aar"));
var files_to_check = new List<string> ();

if (mavenOverrideFilename.HasValue ()) {
files_to_check.Add (Path.Combine (output_directory, mavenOverrideFilename));
} else {
files_to_check.Add (Path.Combine (output_directory, $"{artifact.Id}-{artifact.Version}.jar"));
files_to_check.Add (Path.Combine (output_directory, $"{artifact.Id}-{artifact.Version}.aar"));
}

// We don't need to redownload if we already have a cached copy
if (File.Exists (jar_filename))
return jar_filename;
foreach (var file in files_to_check) {
if (File.Exists (file))
return file;
}

if (File.Exists (aar_filename))
return aar_filename;
// Try to download the file from Maven
var results = new List<(string file, string error)> ();

if (await TryDownloadPayload (repository, artifact, jar_filename, cancellationToken) is not string jar_error)
return jar_filename;
foreach (var file in files_to_check) {
if (await TryDownloadPayload (repository, artifact, file, cancellationToken) is not string error)
return file;

if (await TryDownloadPayload (repository, artifact, aar_filename, cancellationToken) is not string aar_error)
return aar_filename;
results.Add ((file, error));
}

log.LogCodedError ("XA4236", Properties.Resources.XA4236, artifact.GroupId, artifact.Id, Path.GetFileName (jar_filename), jar_error, Path.GetFileName (aar_filename), aar_error);
// Couldn't download the artifact, construct an error message for the user
var error_builder = new StringBuilder ();

foreach (var error in results)
error_builder.AppendLine ($"- {Path.GetFileName (error.file)}: {error.error}");

log.LogCodedError ("XA4236", Properties.Resources.XA4236, artifact.GroupId, artifact.Id, error_builder.ToString ().TrimEnd ());

return null;
}

// Return value is download error message, null represents success (async methods cannot have out parameters)
static async Task<string?> TryDownloadPayload (CachedMavenRepository repository, Artifact artifact, string filename, CancellationToken cancellationToken)
{
var maven_filename = $"{artifact.Id}-{artifact.Version}{Path.GetExtension (filename)}";

try {
if ((await repository.GetFilePathAsync (artifact, maven_filename, cancellationToken)) is string path) {
if ((await repository.GetFilePathAsync (artifact, filename, cancellationToken)) is string path) {
return null;
} else {
// This probably(?) cannot be hit, everything should come back as an exception
return $"Could not download {maven_filename}";
return $"Could not download {filename}";
}

} catch (Exception ex) {
Expand Down

0 comments on commit dda9800

Please sign in to comment.