-
Notifications
You must be signed in to change notification settings - Fork 32
/
GeneratorOption.cs
116 lines (109 loc) 路 4.93 KB
/
GeneratorOption.cs
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
using System;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.IO;
using System.Linq;
using Core.Generators;
using Core.Meta.Extensions;
namespace Compiler.Options
{
/// <summary>
/// Represents a command line option for specifying generator configurations.
/// </summary>
public class GeneratorOption : CliOption<GeneratorConfig[]>
{
/// <summary>
/// Initializes a new instance of the <see cref="GeneratorOption"/> class.
/// </summary>
public GeneratorOption() : base(name: CliStrings.GeneratorFlag, ["-g"])
{
Description = "Specifies code generators to use for compilation.";
AllowMultipleArgumentsPerToken = false;
CustomParser = new((ArgumentResult result) =>
{
if (result.Tokens.Count == 0)
{
return [];
}
return result.Tokens.Select(t => ParseGeneratorToken(t.Value, result)).Where(c => c is not null).Select(c => c!).ToArray();
});
}
/// <summary>
/// Parses a generator token into a <see cref="GeneratorConfig"/> object.
/// </summary>
/// <param name="token">The generator token to parse.</param>
/// <param name="result">The argument result to which errors can be added.</param>
/// <returns>A <see cref="GeneratorConfig"/> object if the token could be parsed; otherwise, null.</returns>
private static GeneratorConfig? ParseGeneratorToken(string token, ArgumentResult result)
{
var parts = token.Split(':', 2);
if (parts.Length != 2)
{
result.AddError($"Incomplete generator token specified '{token}'.");
return null;
}
var generatorAlias = parts[0].Trim().ToLower();
var remaining = parts[1].Split(',');
var outputPath = remaining[0] ?? string.Empty;
if (string.IsNullOrWhiteSpace(outputPath))
{
result.AddError($"Output path not specified for generator '{generatorAlias}'.");
return null;
}
// If the output path is 'stdout', no need to validate it as a file path
if (!string.Equals("stdout", outputPath, StringComparison.OrdinalIgnoreCase) && !outputPath.IsLegalFilePath(out var invalidCharacterIndex))
{
if (invalidCharacterIndex >= 0)
{
result.AddError($"Invalid character '{outputPath[invalidCharacterIndex]}' in output path of generator '{generatorAlias}': '{outputPath}'.");
return null;
}
}
var additionalOptions = remaining.Skip(1).Select(s => s.Split('=')).Where(p => p.Length == 2).ToDictionary(p => p[0].Trim(), p => p[1].Trim());
// Initialize default values
var services = TempoServices.Both;
var emitNotice = true;
var emitBinarySchema = true;
string @namespace = string.Empty;
foreach (var option in additionalOptions)
{
switch (option.Key.ToLower())
{
case "services":
if (!Enum.TryParse<TempoServices>(option.Value, true, out var parsedServices))
{
result.AddError($"Invalid value '{option.Value}' for option 'services'.");
break;
}
services = parsedServices;
break;
case "emitnotice":
if (!bool.TryParse(option.Value, out var parsedEmitNotice))
{
result.AddError($"Invalid value '{option.Value}' for option 'emitNotice'.");
break;
}
emitNotice = parsedEmitNotice;
break;
case "emitbinaryschema":
if (!bool.TryParse(option.Value, out var parsedEmitBinarySchema))
{
result.AddError($"Invalid value '{option.Value}' for option 'emitBinarySchema'.");
break;
}
emitBinarySchema = parsedEmitBinarySchema;
break;
case "namespace":
if (string.IsNullOrWhiteSpace(option.Value))
{
result.AddError($"Invalid value '{option.Value}' for option 'namespace'.");
break;
}
@namespace = option.Value;
break;
}
}
return new GeneratorConfig(generatorAlias, outputPath, services, emitNotice, @namespace, emitBinarySchema, additionalOptions);
}
}
}