/// <summary> /// Publishes given ISharplinerDefinition into a YAML file. /// </summary> /// <param name="definition">ISharplinerDefinition object</param> /// <param name="failIfChanged"> /// True to fail the task if there are new changes to be published. /// This is for example used in the ValidateYamlsArePublished build step that checks that YAML changes were checked in. /// </param> /// <param name="collection">Type of the collection the definition is coming from (if it is)</param> private void PublishDefinition(ISharplinerDefinition definition, bool failIfChanged, Type?collection = null) { var path = GetDestinationPath(definition); var typeName = collection == null?definition.GetType().Name : collection.Name; _logger.LogMessage(MessageImportance.High, $"{typeName} / {Path.GetFileName(path)}:"); Validate(definition, typeName); string?hash = GetFileHash(path); Publish(definition); // Find out if there are new changes in the YAML if (hash == null) { if (failIfChanged) { _logger.LogError($" This definition hasn't been published yet!"); } else { _logger.LogMessage(MessageImportance.High, $" {typeName} created at {path}"); } } else { var newHash = GetFileHash(path); if (hash == newHash) { _logger.LogMessage(MessageImportance.High, $" No new changes to publish"); } else { if (failIfChanged) { _logger.LogError($" Changes detected between {typeName} and {path}"); } else { _logger.LogMessage(MessageImportance.High, $" Published new changes to {path}"); } } } }
/// <summary> /// Runs all validations configured on the definition /// </summary> /// <param name="definition">Definition</param> /// <param name="typeName">Type name, can include parent definition collection</param> private void Validate(ISharplinerDefinition definition, string typeName) { _logger.LogMessage(MessageImportance.High, $" Validating definition.."); foreach (var validation in definition.Validations) { var errors = validation.Validate(); if (errors.Any()) { Log(errors.OrderByDescending(e => e.Severity).First().Severity, $" Validation of definition {typeName} failed!"); } foreach (var error in errors) { Log(error.Severity, " - " + error.Message); } } }