Skip to content

Commit dacd364

Browse files
committed
changed ConfigurationSerializer to implement IConfigurationSerializer
1 parent 6d61efa commit dacd364

21 files changed

+130
-114
lines changed

src/GitVersion.App/GitVersionExecutor.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal class GitVersionExecutor(
1111
IConsole console,
1212
IConfigurationFileLocator configurationFileLocator,
1313
IConfigurationProvider configurationProvider,
14+
IConfigurationSerializer configurationSerializer,
1415
IGitVersionCalculateTool gitVersionCalculateTool,
1516
IGitVersionOutputTool gitVersionOutputTool,
1617
IVersionWriter versionWriter,
@@ -22,6 +23,8 @@ internal class GitVersionExecutor(
2223
private readonly IConsole console = console.NotNull();
2324
private readonly IConfigurationFileLocator configurationFileLocator = configurationFileLocator.NotNull();
2425
private readonly IConfigurationProvider configurationProvider = configurationProvider.NotNull();
26+
private readonly IConfigurationSerializer configurationSerializer = configurationSerializer.NotNull();
27+
2528
private readonly IGitVersionCalculateTool gitVersionCalculateTool = gitVersionCalculateTool.NotNull();
2629
private readonly IGitVersionOutputTool gitVersionOutputTool = gitVersionOutputTool.NotNull();
2730
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
@@ -144,7 +147,8 @@ private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int e
144147
this.configurationFileLocator.Verify(workingDirectory, this.repositoryInfo.ProjectRootDirectory);
145148
}
146149
var configuration = this.configurationProvider.Provide();
147-
this.console.WriteLine(configuration.ToJsonString());
150+
var configurationString = configurationSerializer.Serialize(configuration);
151+
this.console.WriteLine(configurationString);
148152
exitCode = 0;
149153
return true;
150154
}

src/GitVersion.Configuration.Tests/Configuration/ConfigurationProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void CanWriteOutEffectiveConfiguration()
182182
{
183183
var configuration = this.configurationProvider.ProvideForDirectory(this.repoPath);
184184

185-
configuration.ToJsonString().ShouldMatchApproved();
185+
new ConfigurationSerializer().Serialize(configuration).ShouldMatchApproved();
186186
}
187187

188188
[Test]

