-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateCommand.cs
222 lines (190 loc) · 8.18 KB
/
GenerateCommand.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Diagnostics;
using vz_generator.Commands.Settings;
using vz_generator.Generator.Liquid;
using vz_generator.Generator.Settings.SettingResolvers;
using vz_generator.Localization;
namespace vz_generator.Commands;
public sealed class GenerateCommand : Command
{
public GenerateCommand() : base(VzConsts.GenerateCmd.Name, VzLocales.L(VzLocales.Keys.GenerateCommandDesc))
{
foreach (var opt in Opts())
{
AddOption(opt);
}
}
/// <summary>
/// options to override settings
/// </summary>
/// <returns></returns>
private static IEnumerable<Option> Opts()
{
yield return ConfigOpt;
yield return SelectOpt;
yield return TplPathOpt;
// yield return SyntaxOpt;
yield return VarStringOpt;
yield return VarJsonFileOpt;
yield return VarYamlFileOpt;
yield return OutputOpt;
yield return OverrideOpt;
yield return WatchOpt;
}
public static Option<FileInfo> ConfigOpt = new(
aliases: new string[] { "--config", "-c" },
description: VzLocales.L(VzLocales.Keys.GOptConfigOptDesc));
public static Option<FileSystemInfo> OutputOpt = new(
aliases: new string[] { "--output", "-o" },
description: VzLocales.L(VzLocales.Keys.GOptOutputOptDesc)
);
public static Option<bool?> OverrideOpt = new(
name: "--override",
description: VzLocales.L(VzLocales.Keys.GOptOverrideOptDesc),
getDefaultValue: () => (bool?)null
);
public static Option<Dictionary<string, string>> VarStringOpt = new(
aliases: new string[] { "--var" },
description: VzLocales.L(VzLocales.Keys.GOptVarStringOptDesc),
parseArgument: result => result.Tokens.Select(t => t.Value.Split('=')).ToDictionary(p => p[0], p => p[1])
);
public static Option<Dictionary<string, FileInfo>> VarJsonFileOpt = new(
aliases: new string[] { "--var-json-file" },
description: VzLocales.L(VzLocales.Keys.GOptVarJsonFileOptDesc),
parseArgument: result => result.Tokens.Select(t => t.Value.Split('=')).ToDictionary(p => p[0], p => new FileInfo(p[1]))
);
public static Option<Dictionary<string, FileInfo>> VarYamlFileOpt = new(
aliases: new string[] { "--var-yaml-file" },
description: VzLocales.L(VzLocales.Keys.GOptVarYamlFileOptDesc),
parseArgument: result => result.Tokens.Select(t => t.Value.Split('=')).ToDictionary(p => p[0], p => new FileInfo(p[1]))
);
public static Option<FileSystemInfo> TplPathOpt = new(
aliases: new string[] { "--template", "-t" },
description: VzLocales.L(VzLocales.Keys.GOptTplPathOptDesc)
);
// TODO: 当指定 argument 作为模板内容时,忽略本选项。
public static Option<TemplateSyntax> SyntaxOpt = new(
aliases: new string[] { "--syntax", "-s" },
description: VzLocales.L(VzLocales.Keys.GOptSyntaxOptDesc),
getDefaultValue: () => TemplateSyntax.Liquid
);
public static Option<string> SelectOpt = new(
aliases: new string[] { "--option", "-p" },
description: VzLocales.L(VzLocales.Keys.GOptSelectOptDesc)
);
public static Option<bool> WatchOpt = new(
aliases: new string[] { "--watch", "-w" },
description: VzLocales.L(VzLocales.Keys.GOptWatchOptDesc),
getDefaultValue: () => false
);
// TODO: 完全忽略配置,不指定option,从标准输入(argument.0)接收模板内容(仅支持单个文件)
private static FileSystemWatcher? TemplateWatcher;
public async Task GenerateAsync(InvocationContext context)
{
var watched = context.ParseResult.GetValueForOption(WatchOpt);
// TODO: settings hot reload?
var setting = await LoadSettings(context);
if (watched)
{
if (!File.Exists(setting.TemplatePath) && !Directory.Exists(setting.TemplatePath))
{
#if DEBUG
Console.WriteLine($"Watch {nameof(setting.TemplatePath)} Fail: {setting.TemplatePath} Not Exists!{Environment.NewLine}");
#else
context.Console.Error.Write(
VzLocales.L(
VzLocales.Keys.GFailedErrorResult,
$"Watch {nameof(setting.TemplatePath)} Fail: {setting.TemplatePath} Not Exists!", "", Environment.NewLine));
context.ExitCode = 2;
#endif
return;
}
// keep running, timer
var watchPath = Path.GetDirectoryName(setting.TemplatePath);
context.Console.Write($"Start watching {watchPath} ...{Environment.NewLine}");
TemplateWatcher = new FileSystemWatcher(watchPath!)
{
NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size
};
TemplateWatcher.Changed += OnChanged;
TemplateWatcher.Created += OnChanged;
TemplateWatcher.Deleted += OnChanged;
TemplateWatcher.Renamed += OnChanged;
TemplateWatcher.Error += OnError;
TemplateWatcher.Filter = "";
TemplateWatcher.IncludeSubdirectories = true;
TemplateWatcher.EnableRaisingEvents = true;
async void OnChanged(object sender, FileSystemEventArgs e)
{
// TODO: re-generating the Changed Files or directories only
context.Console.Write($"{e.FullPath} {e.ChangeType}. Re-generating...");
var stopWatch = Stopwatch.StartNew();
stopWatch.Start();
await ExecuteAsync(setting, context);
stopWatch.Stop();
context.Console.Write($"completed in {stopWatch.ElapsedMilliseconds}ms.{Environment.NewLine}");
}
void OnError(object sender, ErrorEventArgs e)
{
var ex = e.GetException();
#if DEBUG
Console.WriteLine($"Generate Fail: {ex.Message}{Environment.NewLine}");
#else
context.Console.Error.Write(
VzLocales.L(
VzLocales.Keys.GFailedErrorResult, ex.Message, Environment.NewLine, ex.StackTrace ?? ""));
context.ExitCode = 2;
#endif
}
}
await ExecuteAsync(setting, context);
if (watched)
{
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
TemplateWatcher?.Dispose();
}
}
private async Task<GeneratorSetting> LoadSettings(InvocationContext context)
{
var resolver =
new CliPromptGeneratorSettingResolver(
new CliOptionGeneratorSettingDecorator(
new DefaultGeneratorSettingResolver()));
var ctx = new ResolveContext(context);
await resolver.ResolveAsync(ctx);
return ctx.Result!;
}
private async Task ExecuteAsync(GeneratorSetting setting, InvocationContext context)
{
try
{
if (setting.TemplateSyntax == TemplateSyntax.Liquid)
{
var executor = new LiquidTemplateExecutor(setting, context);
await executor.ExecuteAsync();
return;
}
throw new NotImplementedException();
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine($"Generate Fail: {ex.Message}{Environment.NewLine}");
#else
context.Console.Error.Write(
VzLocales.L(
VzLocales.Keys.GFailedErrorResult, ex.Message, Environment.NewLine, ex.StackTrace ?? ""));
context.ExitCode = 2;
#endif
}
}
}