Skip to content

Commit

Permalink
[msbuild] Port the CreateAssetPack task to subclass XamarinTask.
Browse files Browse the repository at this point in the history
This has a few advantages:

* We simplify and unify more of our code.
* We have more control over the error reporting / logging behavior.

Additionally:

* Use 'xcrun' to invoke 'zip' (partial fix for #3931).
* Allow for overriding the path to the command-line tool in question.
* Add support for cancellation.
  • Loading branch information
rolfbjarne committed Nov 28, 2024
1 parent 37d647b commit 8046395
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
64 changes: 34 additions & 30 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/CreateAssetPack.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
using System;
using System.Collections.Generic;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

using Xamarin.MacDev;
using Xamarin.MacDev.Tasks;
using Xamarin.Messaging.Build.Client;

#nullable enable

namespace Xamarin.MacDev.Tasks {
public class CreateAssetPack : XamarinToolTask {
public class CreateAssetPack : XamarinTask, ICancelableTask {
CancellationTokenSource? cancellationTokenSource;

#region Inputs

[Required]
Expand All @@ -18,34 +24,32 @@ public class CreateAssetPack : XamarinToolTask {
[Required]
public ITaskItem? Source { get; set; }

#endregion
public string ZipPath { get; set; } = string.Empty;

protected override string ToolName {
get { return "zip"; }
}
#endregion

protected override string GenerateFullPathToTool ()
static string GetExecutable (List<string> arguments, string toolName, string toolPathOverride)
{
if (!string.IsNullOrEmpty (ToolPath))
return Path.Combine (ToolPath, ToolExe);

var path = Path.Combine ("/usr/bin", ToolExe);

return File.Exists (path) ? path : ToolExe;
if (string.IsNullOrEmpty (toolPathOverride)) {
arguments.Insert (0, toolName);
return "xcrun";
}
return toolPathOverride;
}

protected override string GetWorkingDirectory ()
protected string GetWorkingDirectory ()
{
return Source!.GetMetadata ("FullPath");
}

protected override string GenerateCommandLineCommands ()
List<string> GenerateCommandLineCommands ()
{
var args = new CommandLineArgumentBuilder ();
var args = new List<string> ();

args.Add ("-r", "-y");
args.AddQuoted (OutputFile!.GetMetadata ("FullPath"));
args.AddQuoted ("META-INF");
args.Add ("-r");
args.Add ("-y");
args.Add (OutputFile!.GetMetadata ("FullPath"));
args.Add ("META-INF");

long size = 0;
int count = 0;
Expand All @@ -64,18 +68,13 @@ protected override string GenerateCommandLineCommands ()
size += info.Length;
}

args.AddQuoted (Path.GetFileName (path));
args.Add (Path.GetFileName (path));
count++;
}

SaveMetaFile (count, size);

return args.ToString ();
}

protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
{
Log.LogMessage (messageImportance, "{0}", singleLine);
return args;
}

public override bool Execute ()
Expand All @@ -87,7 +86,11 @@ public override bool Execute ()
if (File.Exists (OutputFile!.ItemSpec))
File.Delete (OutputFile.ItemSpec);

return base.Execute ();
var args = GenerateCommandLineCommands ();
var executable = GetExecutable (args, "zip", ZipPath);
cancellationTokenSource = new CancellationTokenSource ();
ExecuteAsync (Log, executable, args, workingDirectory: GetWorkingDirectory (), cancellationToken: cancellationTokenSource.Token).Wait ();
return !Log.HasLoggedErrors;
}

void SaveMetaFile (int count, long size)
Expand All @@ -104,12 +107,13 @@ void SaveMetaFile (int count, long size)
meta.Save (Path.Combine (Source.ItemSpec, "META-INF", "com.apple.ZipMetadata.plist"), true, true);
}

public override void Cancel ()
public void Cancel ()
{
if (ShouldExecuteRemotely ())
if (ShouldExecuteRemotely ()) {
BuildConnection.CancelAsync (BuildEngine4).Wait ();

base.Cancel ();
} else {
cancellationTokenSource?.Cancel ();
}
}
}
}
3 changes: 1 addition & 2 deletions msbuild/Xamarin.Shared/Xamarin.iOS.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,9 @@ Copyright (C) 2013-2016 Xamarin. All rights reserved.
<CreateAssetPack
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true' And '$(EmbedOnDemandResources)' == 'false'"
ToolExe="$(ZipExe)"
ToolPath="$(ZipPath)"
Source="%(_AssetPack.Identity)"
OutputFile="$(IpaPackageDir)OnDemandResources\%(_AssetPack.DirectoryName)"
ZipPath="$(ZipPath)"
/>

<Copy
Expand Down

0 comments on commit 8046395

Please sign in to comment.