Skip to content

Commit

Permalink
Refactor caching system in GitVersion
Browse files Browse the repository at this point in the history
Changes include updating cache file format from YML to JSON, removing unused methods, and optimizing file write/read operations. This allows to remove dependency on YamlDotNet. Various changes were also made to use more appropriate method identifiers and improve handling of cache file exceptions.
  • Loading branch information
arturcic committed Nov 23, 2023
1 parent bc81771 commit f1e5ea5
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 173 deletions.
4 changes: 2 additions & 2 deletions src/GitVersion.App.Tests/Helpers/ExecutionResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public virtual GitVersionVariables OutputVariables
{
get
{
var jsonStartIndex = Output.IndexOf("{", StringComparison.Ordinal);
var jsonEndIndex = Output.IndexOf("}", StringComparison.Ordinal);
var jsonStartIndex = Output.IndexOf('{');
var jsonEndIndex = Output.IndexOf('}');
var json = Output.Substring(jsonStartIndex, jsonEndIndex - jsonStartIndex + 1);

return VersionVariablesHelper.FromJson(json);
Expand Down
176 changes: 91 additions & 85 deletions src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class GitVersionExecutorTests : TestBase
{
private IFileSystem fileSystem;
private ILog log;
private IGitVersionCache gitVersionCache;
private GitVersionCache gitVersionCache;
private IServiceProvider sp;

[Test]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -145,7 +147,7 @@ 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]
Expand Down Expand Up @@ -228,40 +230,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();

Expand Down Expand Up @@ -295,33 +299,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();

Expand Down Expand Up @@ -571,7 +577,7 @@ private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVe

this.fileSystem = this.sp.GetRequiredService<IFileSystem>();
this.log = this.sp.GetRequiredService<ILog>();
this.gitVersionCache = this.sp.GetRequiredService<IGitVersionCache>();
this.gitVersionCache = (GitVersionCache)this.sp.GetRequiredService<IGitVersionCache>();

return this.sp.GetRequiredService<IGitVersionCalculateTool>();
}
Expand Down
1 change: 0 additions & 1 deletion src/GitVersion.Core/GitVersion.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<PackageReference Include="System.Net.Requests" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Options" />
<PackageReference Include="YamlDotNet" />
</ItemGroup>

<ItemGroup>
Expand Down
54 changes: 38 additions & 16 deletions src/GitVersion.Core/OutputVariables/VersionVariablesHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Text.Encodings.Web;
using GitVersion.Extensions;
using GitVersion.Helpers;
using YamlDotNet.Serialization;

namespace GitVersion.OutputVariables;

Expand All @@ -14,6 +13,22 @@ public static GitVersionVariables FromJson(string json)
return FromDictionary(variablePairs);
}

public static string ToJsonString(this GitVersionVariables gitVersionVariables)
{
var variablesType = typeof(VersionVariablesJsonModel);
var variables = new VersionVariablesJsonModel();

foreach (var (key, value) in gitVersionVariables.OrderBy(x => x.Key))
{
var propertyInfo = variablesType.GetProperty(key);
propertyInfo?.SetValue(variables, ChangeType(value, propertyInfo.PropertyType));
}

var serializeOptions = JsonSerializerOptions();

return JsonSerializer.Serialize(variables, serializeOptions);
}

public static GitVersionVariables FromFile(string filePath, IFileSystem fileSystem)
{
try
Expand All @@ -33,20 +48,23 @@ public static GitVersionVariables FromFile(string filePath, IFileSystem fileSyst
}
}

public static string ToJsonString(this GitVersionVariables gitVersionVariables)
public static void ToFile(GitVersionVariables gitVersionVariables, string filePath, IFileSystem fileSystem)
{
var variablesType = typeof(VersionVariablesJsonModel);
var variables = new VersionVariablesJsonModel();

foreach (var (key, value) in gitVersionVariables.OrderBy(x => x.Key))
try
{
var propertyInfo = variablesType.GetProperty(key);
propertyInfo?.SetValue(variables, ChangeType(value, propertyInfo.PropertyType));
var retryAction = new RetryAction<IOException>();
retryAction.Execute(() => ToFileInternal(gitVersionVariables, filePath, fileSystem));
}
catch (AggregateException ex)
{
var lastException = ex.InnerExceptions.LastOrDefault() ?? ex.InnerException;
if (lastException != null)
{
throw lastException;
}

var serializeOptions = JsonSerializerOptions();

return JsonSerializer.Serialize(variables, serializeOptions);
throw;
}
}

