protected override int Invoke(InvocationContext context) { // Read or create main Bicep file. this.Logger.LogInformation("Ensure {MainBicepFile} exists...", "main Bicep file"); var mainBicepFile = MainBicepFile.EnsureInFileSystem(this.FileSystem); // Read or create metadata file. this.Logger.LogInformation("Ensure {MetadataFile} exists...", "metadata file"); var metadataFile = MetadataFile.EnsureInFileSystem(this.FileSystem); // Generate main ARM template file. var bicepCliProxy = new BicepCliProxy(this.environmentProxy, this.processProxy, this.FileSystem, this.Logger, context.Console); var mainArmTemplateFile = this.GenerateFileAndLogInformation("main ARM template file", () => MainArmTemplateFile .Generate(this.FileSystem, bicepCliProxy, mainBicepFile) .WriteToFileSystem(FileSystem)); // Generate main ARM template parameters file. var mainArmTemplateParametersFile = this.GenerateFileAndLogInformation("main ARM template parameters file", () => MainArmTemplateParametersFile .Generate(this.FileSystem, mainArmTemplateFile) .WriteToFileSystem(this.FileSystem)); // Generate README file. this.GenerateFileAndLogInformation("README file", () => ReadmeFile .Generate(this.FileSystem, metadataFile, mainArmTemplateFile) .WriteToFileSystem(this.FileSystem)); // Generate version file. this.GenerateFileAndLogInformation("version file", () => VersionFile .Generate(this.FileSystem) .WriteToFileSystem(this.FileSystem)); return(0); }
public void Validate(MainBicepFile file) { this.logger.LogInformation("Making sure descriptions are defined for all parameters and outputs..."); var noDescriptionParameters = latestMainArmTemplateFile.Parameters.Where(parameter => string.IsNullOrEmpty(parameter.Description)); var noDescriptionOutputs = latestMainArmTemplateFile.Outputs.Where(output => string.IsNullOrEmpty(output.Description)); if (noDescriptionParameters.IsEmpty() && noDescriptionOutputs.IsEmpty()) { return; } var errorMessageBuilder = new StringBuilder(); if (noDescriptionParameters.Any()) { errorMessageBuilder.AppendLine($"The file \"{file.Path}\" is invalid. Descriptions for the following parameters are missing:"); foreach (var parameter in noDescriptionParameters) { errorMessageBuilder.AppendLine($" - {parameter.Name}"); } } if (noDescriptionOutputs.Any()) { if (errorMessageBuilder.Length > 0) { errorMessageBuilder.AppendLine(); } errorMessageBuilder.AppendLine($"The file \"{file.Path}\" is invalid. Descriptions for the following outputs are missing:"); foreach (var output in noDescriptionOutputs) { errorMessageBuilder.AppendLine($" - {output.Name}"); } } throw new InvalidModuleException(errorMessageBuilder.ToString()); }
protected override int Invoke(InvocationContext context) { var valid = true; this.Logger.LogInformation("Validating module path..."); valid &= Validate(context.Console, () => ValidateModulePath(this.FileSystem)); this.Logger.LogInformation("Validating main Bicep file..."); var bicepCliProxy = new BicepCliProxy(this.environmentProxy, this.processProxy, this.FileSystem, this.Logger, context.Console); var mainBicepFile = MainBicepFile.ReadFromFileSystem(this.FileSystem); // This also validates that the main Bicep file can be built without errors. var latestMainArmTemplateFile = MainArmTemplateFile.Generate(this.FileSystem, bicepCliProxy, mainBicepFile); var descriptionsValidator = new DescriptionsValidator(this.Logger, latestMainArmTemplateFile); valid &= Validate(context.Console, () => mainBicepFile.ValidatedBy(descriptionsValidator)); var testValidator = new TestValidator(this.FileSystem, this.Logger, bicepCliProxy, latestMainArmTemplateFile); var jsonSchemaValidator = new JsonSchemaValidator(this.Logger); var diffValidator = new DiffValidator(this.FileSystem, this.Logger, latestMainArmTemplateFile); this.Logger.LogInformation("Validating main Bicep test file..."); valid &= Validate(context.Console, () => MainBicepTestFile.ReadFromFileSystem(this.FileSystem).ValidatedBy(testValidator)); this.Logger.LogInformation("Validating main ARM template file..."); valid &= Validate(context.Console, () => MainArmTemplateFile.ReadFromFileSystem(this.FileSystem).ValidatedBy(diffValidator)); this.Logger.LogInformation("Validating metadata file..."); valid &= Validate(context.Console, () => MetadataFile.ReadFromFileSystem(this.FileSystem).ValidatedBy(jsonSchemaValidator)); this.Logger.LogInformation("Validating README file..."); valid &= Validate(context.Console, () => ReadmeFile.ReadFromFileSystem(this.FileSystem).ValidatedBy(diffValidator)); this.Logger.LogInformation("Validating version file..."); valid &= Validate(context.Console, () => VersionFile.ReadFromFileSystem(this.FileSystem).ValidatedBy(jsonSchemaValidator, diffValidator)); return(valid ? 0 : 1); }
public static MainArmTemplateFile Generate(IFileSystem fileSystem, BicepCliProxy bicepCliProxy, MainBicepFile mainBicepFile) { var tempFilePath = fileSystem.Path.GetTempFileName(); try { bicepCliProxy.Build(mainBicepFile.Path, tempFilePath); } catch (Exception) { fileSystem.File.Delete(tempFilePath); throw; } using var tempFileStream = fileSystem.FileStream.CreateDeleteOnCloseStream(tempFilePath); using var streamReader = new StreamReader(tempFileStream); var path = fileSystem.Path.GetFullPath(FileName); var content = streamReader.ReadToEnd(); return(new(path, content)); }
public DescriptionsValidatorTests() { this.fileSystem = MockFileSystemFactory.CreateFileSystemWithValidFiles(); this.fileToValidate = MainBicepFile.ReadFromFileSystem(this.fileSystem); }