diff --git a/src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs b/src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs index d3bc050f36..d8dd9e2f59 100644 --- a/src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs +++ b/src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs @@ -91,33 +91,35 @@ public void CacheKeyForWorktree() [Test] public void CacheFileExistsOnDisk() { - const string versionCacheFileContent = @" - Major: 4 - Minor: 10 - Patch: 3 - PreReleaseTag: test.19 - PreReleaseTagWithDash: -test.19 - PreReleaseLabel: test - PreReleaseLabelWithDash: -test - PreReleaseNumber: 19 - WeightedPreReleaseNumber: 19 - BuildMetaData: - FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - MajorMinorPatch: 4.10.3 - SemVer: 4.10.3-test.19 - AssemblySemVer: 4.10.3.0 - AssemblySemFileVer: 4.10.3.0 - FullSemVer: 4.10.3-test.19 - InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - BranchName: feature/test - EscapedBranchName: feature-test - Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - ShortSha: dd2a29af - VersionSourceSha: 4.10.2 - CommitsSinceVersionSource: 19 - CommitDate: 2015-11-10 - UncommittedChanges: 0 - "; + const string versionCacheFileContent = """ + { + "Major": 4, + "Minor": 10, + "Patch": 3, + "PreReleaseTag": "test.19", + "PreReleaseTagWithDash": "-test.19", + "PreReleaseLabel": "test", + "PreReleaseLabelWithDash": "-test", + "PreReleaseNumber": 19, + "WeightedPreReleaseNumber": 19, + "BuildMetaData": null, + "FullBuildMetaData": "Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "MajorMinorPatch": "4.10.3", + "SemVer": "4.10.3-test.19", + "AssemblySemVer": "4.10.3.0", + "AssemblySemFileVer": "4.10.3.0", + "FullSemVer": "4.10.3-test.19", + "InformationalVersion": "4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "BranchName": "feature/test", + "EscapedBranchName": "feature-test", + "Sha": "dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "ShortSha": "dd2a29af", + "VersionSourceSha": "4.10.2", + "CommitsSinceVersionSource": 19, + "CommitDate": "2015-11-10T00:00:00.000Z", + "UncommittedChanges": 0 + } + """; var stringBuilder = new StringBuilder(); void Action(string s) => stringBuilder.AppendLine(s); @@ -145,37 +147,39 @@ public void CacheFileExistsOnDisk() var logsMessages = stringBuilder.ToString(); - logsMessages.ShouldContain("Deserializing version variables from cache file", Case.Insensitive, logsMessages); + logsMessages.ShouldContain("Loading version variables from disk cache file", Case.Insensitive, logsMessages); } [Test] public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDynamicallyCalculatedWithoutSavingInCache() { - const string versionCacheFileContent = @" - Major: 4 - Minor: 10 - Patch: 3 - PreReleaseTag: test.19 - PreReleaseTagWithDash: -test.19 - PreReleaseLabel: test - PreReleaseLabelWithDash: -test - PreReleaseNumber: 19 - BuildMetaData: - FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - MajorMinorPatch: 4.10.3 - SemVer: 4.10.3-test.19 - AssemblySemVer: 4.10.3.0 - AssemblySemFileVer: 4.10.3.0 - FullSemVer: 4.10.3-test.19 - InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - BranchName: feature/test - EscapedBranchName: feature-test - Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - ShortSha: dd2a29af - CommitsSinceVersionSource: 19 - CommitDate: 2015-11-10 - UncommittedChanges: 0 - "; + const string versionCacheFileContent = """ + { + "Major": 4, + "Minor": 10, + "Patch": 3, + "PreReleaseTag": "test.19", + "PreReleaseTagWithDash": "-test.19", + "PreReleaseLabel": "test", + "PreReleaseLabelWithDash": "-test", + "PreReleaseNumber": 19, + "BuildMetaData": null, + "FullBuildMetaData": "Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "MajorMinorPatch": "4.10.3", + "SemVer": "4.10.3-test.19", + "AssemblySemVer": "4.10.3.0", + "AssemblySemFileVer": "4.10.3.0", + "FullSemVer": "4.10.3-test.19", + "InformationalVersion": "4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "BranchName": "feature/test", + "EscapedBranchName": "feature-test", + "Sha": "dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "ShortSha": "dd2a29af", + "CommitsSinceVersionSource": 19, + "CommitDate": "2015-11-10T00:00:00.000Z", + "UncommittedChanges": 0 + } + """; using var fixture = new EmptyRepositoryFixture(); fixture.Repository.MakeACommit(); @@ -228,40 +232,42 @@ public void CacheFileIsMissing() gitVersionCalculator.CalculateVersionVariables(); var logsMessages = stringBuilder.ToString(); - logsMessages.ShouldContain("yml not found", Case.Insensitive, logsMessages); + logsMessages.ShouldContain(".json not found", Case.Insensitive, logsMessages); } [TestCase(ConfigurationFileLocator.DefaultFileName)] [TestCase(ConfigurationFileLocator.DefaultAlternativeFileName)] public void ConfigChangeInvalidatesCache(string configFileName) { - const string versionCacheFileContent = @" - Major: 4 - Minor: 10 - Patch: 3 - PreReleaseTag: test.19 - PreReleaseTagWithDash: -test.19 - PreReleaseLabel: test - PreReleaseLabelWithDash: -test - PreReleaseNumber: 19 - WeightedPreReleaseNumber: 19 - BuildMetaData: - FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - MajorMinorPatch: 4.10.3 - SemVer: 4.10.3-test.19 - AssemblySemVer: 4.10.3.0 - AssemblySemFileVer: 4.10.3.0 - FullSemVer: 4.10.3-test.19 - InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - BranchName: feature/test - EscapedBranchName: feature-test - Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - ShortSha: dd2a29af - VersionSourceSha: 4.10.2 - CommitsSinceVersionSource: 19 - CommitDate: 2015-11-10 - UncommittedChanges: 0 - "; + const string versionCacheFileContent = """ + { + "Major": 4, + "Minor": 10, + "Patch": 3, + "PreReleaseTag": "test.19", + "PreReleaseTagWithDash": "-test.19", + "PreReleaseLabel": "test", + "PreReleaseLabelWithDash": "-test", + "PreReleaseNumber": 19, + "WeightedPreReleaseNumber": 19, + "BuildMetaData": null, + "FullBuildMetaData": "Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "MajorMinorPatch": "4.10.3", + "SemVer": "4.10.3-test.19", + "AssemblySemVer": "4.10.3.0", + "AssemblySemFileVer": "4.10.3.0", + "FullSemVer": "4.10.3-test.19", + "InformationalVersion": "4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "BranchName": "feature/test", + "EscapedBranchName": "feature-test", + "Sha": "dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "ShortSha": "dd2a29af", + "VersionSourceSha": "4.10.2", + "CommitsSinceVersionSource": 19, + "CommitDate": "2015-11-10T00:00:00.000Z", + "UncommittedChanges": 0 + } + """; using var fixture = new EmptyRepositoryFixture(); @@ -295,33 +301,35 @@ public void ConfigChangeInvalidatesCache(string configFileName) [Test] public void NoCacheBypassesCache() { - const string versionCacheFileContent = @" - Major: 4 - Minor: 10 - Patch: 3 - PreReleaseTag: test.19 - PreReleaseTagWithDash: -test.19 - PreReleaseLabel: test - PreReleaseLabelWithDash: -test - PreReleaseNumber: 19 - WeightedPreReleaseNumber: 19 - BuildMetaData: - FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - MajorMinorPatch: 4.10.3 - SemVer: 4.10.3-test.19 - AssemblySemVer: 4.10.3.0 - AssemblySemFileVer: 4.10.3.0 - FullSemVer: 4.10.3-test.19 - InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - BranchName: feature/test - EscapedBranchName: feature-test - Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f - ShortSha: dd2a29af - VersionSourceSha: 4.10.2 - CommitsSinceVersionSource: 19 - CommitDate: 2015-11-10 - UncommittedChanges: 0 - "; + const string versionCacheFileContent = """ + { + "Major": 4, + "Minor": 10, + "Patch": 3, + "PreReleaseTag": "test.19", + "PreReleaseTagWithDash": "-test.19", + "PreReleaseLabel": "test", + "PreReleaseLabelWithDash": "-test", + "PreReleaseNumber": 19, + "WeightedPreReleaseNumber": 19, + "BuildMetaData": null, + "FullBuildMetaData": "Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "MajorMinorPatch": "4.10.3", + "SemVer": "4.10.3-test.19", + "AssemblySemVer": "4.10.3.0", + "AssemblySemFileVer": "4.10.3.0", + "FullSemVer": "4.10.3-test.19", + "InformationalVersion": "4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "BranchName": "feature/test", + "EscapedBranchName": "feature-test", + "Sha": "dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f", + "ShortSha": "dd2a29af", + "VersionSourceSha": "4.10.2", + "CommitsSinceVersionSource": 19, + "CommitDate": "2015-11-10T00:00:00.000Z", + "UncommittedChanges": 0 + } + """; using var fixture = new EmptyRepositoryFixture(); diff --git a/src/GitVersion.Core/GitVersion.Core.csproj b/src/GitVersion.Core/GitVersion.Core.csproj index 035286f4eb..564781044c 100644 --- a/src/GitVersion.Core/GitVersion.Core.csproj +++ b/src/GitVersion.Core/GitVersion.Core.csproj @@ -15,7 +15,6 @@ - diff --git a/src/GitVersion.Core/OutputVariables/VersionVariablesHelper.cs b/src/GitVersion.Core/OutputVariables/VersionVariablesHelper.cs index 3d002c0b06..02f06e4c27 100644 --- a/src/GitVersion.Core/OutputVariables/VersionVariablesHelper.cs +++ b/src/GitVersion.Core/OutputVariables/VersionVariablesHelper.cs @@ -1,7 +1,6 @@ using System.Text.Encodings.Web; using GitVersion.Extensions; using GitVersion.Helpers; -using YamlDotNet.Serialization; namespace GitVersion.OutputVariables; @@ -49,6 +48,25 @@ public static GitVersionVariables FromFile(string filePath, IFileSystem fileSyst } } + public static void ToFile(GitVersionVariables gitVersionVariables, string filePath, IFileSystem fileSystem) + { + try + { + var retryAction = new RetryAction(); + retryAction.Execute(() => ToFileInternal(gitVersionVariables, filePath, fileSystem)); + } + catch (AggregateException ex) + { + var lastException = ex.InnerExceptions.LastOrDefault() ?? ex.InnerException; + if (lastException != null) + { + throw lastException; + } + + throw; + } + } + private static GitVersionVariables FromDictionary(IEnumerable>? properties) { var type = typeof(GitVersionVariables); @@ -65,11 +83,14 @@ private static GitVersionVariables FromDictionary(IEnumerable>(reader); - var versionVariables = FromDictionary(dictionary); - return versionVariables; + var json = fileSystem.ReadAllText(filePath); + return FromJson(json); + } + + private static void ToFileInternal(GitVersionVariables gitVersionVariables, string filePath, IFileSystem fileSystem) + { + var json = gitVersionVariables.ToJson(); + fileSystem.WriteAllText(filePath, json); } private static JsonSerializerOptions JsonSerializerOptions() => new() { WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, Converters = { new VersionVariablesJsonStringConverter() } }; diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index 204018aae8..cec107bea0 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -865,6 +865,7 @@ static GitVersion.Logging.LogExtensions.Warning(this GitVersion.Logging.ILog! lo static GitVersion.Logging.LogExtensions.Write(this GitVersion.Logging.ILog! log, GitVersion.Logging.LogLevel level, string! format, params object![]! args) -> void static GitVersion.OutputVariables.VersionVariablesHelper.FromFile(string! filePath, GitVersion.IFileSystem! fileSystem) -> GitVersion.OutputVariables.GitVersionVariables! static GitVersion.OutputVariables.VersionVariablesHelper.FromJson(string! json) -> GitVersion.OutputVariables.GitVersionVariables! +static GitVersion.OutputVariables.VersionVariablesHelper.ToFile(GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables, string! filePath, GitVersion.IFileSystem! fileSystem) -> void static GitVersion.OutputVariables.VersionVariablesHelper.ToJson(this GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables) -> string! static GitVersion.ReferenceName.FromBranchName(string! branchName) -> GitVersion.ReferenceName! static GitVersion.ReferenceName.Parse(string! canonicalName) -> GitVersion.ReferenceName! diff --git a/src/GitVersion.Core/VersionCalculation/Caching/GitVersionCache.cs b/src/GitVersion.Core/VersionCalculation/Caching/GitVersionCache.cs index 1f461093ef..73ecddcf37 100644 --- a/src/GitVersion.Core/VersionCalculation/Caching/GitVersionCache.cs +++ b/src/GitVersion.Core/VersionCalculation/Caching/GitVersionCache.cs @@ -2,7 +2,6 @@ using GitVersion.Helpers; using GitVersion.Logging; using GitVersion.OutputVariables; -using YamlDotNet.Serialization; namespace GitVersion.VersionCalculation.Caching; @@ -22,61 +21,48 @@ public GitVersionCache(IFileSystem fileSystem, ILog log, IGitRepositoryInfo repo public void WriteVariablesToDiskCache(GitVersionCacheKey cacheKey, GitVersionVariables versionVariables) { var cacheFileName = GetCacheFileName(cacheKey); - - Dictionary dictionary; - using (this.log.IndentLog("Creating dictionary")) - { - dictionary = versionVariables.ToDictionary(x => x.Key, x => x.Value); - } - - void WriteCacheOperation() + using (this.log.IndentLog($"Write version variables to cache file {cacheFileName}")) { - using var stream = this.fileSystem.OpenWrite(cacheFileName); - using var sw = new StreamWriter(stream); - using (this.log.IndentLog("Storing version variables to cache file " + cacheFileName)) + try { - var serializer = new Serializer(); - serializer.Serialize(sw, dictionary); + VersionVariablesHelper.ToFile(versionVariables, cacheFileName, this.fileSystem); + } + catch (Exception ex) + { + this.log.Error($"Unable to write cache file {cacheFileName}. Got {ex.GetType().FullName} exception."); } } - - var retryOperation = new RetryAction(6); - retryOperation.Execute(WriteCacheOperation); } public GitVersionVariables? LoadVersionVariablesFromDiskCache(GitVersionCacheKey cacheKey) { - using (this.log.IndentLog("Loading version variables from disk cache")) + var cacheFileName = GetCacheFileName(cacheKey); + using (this.log.IndentLog($"Loading version variables from disk cache file {cacheFileName}")) { - var cacheFileName = GetCacheFileName(cacheKey); if (!this.fileSystem.Exists(cacheFileName)) { - this.log.Info("Cache file " + cacheFileName + " not found."); + this.log.Info($"Cache file {cacheFileName} not found."); return null; } - - using (this.log.IndentLog("Deserializing version variables from cache file " + cacheFileName)) + try + { + var loadedVariables = VersionVariablesHelper.FromFile(cacheFileName, this.fileSystem); + return loadedVariables; + } + catch (Exception ex) { + this.log.Warning($"Unable to read cache file {cacheFileName}, deleting it."); + this.log.Info(ex.ToString()); try { - var loadedVariables = VersionVariablesHelper.FromFile(cacheFileName, this.fileSystem); - return loadedVariables; + this.fileSystem.Delete(cacheFileName); } - catch (Exception ex) + catch (Exception deleteEx) { - this.log.Warning("Unable to read cache file " + cacheFileName + ", deleting it."); - this.log.Info(ex.ToString()); - try - { - this.fileSystem.Delete(cacheFileName); - } - catch (Exception deleteEx) - { - this.log.Warning($"Unable to delete corrupted version cache file {cacheFileName}. Got {deleteEx.GetType().FullName} exception."); - } - - return null; + this.log.Warning($"Unable to delete corrupted version cache file {cacheFileName}. Got {deleteEx.GetType().FullName} exception."); } + + return null; } } } @@ -103,5 +89,5 @@ private string PrepareCacheDirectory() return cacheDir; } - private static string GetCacheFileName(GitVersionCacheKey key, string cacheDir) => PathHelper.Combine(cacheDir, string.Concat(key.Value, ".yml")); + private static string GetCacheFileName(GitVersionCacheKey key, string cacheDir) => PathHelper.Combine(cacheDir, string.Concat(key.Value, ".json")); } diff --git a/src/GitVersion.Output/OutputGenerator/OutputGenerator.cs b/src/GitVersion.Output/OutputGenerator/OutputGenerator.cs index b59d0e5516..5963515706 100644 --- a/src/GitVersion.Output/OutputGenerator/OutputGenerator.cs +++ b/src/GitVersion.Output/OutputGenerator/OutputGenerator.cs @@ -35,17 +35,19 @@ public void Execute(GitVersionVariables variables, OutputContext context) { this.buildAgent.WriteIntegration(this.console.WriteLine, variables, context.UpdateBuildNumber ?? true); } + + var json = variables.ToJson(); if (gitVersionOptions.Output.Contains(OutputType.File)) { var retryOperation = new RetryAction(); - retryOperation.Execute(() => this.fileSystem.WriteAllText(context.OutputFile, variables.ToJson())); + retryOperation.Execute(() => this.fileSystem.WriteAllText(context.OutputFile, json)); } if (!gitVersionOptions.Output.Contains(OutputType.Json)) return; if (gitVersionOptions.ShowVariable is null && gitVersionOptions.Format is null) { - this.console.WriteLine(variables.ToJson()); + this.console.WriteLine(json); return; }