-
Notifications
You must be signed in to change notification settings - Fork 663
Refactor caching system in GitVersion #3797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -14,6 +13,22 @@ public static GitVersionVariables FromJson(string json) | |||||||||||||
| return FromDictionary(variablePairs); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| public static string ToJsonString(this GitVersionVariables gitVersionVariables) | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we move the serialization of GitVersion/src/GitVersion.Core/GitVersion.Core.csproj Lines 14 to 19 in d3d16cc
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I started gradually to remove the depenencies, or at least with those non microsoft first, hope we can get to a point when Core is dependency free*. For now I want to get this one to a point when the build passes and then we can prepare a beta.4 |
||||||||||||||
| { | ||||||||||||||
| 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 | ||||||||||||||
|
|
@@ -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) | ||||||||||||||
|
|
@@ -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() | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, we shouldn't have a reference to
System.Text.Encodings.Webor anything else besidesSysteminGitVersion.Core.