Skip to content

Commit 6ff086f

Browse files
rolfbjarneCopilot
andauthored
[msbuild] Pass the XamarinTask instance along when calling CompressionHelper.TryCompress. (#24439)
Pass the XamarinTask instance along when calling CompressionHelper.TryCompress, so that TryCompress can use the instance ExecuteAsync method on XamarinTask. This is required for a future change, where the instance XamarinTask.ExecuteAsync method will set the environment to use the current/correct version of Xcode. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 05c9830 commit 6ff086f

File tree

6 files changed

+39
-31
lines changed

6 files changed

+39
-31
lines changed

msbuild/Xamarin.MacDev.Tasks/Decompress.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ static string CanonicalizeZipEntryPath (string path)
9797
/// <param name="zip">The zip to search in</param>
9898
/// <param name="resource">The relative path inside the zip to extract (may be a file or a directory).</param>
9999
/// <param name="decompressionDir">The location on disk to store the extracted results</param>
100-
/// <param name="cancellationToken">The cancellation token (if any=</param>
100+
/// <param name="cancellationToken">The cancellation token (if any)</param>
101101
/// <param name="decompressedResource">The location on disk to the extracted resource</param>
102102
/// <returns>True if successfully decompressed, false otherwise.</returns>
103-
public static bool TryDecompress (TaskLoggingHelper log, string zip, string resource, string decompressionDir, List<string> createdFiles, CancellationToken? cancellationToken, [NotNullWhen (true)] out string? decompressedResource)
103+
public static bool TryDecompress (XamarinTask task, string zip, string resource, string decompressionDir, List<string> createdFiles, CancellationToken? cancellationToken, [NotNullWhen (true)] out string? decompressedResource)
104104
{
105+
var log = task.Log;
106+
105107
decompressedResource = Path.Combine (decompressionDir, resource);
106108

107109
var stampFile = decompressedResource.TrimEnd ('\\', '/') + ".stamp";
@@ -118,11 +120,11 @@ public static bool TryDecompress (TaskLoggingHelper log, string zip, string reso
118120

119121
bool rv;
120122
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
121-
rv = TryDecompressUsingSystemIOCompression (log, zip, resource, decompressionDir, cancellationToken);
123+
rv = TryDecompressUsingSystemIOCompression (task, zip, resource, decompressionDir, cancellationToken);
122124
} else if (!string.IsNullOrEmpty (Environment.GetEnvironmentVariable ("XAMARIN_USE_SYSTEM_IO_COMPRESSION"))) {
123-
rv = TryDecompressUsingSystemIOCompression (log, zip, resource, decompressionDir, cancellationToken);
125+
rv = TryDecompressUsingSystemIOCompression (task, zip, resource, decompressionDir, cancellationToken);
124126
} else {
125-
rv = TryDecompressUsingUnzip (log, zip, resource, decompressionDir, cancellationToken);
127+
rv = TryDecompressUsingUnzip (task, zip, resource, decompressionDir, cancellationToken);
126128
}
127129

128130
if (rv) {
@@ -145,8 +147,9 @@ public static bool TryDecompress (TaskLoggingHelper log, string zip, string reso
145147
// The dir separator character in zip files is always "/", even on Windows
146148
const char zipDirectorySeparator = '/';
147149

148-
static bool TryDecompressUsingUnzip (TaskLoggingHelper log, string zip, string resource, string decompressionDir, CancellationToken? cancellationToken)
150+
static bool TryDecompressUsingUnzip (XamarinTask task, string zip, string resource, string decompressionDir, CancellationToken? cancellationToken)
149151
{
152+
var log = task.Log;
150153
Directory.CreateDirectory (decompressionDir);
151154
var args = new List<string> {
152155
"-u", "-o",
@@ -176,12 +179,13 @@ static bool TryDecompressUsingUnzip (TaskLoggingHelper log, string zip, string r
176179
args.Add (zipPattern);
177180
}
178181

179-
var rv = XamarinTask.ExecuteAsync (log, "unzip", args, cancellationToken: cancellationToken).Result;
182+
var rv = task.ExecuteAsync ("unzip", args, cancellationToken: cancellationToken).Result;
180183
return rv.ExitCode == 0;
181184
}
182185

183-
static bool TryDecompressUsingSystemIOCompression (TaskLoggingHelper log, string zip, string resource, string decompressionDir, CancellationToken? cancellationToken)
186+
static bool TryDecompressUsingSystemIOCompression (XamarinTask task, string zip, string resource, string decompressionDir, CancellationToken? cancellationToken)
184187
{
188+
var log = task.Log;
185189
var rv = true;
186190

187191
// canonicalize input
@@ -270,8 +274,9 @@ static bool TryDecompressUsingSystemIOCompression (TaskLoggingHelper log, string
270274
/// testing the System.IO.Compression implementation locally (with the caveat that if the resources
271275
/// to compress has symlinks, it may not work).
272276
/// </remarks>
273-
public static bool TryCompress (TaskLoggingHelper log, string zip, IEnumerable<string> resources, bool overwrite, string workingDirectory, bool maxCompression = false)
277+
public static bool TryCompress (XamarinTask task, string zip, IEnumerable<string> resources, bool overwrite, string workingDirectory, bool maxCompression = false)
274278
{
279+
var log = task.Log;
275280
if (overwrite) {
276281
if (File.Exists (zip)) {
277282
log.LogMessage (MessageImportance.Low, "Replacing zip file {0} with {1}", zip, string.Join (", ", resources));
@@ -293,19 +298,20 @@ public static bool TryCompress (TaskLoggingHelper log, string zip, IEnumerable<s
293298

294299
bool rv;
295300
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
296-
rv = TryCompressUsingSystemIOCompression (log, zip, resources, workingDirectory, maxCompression);
301+
rv = TryCompressUsingSystemIOCompression (task, zip, resources, workingDirectory, maxCompression);
297302
} else if (!string.IsNullOrEmpty (Environment.GetEnvironmentVariable ("XAMARIN_USE_SYSTEM_IO_COMPRESSION"))) {
298-
rv = TryCompressUsingSystemIOCompression (log, zip, resources, workingDirectory, maxCompression);
303+
rv = TryCompressUsingSystemIOCompression (task, zip, resources, workingDirectory, maxCompression);
299304
} else {
300-
rv = TryCompressUsingZip (log, zip, resources, workingDirectory, maxCompression);
305+
rv = TryCompressUsingZip (task, zip, resources, workingDirectory, maxCompression);
301306
}
302307

303308
return rv;
304309
}
305310

306311
// Will always add to an existing zip file (not replace)
307-
static bool TryCompressUsingZip (TaskLoggingHelper log, string zip, IEnumerable<string> resources, string workingDirectory, bool maxCompression)
312+
static bool TryCompressUsingZip (XamarinTask task, string zip, IEnumerable<string> resources, string workingDirectory, bool maxCompression)
308313
{
314+
var log = task.Log;
309315
var zipArguments = new List<string> ();
310316
if (maxCompression)
311317
zipArguments.Add ("-9");
@@ -318,7 +324,7 @@ static bool TryCompressUsingZip (TaskLoggingHelper log, string zip, IEnumerable<
318324
var relativePath = PathUtils.AbsoluteToRelative (workingDirectory, fullPath);
319325
zipArguments.Add (relativePath);
320326
}
321-
var rv = XamarinTask.ExecuteAsync (log, "zip", zipArguments, workingDirectory: workingDirectory).Result;
327+
var rv = task.ExecuteAsync ("zip", zipArguments, workingDirectory: workingDirectory).Result;
322328
log.LogMessage (MessageImportance.Low, "Updated {0} with {1}: {2}", zip, string.Join (", ", resources), rv.ExitCode == 0);
323329
return rv.ExitCode == 0;
324330
}
@@ -330,8 +336,9 @@ static bool TryCompressUsingZip (TaskLoggingHelper log, string zip, IEnumerable<
330336
#endif
331337

332338
// Will always add to an existing zip file (not replace)
333-
static bool TryCompressUsingSystemIOCompression (TaskLoggingHelper log, string zip, IEnumerable<string> resources, string workingDirectory, bool maxCompression)
339+
static bool TryCompressUsingSystemIOCompression (XamarinTask task, string zip, IEnumerable<string> resources, string workingDirectory, bool maxCompression)
334340
{
341+
var log = task.Log;
335342
var rv = true;
336343

337344
workingDirectory = Path.GetFullPath (workingDirectory);

msbuild/Xamarin.MacDev.Tasks/Tasks/CreateBindingResourcePackage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public override bool Execute ()
100100
var workingDirectory = Path.GetDirectoryName (nativeRef);
101101
if (string.IsNullOrEmpty (workingDirectory))
102102
workingDirectory = Directory.GetCurrentDirectory ();
103-
CompressionHelper.TryCompress (Log, zipFile, new string [] { nativeRef }, false, workingDirectory, true);
103+
CompressionHelper.TryCompress (this, zipFile, new string [] { nativeRef }, false, workingDirectory, true);
104104
}
105105
packagedFiles.Add (zipFile);
106106
} else {

msbuild/Xamarin.MacDev.Tasks/Tasks/ResolveNativeReferences.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ void ProcessNativeReference (ITaskItem item, string name, List<ITaskItem> native
198198

199199
// (compressed) xcframework
200200
if (name.EndsWith (".xcframework", StringComparison.OrdinalIgnoreCase) || name.EndsWith (".xcframework.zip", StringComparison.OrdinalIgnoreCase)) {
201-
if (!TryResolveXCFramework (Log, TargetFrameworkMoniker, SdkIsSimulator, Architectures, name, GetIntermediateDecompressionDir (item), createdFiles, cancellationToken, out var nativeLibraryPath))
201+
if (!TryResolveXCFramework (this, TargetFrameworkMoniker, SdkIsSimulator, Architectures, name, GetIntermediateDecompressionDir (item), createdFiles, cancellationToken, out var nativeLibraryPath))
202202
return;
203203
var nr = new TaskItem (item);
204204
SetMetadataNativeLibrary (nr, nativeLibraryPath);
@@ -208,7 +208,7 @@ void ProcessNativeReference (ITaskItem item, string name, List<ITaskItem> native
208208

209209
// compressed framework
210210
if (name.EndsWith (".framework.zip", StringComparison.OrdinalIgnoreCase)) {
211-
if (!CompressionHelper.TryDecompress (Log, name, Path.GetFileNameWithoutExtension (name), GetIntermediateDecompressionDir (item), createdFiles, cancellationToken, out var frameworkPath))
211+
if (!CompressionHelper.TryDecompress (this, name, Path.GetFileNameWithoutExtension (name), GetIntermediateDecompressionDir (item), createdFiles, cancellationToken, out var frameworkPath))
212212
return;
213213
var nr = new TaskItem (item);
214214
nr.ItemSpec = GetActualLibrary (frameworkPath);
@@ -311,14 +311,14 @@ void ProcessSidecar (ITaskItem r, string resources, List<ITaskItem> native_frame
311311
ITaskItem t = new TaskItem (r);
312312
var name = referenceNode.Attributes ["Name"].Value.Trim ('\\', '/');
313313
if (name.EndsWith (".xcframework", StringComparison.Ordinal) || name.EndsWith (".xcframework.zip", StringComparison.Ordinal)) {
314-
if (!TryResolveXCFramework (Log, TargetFrameworkMoniker, SdkIsSimulator, Architectures, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out var nativeLibraryPath))
314+
if (!TryResolveXCFramework (this, TargetFrameworkMoniker, SdkIsSimulator, Architectures, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out var nativeLibraryPath))
315315
continue;
316316
SetMetadataNativeLibrary (t, nativeLibraryPath);
317317
} else if (name.EndsWith (".framework", StringComparison.Ordinal)) {
318318
string? frameworkPath;
319319
if (!isCompressed) {
320320
frameworkPath = Path.Combine (resources, name);
321-
} else if (!CompressionHelper.TryDecompress (Log, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out frameworkPath)) {
321+
} else if (!CompressionHelper.TryDecompress (this, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out frameworkPath)) {
322322
continue;
323323
}
324324
t.ItemSpec = GetActualLibrary (frameworkPath);
@@ -330,7 +330,7 @@ void ProcessSidecar (ITaskItem r, string resources, List<ITaskItem> native_frame
330330
string? dylibPath;
331331
if (!isCompressed) {
332332
dylibPath = Path.Combine (resources, name);
333-
} else if (!CompressionHelper.TryDecompress (Log, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out dylibPath)) {
333+
} else if (!CompressionHelper.TryDecompress (this, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out dylibPath)) {
334334
continue;
335335
}
336336
t.ItemSpec = dylibPath;
@@ -341,7 +341,7 @@ void ProcessSidecar (ITaskItem r, string resources, List<ITaskItem> native_frame
341341
string? aPath;
342342
if (!isCompressed) {
343343
aPath = Path.Combine (resources, name);
344-
} else if (!CompressionHelper.TryDecompress (Log, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out aPath)) {
344+
} else if (!CompressionHelper.TryDecompress (this, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out aPath)) {
345345
continue;
346346
}
347347
t.ItemSpec = aPath;
@@ -378,7 +378,7 @@ void ProcessSidecar (ITaskItem r, string resources, List<ITaskItem> native_frame
378378
/// <param name="nativeLibraryPath">A full path to the resolved native library within the xcframework. If 'resourcePath' is compressed, this will point to where the native library is decompressed on disk.</param>
379379
/// <param name="intermediateDecompressionDir"></param>
380380
/// <returns>True if a native library was successfully found. Otherwise false, and an error will have been printed to the log.</returns>
381-
public static bool TryResolveXCFramework (TaskLoggingHelper log, string targetFrameworkMoniker, bool isSimulator, string? architectures, string path, string intermediateDecompressionDir, List<string> createdFiles, CancellationToken? cancellationToken, [NotNullWhen (true)] out string? nativeLibraryPath)
381+
public static bool TryResolveXCFramework (XamarinTask task, string targetFrameworkMoniker, bool isSimulator, string? architectures, string path, string intermediateDecompressionDir, List<string> createdFiles, CancellationToken? cancellationToken, [NotNullWhen (true)] out string? nativeLibraryPath)
382382
{
383383
string resourcePath;
384384
string xcframework;
@@ -390,7 +390,7 @@ public static bool TryResolveXCFramework (TaskLoggingHelper log, string targetFr
390390
resourcePath = Path.GetDirectoryName (path);
391391
xcframework = Path.GetFileName (path);
392392
}
393-
return TryResolveXCFramework (log, targetFrameworkMoniker, isSimulator, architectures, resourcePath, xcframework, intermediateDecompressionDir, createdFiles, cancellationToken, out nativeLibraryPath);
393+
return TryResolveXCFramework (task, targetFrameworkMoniker, isSimulator, architectures, resourcePath, xcframework, intermediateDecompressionDir, createdFiles, cancellationToken, out nativeLibraryPath);
394394
}
395395

396396
/// <summary>
@@ -405,8 +405,9 @@ public static bool TryResolveXCFramework (TaskLoggingHelper log, string targetFr
405405
/// <param name="nativeLibraryPath">A full path to the resolved native library within the xcframework. If 'resourcePath' is compressed, this will point to where the native library is decompressed on disk.</param>
406406
/// <param name="intermediateDecompressionDir"></param>
407407
/// <returns>True if a native library was successfully found. Otherwise false, and an error will have been printed to the log.</returns>
408-
public static bool TryResolveXCFramework (TaskLoggingHelper log, string targetFrameworkMoniker, bool isSimulator, string? architectures, string resourcePath, string xcframework, string intermediateDecompressionDir, List<string> createdFiles, CancellationToken? cancellationToken, [NotNullWhen (true)] out string? nativeLibraryPath)
408+
public static bool TryResolveXCFramework (XamarinTask task, string targetFrameworkMoniker, bool isSimulator, string? architectures, string resourcePath, string xcframework, string intermediateDecompressionDir, List<string> createdFiles, CancellationToken? cancellationToken, [NotNullWhen (true)] out string? nativeLibraryPath)
409409
{
410+
var log = task.Log;
410411
nativeLibraryPath = null;
411412

412413
try {
@@ -421,7 +422,7 @@ public static bool TryResolveXCFramework (TaskLoggingHelper log, string targetFr
421422
if (!isCompressed && CompressionHelper.IsCompressed (xcframework)) {
422423
var zipPath = Path.Combine (resourcePath, xcframework);
423424
var xcframeworkName = Path.GetFileNameWithoutExtension (xcframework);
424-
if (!CompressionHelper.TryDecompress (log, zipPath, xcframeworkName, intermediateDecompressionDir, createdFiles, cancellationToken, out var decompressedXcframeworkPath))
425+
if (!CompressionHelper.TryDecompress (task, zipPath, xcframeworkName, intermediateDecompressionDir, createdFiles, cancellationToken, out var decompressedXcframeworkPath))
425426
return false;
426427

427428
nativeLibraryPath = Path.Combine (intermediateDecompressionDir, xcframeworkName, nativeLibraryRelativePath);
@@ -434,7 +435,7 @@ public static bool TryResolveXCFramework (TaskLoggingHelper log, string targetFr
434435
}
435436

436437
var zipResource = Path.Combine (xcframework, Path.GetDirectoryName (nativeLibraryRelativePath));
437-
if (!CompressionHelper.TryDecompress (log, resourcePath, zipResource, intermediateDecompressionDir, createdFiles, cancellationToken, out var decompressedPath))
438+
if (!CompressionHelper.TryDecompress (task, resourcePath, zipResource, intermediateDecompressionDir, createdFiles, cancellationToken, out var decompressedPath))
438439
return false;
439440

440441
nativeLibraryPath = Path.Combine (intermediateDecompressionDir, xcframework, nativeLibraryRelativePath);

msbuild/Xamarin.MacDev.Tasks/Tasks/Unzip.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ bool ExecuteLocally ()
7979
{
8080
var createdFiles = new List<string> ();
8181
cancellationTokenSource = new CancellationTokenSource ();
82-
if (!CompressionHelper.TryDecompress (Log, ZipFilePath!.ItemSpec, Resource, ExtractionPath, createdFiles, cancellationTokenSource.Token, out var _))
82+
if (!CompressionHelper.TryDecompress (this, ZipFilePath!.ItemSpec, Resource, ExtractionPath, createdFiles, cancellationTokenSource.Token, out var _))
8383
return false;
8484

8585
TouchedFiles = createdFiles.Select (v => new TaskItem (v)).ToArray ();

msbuild/Xamarin.MacDev.Tasks/Tasks/XamarinTask.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ protected string GetSdkPlatform (bool isSimulator)
9797
return PlatformFrameworkHelper.GetSdkPlatform (Platform, isSimulator);
9898
}
9999

100-
protected System.Threading.Tasks.Task<Execution> ExecuteAsync (string fileName, IList<string> arguments, string? sdkDevPath = null, Dictionary<string, string?>? environment = null, bool mergeOutput = true, bool showErrorIfFailure = true, string? workingDirectory = null)
100+
internal protected System.Threading.Tasks.Task<Execution> ExecuteAsync (string fileName, IList<string> arguments, string? sdkDevPath = null, Dictionary<string, string?>? environment = null, bool mergeOutput = true, bool showErrorIfFailure = true, string? workingDirectory = null, CancellationToken? cancellationToken = null)
101101
{
102-
return ExecuteAsync (Log, fileName, arguments, sdkDevPath, environment, mergeOutput, showErrorIfFailure, workingDirectory);
102+
return ExecuteAsync (Log, fileName, arguments, sdkDevPath, environment, mergeOutput, showErrorIfFailure, workingDirectory, cancellationToken);
103103
}
104104

105105
static int executionCounter;

msbuild/Xamarin.MacDev.Tasks/Tasks/Zip.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override bool Execute ()
5454
for (int i = 0; i < Sources.Length; i++)
5555
sources.Add (Sources [i].GetMetadata ("FullPath"));
5656

57-
if (!CompressionHelper.TryCompress (Log, zip, sources, false, workingDirectory, false))
57+
if (!CompressionHelper.TryCompress (this, zip, sources, false, workingDirectory, false))
5858
return false;
5959

6060
return !Log.HasLoggedErrors;

0 commit comments

Comments
 (0)