cancel msbuild action
This commit is contained in:
@@ -4,6 +4,7 @@ using Microsoft.Build.Execution;
|
|||||||
using Microsoft.Build.Framework;
|
using Microsoft.Build.Framework;
|
||||||
using Microsoft.Build.Logging;
|
using Microsoft.Build.Logging;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using SharpIDE.Application.Features.Events;
|
||||||
using SharpIDE.Application.Features.Logging;
|
using SharpIDE.Application.Features.Logging;
|
||||||
|
|
||||||
namespace SharpIDE.Application.Features.Build;
|
namespace SharpIDE.Application.Features.Build;
|
||||||
@@ -19,10 +20,14 @@ public class BuildService(ILogger<BuildService> logger)
|
|||||||
{
|
{
|
||||||
private readonly ILogger<BuildService> _logger = 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();
|
public ChannelTextWriter BuildTextWriter { get; } = new ChannelTextWriter();
|
||||||
|
private CancellationTokenSource? _cancellationTokenSource;
|
||||||
public async Task MsBuildAsync(string solutionOrProjectFilePath, BuildType buildType = BuildType.Build, CancellationToken cancellationToken = default)
|
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)}");
|
using var _ = SharpIdeOtel.Source.StartActivity($"{nameof(BuildService)}.{nameof(MsBuildAsync)}");
|
||||||
|
|
||||||
var terminalLogger = InternalTerminalLoggerFactory.CreateLogger(BuildTextWriter);
|
var terminalLogger = InternalTerminalLoggerFactory.CreateLogger(BuildTextWriter);
|
||||||
@@ -50,13 +55,22 @@ public class BuildService(ILogger<BuildService> logger)
|
|||||||
hostServices: null,
|
hostServices: null,
|
||||||
flags: BuildRequestDataFlags.None);
|
flags: BuildRequestDataFlags.None);
|
||||||
|
|
||||||
await BuildStarted.Invoke().ConfigureAwait(false);
|
BuildStarted.InvokeParallelFireAndForget();
|
||||||
var timer = Stopwatch.StartNew();
|
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();
|
timer.Stop();
|
||||||
|
BuildFinished.InvokeParallelFireAndForget();
|
||||||
|
_cancellationTokenSource = null;
|
||||||
_logger.LogInformation(buildResult.Exception, "Build result: {BuildResult} in {ElapsedMilliseconds}ms", buildResult.OverallResult, timer.ElapsedMilliseconds);
|
_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)
|
private static string[] TargetsToBuild(BuildType buildType)
|
||||||
{
|
{
|
||||||
string[] targetsToBuild = buildType switch
|
string[] targetsToBuild = buildType switch
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ public partial class BuildPanel : Control
|
|||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
_terminal = new Terminal(GetNode<Control>("%Terminal"));
|
_terminal = new Terminal(GetNode<Control>("%Terminal"));
|
||||||
_buildService.BuildStarted += OnBuildStarted;
|
_buildService.BuildStarted.Subscribe(OnBuildStarted);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _Process(double delta)
|
public override void _Process(double delta)
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ public partial class IdeRoot : Control
|
|||||||
private Button _rebuildSlnButton = null!;
|
private Button _rebuildSlnButton = null!;
|
||||||
private Button _cleanSlnButton = null!;
|
private Button _cleanSlnButton = null!;
|
||||||
private Button _restoreSlnButton = null!;
|
private Button _restoreSlnButton = null!;
|
||||||
|
private TextureButton _cancelMsBuildActionButton = null!;
|
||||||
private SearchWindow _searchWindow = null!;
|
private SearchWindow _searchWindow = null!;
|
||||||
private SearchAllFilesWindow _searchAllFilesWindow = null!;
|
private SearchAllFilesWindow _searchAllFilesWindow = null!;
|
||||||
private CodeEditorPanel _codeEditorPanel = null!;
|
private CodeEditorPanel _codeEditorPanel = null!;
|
||||||
@@ -71,6 +72,7 @@ public partial class IdeRoot : Control
|
|||||||
_rebuildSlnButton = GetNode<Button>("%RebuildSlnButton");
|
_rebuildSlnButton = GetNode<Button>("%RebuildSlnButton");
|
||||||
_cleanSlnButton = GetNode<Button>("%CleanSlnButton");
|
_cleanSlnButton = GetNode<Button>("%CleanSlnButton");
|
||||||
_restoreSlnButton = GetNode<Button>("%RestoreSlnButton");
|
_restoreSlnButton = GetNode<Button>("%RestoreSlnButton");
|
||||||
|
_cancelMsBuildActionButton = GetNode<TextureButton>("%CancelMsBuildActionButton");
|
||||||
_runMenuPopup = GetNode<Popup>("%RunMenuPopup");
|
_runMenuPopup = GetNode<Popup>("%RunMenuPopup");
|
||||||
_runMenuButton = GetNode<Button>("%RunMenuButton");
|
_runMenuButton = GetNode<Button>("%RunMenuButton");
|
||||||
_codeEditorPanel = GetNode<CodeEditorPanel>("%CodeEditorPanel");
|
_codeEditorPanel = GetNode<CodeEditorPanel>("%CodeEditorPanel");
|
||||||
@@ -88,6 +90,9 @@ public partial class IdeRoot : Control
|
|||||||
_rebuildSlnButton.Pressed += OnRebuildSlnButtonPressed;
|
_rebuildSlnButton.Pressed += OnRebuildSlnButtonPressed;
|
||||||
_cleanSlnButton.Pressed += OnCleanSlnButtonPressed;
|
_cleanSlnButton.Pressed += OnCleanSlnButtonPressed;
|
||||||
_restoreSlnButton.Pressed += OnRestoreSlnButtonPressed;
|
_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)));
|
GodotGlobalEvents.Instance.BottomPanelVisibilityChangeRequested.Subscribe(async show => await this.InvokeAsync(() => _invertedVSplitContainer.InvertedSetCollapsed(!show)));
|
||||||
GetTree().GetRoot().FocusExited += OnFocusExited;
|
GetTree().GetRoot().FocusExited += OnFocusExited;
|
||||||
_nodeReadyTcs.SetResult();
|
_nodeReadyTcs.SetResult();
|
||||||
|
|||||||
@@ -99,7 +99,8 @@ layout_mode = 2
|
|||||||
size_flags_vertical = 4
|
size_flags_vertical = 4
|
||||||
text = "Restore"
|
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)
|
custom_minimum_size = Vector2(21, 18)
|
||||||
layout_mode = 2
|
layout_mode = 2
|
||||||
size_flags_vertical = 4
|
size_flags_vertical = 4
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
BuildService.BuildStarted += ClearPreviousOutput;
|
BuildService.BuildStarted.Subscribe(ClearPreviousOutput);
|
||||||
_ = Task.Run(async () =>
|
_ = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -34,5 +34,5 @@
|
|||||||
await InvokeAsync(StateHasChanged);
|
await InvokeAsync(StateHasChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() => BuildService.BuildStarted -= ClearPreviousOutput;
|
public void Dispose() => BuildService.BuildStarted.Unsubscribe(ClearPreviousOutput);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user