Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[msbuild] Port the CreateAssetPack task to subclass XamarinTask. #21713

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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