src/GitVersion.Configuration.Tests/Configuration/IgnoreConfigurationTests.cs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,54 @@ namespace GitVersion.Core.Tests.Configuration;
77
[TestFixture]
88
public class IgnoreConfigurationTests : TestBase
99
{
10+
private readonly ConfigurationSerializer serializer = new();
11+
1012
[Test]
1113
public void CanDeserialize()
1214
{
13-
const string yaml = @"
14-
ignore:
15-
sha: [b6c0c9fda88830ebcd563e500a5a7da5a1658e98]
16-
commits-before: 2015-10-23T12:23:15
17-
";
15+
const string yaml =
16+
"""
17+
ignore:
18+
sha: [b6c0c9fda88830ebcd563e500a5a7da5a1658e98]
19+
commits-before: 2015-10-23T12:23:15
20+
""";
1821

19-
using var reader = new StringReader(yaml);
20-
var configuration = ConfigurationSerializer.Read(reader);
22+
var configuration = serializer.ReadConfiguration(yaml);
2123

24+
configuration.ShouldNotBeNull();
2225
configuration.Ignore.ShouldNotBeNull();
2326
configuration.Ignore.Shas.ShouldNotBeEmpty();
24-
configuration.Ignore.Shas.ShouldBe(new[] { "b6c0c9fda88830ebcd563e500a5a7da5a1658e98" });
27+
configuration.Ignore.Shas.ShouldBe(["b6c0c9fda88830ebcd563e500a5a7da5a1658e98"]);
2528
configuration.Ignore.Before.ShouldBe(DateTimeOffset.Parse("2015-10-23T12:23:15"));
2629
}
2730

2831
[Test]
2932
public void ShouldSupportsOtherSequenceFormat()
3033
{
31-
const string yaml = @"
32-
ignore:
33-
sha:
34-
- b6c0c9fda88830ebcd563e500a5a7da5a1658e98
35-
- 6c19c7c219ecf8dbc468042baefa73a1b213e8b1
36-
";
34+
const string yaml =
35+
"""
36+
ignore:
37+
sha:
38+
- b6c0c9fda88830ebcd563e500a5a7da5a1658e98
39+
- 6c19c7c219ecf8dbc468042baefa73a1b213e8b1
40+
""";
3741

38-
using var reader = new StringReader(yaml);
39-
var configuration = ConfigurationSerializer.Read(reader);
42+
var configuration = serializer.ReadConfiguration(yaml);
4043

44+
configuration.ShouldNotBeNull();
4145
configuration.Ignore.ShouldNotBeNull();
4246
configuration.Ignore.Shas.ShouldNotBeEmpty();
43-
configuration.Ignore.Shas.ShouldBe(new[] { "b6c0c9fda88830ebcd563e500a5a7da5a1658e98", "6c19c7c219ecf8dbc468042baefa73a1b213e8b1" });
47+
configuration.Ignore.Shas.ShouldBe(["b6c0c9fda88830ebcd563e500a5a7da5a1658e98", "6c19c7c219ecf8dbc468042baefa73a1b213e8b1"]);
4448
}
4549

4650
[Test]
4751
public void WhenNotInConfigShouldHaveDefaults()
4852
{
49-
const string yaml = @"
50-
next-version: 1.0
51-
";
53+
const string yaml = "next-version: 1.0";
5254

53-
using var reader = new StringReader(yaml);
54-
var configuration = ConfigurationSerializer.Read(reader);
55+
var configuration = serializer.ReadConfiguration(yaml);
5556

57+
configuration.ShouldNotBeNull();
5658
configuration.Ignore.ShouldNotBeNull();
5759
configuration.Ignore.Shas.ShouldBeEmpty();
5860
configuration.Ignore.Before.ShouldBe(null);
@@ -61,13 +63,13 @@ public void WhenNotInConfigShouldHaveDefaults()
6163
[Test]
6264
public void WhenBadDateFormatShouldFail()
6365
{
64-
const string yaml = @"
65-
ignore:
66-
commits-before: bad format date
67-
";
66+
const string yaml =
67+
"""
68+
ignore:
69+
commits-before: bad format date
70+
""";
6871

69-
using var reader = new StringReader(yaml);
70-
Should.Throw<YamlException>(() => ConfigurationSerializer.Read(reader));
72+
Should.Throw<YamlException>(() => serializer.ReadConfiguration(yaml));
7173
}
7274

7375
[Test]

src/GitVersion.Configuration/ConfigurationFileLocator.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace GitVersion.Configuration;
66

7-
internal class ConfigurationFileLocator(IFileSystem fileSystem, IOptions<GitVersionOptions> options)
7+
internal class ConfigurationFileLocator(IFileSystem fileSystem, IConfigurationSerializer configurationSerializer, IOptions<GitVersionOptions> options)
88
: IConfigurationFileLocator
99
{
1010
public const string DefaultFileName = "GitVersion.yml";
@@ -29,7 +29,9 @@ public IGitVersionConfiguration ReadConfiguration(string? configFilePath)
2929
if (configFilePath == null || !fileSystem.Exists(configFilePath)) return GitHubFlowConfigurationBuilder.New.Build();
3030

3131
var readAllText = fileSystem.ReadAllText(configFilePath);
32-
return ConfigurationSerializer.Read(new StringReader(readAllText));
32+
var configuration = configurationSerializer.ReadConfiguration(readAllText)
33+
?? GitHubFlowConfigurationBuilder.New.Build();
34+
return configuration;
3335
}
3436

3537
public IReadOnlyDictionary<object, object?>? ReadOverrideConfiguration(string? configFilePath)
@@ -38,7 +40,7 @@ public IGitVersionConfiguration ReadConfiguration(string? configFilePath)
3840

3941
var readAllText = fileSystem.ReadAllText(configFilePath);
4042

41-
return ConfigurationSerializer.Deserialize<Dictionary<object, object?>>(readAllText);
43+
return configurationSerializer.Deserialize<Dictionary<object, object?>>(readAllText);
4244
}
4345

4446
private bool HasConfigurationFile(string? workingDirectory, out string? path)

src/GitVersion.Configuration/ConfigurationHelper.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@ namespace GitVersion.Configuration;
44

55
internal class ConfigurationHelper
66
{
7-
private string Yaml => this._yaml ??= this._dictionary == null
8-
? ConfigurationSerializer.Serialize(this.configuration!)
9-
: ConfigurationSerializer.Serialize(this._dictionary);
10-
private string? _yaml;
7+
private static ConfigurationSerializer Serializer => new();
8+
private string Yaml => this.yaml ??= this.dictionary == null
9+
? Serializer.Serialize(this.configuration!)
10+
: Serializer.Serialize(this.dictionary);
11+
private string? yaml;
1112

1213
internal IReadOnlyDictionary<object, object?> Dictionary
1314
{
1415
get
1516
{
16-
if (this._dictionary == null)
17+
if (this.dictionary == null)
1718
{
18-
this._yaml ??= ConfigurationSerializer.Serialize(this.configuration!);
19-
this._dictionary = ConfigurationSerializer.Deserialize<Dictionary<object, object?>>(this._yaml);
19+
this.yaml ??= Serializer.Serialize(this.configuration!);
20+
this.dictionary = Serializer.Deserialize<Dictionary<object, object?>>(this.yaml);
2021
}
21-
return this._dictionary;
22+
return this.dictionary;
2223
}
2324
}
24-
private IReadOnlyDictionary<object, object?>? _dictionary;
25+
private IReadOnlyDictionary<object, object?>? dictionary;
2526

26-
public IGitVersionConfiguration Configuration => this.configuration ??= ConfigurationSerializer.Deserialize<GitVersionConfiguration>(Yaml);
27+
public IGitVersionConfiguration Configuration => this.configuration ??= Serializer.Deserialize<GitVersionConfiguration>(Yaml);
2728
private IGitVersionConfiguration? configuration;
2829

29-
internal ConfigurationHelper(string yaml) => this._yaml = yaml.NotNull();
30+
internal ConfigurationHelper(string yaml) => this.yaml = yaml.NotNull();
3031

31-
internal ConfigurationHelper(IReadOnlyDictionary<object, object?> dictionary) => this._dictionary = dictionary.NotNull();
32+
internal ConfigurationHelper(IReadOnlyDictionary<object, object?> dictionary) => this.dictionary = dictionary.NotNull();
3233

3334
public ConfigurationHelper(IGitVersionConfiguration configuration) => this.configuration = configuration.NotNull();
3435

@@ -38,10 +39,10 @@ public void Override(IReadOnlyDictionary<object, object?> value)
3839

3940
if (value.Any())
4041
{
41-
var dictionary = Dictionary.ToDictionary(element => element.Key, element => element.Value);
42-
Merge(dictionary, value);
43-
this._dictionary = dictionary;
44-
this._yaml = null;
42+
var map = Dictionary.ToDictionary(element => element.Key, element => element.Value);
43+
Merge(map, value);
44+
this.dictionary = map;
45+
this.yaml = null;
4546
this.configuration = null;
4647
}
4748
}

src/GitVersion.Configuration/ConfigurationSerializer.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace GitVersion.Configuration;
66

7-
internal static class ConfigurationSerializer
7+
internal class ConfigurationSerializer : IConfigurationSerializer
88
{
99
private static IDeserializer Deserializer => new DeserializerBuilder()
1010
.WithNamingConvention(HyphenatedNamingConvention.Instance)
@@ -16,18 +16,9 @@ internal static class ConfigurationSerializer
1616
.WithTypeInspector(inspector => new JsonPropertyNameInspector(inspector))
1717
.WithNamingConvention(HyphenatedNamingConvention.Instance).Build();
1818

19-
public static T Deserialize<T>(string input) => Deserializer.Deserialize<T>(input);
20-
21-
public static string Serialize(object graph) => Serializer.Serialize(graph);
22-
23-
public static IGitVersionConfiguration Read(TextReader reader)
24-
{
25-
var configuration = Deserializer.Deserialize<GitVersionConfiguration?>(reader);
26-
return configuration ?? GitHubFlowConfigurationBuilder.New.Build();
27-
}
28-
29-
public static void Write(IGitVersionConfiguration configuration, TextWriter writer)
30-
=> Serializer.Serialize(writer, configuration);
19+
public T Deserialize<T>(string input) => Deserializer.Deserialize<T>(input);
20+
public string Serialize(object graph) => Serializer.Serialize(graph);
21+
public IGitVersionConfiguration? ReadConfiguration(string input) => Deserialize<GitVersionConfiguration?>(input);
3122

3223
private sealed class JsonPropertyNameInspector(ITypeInspector innerTypeDescriptor) : TypeInspectorSkeleton
3324
{

src/GitVersion.Configuration/GitVersionConfiguration.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,10 @@ IReadOnlyDictionary<string, IBranchConfiguration> IGitVersionConfiguration.Branc
149149

150150
public override IBranchConfiguration Inherit(IBranchConfiguration configuration) => throw new NotSupportedException();
151151

152-
public string ToJsonString()
153-
{
154-
var stringBuilder = new StringBuilder();
155-
using var stream = new StringWriter(stringBuilder);
156-
ConfigurationSerializer.Write(this, stream);
157-
stream.Flush();
158-
return stringBuilder.ToString();
159-
}
160-
161152
public IBranchConfiguration GetEmptyBranchConfiguration() => new BranchConfiguration
162153
{
163154
RegularExpression = string.Empty,
164-
Label = ConfigurationConstants.BranchNamePlaceholder,
155+
Label = BranchNamePlaceholder,
165156
Increment = IncrementStrategy.Inherit
166157
};
167158
}

src/GitVersion.Configuration/GitVersionConfigurationModule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class GitVersionConfigurationModule : IGitVersionModule
88
public void RegisterTypes(IServiceCollection services)
99
{
1010
services.AddSingleton<IGitVersionCacheKeyFactory, GitVersionCacheKeyFactory>();
11+
services.AddSingleton<IConfigurationSerializer, ConfigurationSerializer>();
1112
services.AddSingleton<IConfigurationProvider, ConfigurationProvider>();
1213
services.AddSingleton<IConfigurationFileLocator, ConfigurationFileLocator>();
1314
}

src/GitVersion.Configuration/SupportedWorkflows/WorkflowManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace GitVersion.Configuration.SupportedWorkflows;
55
internal static class WorkflowManager
66
{
77
private static readonly string ResourceNameTemplate = DetermineResourceNameTemplate();
8+
private static ConfigurationSerializer Serializer => new();
89

910
private static string DetermineResourceNameTemplate()
1011
{
@@ -19,7 +20,7 @@ private static string DetermineResourceNameTemplate()
1920

2021
var resourceName = GetResourceName(workflow);
2122
var embeddedResource = resourceName.ReadAsStringFromEmbeddedResource(typeof(WorkflowManager).Assembly);
22-
return ConfigurationSerializer.Deserialize<Dictionary<object, object?>>(embeddedResource);
23+
return Serializer.Deserialize<Dictionary<object, object?>>(embeddedResource);
2324
}
2425

2526
private static string GetResourceName(string workflow)

src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class GitVersionExecutorTests : TestBase
1717
{
1818
private IFileSystem fileSystem;
1919
private ILog log;
20-
private GitVersionCache gitVersionCache;
20+
private GitVersionCacheProvider gitVersionCacheProvider;
2121
private IServiceProvider sp;
2222

2323
[Test]
@@ -80,7 +80,8 @@ public void CacheKeyForWorktree()
8080

8181
var preparer = this.sp.GetRequiredService<IGitPreparer>();
8282
preparer.Prepare();
83-
var cacheKey = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>().Create(null);
83+
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
84+
var cacheKey = cacheKeyFactory.Create(null);
8485
cacheKey.Value.ShouldNotBeEmpty();
8586
}
8687
finally
@@ -140,7 +141,7 @@ public void CacheFileExistsOnDisk()
140141

141142
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
142143
var cacheKey = cacheKeyFactory.Create(null);
143-
var cacheFileName = this.gitVersionCache.GetCacheFileName(cacheKey);
144+
var cacheFileName = this.gitVersionCacheProvider.GetCacheFileName(cacheKey);
144145

145146
this.fileSystem.WriteAllText(cacheFileName, versionCacheFileContent);
146147
versionVariables = gitVersionCalculator.CalculateVersionVariables();
@@ -193,10 +194,10 @@ public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDyn
193194

194195
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
195196
var cacheKey = cacheKeyFactory.Create(null);
196-
var cacheFileName = this.gitVersionCache.GetCacheFileName(cacheKey);
197+
var cacheFileName = this.gitVersionCacheProvider.GetCacheFileName(cacheKey);
197198
this.fileSystem.WriteAllText(cacheFileName, versionCacheFileContent);
198199

199-
var cacheDirectory = this.gitVersionCache.GetCacheDirectory();
200+
var cacheDirectory = this.gitVersionCacheProvider.GetCacheDirectory();
200201

201202
var cacheDirectoryTimestamp = this.fileSystem.GetLastDirectoryWrite(cacheDirectory);
202203

@@ -283,7 +284,7 @@ public void ConfigChangeInvalidatesCache(string configFileName)
283284

284285
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
285286
var cacheKey = cacheKeyFactory.Create(null);
286-
var cacheFileName = this.gitVersionCache.GetCacheFileName(cacheKey);
287+
var cacheFileName = this.gitVersionCacheProvider.GetCacheFileName(cacheKey);
287288

288289
this.fileSystem.WriteAllText(cacheFileName, versionCacheFileContent);
289290

@@ -345,7 +346,7 @@ public void NoCacheBypassesCache()
345346

346347
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
347348
var cacheKey = cacheKeyFactory.Create(null);
348-
var cacheFileName = this.gitVersionCache.GetCacheFileName(cacheKey);
349+
var cacheFileName = this.gitVersionCacheProvider.GetCacheFileName(cacheKey);
349350

350351
this.fileSystem.WriteAllText(cacheFileName, versionCacheFileContent);
351352
versionVariables = gitVersionCalculator.CalculateVersionVariables();
@@ -580,7 +581,7 @@ private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVe
580581

581582
this.fileSystem = this.sp.GetRequiredService<IFileSystem>();
582583
this.log = this.sp.GetRequiredService<ILog>();
583-
this.gitVersionCache = (GitVersionCache)this.sp.GetRequiredService<IGitVersionCache>();
584+
this.gitVersionCacheProvider = (GitVersionCacheProvider)this.sp.GetRequiredService<IGitVersionCacheProvider>();
584585

585586
return this.sp.GetRequiredService<IGitVersionCalculateTool>();
586587
}

0 commit comments

Comments
 (0)