Skip to content

Commit

Permalink
[msbuild] Port the SpotlightIndexer 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 377f0f0 commit c785d4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
6 changes: 6 additions & 0 deletions docs/build-apps/build-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ Example:

This property was introduced in .NET 9.

## MdimportPath

The full path to the `mdimport` tool.

The default behavior is to use `xcrun mdimport`.

## MetalLibPath

The full path to the `metallib` tool (the Metal Linker).
Expand Down
56 changes: 29 additions & 27 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/SpotlightIndexer.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

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

using Xamarin.Messaging.Build.Client;

#nullable enable

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

[Required]
public string Input { get; set; } = string.Empty;

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

protected override string ToolName {
get { return "mdimport"; }
}
#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 GenerateCommandLineCommands ()
List<string> GenerateCommandLineCommands ()
{
var args = new CommandLineBuilder ();
var args = new List<string> ();

args.AppendFileNameIfNotNull (Input);
args.Add (Input);

return args.ToString ();
}

protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
{
// TODO: do proper parsing of error messages and such
Log.LogMessage (messageImportance, "{0}", singleLine);
return args;
}

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

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

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

base.Execute ();
} else {
cancellationTokenSource?.Cancel ();
}
}
}
}
1 change: 1 addition & 0 deletions msbuild/Xamarin.Shared/Xamarin.Shared.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2962,6 +2962,7 @@ Copyright (C) 2018 Microsoft. All rights reserved.
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true'"
Input="$(_AppContainerDir)"
MdimportPath="$(MdimportPath)"
/>
</Target>

Expand Down

0 comments on commit c785d4b

Please sign in to comment.