-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathPowerShellCmdlet.cs
More file actions
632 lines (566 loc) · 23 KB
/
PowerShellCmdlet.cs
File metadata and controls
632 lines (566 loc) · 23 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// -----------------------------------------------------------------------------
// <copyright file="PowerShellCmdlet.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
namespace Microsoft.WinGet.Common.Command
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Management.Automation;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.WinGet.Resources;
using Microsoft.WinGet.SharedLib.Exceptions;
using Microsoft.WinGet.SharedLib.PolicySettings;
/// <summary>
/// This must be the base class for every cmdlet for winget PowerShell modules.
/// It supports:
/// - Async operations.
/// - Execute on an MTA. If the thread is already running on an MTA it will executed it, otherwise
/// it will create a new MTA thread.
/// Wait must be used to synchronously wait con the task.
/// </summary>
public abstract class PowerShellCmdlet
{
private const string Debug = "Debug";
private static readonly string[] WriteInformationTags = new string[] { "PSHOST" };
private readonly PSCmdlet psCmdlet;
private readonly Thread pwshThread;
private readonly CancellationTokenSource source = new ();
private readonly SemaphoreSlim semaphore = new (1, 1);
private readonly ManualResetEventSlim pwshThreadActionReady = new (false);
private readonly ManualResetEventSlim pwshThreadActionCompleted = new (false);
private BlockingCollection<QueuedStream> queuedStreams = new ();
private int progressActivityId = 0;
private ConcurrentDictionary<int, ProgressRecordType> progressRecords = new ();
private Action? pwshThreadAction = null;
private ExceptionDispatchInfo? pwshThreadEdi = null;
/// <summary>
/// Initializes a new instance of the <see cref="PowerShellCmdlet"/> class.
/// </summary>
/// <param name="psCmdlet">PSCmdlet.</param>
/// <param name="policies">Policies.</param>
public PowerShellCmdlet(PSCmdlet psCmdlet, HashSet<Policy> policies)
{
// Passing Debug will make all the message actions to be Inquire. For async operations
// and the current queue message implementation this doesn't make sense.
// PowerShell will inquire for any message giving the impression that the task is
// paused, but the async operation is still running.
if (psCmdlet.MyInvocation.BoundParameters.ContainsKey(Debug))
{
throw new NotSupportedException(Resources.DebugNotSupported);
}
this.ValidatePolicies(policies);
this.psCmdlet = psCmdlet;
this.pwshThread = Thread.CurrentThread;
}
/// <summary>
/// Request cancellation for this command.
/// </summary>
public void Cancel()
{
this.source.Cancel();
}
/// <summary>
/// Execute the delegate in a MTA thread.
/// Caller must wait on task.
/// </summary>
/// <param name="func">Function to execute.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
internal Task RunOnMTA(Func<Task> func)
{
// .NET 4.8 doesn't support TaskCompletionSource.
#if POWERSHELL_WINDOWS
throw new NotImplementedException();
#else
// This must be called in the main thread.
if (this.pwshThread != Thread.CurrentThread)
{
throw new InvalidOperationException();
}
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA)
{
this.Write(StreamType.Verbose, "Already running on MTA");
try
{
Task result = func();
result.ContinueWith((task) => this.Complete(), TaskContinuationOptions.ExecuteSynchronously);
return result;
}
catch
{
this.Complete();
throw;
}
}
this.Write(StreamType.Verbose, "Creating MTA thread");
var tcs = new TaskCompletionSource();
var thread = new Thread(() =>
{
try
{
func().GetAwaiter().GetResult();
tcs.SetResult();
}
catch (Exception e)
{
tcs.SetException(e);
}
finally
{
this.Complete();
}
});
thread.SetApartmentState(ApartmentState.MTA);
thread.Start();
return tcs.Task;
#endif
}
/// <summary>
/// Execute the delegate in a MTA thread.
/// Caller must wait on task.
/// </summary>
/// <param name="func">Function to execute.</param>
/// <typeparam name="TResult">Return type of function.</typeparam>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
internal Task<TResult> RunOnMTA<TResult>(Func<Task<TResult>> func)
{
// This must be called in the main thread.
if (this.pwshThread != Thread.CurrentThread)
{
throw new InvalidOperationException();
}
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA)
{
this.Write(StreamType.Verbose, "Already running on MTA");
try
{
Task<TResult> result = func();
result.ContinueWith((task) => this.Complete(), TaskContinuationOptions.ExecuteSynchronously);
return result;
}
catch
{
this.Complete();
throw;
}
}
this.Write(StreamType.Verbose, "Creating MTA thread");
var tcs = new TaskCompletionSource<TResult>();
var thread = new Thread(() =>
{
try
{
var result = func().GetAwaiter().GetResult();
tcs.SetResult(result);
}
catch (Exception e)
{
tcs.SetException(e);
}
finally
{
this.Complete();
}
});
thread.SetApartmentState(ApartmentState.MTA);
thread.Start();
return tcs.Task;
}
/// <summary>
/// Execute the delegate in a MTA thread.
/// Synchronous call.
/// </summary>
/// <param name="func">Function to execute.</param>
/// <typeparam name="TResult">Return type of function.</typeparam>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
internal TResult RunOnMTA<TResult>(Func<TResult> func)
{
// This must be called in the main thread.
if (this.pwshThread != Thread.CurrentThread)
{
throw new InvalidOperationException();
}
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA)
{
this.Write(StreamType.Verbose, "Already running on MTA");
try
{
return func();
}
finally
{
this.Complete();
}
}
this.Write(StreamType.Verbose, "Creating MTA thread");
var tcs = new TaskCompletionSource<TResult>();
var thread = new Thread(() =>
{
try
{
var result = func();
tcs.SetResult(result);
}
catch (Exception e)
{
tcs.SetException(e);
}
finally
{
this.Complete();
}
});
thread.SetApartmentState(ApartmentState.MTA);
thread.Start();
this.Wait(tcs.Task);
return tcs.Task.Result;
}
/// <summary>
/// Executes an action in the main thread.
/// Blocks until call is executed.
/// </summary>
/// <param name="action">Action to perform.</param>
internal void ExecuteInPowerShellThread(Action action)
{
if (this.pwshThread == Thread.CurrentThread)
{
action();
return;
}
this.WaitForOurTurn();
this.pwshThreadAction = action;
this.pwshThreadActionReady.Set();
this.WaitMainThreadActionCompletion();
}
/// <summary>
/// Waits for the task to be completed. This MUST be called from the main thread.
/// </summary>
/// <param name="runningTask">Task to wait for.</param>
/// <param name="writeCmdlet">The cmdlet that can write to PowerShell.</param>
internal void Wait(Task runningTask, PowerShellCmdlet? writeCmdlet = null)
{
writeCmdlet ??= this;
// This must be called in the main thread.
if (this.pwshThread != Thread.CurrentThread)
{
throw new InvalidOperationException();
}
do
{
if (this.pwshThreadActionReady.IsSet)
{
// Someone needs the main thread.
this.pwshThreadActionReady.Reset();
if (this.pwshThreadAction != null)
{
try
{
this.pwshThreadEdi = null;
this.pwshThreadAction();
}
catch (Exception e)
{
// Make sure we don't throw in the PowerShell thread, this way
// we'll get a more meaningful stack by Get-Error.
this.pwshThreadEdi = ExceptionDispatchInfo.Capture(e);
}
this.pwshThreadAction = null;
}
// Done.
this.pwshThreadActionCompleted.Set();
}
// Take from the blocking collection.
if (!this.queuedStreams.IsCompleted && this.queuedStreams.Count > 0)
{
try
{
var queuedOutput = this.queuedStreams.Take();
if (queuedOutput != null)
{
this.CmdletWrite(queuedOutput.Type, queuedOutput.Data, writeCmdlet);
}
}
catch (InvalidOperationException)
{
// An InvalidOperationException means that Take() was called on a completed collection.
}
}
}
while (!(runningTask.IsCompleted && this.queuedStreams.IsCompleted));
if (runningTask.IsFaulted)
{
// If IsFaulted is true, the task's Status is equal to Faulted,
// and its Exception property will be non-null.
AggregateException? ae = runningTask.Exception! as AggregateException;
if (ae != null && ae.InnerExceptions.Count == 1)
{
ExceptionDispatchInfo.Capture(ae.InnerExceptions[0]).Throw();
}
throw runningTask.Exception!;
}
}
/// <summary>
/// Writes into the corresponding stream if running on the main thread.
/// Otherwise queue the message.
/// Is the caller responsibility to use the correct types.
/// </summary>
/// <param name="type">Stream type.</param>
/// <param name="data">Data.</param>
internal void Write(StreamType type, object data)
{
if (type == StreamType.Progress)
{
ProgressRecord progressRecord = (ProgressRecord)data;
if (progressRecord.RecordType == ProgressRecordType.Completed)
{
throw new NotSupportedException("Use CompleteProgress");
}
// Keep track of all progress activity.
_ = this.progressRecords.TryAdd(progressRecord.ActivityId, progressRecord.RecordType);
}
if (this.pwshThread == Thread.CurrentThread)
{
this.CmdletWrite(type, data, this);
return;
}
this.queuedStreams.Add(new QueuedStream(type, data));
}
/// <summary>
/// Helper to compute percentage and write progress for processing activities.
/// </summary>
/// <param name="activityId">Activity id.</param>
/// <param name="activity">The activity in progress.</param>
/// <param name="status">The status of the activity.</param>
/// <param name="completed">Number of completed actions.</param>
/// <param name="total">The expected total.</param>
internal void WriteProgressWithPercentage(int activityId, string activity, string status, int completed, int total)
{
double percentComplete = (double)completed / total;
var record = new ProgressRecord(activityId, activity, status)
{
RecordType = ProgressRecordType.Processing,
PercentComplete = (int)(100.0 * percentComplete),
};
this.Write(StreamType.Progress, record);
}
/// <summary>
/// Helper to complete progress records.
/// </summary>
/// <param name="activityId">Activity id.</param>
/// <param name="activity">The activity in progress.</param>
/// <param name="status">The status of the activity.</param>
/// <param name="force">Force write complete progress.</param>
internal void CompleteProgress(int activityId, string activity, string status, bool force = false)
{
var record = new ProgressRecord(activityId, activity, status)
{
RecordType = ProgressRecordType.Completed,
PercentComplete = 100,
};
if (!this.progressRecords.TryAdd(activityId, record.RecordType))
{
_ = this.progressRecords.TryUpdate(activityId, record.RecordType, ProgressRecordType.Processing);
}
if (this.pwshThread == Thread.CurrentThread)
{
this.CmdletWrite(StreamType.Progress, record, this);
}
else
{
// You should only use force if you know the cmdlet that is completing this progress is a sync cmdlet that
// is running in an async context. A sync cmdlet is anything that doesn't start with Start-*
if (force)
{
this.ExecuteInPowerShellThread(() => this.CmdletWrite(StreamType.Progress, record, this));
}
else
{
this.queuedStreams.Add(new QueuedStream(StreamType.Progress, record));
}
}
}
/// <summary>
/// Writes to PowerShell streams.
/// This method must be called in the original thread.
/// WARNING: You must only call this when the task is completed.
/// </summary>
/// <param name="writeCmdlet">The cmdlet that can write to PowerShell.</param>
internal void ConsumeAndWriteStreams(PowerShellCmdlet writeCmdlet)
{
// This must be called in the main thread.
if (this.pwshThread != Thread.CurrentThread)
{
throw new InvalidOperationException();
}
// Take from the blocking collection until is completed.
try
{
while (true)
{
var queuedOutput = this.queuedStreams.Take();
if (queuedOutput != null)
{
this.CmdletWrite(queuedOutput.Type, queuedOutput.Data, writeCmdlet);
}
}
}
catch (InvalidOperationException)
{
// We are done.
// An InvalidOperationException means that Take() was called on a completed collection.
}
}
/// <summary>
/// Gets a new progress activity id.
/// </summary>
/// <returns>The new progress record id.</returns>
internal int GetNewProgressActivityId()
{
return Interlocked.Increment(ref this.progressActivityId);
}
/// <summary>
/// Gets the cancellation token.
/// </summary>
/// <returns>CancellationToken.</returns>
internal CancellationToken GetCancellationToken()
{
return this.source.Token;
}
/// <summary>
/// Gets the current file system location from the cmdlet.
/// </summary>
/// <returns>Path.</returns>
internal string GetCurrentFileSystemLocation()
{
return this.psCmdlet.SessionState.Path.CurrentFileSystemLocation.Path;
}
/// <summary>
/// Sets a variable.
/// </summary>
/// <param name="variableName">Variable name.</param>
/// <param name="value">Value.</param>
internal void SetVariable(string variableName, object value)
{
this.psCmdlet.SessionState.PSVariable.Set(variableName, value);
}
/// <summary>
/// Prompts the user if it should continue processing if possible.
/// </summary>
/// <param name="target">Message.</param>
/// <returns>If the operation should continue.</returns>
internal bool ShouldProcess(string target)
{
// If not on the main thread just continue.
if (this.pwshThread != Thread.CurrentThread)
{
return true;
}
return this.psCmdlet.ShouldProcess(target);
}
private void Complete()
{
this.queuedStreams.CompleteAdding();
}
private void CmdletWrite(StreamType streamType, object data, PowerShellCmdlet writeCmdlet)
{
switch (streamType)
{
case StreamType.Debug:
throw new NotSupportedException();
case StreamType.Verbose:
writeCmdlet.psCmdlet.WriteVerbose((string)data);
break;
case StreamType.Warning:
writeCmdlet.psCmdlet.WriteWarning((string)data);
break;
case StreamType.Error:
writeCmdlet.psCmdlet.WriteError((ErrorRecord)data);
break;
case StreamType.Progress:
// If the activity is already completed don't write progress.
var progressRecord = (ProgressRecord)data;
if (this.progressRecords[progressRecord.ActivityId] == progressRecord.RecordType)
{
writeCmdlet.psCmdlet.WriteProgress(progressRecord);
}
break;
case StreamType.Object:
writeCmdlet.psCmdlet.WriteObject(data);
break;
case StreamType.Information:
writeCmdlet.psCmdlet.WriteInformation(data, WriteInformationTags);
break;
}
}
private void ValidatePolicies(HashSet<Policy> policies)
{
GroupPolicy groupPolicy = GroupPolicy.GetInstance();
if (policies.Contains(Policy.WinGet))
{
if (!groupPolicy.IsEnabled(Policy.WinGet))
{
throw new GroupPolicyException(Policy.WinGet, GroupPolicyFailureType.BlockedByPolicy);
}
policies.Remove(Policy.WinGet);
}
if (policies.Contains(Policy.Configuration))
{
if (!groupPolicy.IsEnabled(Policy.Configuration))
{
throw new GroupPolicyException(Policy.Configuration, GroupPolicyFailureType.BlockedByPolicy);
}
policies.Remove(Policy.Configuration);
}
if (policies.Contains(Policy.WinGetCommandLineInterfaces))
{
if (!groupPolicy.IsEnabled(Policy.WinGetCommandLineInterfaces))
{
throw new GroupPolicyException(Policy.WinGetCommandLineInterfaces, GroupPolicyFailureType.BlockedByPolicy);
}
policies.Remove(Policy.WinGetCommandLineInterfaces);
}
if (policies.Count > 0)
{
throw new NotSupportedException($"Invalid policies {string.Join(",", policies)}");
}
}
private void WaitForOurTurn()
{
this.semaphore.Wait(this.GetCancellationToken());
this.pwshThreadActionCompleted.Reset();
}
private void WaitMainThreadActionCompletion()
{
WaitHandle.WaitAny(new[]
{
this.GetCancellationToken().WaitHandle,
this.pwshThreadActionCompleted.WaitHandle,
});
try
{
if (this.pwshThreadEdi != null)
{
this.pwshThreadEdi.Throw();
}
}
finally
{
this.semaphore.Release();
}
}
private class QueuedStream
{
public QueuedStream(StreamType type, object data)
{
this.Type = type;
this.Data = data;
}
public StreamType Type { get; }
public object Data { get; }
}
}
}