Skip to content

Commit

Permalink
[msbuild] Port the OptimizePropertyLists 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 'altool' (partial fix for #3931).
* Allow for overriding the path to the command-line tool in question.
* Add support for cancellation.
* Fix nullability.
  • Loading branch information
rolfbjarne committed Nov 13, 2024
1 parent 9ea3cff commit 3217932
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
70 changes: 34 additions & 36 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/OptimizePropertyList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,65 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;

using Xamarin.Messaging.Build.Client;

// Disable until we get around to enable + fix any issues.
#nullable disable
#nullable enable

namespace Xamarin.MacDev.Tasks {
public class OptimizePropertyList : XamarinToolTask, ITaskCallback {
public class OptimizePropertyList : XamarinTask, ICancelableTask {
CancellationTokenSource? cancellationTokenSource;
#region Inputs

[Required]
public ITaskItem Input { get; set; }
public ITaskItem? Input { get; set; }

[Required]
[Output]
public ITaskItem Output { get; set; }
public ITaskItem? Output { get; set; }

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

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

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

const string path = "/usr/bin/plutil";

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

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

args.AppendSwitch ("-convert");
args.AppendSwitch ("binary1");
args.AppendSwitch ("-o");
args.AppendFileNameIfNotNull (Output.ItemSpec);
args.AppendFileNameIfNotNull (Input.ItemSpec);
args.Add ("-convert");
args.Add ("binary1");
args.Add ("-o");
args.Add (Output!.ItemSpec);
args.Add (Input!.ItemSpec);

return args.ToString ();
}

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

public override bool Execute ()
{
if (ShouldExecuteRemotely ())
return new TaskRunner (SessionId, BuildEngine4).RunAsync (this).Result;

Directory.CreateDirectory (Path.GetDirectoryName (Output.ItemSpec));

return base.Execute ();
Directory.CreateDirectory (Path.GetDirectoryName (Output!.ItemSpec));
var args = GenerateCommandLineCommands ();
var executable = GetExecutable (args, "plutil", PlutilPath);
cancellationTokenSource = new CancellationTokenSource ();
ExecuteAsync (Log, executable, args, cancellationToken: cancellationTokenSource.Token).Wait ();
return !Log.HasLoggedErrors;
}

public bool ShouldCopyToBuildServer (Microsoft.Build.Framework.ITaskItem item) => false;
Expand All @@ -72,12 +69,13 @@ public override bool Execute ()

public IEnumerable<ITaskItem> GetAdditionalItemsToBeCopied () => Enumerable.Empty<ITaskItem> ();

public override void Cancel ()
public void Cancel ()
{
base.Cancel ();

if (!string.IsNullOrEmpty (SessionId))
if (!string.IsNullOrEmpty (SessionId)) {
BuildConnection.CancelAsync (BuildEngine4).Wait ();
} else {
cancellationTokenSource?.Cancel ();
}
}
}
}
6 changes: 2 additions & 4 deletions msbuild/Xamarin.Shared/Xamarin.Shared.targets
Original file line number Diff line number Diff line change
Expand Up @@ -1619,8 +1619,7 @@ Copyright (C) 2018 Microsoft. All rights reserved.
<OptimizePropertyList
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true'"
ToolExe="$(PlUtilExe)"
ToolPath="$(PlUtilPath)"
PlutilPath="$(PlutilPath)"
Input="%(_PropertyList.Identity)"
Output="$(DeviceSpecificIntermediateOutputPath)optimized\%(_PropertyList.LogicalName)">
</OptimizePropertyList>
Expand Down Expand Up @@ -1666,8 +1665,7 @@ Copyright (C) 2018 Microsoft. All rights reserved.
<OptimizePropertyList
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true'"
ToolExe="$(PlUtilExe)"
ToolPath="$(PlUtilPath)"
PlutilPath="$(PlutilPath)"
Input="%(_LocalizationFile.Identity)"
Output="$(DeviceSpecificIntermediateOutputPath)optimized\%(_LocalizationFile.LogicalName)">
</OptimizePropertyList>
Expand Down

0 comments on commit 3217932

Please sign in to comment.