-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAsyncCmdlet.cs
More file actions
252 lines (200 loc) · 8.29 KB
/
AsyncCmdlet.cs
File metadata and controls
252 lines (200 loc) · 8.29 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
namespace Kubectl {
/// <summary>
/// Base class for Cmdlets that run asynchronously.
/// </summary>
/// <remarks>
/// Inherit from this class if your Cmdlet needs to use <c>async</c> / <c>await</c> functionality.
/// </remarks>
public abstract class AsyncCmdlet
: PSCmdlet, IDisposable {
/// <summary>
/// The source for cancellation tokens that can be used to cancel the operation.
/// </summary>
readonly CancellationTokenSource _cancellationSource = new CancellationTokenSource();
/// <summary>
/// Initialise the <see cref="AsyncCmdlet"/>.
/// </summary>
protected AsyncCmdlet() {
}
/// <summary>
/// Finaliser for <see cref="AsyncCmdlet"/>.
/// </summary>
~AsyncCmdlet() {
Dispose(false);
}
/// <summary>
/// Dispose of resources being used by the Cmdlet.
/// </summary>
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Dispose of resources being used by the Cmdlet.
/// </summary>
/// <param name="disposing">
/// Explicit disposal?
/// </param>
protected virtual void Dispose(bool disposing) {
if (disposing)
_cancellationSource.Dispose();
}
/// <summary>
/// Asynchronously perform Cmdlet pre-processing.
/// </summary>
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation.
/// </returns>
protected virtual Task BeginProcessingAsync() {
return BeginProcessingAsync(_cancellationSource.Token);
}
/// <summary>
/// Asynchronously perform Cmdlet pre-processing.
/// </summary>
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> that can be used to cancel the asynchronous operation.
/// </param>
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation.
/// </returns>
protected virtual Task BeginProcessingAsync(CancellationToken cancellationToken) {
return Task.CompletedTask;
}
/// <summary>
/// Asynchronously perform Cmdlet processing.
/// </summary>
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation.
/// </returns>
protected virtual Task ProcessRecordAsync() {
return ProcessRecordAsync(_cancellationSource.Token);
}
/// <summary>
/// Asynchronously perform Cmdlet processing.
/// </summary>
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> that can be used to cancel the asynchronous operation.
/// </param>
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation.
/// </returns>
protected virtual Task ProcessRecordAsync(CancellationToken cancellationToken) {
return Task.CompletedTask;
}
/// <summary>
/// Asynchronously perform Cmdlet post-processing.
/// </summary>
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation.
/// </returns>
protected virtual Task EndProcessingAsync() {
return EndProcessingAsync(_cancellationSource.Token);
}
/// <summary>
/// Asynchronously perform Cmdlet post-processing.
/// </summary>
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> that can be used to cancel the asynchronous operation.
/// </param>
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation.
/// </returns>
protected virtual Task EndProcessingAsync(CancellationToken cancellationToken) {
return Task.CompletedTask;
}
/// <summary>
/// Perform Cmdlet pre-processing.
/// </summary>
protected sealed override void BeginProcessing() {
ThreadAffinitiveSynchronizationContext.RunSynchronized(
() => BeginProcessingAsync()
);
}
/// <summary>
/// Perform Cmdlet processing.
/// </summary>
protected sealed override void ProcessRecord() {
ThreadAffinitiveSynchronizationContext.RunSynchronized(
() => ProcessRecordAsync()
);
}
/// <summary>
/// Perform Cmdlet post-processing.
/// </summary>
protected sealed override void EndProcessing() {
ThreadAffinitiveSynchronizationContext.RunSynchronized(
() => EndProcessingAsync()
);
}
/// <summary>
/// Interrupt Cmdlet processing (if possible).
/// </summary>
protected sealed override void StopProcessing() {
_cancellationSource.Cancel();
base.StopProcessing();
}
/// <summary>
/// Write a progress record to the output stream, and as a verbose message.
/// </summary>
/// <param name="progressRecord">
/// The progress record to write.
/// </param>
protected void WriteVerboseProgress(ProgressRecord progressRecord) {
if (progressRecord == null)
throw new ArgumentNullException(nameof(progressRecord));
WriteProgress(progressRecord);
WriteVerbose(progressRecord.StatusDescription);
}
/// <summary>
/// Write a progress record to the output stream, and as a verbose message.
/// </summary>
/// <param name="progressRecord">
/// The progress record to write.
/// </param>
/// <param name="messageOrFormat">
/// The message or message-format specifier.
/// </param>
/// <param name="formatArguments">
/// Optional format arguments.
/// </param>
protected void WriteVerboseProgress(ProgressRecord progressRecord, string messageOrFormat, params object[] formatArguments) {
if (progressRecord == null)
throw new ArgumentNullException(nameof(progressRecord));
if (String.IsNullOrWhiteSpace(messageOrFormat))
throw new ArgumentException("Argument cannot be null, empty, or composed entirely of whitespace: 'messageOrFormat'.", nameof(messageOrFormat));
if (formatArguments == null)
throw new ArgumentNullException(nameof(formatArguments));
progressRecord.StatusDescription = String.Format(messageOrFormat, formatArguments);
WriteVerboseProgress(progressRecord);
}
/// <summary>
/// Write a completed progress record to the output stream.
/// </summary>
/// <param name="progressRecord">
/// The progress record to complete.
/// </param>
/// <param name="completionMessageOrFormat">
/// The completion message or message-format specifier.
/// </param>
/// <param name="formatArguments">
/// Optional format arguments.
/// </param>
protected void WriteProgressCompletion(ProgressRecord progressRecord, string completionMessageOrFormat, params object[] formatArguments) {
if (progressRecord == null)
throw new ArgumentNullException(nameof(progressRecord));
if (String.IsNullOrWhiteSpace(completionMessageOrFormat))
throw new ArgumentException("Argument cannot be null, empty, or composed entirely of whitespace: 'completionMessageOrFormat'.", nameof(completionMessageOrFormat));
if (formatArguments == null)
throw new ArgumentNullException(nameof(formatArguments));
progressRecord.StatusDescription = String.Format(completionMessageOrFormat, formatArguments);
progressRecord.PercentComplete = 100;
progressRecord.RecordType = ProgressRecordType.Completed;
WriteProgress(progressRecord);
WriteVerbose(progressRecord.StatusDescription);
}
}
}