private static GitVersionVariables FromDictionary(IEnumerable<KeyValuePair<string, string>>? properties)
Expand All @@ -65,11 +83,15 @@ private static GitVersionVariables FromDictionary(IEnumerable<KeyValuePair<strin

private static GitVersionVariables FromFileInternal(string filePath, IFileSystem fileSystem)
{
using var stream = fileSystem.OpenRead(filePath);
using var reader = new StreamReader(stream);
var dictionary = new Deserializer().Deserialize<Dictionary<string, string>>(reader);
var versionVariables = FromDictionary(dictionary);
return versionVariables;
var json = fileSystem.ReadAllText(filePath);
var variables = FromJson(json);
return variables;
}

private static void ToFileInternal(GitVersionVariables gitVersionVariables, string filePath, IFileSystem fileSystem)
{
var json = gitVersionVariables.ToJsonString();
fileSystem.WriteAllText(filePath, json);
}

private static JsonSerializerOptions JsonSerializerOptions()
Expand Down
9 changes: 3 additions & 6 deletions src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -688,18 +688,14 @@ GitVersion.VersionCalculation.BaseVersion.ShouldIncrement.init -> void
GitVersion.VersionCalculation.BaseVersion.Source.get -> string!
GitVersion.VersionCalculation.BaseVersion.Source.init -> void
GitVersion.VersionCalculation.Caching.GitVersionCache
GitVersion.VersionCalculation.Caching.GitVersionCache.GetCacheDirectory() -> string!
GitVersion.VersionCalculation.Caching.GitVersionCache.GetCacheFileName(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey) -> string!
GitVersion.VersionCalculation.Caching.GitVersionCache.GitVersionCache(GitVersion.IFileSystem! fileSystem, GitVersion.Logging.ILog! log, GitVersion.IGitRepositoryInfo! repositoryInfo) -> void
GitVersion.VersionCalculation.Caching.GitVersionCache.LoadVersionVariablesFromDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! key) -> GitVersion.OutputVariables.GitVersionVariables?
GitVersion.VersionCalculation.Caching.GitVersionCache.LoadVersionVariablesFromDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey) -> GitVersion.OutputVariables.GitVersionVariables?
GitVersion.VersionCalculation.Caching.GitVersionCache.WriteVariablesToDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey, GitVersion.OutputVariables.GitVersionVariables! variablesFromCache) -> void
GitVersion.VersionCalculation.Caching.GitVersionCacheKey
GitVersion.VersionCalculation.Caching.GitVersionCacheKey.GitVersionCacheKey(string! value) -> void
GitVersion.VersionCalculation.Caching.GitVersionCacheKey.Value.get -> string!
GitVersion.VersionCalculation.Caching.IGitVersionCache
GitVersion.VersionCalculation.Caching.IGitVersionCache.GetCacheDirectory() -> string!
GitVersion.VersionCalculation.Caching.IGitVersionCache.GetCacheFileName(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey) -> string!
GitVersion.VersionCalculation.Caching.IGitVersionCache.LoadVersionVariablesFromDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! key) -> GitVersion.OutputVariables.GitVersionVariables?
GitVersion.VersionCalculation.Caching.IGitVersionCache.LoadVersionVariablesFromDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey) -> GitVersion.OutputVariables.GitVersionVariables?
GitVersion.VersionCalculation.Caching.IGitVersionCache.WriteVariablesToDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey, GitVersion.OutputVariables.GitVersionVariables! variablesFromCache) -> void
GitVersion.VersionCalculation.CommitMessageIncrementMode
GitVersion.VersionCalculation.CommitMessageIncrementMode.Disabled = 1 -> GitVersion.VersionCalculation.CommitMessageIncrementMode
Expand Down Expand Up @@ -869,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.ToJsonString(this GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables) -> string!
static GitVersion.ReferenceName.FromBranchName(string! branchName) -> GitVersion.ReferenceName!
static GitVersion.ReferenceName.Parse(string! canonicalName) -> GitVersion.ReferenceName!
Expand Down
Loading

0 comments on commit f1e5ea5

Please sign in to comment.