|
5 | 5 | using System.Diagnostics.CodeAnalysis;
|
6 | 6 | using System.IO;
|
7 | 7 | using System.Linq;
|
| 8 | +using System.Text; |
8 | 9 | using System.Threading;
|
9 | 10 | using System.Threading.Tasks;
|
10 | 11 | using Java.Interop.Tools.Maven.Models;
|
@@ -77,7 +78,7 @@ public static bool TryParseArtifacts (string id, TaskLoggingHelper log, out List
|
77 | 78 | return result;
|
78 | 79 | }
|
79 | 80 |
|
80 |
| - public static bool TryParseJavaArtifact (this ITaskItem task, string type, TaskLoggingHelper log, [NotNullWhen (true)]out Artifact? artifact, out bool attributesSpecified) |
| 81 | + public static bool TryParseJavaArtifact (this ITaskItem task, string type, TaskLoggingHelper log, [NotNullWhen (true)] out Artifact? artifact, out bool attributesSpecified) |
81 | 82 | {
|
82 | 83 | var result = TryParseJavaArtifacts (task, type, log, out var artifacts, out attributesSpecified);
|
83 | 84 |
|
@@ -130,45 +131,57 @@ public static bool TryParseJavaArtifacts (this ITaskItem task, string type, Task
|
130 | 131 | }
|
131 | 132 |
|
132 | 133 | // Returns artifact output path
|
133 |
| - public static async Task<string?> DownloadPayload (CachedMavenRepository repository, Artifact artifact, string cacheDir, TaskLoggingHelper log, CancellationToken cancellationToken) |
| 134 | + public static async Task<string?> DownloadPayload (CachedMavenRepository repository, Artifact artifact, string cacheDir, string? mavenOverrideFilename, TaskLoggingHelper log, CancellationToken cancellationToken) |
134 | 135 | {
|
135 | 136 | var output_directory = Path.Combine (cacheDir, repository.Name, artifact.GroupId, artifact.Id, artifact.Version);
|
136 | 137 |
|
137 | 138 | Directory.CreateDirectory (output_directory);
|
138 | 139 |
|
139 |
| - var filename = $"{artifact.Id}-{artifact.Version}"; |
140 |
| - var jar_filename = Path.Combine (output_directory, Path.Combine ($"{filename}.jar")); |
141 |
| - var aar_filename = Path.Combine (output_directory, Path.Combine ($"{filename}.aar")); |
| 140 | + var files_to_check = new List<string> (); |
| 141 | + |
| 142 | + if (mavenOverrideFilename.HasValue ()) { |
| 143 | + files_to_check.Add (Path.Combine (output_directory, mavenOverrideFilename)); |
| 144 | + } else { |
| 145 | + files_to_check.Add (Path.Combine (output_directory, $"{artifact.Id}-{artifact.Version}.jar")); |
| 146 | + files_to_check.Add (Path.Combine (output_directory, $"{artifact.Id}-{artifact.Version}.aar")); |
| 147 | + } |
142 | 148 |
|
143 | 149 | // We don't need to redownload if we already have a cached copy
|
144 |
| - if (File.Exists (jar_filename)) |
145 |
| - return jar_filename; |
| 150 | + foreach (var file in files_to_check) { |
| 151 | + if (File.Exists (file)) |
| 152 | + return file; |
| 153 | + } |
146 | 154 |
|
147 |
| - if (File.Exists (aar_filename)) |
148 |
| - return aar_filename; |
| 155 | + // Try to download the file from Maven |
| 156 | + var results = new List<(string file, string error)> (); |
149 | 157 |
|
150 |
| - if (await TryDownloadPayload (repository, artifact, jar_filename, cancellationToken) is not string jar_error) |
151 |
| - return jar_filename; |
| 158 | + foreach (var file in files_to_check) { |
| 159 | + if (await TryDownloadPayload (repository, artifact, Path.GetFileName (file), cancellationToken) is not string error) |
| 160 | + return file; |
152 | 161 |
|
153 |
| - if (await TryDownloadPayload (repository, artifact, aar_filename, cancellationToken) is not string aar_error) |
154 |
| - return aar_filename; |
| 162 | + results.Add ((file, error)); |
| 163 | + } |
155 | 164 |
|
156 |
| - log.LogCodedError ("XA4236", Properties.Resources.XA4236, artifact.GroupId, artifact.Id, Path.GetFileName (jar_filename), jar_error, Path.GetFileName (aar_filename), aar_error); |
| 165 | + // Couldn't download the artifact, construct an error message for the user |
| 166 | + var error_builder = new StringBuilder (); |
| 167 | + |
| 168 | + foreach (var error in results) |
| 169 | + error_builder.AppendLine ($"- {Path.GetFileName (error.file)}: {error.error}"); |
| 170 | + |
| 171 | + log.LogCodedError ("XA4236", Properties.Resources.XA4236, artifact.GroupId, artifact.Id, error_builder.ToString ().TrimEnd ()); |
157 | 172 |
|
158 | 173 | return null;
|
159 | 174 | }
|
160 | 175 |
|
161 | 176 | // Return value is download error message, null represents success (async methods cannot have out parameters)
|
162 | 177 | static async Task<string?> TryDownloadPayload (CachedMavenRepository repository, Artifact artifact, string filename, CancellationToken cancellationToken)
|
163 | 178 | {
|
164 |
| - var maven_filename = $"{artifact.Id}-{artifact.Version}{Path.GetExtension (filename)}"; |
165 |
| - |
166 | 179 | try {
|
167 |
| - if ((await repository.GetFilePathAsync (artifact, maven_filename, cancellationToken)) is string path) { |
| 180 | + if ((await repository.GetFilePathAsync (artifact, filename, cancellationToken)) is string path) { |
168 | 181 | return null;
|
169 | 182 | } else {
|
170 | 183 | // This probably(?) cannot be hit, everything should come back as an exception
|
171 |
| - return $"Could not download {maven_filename}"; |
| 184 | + return $"Could not download {filename}"; |
172 | 185 | }
|
173 | 186 |
|
174 | 187 | } catch (Exception ex) {
|
|
0 commit comments