-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineApplicationOnExecuteMethodsGenerator.cs
More file actions
120 lines (101 loc) · 4.84 KB
/
Copy pathCommandLineApplicationOnExecuteMethodsGenerator.cs
File metadata and controls
120 lines (101 loc) · 4.84 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
namespace System.CommandLine.Extensions.SourceGenerators
{
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Text;
[Generator]
public sealed class CommandLineApplicationOnExecuteMethodsGenerator : IIncrementalGenerator
{
private const int NumOverloads = 8;
private const string NamespaceName = "System.CommandLine.Extensions";
private const string TypeName = "CommandLineApplication";
private static readonly IEnumerable<string> UsingList = new List<string>
{
"Collections.Generic",
"System.CommandLine.Binding",
"System.Linq",
"System.Threading.Tasks"
};
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterPostInitializationOutput(static ctx =>
{
var sourceText = GenerateSource();
ctx.AddSource(nameof(CommandLineApplicationOnExecuteMethodsGenerator), sourceText);
});
}
private static SourceText GenerateSource()
{
var builder = new StringBuilder();
builder.AppendLine($"namespace {NamespaceName}");
builder.AppendBlock(ns =>
{
foreach (var importedNamespace in UsingList.OrderBy(s => s)) ns.AppendLine($"using {importedNamespace};");
ns.AppendLine($"partial class {TypeName}");
ns.AppendBlock(t =>
{
for (var i = 1; i <= NumOverloads; i++)
{
var methodArgumentsCount = i;
var typeArgumentsString = string.Join(", ", Enumerable.Range(1, methodArgumentsCount).Select(typeParameterIndex => $"T{typeParameterIndex}"));
t.AppendLine($"public void OnExecute<{typeArgumentsString}>(Func<{typeArgumentsString}, Task<int>> execute)");
t.AppendBlock(methodBody =>
{
methodBody.AppendLine("IReadOnlyList<Option> options = this.command.Options.ToList();");
var parameters = new List<string>();
for (var j = 0; j < methodArgumentsCount; j++)
{
var parameterIndex = j + 1;
var variableName = $"descriptor{parameterIndex}";
methodBody.AppendLine($"IValueDescriptor<T{parameterIndex}> {variableName} = this.FindValueDescriptorForOption<T{parameterIndex}>({j});");
parameters.Add(variableName);
}
var parametersString = string.Join(", ", parameters);
methodBody.AppendLine($"this.command.SetHandler(execute, {parametersString});");
});
}
});
ns.AppendLine("partial class CommandOptionExtensions");
ns.AppendBlock(t =>
{
for (var i = 1; i <= NumOverloads; i++)
{
var methodArgumentsCount = i;
var typeArgumentsString = string.Join(", ", Enumerable.Range(1, methodArgumentsCount).Select(typeParameterIndex => $"T{typeParameterIndex}"));
t.AppendLine($"public static void OnExecute<{typeArgumentsString}>(this CommandOption commandOption, Func<{typeArgumentsString}, Task<int>> execute) => commandOption.Command.OnExecute(execute);");
}
});
});
return SourceText.From(builder.ToString(), Encoding.UTF8);
}
}
internal static class StringBuilderExtensions
{
public static void AppendBlock(this StringBuilder builder, Action<IndentedStringWriter> blockWriter)
{
builder.AppendLine("{");
var writer = new IndentedStringWriter(builder, 1);
blockWriter(writer);
builder.AppendLine("}");
}
}
internal sealed class IndentedStringWriter(StringBuilder stringBuilder, int indentionLevel)
{
private readonly string indentionString = new(' ', indentionLevel * 4);
public void AppendBlock(Action<IndentedStringWriter> blockWriter)
{
stringBuilder.Append(this.indentionString);
stringBuilder.AppendLine("{");
var writer = new IndentedStringWriter(stringBuilder, indentionLevel + 1);
blockWriter(writer);
stringBuilder.Append(this.indentionString);
stringBuilder.AppendLine("}");
stringBuilder.AppendLine();
}
public void AppendLine(string text)
{
stringBuilder.Append(this.indentionString);
stringBuilder.AppendLine(text);
}
}
}