forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVersionVariableSerializer.cs
More file actions
105 lines (89 loc) · 3.42 KB
/
VersionVariableSerializer.cs
File metadata and controls
105 lines (89 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.IO.Abstractions;
using GitVersion.Extensions;
using GitVersion.Helpers;
namespace GitVersion.OutputVariables;
internal class VersionVariableSerializer(IFileSystem fileSystem) : IVersionVariableSerializer
{
public static GitVersionVariables FromJson(string json)
{
var variablePairs = JsonSerializer.Deserialize(json, VersionVariablesJsonContext.Custom.DictionaryStringString);
return FromDictionary(variablePairs);
}
public string ToJson(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));
}
return JsonSerializer.Serialize(variables, VersionVariablesJsonContext.Custom.VersionVariablesJsonModel);
}
public GitVersionVariables FromFile(string filePath)
{
try
{
var retryAction = new RetryAction<IOException, GitVersionVariables>();
return retryAction.Execute(() => FromFileInternal(filePath));
}
catch (AggregateException ex)
{
var lastException = ex.InnerExceptions.LastOrDefault() ?? ex.InnerException;
if (lastException != null)
{
throw lastException;
}
throw;
}
}
public void ToFile(GitVersionVariables gitVersionVariables, string filePath)
{
try
{
var retryAction = new RetryAction<IOException>();
retryAction.Execute(() => ToFileInternal(gitVersionVariables, filePath));
}
catch (AggregateException ex)
{
var lastException = ex.InnerExceptions.LastOrDefault() ?? ex.InnerException;
if (lastException != null)
{
throw lastException;
}
throw;
}
}
private static GitVersionVariables FromDictionary(IEnumerable<KeyValuePair<string, string>>? properties)
{
var type = typeof(GitVersionVariables);
var constructors = type.GetConstructors();
var ctor = constructors.Single();
var ctorArgs = ctor.GetParameters()
.Select(p => properties?.Single(v => string.Equals(v.Key, p.Name, StringComparison.InvariantCultureIgnoreCase)).Value)
.Cast<object>()
.ToArray();
var instance = Activator.CreateInstance(type, ctorArgs).NotNull();
return (GitVersionVariables)instance;
}
private GitVersionVariables FromFileInternal(string filePath)
{
var json = fileSystem.File.ReadAllText(filePath);
return FromJson(json);
}
private void ToFileInternal(GitVersionVariables gitVersionVariables, string filePath)
{
var json = ToJson(gitVersionVariables);
fileSystem.File.WriteAllText(filePath, json);
}
private static object? ChangeType(object? value, Type type)
{
if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(Nullable<>)) return Convert.ChangeType(value, type);
if (value == null || value.ToString()?.Length == 0)
{
return null;
}
type = Nullable.GetUnderlyingType(type)!;
return Convert.ChangeType(value, type);
}
}