-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathCommandExtensions.cs
More file actions
75 lines (68 loc) · 2.88 KB
/
CommandExtensions.cs
File metadata and controls
75 lines (68 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using CliWrap.Builders;
using CliWrap.EventStream;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace CliWrap;
public static class CommandExtensions
{
/// <summary>
/// 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 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 foreach (var commandEvent in command.ListenAsync(cancellationToken))
{
if (commandEvent is StandardOutputCommandEvent stdOut &&
stdOut.Text.ContainsOrdinalIgnoreCase(outputToWaitFor))
{
return;
}
if (commandEvent is StandardErrorCommandEvent stdErr)
{
stdErrHandler?.Invoke(stdErr);
}
}
}
/// <summary>
/// Same as <see cref="Command.WithArguments(Action{ArgumentsBuilder})"/>, but the configuration delegate is
/// asynchronous.
/// </summary>
public static async Task<Command> WithArgumentsAsync(this Command command, Func<ArgumentsBuilder, Task> configureAsync)
{
var builder = new ArgumentsBuilder();
await configureAsync(builder);
return command.WithArguments(builder.Build());
}
/// <summary>
/// Same as <see cref="Command.WithEnvironmentVariables(IReadOnlyDictionary{string,string?})"/>, but the values can
/// be any type. If a value is <see cref="IConvertible"/>, like all C# primitive types then <see
/// cref="NumberExtensions.ToTechnicalString(IConvertible)"/> is called, otherwise <see cref="object.ToString"/>.
/// </summary>
public static Command WithEnvironmentVariables(this Command command, IEnumerable<KeyValuePair<string?, object?>> variables) =>
command.WithEnvironmentVariables(variables
.Where(pair => pair is { Key: { } })
.ToDictionary(
pair => pair.Key!,
pair => pair.Value switch
{
string text => text,
IConvertible convertible => convertible.ToTechnicalString(),
_ => pair.Value?.ToString(),
}));
}