Skip to content

Commit 9e001fe

Browse files
Added PipelinePhase
1 parent 1064c50 commit 9e001fe

14 files changed

+414
-131
lines changed

src/System.CommandLine.Subsystems/CompletionSubsystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.CommandLine;
1010
public class CompletionSubsystem : CliSubsystem
1111
{
1212
public CompletionSubsystem(IAnnotationProvider? annotationProvider = null)
13-
: base(CompletionAnnotations.Prefix, SubsystemKind.Completion, annotationProvider)
13+
: base(CompletionAnnotations.Prefix, SubsystemKind.Completion, annotationProvider)
1414
{ }
1515

1616
// TODO: Figure out trigger for completions

src/System.CommandLine.Subsystems/Directives/DiagramSubsystem.cs

Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace System.CommandLine.Directives;
99

10-
public class DiagramSubsystem( IAnnotationProvider? annotationProvider = null)
10+
public class DiagramSubsystem(IAnnotationProvider? annotationProvider = null)
1111
: DirectiveSubsystem("diagram", SubsystemKind.Diagram, annotationProvider)
1212
{
1313
//protected internal override bool GetIsActivated(ParseResult? parseResult)
@@ -68,115 +68,115 @@ private static void Diagram(
6868
builder.Append('!');
6969
}
7070
*/
71-
// TODO: Directives
72-
/*
73-
switch (symbolResult)
71+
// TODO: Directives
72+
/*
73+
switch (symbolResult)
74+
{
75+
case DirectiveResult { Directive: not DiagramDirective }:
76+
break;
77+
*/
78+
79+
// TODO: This logic is deeply tied to internal types/properties. These aren't things we probably want to expose like SymbolNode. See #2349 for alternatives
80+
/*
81+
case ArgumentResult argumentResult:
7482
{
75-
case DirectiveResult { Directive: not DiagramDirective }:
76-
break;
77-
*/
83+
var includeArgumentName =
84+
argumentResult.Argument.FirstParent!.Symbol is CliCommand { HasArguments: true, Arguments.Count: > 1 };
7885
79-
// TODO: This logic is deeply tied to internal types/properties. These aren't things we probably want to expose like SymbolNode. See #2349 for alternatives
80-
/*
81-
case ArgumentResult argumentResult:
86+
if (includeArgumentName)
87+
{
88+
builder.Append("[ ");
89+
builder.Append(argumentResult.Argument.Name);
90+
builder.Append(' ');
91+
}
92+
93+
if (argumentResult.Argument.Arity.MaximumNumberOfValues > 0)
94+
{
95+
ArgumentConversionResult conversionResult = argumentResult.GetArgumentConversionResult();
96+
switch (conversionResult.Result)
8297
{
83-
var includeArgumentName =
84-
argumentResult.Argument.FirstParent!.Symbol is CliCommand { HasArguments: true, Arguments.Count: > 1 };
85-
86-
if (includeArgumentName)
87-
{
88-
builder.Append("[ ");
89-
builder.Append(argumentResult.Argument.Name);
90-
builder.Append(' ');
91-
}
92-
93-
if (argumentResult.Argument.Arity.MaximumNumberOfValues > 0)
94-
{
95-
ArgumentConversionResult conversionResult = argumentResult.GetArgumentConversionResult();
96-
switch (conversionResult.Result)
98+
case ArgumentConversionResultType.NoArgument:
99+
break;
100+
case ArgumentConversionResultType.Successful:
101+
switch (conversionResult.Value)
97102
{
98-
case ArgumentConversionResultType.NoArgument:
103+
case string s:
104+
builder.Append($"<{s}>");
99105
break;
100-
case ArgumentConversionResultType.Successful:
101-
switch (conversionResult.Value)
102-
{
103-
case string s:
104-
builder.Append($"<{s}>");
105-
break;
106-
107-
case IEnumerable items:
108-
builder.Append('<');
109-
builder.Append(
110-
string.Join("> <",
111-
items.Cast<object>().ToArray()));
112-
builder.Append('>');
113-
break;
114-
115-
default:
116-
builder.Append('<');
117-
builder.Append(conversionResult.Value);
118-
builder.Append('>');
119-
break;
120-
}
121106
107+
case IEnumerable items:
108+
builder.Append('<');
109+
builder.Append(
110+
string.Join("> <",
111+
items.Cast<object>().ToArray()));
112+
builder.Append('>');
122113
break;
123114
124-
default: // failures
115+
default:
125116
builder.Append('<');
126-
builder.Append(string.Join("> <", symbolResult.Tokens.Select(t => t.Value)));
117+
builder.Append(conversionResult.Value);
127118
builder.Append('>');
128-
129119
break;
130120
}
131-
}
132121
133-
if (includeArgumentName)
134-
{
135-
builder.Append(" ]");
136-
}
122+
break;
137123
138-
break;
124+
default: // failures
125+
builder.Append('<');
126+
builder.Append(string.Join("> <", symbolResult.Tokens.Select(t => t.Value)));
127+
builder.Append('>');
128+
129+
break;
139130
}
131+
}
140132
141-
default:
142-
{
143-
OptionResult? optionResult = symbolResult as OptionResult;
144-
145-
if (optionResult is { Implicit: true })
146-
{
147-
builder.Append('*');
148-
}
149-
150-
builder.Append("[ ");
151-
152-
if (optionResult is not null)
153-
{
154-
builder.Append(optionResult.IdentifierToken?.Value ?? optionResult.Option.Name);
155-
}
156-
else
157-
{
158-
builder.Append(((CommandResult)symbolResult).IdentifierToken.Value);
159-
}
160-
161-
foreach (SymbolResult child in symbolResult.SymbolResultTree.GetChildren(symbolResult))
162-
{
163-
if (child is ArgumentResult arg &&
164-
(arg.Argument.ValueType == typeof(bool) ||
165-
arg.Argument.Arity.MaximumNumberOfValues == 0))
166-
{
167-
continue;
168-
}
133+
if (includeArgumentName)
134+
{
135+
builder.Append(" ]");
136+
}
169137
170-
builder.Append(' ');
138+
break;
139+
}
171140
172-
Diagram(builder, child, parseResult);
173-
}
141+
default:
142+
{
143+
OptionResult? optionResult = symbolResult as OptionResult;
174144
175-
builder.Append(" ]");
176-
break;
145+
if (optionResult is { Implicit: true })
146+
{
147+
builder.Append('*');
148+
}
149+
150+
builder.Append("[ ");
151+
152+
if (optionResult is not null)
153+
{
154+
builder.Append(optionResult.IdentifierToken?.Value ?? optionResult.Option.Name);
155+
}
156+
else
157+
{
158+
builder.Append(((CommandResult)symbolResult).IdentifierToken.Value);
159+
}
160+
161+
foreach (SymbolResult child in symbolResult.SymbolResultTree.GetChildren(symbolResult))
162+
{
163+
if (child is ArgumentResult arg &&
164+
(arg.Argument.ValueType == typeof(bool) ||
165+
arg.Argument.Arity.MaximumNumberOfValues == 0))
166+
{
167+
continue;
177168
}
169+
170+
builder.Append(' ');
171+
172+
Diagram(builder, child, parseResult);
178173
}
174+
175+
builder.Append(" ]");
176+
break;
179177
}
180178
}
179+
}
180+
}
181181
*/
182182
}

src/System.CommandLine.Subsystems/Directives/DirectiveSubsystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public abstract class DirectiveSubsystem : CliSubsystem
1313
public string Id { get; }
1414
public Location? Location { get; private set; }
1515

16-
public DirectiveSubsystem(string name, SubsystemKind kind, IAnnotationProvider? annotationProvider = null, string? id = null)
16+
public DirectiveSubsystem(string name, SubsystemKind kind, IAnnotationProvider? annotationProvider = null, string? id = null)
1717
: base(name, kind, annotationProvider: annotationProvider)
1818
{
1919
Id = id ?? name;

src/System.CommandLine.Subsystems/Directives/ResponseSubsystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace System.CommandLine.Directives;
99
public class ResponseSubsystem()
1010
: CliSubsystem("Response", SubsystemKind.Response, null)
1111
{
12-
protected internal override void Initialize(InitializationContext context)
12+
protected internal override void Initialize(InitializationContext context)
1313
=> context.Configuration.ResponseFileTokenReplacer = Replacer;
1414

1515
public static (List<string>? tokens, List<string>? errors) Replacer(string responseSourceName)

src/System.CommandLine.Subsystems/HelpSubsystem.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class HelpSubsystem(IAnnotationProvider? annotationProvider = null)
2424
Arity = ArgumentArity.Zero
2525
};
2626

27-
protected internal override void Initialize(InitializationContext context)
27+
protected internal override void Initialize(InitializationContext context)
2828
=> context.Configuration.RootCommand.Add(HelpOption);
2929

3030
protected internal override bool GetIsActivated(ParseResult? parseResult)
@@ -37,6 +37,6 @@ protected internal override void Execute(PipelineResult pipelineResult)
3737
pipelineResult.SetSuccess();
3838
}
3939

40-
public bool TryGetDescription (CliSymbol symbol, out string? description)
41-
=> TryGetAnnotation (symbol, HelpAnnotations.Description, out description);
40+
public bool TryGetDescription(CliSymbol symbol, out string? description)
41+
=> TryGetAnnotation(symbol, HelpAnnotations.Description, out description);
4242
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections;
5+
using System.CommandLine.Subsystems;
6+
7+
namespace System.CommandLine;
8+
9+
public partial class Pipeline
10+
{
11+
private class Subsystems : IEnumerable<CliSubsystem>
12+
{
13+
internal List<CliSubsystem> subsystemList = [];
14+
private bool dirty;
15+
16+
internal void Add(CliSubsystem? subsystem, bool insertAtStart = false)
17+
{
18+
if (subsystem is not null)
19+
{
20+
// TODO: Determine whether to remove and readd. This affects the position in the list
21+
//if (subsystemList.Contains(subsystem))
22+
//{
23+
// subsystemList.Remove(subsystem);
24+
//}
25+
subsystemList.Add(subsystem);
26+
dirty = true;
27+
}
28+
}
29+
30+
internal void Insert(CliSubsystem? subsystem, CliSubsystem existingSubsystem, bool insertBefore = false)
31+
{
32+
if (subsystem is not null)
33+
{
34+
if (existingSubsystem.Phase != subsystem.Phase)
35+
{
36+
throw new InvalidOperationException("Subsystems can only be inserted relative to other subsystems in the same phase");
37+
}
38+
if (subsystemList.Contains(subsystem))
39+
{
40+
// TODO: Exception or last wins? Same for Add above
41+
throw new InvalidOperationException("Subsystems can only be inserted if it had not already been added");
42+
}
43+
44+
var existing = subsystemList.IndexOf(existingSubsystem);
45+
if (existing != -1)
46+
{
47+
throw new InvalidOperationException("Subsystems can only be added relative to subsystems that have previously been added");
48+
}
49+
50+
var insertAt = insertBefore ? existing + 1 : existing;
51+
subsystemList.Insert(insertAt, subsystem);
52+
dirty = true;
53+
}
54+
}
55+
56+
public IEnumerator<CliSubsystem> GetEnumerator()
57+
{
58+
return subsystemList.GetEnumerator();
59+
}
60+
61+
IEnumerator IEnumerable.GetEnumerator()
62+
{
63+
return subsystemList.GetEnumerator();
64+
}
65+
66+
internal IEnumerable<CliSubsystem> EarlyReturnSubsystems
67+
=> subsystemList.Where(x => x.Phase == SubsystemPhase.EarlyReturn).ToList();
68+
69+
internal IEnumerable<CliSubsystem> ValidationSubsystems
70+
=> subsystemList.Where(x => x.Phase == SubsystemPhase.Validate).ToList();
71+
72+
internal IEnumerable<CliSubsystem> ExecutionSubsystems
73+
=> subsystemList.Where(x => x.Phase == SubsystemPhase.Execute).ToList();
74+
75+
internal IEnumerable<CliSubsystem> FinishSubsystems
76+
=> subsystemList.Where(x => x.Phase == SubsystemPhase.Finish).ToList();
77+
78+
}
79+
}

0 commit comments

Comments
 (0)