Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions msbuild/Xamarin.MacDev.Tasks/Decompress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ static string CanonicalizeZipEntryPath (string path)
/// <param name="cancellationToken">The cancellation token (if any=</param>
/// <param name="decompressedResource">The location on disk to the extracted resource</param>
/// <returns>True if successfully decompressed, false otherwise.</returns>
public static bool TryDecompress (TaskLoggingHelper log, string zip, string resource, string decompressionDir, List<string> createdFiles, CancellationToken? cancellationToken, [NotNullWhen (true)] out string? decompressedResource)
public static bool TryDecompress (XamarinTask task, string zip, string resource, string decompressionDir, List<string> createdFiles, CancellationToken? cancellationToken, [NotNullWhen (true)] out string? decompressedResource)
{
var log = task.Log;

decompressedResource = Path.Combine (decompressionDir, resource);

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

bool rv;
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
rv = TryDecompressUsingSystemIOCompression (log, zip, resource, decompressionDir, cancellationToken);
rv = TryDecompressUsingSystemIOCompression (task, zip, resource, decompressionDir, cancellationToken);
} else if (!string.IsNullOrEmpty (Environment.GetEnvironmentVariable ("XAMARIN_USE_SYSTEM_IO_COMPRESSION"))) {
rv = TryDecompressUsingSystemIOCompression (log, zip, resource, decompressionDir, cancellationToken);
rv = TryDecompressUsingSystemIOCompression (task, zip, resource, decompressionDir, cancellationToken);
} else {
rv = TryDecompressUsingUnzip (log, zip, resource, decompressionDir, cancellationToken);
rv = TryDecompressUsingUnzip (task, zip, resource, decompressionDir, cancellationToken);
}

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

static bool TryDecompressUsingUnzip (TaskLoggingHelper log, string zip, string resource, string decompressionDir, CancellationToken? cancellationToken)
static bool TryDecompressUsingUnzip (XamarinTask task, string zip, string resource, string decompressionDir, CancellationToken? cancellationToken)
{
var log = task.Log;
Directory.CreateDirectory (decompressionDir);
var args = new List<string> {
"-u", "-o",
Expand Down Expand Up @@ -176,12 +179,13 @@ static bool TryDecompressUsingUnzip (TaskLoggingHelper log, string zip, string r
args.Add (zipPattern);
}

var rv = XamarinTask.ExecuteAsync (log, "unzip", args, cancellationToken: cancellationToken).Result;
var rv = task.ExecuteAsync ("unzip", args, cancellationToken: cancellationToken).Result;
return rv.ExitCode == 0;
}

static bool TryDecompressUsingSystemIOCompression (TaskLoggingHelper log, string zip, string resource, string decompressionDir, CancellationToken? cancellationToken)
static bool TryDecompressUsingSystemIOCompression (XamarinTask task, string zip, string resource, string decompressionDir, CancellationToken? cancellationToken)
{
var log = task.Log;
var rv = true;

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

bool rv;
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
rv = TryCompressUsingSystemIOCompression (log, zip, resources, workingDirectory, maxCompression);
rv = TryCompressUsingSystemIOCompression (task, zip, resources, workingDirectory, maxCompression);
} else if (!string.IsNullOrEmpty (Environment.GetEnvironmentVariable ("XAMARIN_USE_SYSTEM_IO_COMPRESSION"))) {
rv = TryCompressUsingSystemIOCompression (log, zip, resources, workingDirectory, maxCompression);
rv = TryCompressUsingSystemIOCompression (task, zip, resources, workingDirectory, maxCompression);
} else {
rv = TryCompressUsingZip (log, zip, resources, workingDirectory, maxCompression);
rv = TryCompressUsingZip (task, zip, resources, workingDirectory, maxCompression);
}

return rv;
}

