Skip to content

Commit

Permalink
[msbuild] Convert Ditto to a XamarinTask.
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfbjarne committed Sep 20, 2024
1 parent 3222d66 commit 5a64682
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 52 deletions.
66 changes: 28 additions & 38 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/Ditto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;

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

using Xamarin.Messaging.Build.Client;
using Xamarin.Utils;

namespace Xamarin.MacDev.Tasks {
public class Ditto : XamarinToolTask, ITaskCallback {
public class Ditto : XamarinTask, ICancelableTask {
#region Inputs

public string? AdditionalArguments { get; set; }
Expand Down Expand Up @@ -40,31 +42,7 @@ public class Ditto : XamarinToolTask, ITaskCallback {

#endregion

protected override string ToolName {
get { return "ditto"; }
}

protected override string GenerateFullPathToTool ()
{
if (!string.IsNullOrEmpty (ToolPath))
return Path.Combine (ToolPath, ToolExe);

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

return File.Exists (path) ? path : ToolExe;
}

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

args.AddQuoted (Path.GetFullPath (Source!.ItemSpec));
args.AddQuoted (Path.GetFullPath (Destination!.ItemSpec));
if (!string.IsNullOrEmpty (AdditionalArguments))
args.Add (AdditionalArguments);

return args.ToString ();
}
CancellationTokenSource? cancellationTokenSource;

public override bool Execute ()
{
Expand All @@ -76,8 +54,25 @@ public override bool Execute ()
return taskRunner.RunAsync (this).Result;
}

if (!base.Execute ())
return false;
var args = new List<string> ();
args.Add (Path.GetFullPath (Source!.ItemSpec));
args.Add (Path.GetFullPath (Destination!.ItemSpec));
#if NET
if (!string.IsNullOrEmpty (AdditionalArguments)) {
#else
if (AdditionalArguments is not null && !string.IsNullOrEmpty (AdditionalArguments)) {
#endif
if (StringUtils.TryParseArguments (AdditionalArguments, out var additionalArgs, out var ex)) {
args.AddRange (additionalArgs);
} else {
Log.LogError ("Unable to parse the AdditionalArguments: {0}", AdditionalArguments);
return false;
}
}

cancellationTokenSource = new CancellationTokenSource ();
cancellationTokenSource.CancelAfter (TimeSpan.FromMinutes (2)); // FIXME: remove this
ExecuteAsync (Log, "/usr/bin/ditto", args, cancellationToken: cancellationTokenSource.Token).Wait ();

// Create a list of all the files we've copied
var copiedFiles = new List<ITaskItem> ();
Expand All @@ -96,18 +91,13 @@ public override bool Execute ()
return !Log.HasLoggedErrors;
}

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

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

if (ShouldExecuteRemotely ())
if (ShouldExecuteRemotely ()) {
BuildConnection.CancelAsync (BuildEngine4).Wait ();
} else {
cancellationTokenSource?.Cancel ();
}
}

public IEnumerable<ITaskItem> GetAdditionalItemsToBeCopied ()
Expand Down
5 changes: 3 additions & 2 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/XamarinTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;

using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;
Expand Down Expand Up @@ -120,15 +121,15 @@ protected System.Threading.Tasks.Task<Execution> ExecuteAsync (string fileName,
return ExecuteAsync (Log, fileName, arguments, sdkDevPath, environment, mergeOutput, showErrorIfFailure, workingDirectory);
}

internal protected static async System.Threading.Tasks.Task<Execution> ExecuteAsync (TaskLoggingHelper log, string fileName, IList<string> arguments, string? sdkDevPath = null, Dictionary<string, string?>? environment = null, bool mergeOutput = true, bool showErrorIfFailure = true, string? workingDirectory = null)
internal protected static async System.Threading.Tasks.Task<Execution> ExecuteAsync (TaskLoggingHelper log, 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)
{
// Create a new dictionary if we're given one, to make sure we don't change the caller's dictionary.
var launchEnvironment = environment is null ? new Dictionary<string, string?> () : new Dictionary<string, string?> (environment);
if (!string.IsNullOrEmpty (sdkDevPath))
launchEnvironment ["DEVELOPER_DIR"] = sdkDevPath;

log.LogMessage (MessageImportance.Normal, MSBStrings.M0001, fileName, StringUtils.FormatArguments (arguments));
var rv = await Execution.RunAsync (fileName, arguments, environment: launchEnvironment, mergeOutput: mergeOutput, workingDirectory: workingDirectory);
var rv = await Execution.RunAsync (fileName, arguments, environment: launchEnvironment, mergeOutput: mergeOutput, workingDirectory: workingDirectory, cancellationToken: cancellationToken);
log.LogMessage (rv.ExitCode == 0 ? MessageImportance.Low : MessageImportance.High, MSBStrings.M0002, fileName, rv.ExitCode);

// Show the output
Expand Down
2 changes: 0 additions & 2 deletions msbuild/Xamarin.Shared/Xamarin.Shared.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2623,8 +2623,6 @@ Copyright (C) 2018 Microsoft. All rights reserved.
<Ditto
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true'"
ToolExe="$(DittoExe)"
ToolPath="$(DittoPath)"
Source="@(_ResolvedAppExtensionReferences)"
Destination="$(_AppExtensionRoot)%(_ResolvedAppExtensionReferences.ContainerName)\%(_ResolvedAppExtensionReferences.FileName)%(_ResolvedAppExtensionReferences.Extension)"
TouchDestinationFiles="true"
Expand Down
4 changes: 0 additions & 4 deletions msbuild/Xamarin.Shared/Xamarin.WatchOS.App.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ Copyright (C) 2015-2016 Xamarin. All rights reserved.
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)'"
AdditionalArguments="$(WKDittoArchitectures)"
ToolExe="$(DittoExe)"
ToolPath="$(DittoPath)"
Source="$(_NativeWatchApp)"
Destination="$(_AppBundlePath)$(AssemblyName)"
/>
Expand All @@ -93,8 +91,6 @@ Copyright (C) 2015-2016 Xamarin. All rights reserved.
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)'"
AdditionalArguments="$(WKDittoArchitectures)"
ToolExe="$(DittoExe)"
ToolPath="$(DittoPath)"
Source="$(_NativeWatchApp)"
Destination="$(_AppBundlePath)_WatchKitStub\WK"
/>
Expand Down
6 changes: 0 additions & 6 deletions msbuild/Xamarin.Shared/Xamarin.iOS.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,6 @@ Copyright (C) 2013-2016 Xamarin. All rights reserved.
<Ditto
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true' And '@(_ResolvedWatchAppReferences)' != '' And '%(_ResolvedWatchAppReferences.Identity)' != ''"
ToolExe="$(DittoExe)"
ToolPath="$(DittoPath)"
Source="@(_ResolvedWatchAppReferences)"
Destination="$(_AppBundlePath)Watch\%(_ResolvedWatchAppReferences.FileName)%(_ResolvedWatchAppReferences.Extension)"
/>
Expand All @@ -391,8 +389,6 @@ Copyright (C) 2013-2016 Xamarin. All rights reserved.
<Ditto
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true'"
ToolExe="$(DittoExe)"
ToolPath="$(DittoPath)"
Source="$(AppBundleDir)"
Destination="$(_IpaAppBundleDir)"
/>
Expand Down Expand Up @@ -481,8 +477,6 @@ Copyright (C) 2013-2016 Xamarin. All rights reserved.
<Ditto
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true' And Exists('$(DeviceSpecificOutputPath)OnDemandResources\')"
ToolExe="$(DittoExe)"
ToolPath="$(DittoPath)"
Source="$(DeviceSpecificOutputPath)OnDemandResources\"
Destination="$(_IntermediateODRDir)"
/>
Expand Down

0 comments on commit 5a64682

Please sign in to comment.