-
Notifications
You must be signed in to change notification settings - Fork 760
/
Copy pathValidateCommand.cs
87 lines (68 loc) · 3.55 KB
/
ValidateCommand.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO.Abstractions;
using Bicep.Core;
using Bicep.RegistryModuleTool.Exceptions;
using Bicep.RegistryModuleTool.Extensions;
using Bicep.RegistryModuleTool.ModuleFiles;
using Bicep.RegistryModuleTool.ModuleFileValidators;
using Microsoft.Extensions.Logging;
namespace Bicep.RegistryModuleTool.Commands
{
public sealed class ValidateCommand : Command
{
public ValidateCommand()
: base("validate", "Validate files for the Bicep registry module")
{
}
public sealed class CommandHandler : BaseCommandHandler
{
private readonly BicepCompiler compiler;
public CommandHandler(IFileSystem fileSystem, BicepCompiler compiler, ILogger<ValidateCommand> logger)
: base(fileSystem, logger)
{
this.compiler = compiler;
}
protected override async Task<int> InvokeInternalAsync(InvocationContext context)
{
var valid = true;
this.Logger.LogInformation("Validating main Bicep file...");
var mainBicepFile = await MainBicepFile.OpenAsync(this.FileSystem, this.compiler, context.Console);
var descriptionsValidator = new DescriptionsValidator(this.Logger);
var metadataValidator = new BicepMetadataValidator(this.Logger);
valid &= await ValidateFileAsync(context.Console, () => mainBicepFile.ValidatedByAsync(descriptionsValidator, metadataValidator));
this.Logger.LogInformation("Validating main Bicep test file...");
var mainBicepTestFile = MainBicepTestFile.Open(this.FileSystem);
var testValidator = new TestValidator(this.Logger, context.Console, this.compiler, mainBicepFile);
valid &= await ValidateFileAsync(context.Console, () => mainBicepTestFile.ValidatedByAsync(testValidator));
this.Logger.LogInformation("Validating main ARM template file...");
var diffValidator = new DiffValidator(this.FileSystem, this.Logger, mainBicepFile);
var mainArmTemplateFile = await MainArmTemplateFile.OpenAsync(this.FileSystem);
valid &= await ValidateFileAsync(context.Console, () => mainArmTemplateFile.ValidatedByAsync(diffValidator));
this.Logger.LogInformation("Validating README file...");
var readmeFile = await ReadmeFile.OpenAsync(this.FileSystem);
valid &= await ValidateFileAsync(context.Console, () => readmeFile.ValidatedByAsync(diffValidator));
this.Logger.LogInformation("Validating version file...");
var versionFile = await VersionFile.OpenAsync(this.FileSystem);
var jsonSchemaValidator = new JsonSchemaValidator(this.Logger);
valid &= await ValidateFileAsync(context.Console, () => versionFile.ValidatedByAsync(jsonSchemaValidator, diffValidator));
return valid ? 0 : 1;
}
private static async Task<bool> ValidateFileAsync(IConsole console, Func<Task> fileValidator)
{
try
{
await fileValidator();
}
catch (InvalidModuleFileException exception)
{
console.WriteError(exception.Message);
return false;
}
return true;
}
}
}
}