-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathEventStreamCommandExtensions.cs
172 lines (153 loc) · 6.44 KB
/
EventStreamCommandExtensions.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CliWrap.Utils;
namespace CliWrap.EventStream;
/// <summary>
/// Event stream execution model.
/// </summary>
public static class EventStreamCommandExtensions
{
/// <summary>
/// Executes the command as an asynchronous (pull-based) event stream.
/// </summary>
/// <remarks>
/// Use pattern matching to handle specific instances of <see cref="CommandEvent"/>.
/// </remarks>
public static async IAsyncEnumerable<CommandEvent> ListenAsync(
this Command command,
Encoding standardOutputEncoding,
Encoding standardErrorEncoding,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
using var channel = new Channel<CommandEvent>();
var stdOutPipe = PipeTarget.Merge(
command.StandardOutputPipe,
PipeTarget.ToDelegate(
s => channel.PublishAsync(new StandardOutputCommandEvent(s), cancellationToken),
standardOutputEncoding)
);
var stdErrPipe = PipeTarget.Merge(
command.StandardErrorPipe,
PipeTarget.ToDelegate(
s => channel.PublishAsync(new StandardErrorCommandEvent(s), cancellationToken),
standardErrorEncoding)
);
var commandPiped = command
.WithStandardOutputPipe(stdOutPipe)
.WithStandardErrorPipe(stdErrPipe);
var commandTask = commandPiped.ExecuteAsync(cancellationToken);
yield return new StartedCommandEvent(commandTask.ProcessId);
// Don't pass cancellation token to continuation because we need it to always trigger
// regardless of how the task completed.
_ = commandTask
.Task
.ContinueWith(_ => channel.Close(), TaskContinuationOptions.None);
await foreach (var cmdEvent in channel.ReceiveAsync(cancellationToken).ConfigureAwait(false))
{
yield return cmdEvent;
}
var exitCode = await commandTask.Select(r => r.ExitCode).ConfigureAwait(false);
yield return new ExitedCommandEvent(exitCode);
}
/// <summary>
/// Executes the command as an asynchronous (pull-based) event stream.
/// </summary>
/// <remarks>
/// Use pattern matching to handle specific instances of <see cref="CommandEvent"/>.
/// </remarks>
public static IAsyncEnumerable<CommandEvent> ListenAsync(
this Command command,
Encoding encoding,
CancellationToken cancellationToken = default) =>
command.ListenAsync(encoding, encoding, cancellationToken);
/// <summary>
/// Executes the command as an asynchronous (pull-based) event stream.
/// Uses <see cref="Console.OutputEncoding"/> to decode the byte stream.
/// </summary>
/// <remarks>
/// Use pattern matching to handle specific instances of <see cref="CommandEvent"/>.
/// </remarks>
public static IAsyncEnumerable<CommandEvent> ListenAsync(
this Command command,
CancellationToken cancellationToken = default) =>
command.ListenAsync(Console.OutputEncoding, cancellationToken);
/// <summary>
/// Executes the command as an observable (push-based) event stream.
/// </summary>
/// <remarks>
/// Use pattern matching to handle specific instances of <see cref="CommandEvent"/>.
/// </remarks>
public static IObservable<CommandEvent> Observe(
this Command command,
Encoding standardOutputEncoding,
Encoding standardErrorEncoding,
CancellationToken cancellationToken = default) =>
Observable.Create<CommandEvent>(observer =>
{
var stdOutPipe = PipeTarget.Merge(
command.StandardOutputPipe,
PipeTarget.ToDelegate(
s => observer.OnNext(new StandardOutputCommandEvent(s)),
standardOutputEncoding)
);
var stdErrPipe = PipeTarget.Merge(
command.StandardErrorPipe,
PipeTarget.ToDelegate(
s => observer.OnNext(new StandardErrorCommandEvent(s)),
standardErrorEncoding)
);
var commandPiped = command
.WithStandardOutputPipe(stdOutPipe)
.WithStandardErrorPipe(stdErrPipe);
var commandTask = commandPiped.ExecuteAsync(cancellationToken);
observer.OnNext(new StartedCommandEvent(commandTask.ProcessId));
// Don't pass cancellation token to continuation because we need it to always trigger
// regardless of how the task completed.
_ = commandTask
.Task
.ContinueWith(t =>
{
// Canceled tasks don't have exception
if (t.IsCanceled)
{
observer.OnError(new OperationCanceledException("Command execution has been canceled."));
}
else if (t.Exception is null)
{
observer.OnNext(new ExitedCommandEvent(t.Result.ExitCode));
observer.OnCompleted();
}
else
{
observer.OnError(t.Exception);
}
}, TaskContinuationOptions.None);
return Disposable.Null;
});
/// <summary>
/// Executes the command as an observable (push-based) event stream.
/// </summary>
/// <remarks>
/// Use pattern matching to handle specific instances of <see cref="CommandEvent"/>.
/// </remarks>
public static IObservable<CommandEvent> Observe(
this Command command,
Encoding encoding,
CancellationToken cancellationToken = default) =>
command.Observe(encoding, encoding, cancellationToken);
/// <summary>
/// Executes the command as an observable (push-based) event stream.
/// Uses <see cref="Console.OutputEncoding"/> to decode the byte stream.
/// </summary>
/// <remarks>
/// Use pattern matching to handle specific instances of <see cref="CommandEvent"/>.
/// </remarks>
public static IObservable<CommandEvent> Observe(
this Command command,
CancellationToken cancellationToken = default) =>
command.Observe(Console.OutputEncoding, cancellationToken);
}