-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathValidateCommand.cs
More file actions
287 lines (243 loc) · 10.9 KB
/
ValidateCommand.cs
File metadata and controls
287 lines (243 loc) · 10.9 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System.Reflection;
using System.Text;
using DotMake.CommandLine;
using EXDTooler.BreakingValidators;
using EXDTooler.Schema;
using EXDTooler.Validators;
using Json.Schema;
using Lumina.Data.Structs.Excel;
using Yaml2JsonNode;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace EXDTooler;
[CliCommand(Parent = typeof(MainCommand))]
public sealed class ValidateCommand
{
public required MainCommand Parent { get; set; }
[CliOption(Required = false, Description = "Path to the game directory. Should be the root of the game's repository.", ValidationRules = CliValidationRules.ExistingDirectory)]
public string? GamePath { get; set; }
[CliOption(Required = false, Description = "Path to the columns file generated by export-columns.", ValidationRules = CliValidationRules.ExistingFile)]
public string? ColumnsFile { get; set; }
[CliOption(Required = true, Description = "Path to the schema directory. Should be a folder with just .yml schemas.", ValidationRules = CliValidationRules.ExistingDirectory)]
public required string SchemaPath { get; set; }
[CliOption(Required = false, Description = "Path to the base schema directory. Should be a folder with just .yml schemas. Used for ensuring no breaking changes occured.", ValidationRules = CliValidationRules.ExistingDirectory)]
public string? BaseSchemaPath { get; set; }
[CliOption(Required = false, Description = "Path to a schema.json. If omitted, the built-in one will be used.", ValidationRules = CliValidationRules.ExistingFile)]
public string? JsonSchemaPath { get; set; }
public async Task<int> RunAsync()
{
var token = Parent.Init();
var sheets = ColDefReader.FromInputs(GamePath, ColumnsFile);
JsonSchema schema;
if (JsonSchemaPath != null)
schema = JsonSchema.FromFile(JsonSchemaPath);
else
{
using var s = Assembly.GetExecutingAssembly().GetManifestResourceStream("EXDTooler.schema.json")!;
schema = await JsonSchema.FromStream(s).ConfigureAwait(false);
}
var schemaOptions = new EvaluationOptions
{
ValidateAgainstMetaSchema = true,
RequireFormatValidation = true,
OutputFormat = OutputFormat.List,
OnlyKnownFormats = true,
AllowReferencesIntoUnknownKeywords = false,
};
var schemaDeserializer = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build();
string[] filesToVerify = [.. Directory.EnumerateFiles(SchemaPath, "*.yml")];
var existingSchemas = filesToVerify.Select(f => Path.GetFileNameWithoutExtension(f));
var requiredSchemas = sheets.Sheets.Keys;
var missingSchemas = requiredSchemas.Except(existingSchemas);
var extraSchemas = existingSchemas.Except(requiredSchemas);
if (missingSchemas.Any())
{
foreach (var missingSchema in missingSchemas)
Log.AnnotatedError($"Missing Schema: {missingSchema}");
}
if (extraSchemas.Any())
{
foreach (var extraSchema in extraSchemas)
Log.AnnotatedError($"Redundant Schema: {extraSchema}", new() { File = $"{extraSchema}.yml" });
}
Log.Verbose($"Verifying {filesToVerify.Length} files");
var validatedFiles = 0;
foreach (var (idx, sheetFile) in filesToVerify.Index())
{
var baseSheetFile = BaseSchemaPath == null ? null : Path.Combine(BaseSchemaPath, Path.GetRelativePath(SchemaPath, sheetFile));
if (BaseSchemaPath != null && !File.Exists(baseSheetFile))
{
Log.AnnotatedWarn("Cannot verify sheet for breaking changes. Base sheet does not exist.", new() { File = Path.GetFileName(sheetFile) });
baseSheetFile = null;
}
if (Validate(sheetFile, sheets, schemaDeserializer, d => schema.Evaluate(d.ToJsonNode(), schemaOptions), baseSheetFile))
validatedFiles++;
if ((idx & 3) == 0)
Log.VerboseProgress($"Verified {validatedFiles}/{idx + 1} files. ({(idx + 1) / (double)filesToVerify.Length * 100:0.00}% done)");
}
Log.VerboseProgressClear();
Log.Info($"Verified {validatedFiles}/{filesToVerify.Length} files. ({(filesToVerify.Length == 0 ? 1 : (validatedFiles / (double)filesToVerify.Length)) * 100:0.00}%)");
var failed = missingSchemas.Any() || extraSchemas.Any() || validatedFiles != filesToVerify.Length;
var summary = CreateSummary(failed);
Log.Output("summary", summary);
return failed ? 1 : 0;
}
private static string CreateSummary(bool failed)
{
var s = new StringBuilder();
if (failed)
s.AppendLine("## ❌ Validation failed");
else
s.AppendLine("## ✅ Validation succeeded");
s.AppendLine();
foreach (var group in Log.Annotations.GroupBy(a => Path.GetFileNameWithoutExtension(a.Item3?.File)).OrderBy(a => a.Key))
{
s.AppendLine($"## {group.Key ?? "Untagged"}");
s.AppendLine();
foreach (var level in group.GroupBy(a => a.Item1).OrderBy(a => a.Key))
{
s.AppendLine("<details>");
s.AppendLine();
s.Append("<summary><h3>");
var name = level.Key switch
{
Log.LogLevel.Error => "❌ Errors",
Log.LogLevel.Warn => "⚠️ Warnings",
Log.LogLevel.Info => "💬 Info",
Log.LogLevel.Verbose => "💭 Verbose",
Log.LogLevel.Debug => "📝 Debug",
_ => "❓ Unknown",
};
s.Append(name);
s.AppendLine("</h3></summary>");
s.AppendLine();
foreach (var note in group)
{
var title = note.Item3?.Title ?? note.Item2;
var text = note.Item3.HasValue ? note.Item2 : null;
s.AppendLine($"**{title}**");
if (text != null)
s.AppendLine(text);
s.AppendLine();
}
s.AppendLine();
s.AppendLine("</details>");
s.AppendLine();
}
s.AppendLine();
}
return s.ToString();
}
private static bool Validate(string sheetFile, ColDefReader colDefs, IDeserializer schemaDeserializer, Func<YamlDocument, EvaluationResults> evaluateSchema, string? baseSheetFile)
{
{
using var f = File.OpenText(sheetFile);
var yamlStream = new YamlStream();
yamlStream.Load(f);
if (yamlStream.Documents.Count > 1)
{
Log.AnnotatedError("Multiple YAML documents in file", new() { File = Path.GetFileName(sheetFile) });
return false;
}
var results = evaluateSchema(yamlStream.Documents[0]);
if (!results.IsValid)
{
foreach (var result in results.Details)
{
if (result.IsValid)
continue;
if (!result.HasErrors)
continue;
foreach (var error in result.Errors!.Values)
{
var s = new StringBuilder("Schema: ");
if (result.InstanceLocation.Count > 0)
s.Append(result.InstanceLocation);
else
s.Append('/');
s.Append(result.EvaluationPath);
var isInfo = result.EvaluationPath.Contains("allOf");
if (isInfo)
Log.AnnotatedInfo(error, new() { Title = s.ToString(), File = Path.GetFileName(sheetFile) });
else
Log.AnnotatedError(error, new() { Title = s.ToString(), File = Path.GetFileName(sheetFile) });
}
}
return false;
}
}
Sheet sheet;
{
using var f = File.OpenText(sheetFile);
sheet = schemaDeserializer.Deserialize<Sheet>(f);
}
if (sheet == null)
{
Log.AnnotatedError("Failed to deserialize", new() { File = Path.GetFileName(sheetFile) });
return false;
}
if (Path.GetFileNameWithoutExtension(sheet.Name) != sheet.Name)
{
Log.AnnotatedError($"Sheet name ({sheet.Name}) does not match file name", new() { File = Path.GetFileName(sheetFile) });
return false;
}
if (!colDefs.Sheets.TryGetValue(sheet.Name, out var cols))
{
Log.AnnotatedError("Failed to load columns", new() { File = Path.GetFileName(sheetFile) });
return false;
}
bool[] checks = [
Validate<ColumnCount>(sheet, cols, colDefs),
Validate<ColumnTypes>(sheet, cols, colDefs),
Validate<DisplayField>(sheet, cols, colDefs),
Validate<LinkConditionType>(sheet, cols, colDefs),
Validate<LinkSwitchField>(sheet, cols, colDefs),
Validate<Relations>(sheet, cols, colDefs),
Validate<SheetRefs>(sheet, cols, colDefs),
];
if (checks.Any(x => !x))
return false;
// if (baseSheetFile != null)
// {
// Sheet baseSheet;
// {
// using var f = File.OpenText(baseSheetFile);
// baseSheet = schemaDeserializer.Deserialize<Sheet>(f);
// }
// bool[] baseChecks = [
// Validate<FieldNamesAndTypes>(baseSheet, sheet, cols, colDefs)
// ];
// if (!baseChecks.Any(x => x))
// return false;
// }
return true;
}
private static bool Validate<T>(Sheet sheet, ExcelColumnDefinition[] cols, ColDefReader colDefs) where T : IValidator<T>
{
try
{
T.Validate(sheet, cols, colDefs);
return true;
}
catch (Exception ex)
{
Log.AnnotatedError($"{typeof(T).Name}: {ex.Message}", new() { Title = "Failed to validate", File = $"{sheet.Name}.yml" });
return false;
}
}
[Obsolete("Pending fields are no longer used, but this may be useful in the future")]
private static bool Validate<T>(Sheet baseSheet, Sheet newSheet, ExcelColumnDefinition[] cols, ColDefReader colDefs) where T : IBreakingValidator<T>
{
try
{
T.Validate(baseSheet, newSheet, cols, colDefs);
return true;
}
catch (Exception ex)
{
Log.AnnotatedError(ex.Message, new() { Title = $"Failed to validate breaking changes", File = $"{newSheet.Name}.yml" });
return false;
}
}
}