From 6904e0cfd86f8a740f7f9ecd610564ad299fae42 Mon Sep 17 00:00:00 2001 From: qwsdcvghyu89 <61093706+qwsdcvghyu89@users.noreply.github.com> Date: Sat, 27 Sep 2025 16:26:12 +1000 Subject: [PATCH] Improve process handling and async support in publish tool Refactored process execution to use async methods and cancellation tokens for better responsiveness, especially when handling 'dotnet pack' and 'dotnet nuget push' commands. Updated .csproj to use C# preview language features and added 'bin/' to .gitignore. --- aeqw89.tools.Publish/.gitignore | 1 + aeqw89.tools.Publish/Program.cs | 29 +++++++++++++++---- .../aeqw89.tools.Publish.csproj | 1 + 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/aeqw89.tools.Publish/.gitignore b/aeqw89.tools.Publish/.gitignore index e69de29..6dd29b7 100644 --- a/aeqw89.tools.Publish/.gitignore +++ b/aeqw89.tools.Publish/.gitignore @@ -0,0 +1 @@ +bin/ \ No newline at end of file diff --git a/aeqw89.tools.Publish/Program.cs b/aeqw89.tools.Publish/Program.cs index 9a30d17..6dec9d3 100644 --- a/aeqw89.tools.Publish/Program.cs +++ b/aeqw89.tools.Publish/Program.cs @@ -250,9 +250,9 @@ public static class Program { }; string processError = ""; - var exitCode = AnsiConsole.Status() + var exitCode = await AnsiConsole.Status() .Spinner(Spinner.Known.Dots) - .Start("Creating package with 'dotnet pack' ", ctx => { + .StartAsync("Creating package with 'dotnet pack' ", async ctx => { var p = Process.Start(new ProcessStartInfo() { FileName = "dotnet", Arguments = $"pack -o {outDir}", @@ -261,7 +261,17 @@ public static class Program { RedirectStandardOutput = !Verbose, RedirectStandardError = !Verbose }); - p?.WaitForExit(); + + CancellationTokenSource cts = new CancellationTokenSource(); + p?.ErrorDataReceived += (sender, eventArgs) => { + cts.Cancel(); + }; + p?.OutputDataReceived += (sender, eventArgs) => { + if (eventArgs.Data?.ToLower().Contains("press any key") == true) + cts.Cancel(); + }; + + await (p?.WaitForExitAsync(cts.Token) ?? Task.CompletedTask); processError = p?.StandardError?.ReadToEnd() ?? ""; return p?.ExitCode ?? -1; }); @@ -416,17 +426,26 @@ public static class Program { RedirectStandardError = !Verbose }); + var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); + p?.ErrorDataReceived += (sender, eventArgs) => { + cts.Cancel(); + }; + p?.OutputDataReceived += (sender, eventArgs) => { + if (eventArgs.Data?.ToLower().Contains("press any key") == true) + cts.Cancel(); + }; + if (p == null) { ShowError(Exceptions.generic_error.EscapeMarkup()); } task.Increment(size / 2); if (p != null) - await p.WaitForExitAsync(ct); + await p.WaitForExitAsync(cts.Token); processError += p?.StandardError?.ReadToEnd() ?? ""; if (p?.ExitCode != 0) { ShowError(processError.EscapeMarkup()); - ShowError(Exceptions.dotnet_nuget_push_failure, p.ExitCode); + ShowError(Exceptions.dotnet_nuget_push_failure, p?.ExitCode ?? -1); } task.Increment(size / 2); } diff --git a/aeqw89.tools.Publish/aeqw89.tools.Publish.csproj b/aeqw89.tools.Publish/aeqw89.tools.Publish.csproj index b777737..d31429e 100644 --- a/aeqw89.tools.Publish/aeqw89.tools.Publish.csproj +++ b/aeqw89.tools.Publish/aeqw89.tools.Publish.csproj @@ -5,6 +5,7 @@ net9.0 enable enable + preview