-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathInvocationPipeline.cs
More file actions
167 lines (141 loc) · 6.41 KB
/
InvocationPipeline.cs
File metadata and controls
167 lines (141 loc) · 6.41 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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Threading;
using System.Threading.Tasks;
namespace System.CommandLine.Invocation
{
internal static class InvocationPipeline
{
internal static async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
{
ProcessTerminationHandler? terminationHandler = null;
using CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
try
{
int actionResult = 0;
int preActionResult = 0;
if (parseResult.PreActions is not null)
{
for (int i = 0; i < parseResult.PreActions.Count; i++)
{
var action = parseResult.PreActions[i];
var result = 0;
switch (action)
{
case SynchronousCommandLineAction syncAction:
result = syncAction.Invoke(parseResult);
break;
case AsynchronousCommandLineAction asyncAction:
result = await asyncAction.InvokeAsync(parseResult, cts.Token);
break;
}
if (result != 0)
{
preActionResult = result;
}
}
}
if (parseResult.Action is null)
{
return preActionResult != 0 ? preActionResult : ReturnCodeForMissingAction(parseResult);
}
switch (parseResult.Action)
{
case SynchronousCommandLineAction syncAction:
actionResult = syncAction.Invoke(parseResult);
break;
case AsynchronousCommandLineAction asyncAction:
var startedInvocation = asyncAction.InvokeAsync(parseResult, cts.Token);
var timeout = parseResult.InvocationConfiguration.ProcessTerminationTimeout;
if (timeout.HasValue)
{
terminationHandler = new(cts, startedInvocation, timeout.Value);
}
if (terminationHandler is null)
{
actionResult = await startedInvocation;
}
else
{
// Handlers may not implement cancellation.
// In such cases, when CancelOnProcessTermination is configured and user presses Ctrl+C,
// ProcessTerminationCompletionSource completes first, with the result equal to native exit code for given signal.
Task<int> firstCompletedTask = await Task.WhenAny(startedInvocation, terminationHandler.ProcessTerminationCompletionSource.Task);
actionResult = await firstCompletedTask; // return the result or propagate the exception
}
break;
default:
throw new ArgumentOutOfRangeException(nameof(parseResult.Action));
}
return preActionResult != 0 ? preActionResult : actionResult;
}
catch (Exception ex) when (parseResult.InvocationConfiguration.EnableDefaultExceptionHandler)
{
return DefaultExceptionHandler(ex, parseResult);
}
finally
{
terminationHandler?.Dispose();
}
}
internal static int Invoke(ParseResult parseResult)
{
try
{
int preActionResult = 0;
if (parseResult.PreActions is not null)
{
for (var i = 0; i < parseResult.PreActions.Count; i++)
{
if (parseResult.PreActions[i] is SynchronousCommandLineAction syncPreAction)
{
int result = syncPreAction.Invoke(parseResult);
if (result != 0)
{
preActionResult = result;
}
}
}
}
switch (parseResult.Action)
{
case null:
return preActionResult != 0 ? preActionResult : ReturnCodeForMissingAction(parseResult);
case SynchronousCommandLineAction syncAction:
int actionResult = syncAction.Invoke(parseResult);
return preActionResult != 0 ? preActionResult : actionResult;
default:
throw new InvalidOperationException($"{nameof(AsynchronousCommandLineAction)} called within non-async invocation.");
}
}
catch (Exception ex) when (parseResult.InvocationConfiguration.EnableDefaultExceptionHandler)
{
return DefaultExceptionHandler(ex, parseResult);
}
}
private static int DefaultExceptionHandler(Exception exception, ParseResult parseResult)
{
if (exception is not OperationCanceledException)
{
ConsoleHelpers.ResetTerminalForegroundColor();
ConsoleHelpers.SetTerminalForegroundRed();
var error = parseResult.InvocationConfiguration.Error;
error.Write(LocalizationResources.ExceptionHandlerHeader());
error.WriteLine(exception.ToString());
ConsoleHelpers.ResetTerminalForegroundColor();
}
return 1;
}
private static int ReturnCodeForMissingAction(ParseResult parseResult)
{
if (parseResult.Errors.Count > 0)
{
return 1;
}
else
{
return 0;
}
}
}
}