From dbadbffe419fe7ff0bbb5f0c6d5ba4b8a477bbad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Sun, 29 Dec 2024 05:31:38 +0100 Subject: [PATCH] Adding ExecuteUntilOutputAsync --- .../Extensions/CommandExtensions.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Lombiq.HelpfulLibraries.Cli/Extensions/CommandExtensions.cs b/Lombiq.HelpfulLibraries.Cli/Extensions/CommandExtensions.cs index d103876e..24b95e59 100644 --- a/Lombiq.HelpfulLibraries.Cli/Extensions/CommandExtensions.cs +++ b/Lombiq.HelpfulLibraries.Cli/Extensions/CommandExtensions.cs @@ -16,17 +16,26 @@ public static class CommandExtensions /// Executes a as a dotnet command that starts a long-running application, and waits /// for the app to be started. /// - public static async Task ExecuteDotNetApplicationAsync( + public static Task ExecuteDotNetApplicationAsync( this Command command, Action? stdErrHandler = default, + CancellationToken cancellationToken = default) => + command.ExecuteUntilOutputAsync("Application started. Press Ctrl+C to shut down.", stdErrHandler, cancellationToken); + + /// + /// Executes a until the given output is received, then returns. + /// + public static async Task ExecuteUntilOutputAsync( + this Command command, + string outputToWaitFor, + Action? stdErrHandler = default, CancellationToken cancellationToken = default) { await using var enumerator = command.ListenAsync(cancellationToken).GetAsyncEnumerator(cancellationToken); while (await enumerator.MoveNextAsync()) { - if (enumerator.Current is StandardOutputCommandEvent stdOut && - stdOut.Text.ContainsOrdinalIgnoreCase("Application started. Press Ctrl+C to shut down.")) + if (enumerator.Current is StandardOutputCommandEvent stdOut && stdOut.Text.ContainsOrdinalIgnoreCase(outputToWaitFor)) { return; }