/// <summary> /// Throws <see cref="ChangeLogValidationException"/> if version references are not valid. /// </summary> /// <param name="change">Instance of <see cref="Change"/> to check version references on.</param> /// <param name="changeLog">The instance of <see cref="ChangeLog"/> listing all versions.</param> public static void Validate(this Change change, ChangeLog changeLog) { if (change.ReleaseVersion == null) { throw new ChangeLogValidationException("Change has Release version reference missing."); } bool found = false; foreach (VersionRef versionRef in changeLog.Versions) { if (versionRef.VersionName == change.ReleaseVersion.VersionName && versionRef.RelativeFilePath == change.ReleaseVersion.RelativeFilePath) { found = true; } } if (!found) { throw new ChangeLogValidationException("Change has invalid version reference."); } }
/// <summary> /// Retrieves application changelog which lists all versions. /// </summary> /// <returns>Returns object representing application ChangeLog.</returns> public async Task <ChangeLog> GetChangeLog() { if (CachedChangeLog != null) { return(CachedChangeLog); } string changelogContent = await FileProvider.GetFile("changelog.json"); if (string.IsNullOrEmpty(changelogContent)) { return(null); } ChangeLog changeLog = ChangeLog.FromJson(changelogContent, this); if (changeLog != null) { CachedChangeLog = changeLog; } return(changeLog); }
/// <summary> /// Serializes instance of <see cref="ChangeLog"/> to JSON string. /// </summary> /// <param name="changeLog">Instance of <see cref="ChangeLog"/> to serialize.</param> /// <returns>Returns JSON string.</returns> public static string ToJson(this ChangeLog changeLog) { return(JsonConvert.SerializeObject(changeLog, Formatting.Indented)); }
/// <summary> /// Writes ChangeLog, Version and Change files using given instance of <see cref="IFileSaver"/> /// </summary> /// <param name="changeLog">ChangeLog to serialize.</param> /// <param name="saver">Service to save json strings to file paths.</param> /// <returns>True if was successful, false otherwise.</returns> public async Task <bool> Serialize(ChangeLog changeLog, IFileSaver saver) { if (changeLog == null) { return(false); } try { changeLog.Validate(); } catch (ChangeLogValidationException) { await saver.CancelPublish(); throw; } if (!(await saver.SaveFile(Path.Combine("temp", "changelog.json"), changeLog.ToJson()))) { return(false); } foreach (VersionRef versionRef in changeLog.Versions) { Version version = await versionRef.GetVersion(false); version.Changes.RemoveAll(c => c == null); if (version == null) { continue; } try { version.Validate(); } catch (ChangeLogValidationException) { await saver.CancelPublish(); throw; } if (!(await saver.SaveFile(Path.Combine("temp", version.VersionName, "version.json"), version.ToJson()))) { return(false); } foreach (ChangeRef changeRef in version.Changes) { Change change = await changeRef.GetChange(false); if (change == null) { continue; } try { change.Validate(changeLog); } catch (ChangeLogValidationException) { await saver.CancelPublish(); throw; } if (!(await saver.SaveFile(Path.Combine("temp", version.VersionName, "changes", $"{change.Guid}.json"), change.ToJson()))) { return(false); } } } bool success = await saver.PublishFromTemp(); // Clear caches CachedChangeLog = null; CachedVersions = new List <Version>(); CachedChanges = new List <Change>(); return(success); }