// Will always add to an existing zip file (not replace)
static bool TryCompressUsingZip (TaskLoggingHelper log, string zip, IEnumerable<string> resources, string workingDirectory, bool maxCompression)
static bool TryCompressUsingZip (XamarinTask task, string zip, IEnumerable<string> resources, string workingDirectory, bool maxCompression)
{
var log = task.Log;
var zipArguments = new List<string> ();
if (maxCompression)
zipArguments.Add ("-9");
Expand All @@ -318,7 +324,7 @@ static bool TryCompressUsingZip (TaskLoggingHelper log, string zip, IEnumerable<
var relativePath = PathUtils.AbsoluteToRelative (workingDirectory, fullPath);
zipArguments.Add (relativePath);
}
var rv = XamarinTask.ExecuteAsync (log, "zip", zipArguments, workingDirectory: workingDirectory).Result;
var rv = task.ExecuteAsync ("zip", zipArguments, workingDirectory: workingDirectory).Result;
log.LogMessage (MessageImportance.Low, "Updated {0} with {1}: {2}", zip, string.Join (", ", resources), rv.ExitCode == 0);
return rv.ExitCode == 0;
}
Expand All @@ -330,8 +336,9 @@ static bool TryCompressUsingZip (TaskLoggingHelper log, string zip, IEnumerable<
#endif

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

workingDirectory = Path.GetFullPath (workingDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public override bool Execute ()
var workingDirectory = Path.GetDirectoryName (nativeRef);
if (string.IsNullOrEmpty (workingDirectory))
workingDirectory = Directory.GetCurrentDirectory ();
CompressionHelper.TryCompress (Log, zipFile, new string [] { nativeRef }, false, workingDirectory, true);
CompressionHelper.TryCompress (this, zipFile, new string [] { nativeRef }, false, workingDirectory, true);
}
packagedFiles.Add (zipFile);
} else {
Expand Down
23 changes: 12 additions & 11 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/ResolveNativeReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void ProcessNativeReference (ITaskItem item, string name, List<ITaskItem> native

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

// compressed framework
if (name.EndsWith (".framework.zip", StringComparison.OrdinalIgnoreCase)) {
if (!CompressionHelper.TryDecompress (Log, name, Path.GetFileNameWithoutExtension (name), GetIntermediateDecompressionDir (item), createdFiles, cancellationToken, out var frameworkPath))
if (!CompressionHelper.TryDecompress (this, name, Path.GetFileNameWithoutExtension (name), GetIntermediateDecompressionDir (item), createdFiles, cancellationToken, out var frameworkPath))
return;
var nr = new TaskItem (item);
nr.ItemSpec = GetActualLibrary (frameworkPath);
Expand Down Expand Up @@ -311,14 +311,14 @@ void ProcessSidecar (ITaskItem r, string resources, List<ITaskItem> native_frame
ITaskItem t = new TaskItem (r);
var name = referenceNode.Attributes ["Name"].Value.Trim ('\\', '/');
if (name.EndsWith (".xcframework", StringComparison.Ordinal) || name.EndsWith (".xcframework.zip", StringComparison.Ordinal)) {
if (!TryResolveXCFramework (Log, TargetFrameworkMoniker, SdkIsSimulator, Architectures, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out var nativeLibraryPath))
if (!TryResolveXCFramework (this, TargetFrameworkMoniker, SdkIsSimulator, Architectures, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out var nativeLibraryPath))
continue;
SetMetadataNativeLibrary (t, nativeLibraryPath);
} else if (name.EndsWith (".framework", StringComparison.Ordinal)) {
string? frameworkPath;
if (!isCompressed) {
frameworkPath = Path.Combine (resources, name);
} else if (!CompressionHelper.TryDecompress (Log, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out frameworkPath)) {
} else if (!CompressionHelper.TryDecompress (this, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out frameworkPath)) {
continue;
}
t.ItemSpec = GetActualLibrary (frameworkPath);
Expand All @@ -330,7 +330,7 @@ void ProcessSidecar (ITaskItem r, string resources, List<ITaskItem> native_frame
string? dylibPath;
if (!isCompressed) {
dylibPath = Path.Combine (resources, name);
} else if (!CompressionHelper.TryDecompress (Log, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out dylibPath)) {
} else if (!CompressionHelper.TryDecompress (this, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out dylibPath)) {
continue;
}
t.ItemSpec = dylibPath;
Expand All @@ -341,7 +341,7 @@ void ProcessSidecar (ITaskItem r, string resources, List<ITaskItem> native_frame
string? aPath;
if (!isCompressed) {
aPath = Path.Combine (resources, name);
} else if (!CompressionHelper.TryDecompress (Log, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out aPath)) {
} else if (!CompressionHelper.TryDecompress (this, resources, name, GetIntermediateDecompressionDir (resources), createdFiles, cancellationToken, out aPath)) {
continue;
}
t.ItemSpec = aPath;
Expand Down Expand Up @@ -378,7 +378,7 @@ void ProcessSidecar (ITaskItem r, string resources, List<ITaskItem> native_frame
/// <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>
/// <param name="intermediateDecompressionDir"></param>
/// <returns>True if a native library was successfully found. Otherwise false, and an error will have been printed to the log.</returns>
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)
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)
{
string resourcePath;
string xcframework;
Expand All @@ -390,7 +390,7 @@ public static bool TryResolveXCFramework (TaskLoggingHelper log, string targetFr
resourcePath = Path.GetDirectoryName (path);
xcframework = Path.GetFileName (path);
}
return TryResolveXCFramework (log, targetFrameworkMoniker, isSimulator, architectures, resourcePath, xcframework, intermediateDecompressionDir, createdFiles, cancellationToken, out nativeLibraryPath);
return TryResolveXCFramework (task, targetFrameworkMoniker, isSimulator, architectures, resourcePath, xcframework, intermediateDecompressionDir, createdFiles, cancellationToken, out nativeLibraryPath);
}

/// <summary>
Expand All @@ -405,8 +405,9 @@ public static bool TryResolveXCFramework (TaskLoggingHelper log, string targetFr
/// <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>
/// <param name="intermediateDecompressionDir"></param>
/// <returns>True if a native library was successfully found. Otherwise false, and an error will have been printed to the log.</returns>
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)
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)
{
var log = task.Log;
nativeLibraryPath = null;

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

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

var zipResource = Path.Combine (xcframework, Path.GetDirectoryName (nativeLibraryRelativePath));
if (!CompressionHelper.TryDecompress (log, resourcePath, zipResource, intermediateDecompressionDir, createdFiles, cancellationToken, out var decompressedPath))
if (!CompressionHelper.TryDecompress (task, resourcePath, zipResource, intermediateDecompressionDir, createdFiles, cancellationToken, out var decompressedPath))
return false;

nativeLibraryPath = Path.Combine (intermediateDecompressionDir, xcframework, nativeLibraryRelativePath);
Expand Down
2 changes: 1 addition & 1 deletion msbuild/Xamarin.MacDev.Tasks/Tasks/Unzip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool ExecuteLocally ()
{
var createdFiles = new List<string> ();
cancellationTokenSource = new CancellationTokenSource ();
if (!CompressionHelper.TryDecompress (Log, ZipFilePath!.ItemSpec, Resource, ExtractionPath, createdFiles, cancellationTokenSource.Token, out var _))
if (!CompressionHelper.TryDecompress (this, ZipFilePath!.ItemSpec, Resource, ExtractionPath, createdFiles, cancellationTokenSource.Token, out var _))
return false;

TouchedFiles = createdFiles.Select (v => new TaskItem (v)).ToArray ();
Expand Down
4 changes: 2 additions & 2 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/XamarinTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ protected string GetSdkPlatform (bool isSimulator)
return PlatformFrameworkHelper.GetSdkPlatform (Platform, isSimulator);
}

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)
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)
{
return ExecuteAsync (Log, fileName, arguments, sdkDevPath, environment, mergeOutput, showErrorIfFailure, workingDirectory);
return ExecuteAsync (Log, fileName, arguments, sdkDevPath, environment, mergeOutput, showErrorIfFailure, workingDirectory, cancellationToken);
}

static int executionCounter;
Expand Down
2 changes: 1 addition & 1 deletion msbuild/Xamarin.MacDev.Tasks/Tasks/Zip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override bool Execute ()
for (int i = 0; i < Sources.Length; i++)
sources.Add (Sources [i].GetMetadata ("FullPath"));

if (!CompressionHelper.TryCompress (Log, zip, sources, false, workingDirectory, false))
if (!CompressionHelper.TryCompress (this, zip, sources, false, workingDirectory, false))
return false;

return !Log.HasLoggedErrors;
Expand Down
Loading