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.
This commit is contained in:
qwsdcvghyu89
2025-09-27 16:26:12 +10:00
parent 093da5d0f3
commit 6904e0cfd8
3 changed files with 26 additions and 5 deletions
+1
View File
@@ -0,0 +1 @@
bin/
+24 -5
View File
@@ -250,9 +250,9 @@ public static class Program {
};
string processError = "";
var exitCode = AnsiConsole.Status()
var exitCode = await AnsiConsole.Status()
.Spinner(Spinner.Known.Dots)
.Start<int>("Creating package with 'dotnet pack' ", ctx => {
.StartAsync<int>("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);
}
@@ -5,6 +5,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>