Skip to content

Commit

Permalink
Adding ExecuteUntilOutputAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
Piedone committed Dec 29, 2024
1 parent 770df18 commit dbadbff
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Lombiq.HelpfulLibraries.Cli/Extensions/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@ public static class CommandExtensions
/// Executes a <see cref="Command"/> as a <c>dotnet</c> command that starts a long-running application, and waits
/// for the app to be started.
/// </summary>
public static async Task ExecuteDotNetApplicationAsync(
public static Task ExecuteDotNetApplicationAsync(
this Command command,
Action<StandardErrorCommandEvent>? stdErrHandler = default,
CancellationToken cancellationToken = default) =>
command.ExecuteUntilOutputAsync("Application started. Press Ctrl+C to shut down.", stdErrHandler, cancellationToken);

/// <summary>
/// Executes a <see cref="Command"/> until the given output is received, then returns.
/// </summary>
public static async Task ExecuteUntilOutputAsync(
this Command command,
string outputToWaitFor,
Action<StandardErrorCommandEvent>? 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;
}
Expand Down

0 comments on commit dbadbff

Please sign in to comment.