cancel msbuild action

This commit is contained in:
Matt Parker
2026-01-17 15:03:44 +10:00
parent 08cd10dfbc
commit 7f603ddb7a
5 changed files with 27 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
using Microsoft.Extensions.Logging;
using SharpIDE.Application.Features.Events;
using SharpIDE.Application.Features.Logging;
namespace SharpIDE.Application.Features.Build;
@@ -19,10 +20,14 @@ public class BuildService(ILogger<BuildService> logger)
{
private readonly ILogger<BuildService> _logger = logger;
public event Func<Task> BuildStarted = () => Task.CompletedTask;
public EventWrapper<Task> BuildStarted { get; } = new(() => Task.CompletedTask);
public EventWrapper<Task> BuildFinished { get; } = new(() => Task.CompletedTask);
public ChannelTextWriter BuildTextWriter { get; } = new ChannelTextWriter();
private CancellationTokenSource? _cancellationTokenSource;
public async Task MsBuildAsync(string solutionOrProjectFilePath, BuildType buildType = BuildType.Build, CancellationToken cancellationToken = default)
{
if (_cancellationTokenSource is not null) throw new InvalidOperationException("A build is already in progress.");
_cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
using var _ = SharpIdeOtel.Source.StartActivity($"{nameof(BuildService)}.{nameof(MsBuildAsync)}");
var terminalLogger = InternalTerminalLoggerFactory.CreateLogger(BuildTextWriter);
@@ -50,13 +55,22 @@ public class BuildService(ILogger<BuildService> logger)
hostServices: null,
flags: BuildRequestDataFlags.None);
await BuildStarted.Invoke().ConfigureAwait(false);
BuildStarted.InvokeParallelFireAndForget();
var timer = Stopwatch.StartNew();
var buildResult = await BuildManager.DefaultBuildManager.BuildAsync(buildParameters, buildRequest, cancellationToken).ConfigureAwait(false);
var buildResult = await BuildManager.DefaultBuildManager.BuildAsync(buildParameters, buildRequest, _cancellationTokenSource.Token).ConfigureAwait(false);
timer.Stop();
BuildFinished.InvokeParallelFireAndForget();
_cancellationTokenSource = null;
_logger.LogInformation(buildResult.Exception, "Build result: {BuildResult} in {ElapsedMilliseconds}ms", buildResult.OverallResult, timer.ElapsedMilliseconds);
}
public async Task CancelBuildAsync()
{
if (_cancellationTokenSource is null) throw new InvalidOperationException("No build is in progress.");
await _cancellationTokenSource.CancelAsync();
_cancellationTokenSource = null;
}
private static string[] TargetsToBuild(BuildType buildType)
{
string[] targetsToBuild = buildType switch

View File

@@ -14,7 +14,7 @@ public partial class BuildPanel : Control
public override void _Ready()
{
_terminal = new Terminal(GetNode<Control>("%Terminal"));
_buildService.BuildStarted += OnBuildStarted;
_buildService.BuildStarted.Subscribe(OnBuildStarted);
}
public override void _Process(double delta)

View File

@@ -28,6 +28,7 @@ public partial class IdeRoot : Control
private Button _rebuildSlnButton = null!;
private Button _cleanSlnButton = null!;
private Button _restoreSlnButton = null!;
private TextureButton _cancelMsBuildActionButton = null!;
private SearchWindow _searchWindow = null!;
private SearchAllFilesWindow _searchAllFilesWindow = null!;
private CodeEditorPanel _codeEditorPanel = null!;
@@ -71,6 +72,7 @@ public partial class IdeRoot : Control
_rebuildSlnButton = GetNode<Button>("%RebuildSlnButton");
_cleanSlnButton = GetNode<Button>("%CleanSlnButton");
_restoreSlnButton = GetNode<Button>("%RestoreSlnButton");
_cancelMsBuildActionButton = GetNode<TextureButton>("%CancelMsBuildActionButton");
_runMenuPopup = GetNode<Popup>("%RunMenuPopup");
_runMenuButton = GetNode<Button>("%RunMenuButton");
_codeEditorPanel = GetNode<CodeEditorPanel>("%CodeEditorPanel");
@@ -88,6 +90,9 @@ public partial class IdeRoot : Control
_rebuildSlnButton.Pressed += OnRebuildSlnButtonPressed;
_cleanSlnButton.Pressed += OnCleanSlnButtonPressed;
_restoreSlnButton.Pressed += OnRestoreSlnButtonPressed;
_cancelMsBuildActionButton.Pressed += async () => await _buildService.CancelBuildAsync();
_buildService.BuildStarted.Subscribe(async () => await this.InvokeAsync(() => _cancelMsBuildActionButton.Disabled = false));
_buildService.BuildFinished.Subscribe(async () => await this.InvokeAsync(() => _cancelMsBuildActionButton.Disabled = true));
GodotGlobalEvents.Instance.BottomPanelVisibilityChangeRequested.Subscribe(async show => await this.InvokeAsync(() => _invertedVSplitContainer.InvertedSetCollapsed(!show)));
GetTree().GetRoot().FocusExited += OnFocusExited;
_nodeReadyTcs.SetResult();

View File

@@ -99,7 +99,8 @@ layout_mode = 2
size_flags_vertical = 4
text = "Restore"
[node name="TextureButton" type="TextureButton" parent="VBoxContainer/Panel/HBoxContainer"]
[node name="CancelMsBuildActionButton" type="TextureButton" parent="VBoxContainer/Panel/HBoxContainer"]
unique_name_in_owner = true
custom_minimum_size = Vector2(21, 18)
layout_mode = 2
size_flags_vertical = 4

View File

@@ -11,7 +11,7 @@
protected override async Task OnInitializedAsync()
{
BuildService.BuildStarted += ClearPreviousOutput;
BuildService.BuildStarted.Subscribe(ClearPreviousOutput);
_ = Task.Run(async () =>
{
try
@@ -34,5 +34,5 @@
await InvokeAsync(StateHasChanged);
}
public void Dispose() => BuildService.BuildStarted -= ClearPreviousOutput;
public void Dispose() => BuildService.BuildStarted.Unsubscribe(ClearPreviousOutput);
}