-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathInvocationPipeline.cs
More file actions
183 lines (156 loc) · 7.21 KB
/
InvocationPipeline.cs
File metadata and controls
183 lines (156 loc) · 7.21 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
// 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 exitCode = 0;
if (parseResult.PreActions is not null)
{
for (int i = 0; i < parseResult.PreActions.Count; i++)
{
var action = parseResult.PreActions[i];
int preActionResult;
switch (action)
{
case SynchronousCommandLineAction syncAction:
preActionResult = syncAction.Invoke(parseResult);
break;
case AsynchronousCommandLineAction asyncAction:
preActionResult = await asyncAction.InvokeAsync(parseResult, cts.Token);
break;
default:
preActionResult = 0;
break;
}
if (exitCode == 0)
{
exitCode = preActionResult;
}
}
}
if (parseResult.Action is null)
{
return exitCode != 0 ? exitCode : ReturnCodeForMissingAction(parseResult);
}
int actionResult;
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 exitCode != 0 ? exitCode : actionResult;
}
catch (Exception ex) when (parseResult.InvocationConfiguration.EnableDefaultExceptionHandler)
{
return DefaultExceptionHandler(ex, parseResult);
}
finally
{
terminationHandler?.Dispose();
}
}
internal static int Invoke(ParseResult parseResult)
{
try
{
int exitCode = 0;
if (parseResult.PreActions is not null)
{
#if DEBUG
for (var i = 0; i < parseResult.PreActions.Count; i++)
{
var action = parseResult.PreActions[i];
if (action is not SynchronousCommandLineAction)
{
parseResult.InvocationConfiguration.EnableDefaultExceptionHandler = false;
throw new Exception(
$"This should not happen. An instance of {nameof(AsynchronousCommandLineAction)} ({action}) was called within {nameof(InvocationPipeline)}.{nameof(Invoke)}. This is supposed to be detected earlier resulting in a call to {nameof(InvocationPipeline)}{nameof(InvokeAsync)}");
}
}
#endif
for (var i = 0; i < parseResult.PreActions.Count; i++)
{
if (parseResult.PreActions[i] is SynchronousCommandLineAction syncPreAction)
{
int preActionResult = syncPreAction.Invoke(parseResult);
if (exitCode == 0)
{
exitCode = preActionResult;
}
}
}
}
switch (parseResult.Action)
{
case null:
return exitCode != 0 ? exitCode : ReturnCodeForMissingAction(parseResult);
case SynchronousCommandLineAction syncAction:
int actionResult = syncAction.Invoke(parseResult);
return exitCode != 0 ? exitCode : 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;
}
}
}
}