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] Make the GetFileSystemEntries task actually cancellable. #20209

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions msbuild/Xamarin.Localization.MSBuild/MSBStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1533,4 +1533,8 @@
{1}: the exit code of a process
</comment>
</data>

<data name="E7119" xml:space="preserve">
<value>Task execution was cancelled.</value>
</data>
</root>
14 changes: 12 additions & 2 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/GetFileSystemEntries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

using Xamarin.Localization.MSBuild;
using Xamarin.Messaging.Build.Client;

#nullable enable
Expand Down Expand Up @@ -38,6 +39,8 @@ public class GetFileSystemEntries : XamarinTask, ICancelableTask, ITaskCallback

#endregion

bool cancelled;

public override bool Execute ()
{
if (ShouldExecuteRemotely ())
Expand All @@ -48,10 +51,16 @@ public override bool Execute ()
foreach (var item in DirectoryPath) {
var path = item.ItemSpec.TrimEnd ('\\', '/');
var entriesFullPath = IncludeDirectories ?
Directory.GetFileSystemEntries (path, Pattern, searchOption) :
Directory.GetFiles (path, Pattern, searchOption);
Directory.EnumerateFileSystemEntries (path, Pattern, searchOption) :
Directory.EnumerateFiles (path, Pattern, searchOption);

Log.LogMessage (MessageImportance.Low, $"Searching for {(IncludeDirectories ? "files and directories" : "files")} in {path} with pattern '{Pattern}' and search option {searchOption}. Current directory: {Environment.CurrentDirectory}");

foreach (var entry in entriesFullPath) {
if (cancelled) {
Log.LogError (MSBStrings.E7119 /* Task execution was cancelled. */);
return false;
}
var recursiveDir = entry.Substring (path.Length + 1);
var newItem = new TaskItem (entry);
item.CopyMetadataTo (newItem);
Expand All @@ -70,6 +79,7 @@ public void Cancel ()
{
if (ShouldExecuteRemotely ())
BuildConnection.CancelAsync (BuildEngine4).Wait ();
cancelled = true;
}

public IEnumerable<ITaskItem> GetAdditionalItemsToBeCopied ()
Expand Down
Loading