-
Notifications
You must be signed in to change notification settings - Fork 28
/
CommandExtensions.cs
69 lines (61 loc) · 2.54 KB
/
CommandExtensions.cs
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
#nullable enable
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 async Task ExecuteDotNetApplicationAsync(
this Command command,
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."))
{
return;
}
if (enumerator.Current 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(),
}));
}