From 2c90bb219e84be781848dec1a0383915e5dd8223 Mon Sep 17 00:00:00 2001 From: Stefan Schick Date: Thu, 30 Jan 2025 15:49:04 +0100 Subject: [PATCH 1/9] Add Test and add EnumConverter --- src/GitVersion.App.Tests/ArgumentParserTests.cs | 7 +++++++ src/GitVersion.App/OverrideConfigurationOptionParser.cs | 4 +++- .../VersionCalculation/VersionStrategies.cs | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/GitVersion.App.Tests/ArgumentParserTests.cs b/src/GitVersion.App.Tests/ArgumentParserTests.cs index 6569742bb3..f4c5d197e1 100644 --- a/src/GitVersion.App.Tests/ArgumentParserTests.cs +++ b/src/GitVersion.App.Tests/ArgumentParserTests.cs @@ -533,6 +533,13 @@ private static IEnumerable OverrideConfigWithSingleOptionTestData( UpdateBuildNumber = true } ); + yield return new TestCaseData( + "strategies=[\"None\",\"Mainline\"]", + new GitVersionConfiguration() + { + VersionStrategies = [ VersionStrategies.None, VersionStrategies.Mainline] + } + ); } [TestCaseSource(nameof(OverrideConfigWithMultipleOptionsTestData))] diff --git a/src/GitVersion.App/OverrideConfigurationOptionParser.cs b/src/GitVersion.App/OverrideConfigurationOptionParser.cs index 9b85428f71..1d4d6fa985 100644 --- a/src/GitVersion.App/OverrideConfigurationOptionParser.cs +++ b/src/GitVersion.App/OverrideConfigurationOptionParser.cs @@ -1,4 +1,5 @@ using GitVersion.Configuration; +using GitVersion.VersionCalculation; namespace GitVersion; @@ -45,7 +46,8 @@ private static bool IsSupportedPropertyType(Type propertyType) return unwrappedType == typeof(string) || unwrappedType.IsEnum || unwrappedType == typeof(int) - || unwrappedType == typeof(bool); + || unwrappedType == typeof(bool) + || unwrappedType == typeof(VersionStrategies[]); } internal void SetValue(string key, string value) => overrideConfiguration[key] = QuotedStringHelpers.UnquoteText(value); diff --git a/src/GitVersion.Core/VersionCalculation/VersionStrategies.cs b/src/GitVersion.Core/VersionCalculation/VersionStrategies.cs index 4fe020c87a..4bef24d860 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionStrategies.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionStrategies.cs @@ -1,6 +1,7 @@ namespace GitVersion.VersionCalculation; [Flags] +[JsonConverter(typeof(JsonStringEnumConverter))] public enum VersionStrategies { None = 0, From 7e3e268917f64fac82bc93fc732714c1593c4346 Mon Sep 17 00:00:00 2001 From: Stefan Schick Date: Fri, 31 Jan 2025 12:53:22 +0100 Subject: [PATCH 2/9] Implement custom deserializer --- .../ConfigurationSerializer.cs | 2 ++ src/GitVersion.Core/GitVersion.Core.csproj | 1 + src/GitVersion.Core/PublicAPI.Shipped.txt | 6 ++++ .../VersionCalculation/VersionStrategies.cs | 1 - .../VersionStrategiesConverter.cs | 34 +++++++++++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs diff --git a/src/GitVersion.Configuration/ConfigurationSerializer.cs b/src/GitVersion.Configuration/ConfigurationSerializer.cs index df18446214..bb87dce97a 100644 --- a/src/GitVersion.Configuration/ConfigurationSerializer.cs +++ b/src/GitVersion.Configuration/ConfigurationSerializer.cs @@ -1,3 +1,4 @@ +using GitVersion.VersionCalculation; using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; using YamlDotNet.Serialization.TypeInspectors; @@ -8,6 +9,7 @@ internal class ConfigurationSerializer : IConfigurationSerializer { private static IDeserializer Deserializer => new DeserializerBuilder() .WithNamingConvention(HyphenatedNamingConvention.Instance) + .WithTypeConverter(VersionStrategiesConverter.Instance) .WithTypeInspector(inspector => new JsonPropertyNameInspector(inspector)) .Build(); diff --git a/src/GitVersion.Core/GitVersion.Core.csproj b/src/GitVersion.Core/GitVersion.Core.csproj index a11a076110..5f82cba761 100644 --- a/src/GitVersion.Core/GitVersion.Core.csproj +++ b/src/GitVersion.Core/GitVersion.Core.csproj @@ -15,6 +15,7 @@ + diff --git a/src/GitVersion.Core/PublicAPI.Shipped.txt b/src/GitVersion.Core/PublicAPI.Shipped.txt index e8e7ed46a5..ea6aeafe73 100644 --- a/src/GitVersion.Core/PublicAPI.Shipped.txt +++ b/src/GitVersion.Core/PublicAPI.Shipped.txt @@ -707,6 +707,12 @@ GitVersion.VersionCalculation.VersionStrategies.None = 0 -> GitVersion.VersionCa GitVersion.VersionCalculation.VersionStrategies.TaggedCommit = 8 -> GitVersion.VersionCalculation.VersionStrategies GitVersion.VersionCalculation.VersionStrategies.TrackReleaseBranches = 16 -> GitVersion.VersionCalculation.VersionStrategies GitVersion.VersionCalculation.VersionStrategies.VersionInBranchName = 32 -> GitVersion.VersionCalculation.VersionStrategies +GitVersion.VersionCalculation.VersionStrategiesConverter +GitVersion.VersionCalculation.VersionStrategiesConverter.VersionStrategiesConverter() -> void +static readonly GitVersion.VersionCalculation.VersionStrategiesConverter.Instance -> YamlDotNet.Serialization.IYamlTypeConverter! +GitVersion.VersionCalculation.VersionStrategiesConverter.Accepts(System.Type type) -> bool +GitVersion.VersionCalculation.VersionStrategiesConverter.ReadYaml(YamlDotNet.Core.IParser! parser, System.Type! type, YamlDotNet.Serialization.ObjectDeserializer! rootDeserializer) -> object? +GitVersion.VersionCalculation.VersionStrategiesConverter.WriteYaml(YamlDotNet.Core.IEmitter! emitter, object? value, System.Type! type, YamlDotNet.Serialization.ObjectSerializer! serializer) -> void GitVersion.VersionCalculation.VersionStrategyModule GitVersion.VersionCalculation.VersionStrategyModule.RegisterTypes(Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> void GitVersion.VersionCalculation.VersionStrategyModule.VersionStrategyModule() -> void diff --git a/src/GitVersion.Core/VersionCalculation/VersionStrategies.cs b/src/GitVersion.Core/VersionCalculation/VersionStrategies.cs index 4bef24d860..4fe020c87a 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionStrategies.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionStrategies.cs @@ -1,7 +1,6 @@ namespace GitVersion.VersionCalculation; [Flags] -[JsonConverter(typeof(JsonStringEnumConverter))] public enum VersionStrategies { None = 0, diff --git a/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs b/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs new file mode 100644 index 0000000000..fe67c5b42b --- /dev/null +++ b/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs @@ -0,0 +1,34 @@ +using YamlDotNet.Core; +using YamlDotNet.Core.Events; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace GitVersion.VersionCalculation; + +public class VersionStrategiesConverter : IYamlTypeConverter +{ + public static readonly IYamlTypeConverter Instance = new VersionStrategiesConverter(); + + public bool Accepts(Type type) + { + return type == typeof(VersionStrategies[]); + } + + public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) + { + string data = parser.Consume().Value; + + var deserializer = new DeserializerBuilder() + .WithNamingConvention(UnderscoredNamingConvention.Instance) // see height_in_inches in sample yml + .Build(); + + VersionStrategies[] strategies = deserializer.Deserialize(data); + + return strategies; + } + + public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) + { + // Convert from an object to text during serialization. + } +} From ce557a3efff24fb9ca375ffebf8d2a4421306094 Mon Sep 17 00:00:00 2001 From: Stefan Schick Date: Fri, 31 Jan 2025 13:23:37 +0100 Subject: [PATCH 3/9] Add throw statement for serialization --- .../VersionCalculation/VersionStrategiesConverter.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs b/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs index fe67c5b42b..cefc60403b 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs @@ -30,5 +30,6 @@ public bool Accepts(Type type) public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) { // Convert from an object to text during serialization. + throw new NotImplementedException(); } } From 2cd4bee8e866319aae27e5784c6361973fc8f987 Mon Sep 17 00:00:00 2001 From: Stefan Schick Date: Fri, 31 Jan 2025 15:44:56 +0100 Subject: [PATCH 4/9] Implement Serialization --- .../VersionCalculation/VersionStrategiesConverter.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs b/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs index cefc60403b..fc8ada9478 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs @@ -29,7 +29,15 @@ public bool Accepts(Type type) public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) { - // Convert from an object to text during serialization. - throw new NotImplementedException(); + VersionStrategies[] strategies = (VersionStrategies[])value!; + + var s = new SerializerBuilder() + .JsonCompatible() // <- Looks good + //.WithDefaultScalarStyle(ScalarStyle.DoubleQuoted) + //.WithNamingConvention(CamelCaseNamingConvention.Instance) + .Build(); + var data = s.Serialize(strategies); + + emitter.Emit(new Scalar(data)); } } From 75db173b2527a23bf0cfac77767c8922ffa3a98a Mon Sep 17 00:00:00 2001 From: Stefan Schick Date: Fri, 31 Jan 2025 16:37:36 +0100 Subject: [PATCH 5/9] Extend converter --- .../VersionStrategiesConverter.cs | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs b/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs index fc8ada9478..e115ca5056 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs @@ -9,22 +9,34 @@ public class VersionStrategiesConverter : IYamlTypeConverter { public static readonly IYamlTypeConverter Instance = new VersionStrategiesConverter(); - public bool Accepts(Type type) - { - return type == typeof(VersionStrategies[]); - } + public bool Accepts(Type type) => type == typeof(VersionStrategies[]); public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) { - string data = parser.Consume().Value; - - var deserializer = new DeserializerBuilder() - .WithNamingConvention(UnderscoredNamingConvention.Instance) // see height_in_inches in sample yml - .Build(); - - VersionStrategies[] strategies = deserializer.Deserialize(data); - - return strategies; + List strategies = new(); + + if (parser.TryConsume(out var _)) + { + while (!parser.TryConsume(out var _)) + { + string data = parser.Consume().Value; + + var strategy = Enum.Parse(data); + strategies.Add(strategy); + } + } + else + { + string data = parser.Consume().Value; + + var deserializer = new DeserializerBuilder() + .WithNamingConvention(UnderscoredNamingConvention.Instance) // see height_in_inches in sample yml + .Build(); + + strategies = deserializer.Deserialize>(data); + } + + return strategies.ToArray(); } public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) From f529641dc80a7a49adc934e0c5114717b338146a Mon Sep 17 00:00:00 2001 From: Stefan Schick Date: Mon, 3 Feb 2025 08:36:12 +0100 Subject: [PATCH 6/9] Remove comments --- .../VersionCalculation/VersionStrategiesConverter.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs b/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs index e115ca5056..0718238ff7 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs @@ -30,7 +30,7 @@ public class VersionStrategiesConverter : IYamlTypeConverter string data = parser.Consume().Value; var deserializer = new DeserializerBuilder() - .WithNamingConvention(UnderscoredNamingConvention.Instance) // see height_in_inches in sample yml + .WithNamingConvention(UnderscoredNamingConvention.Instance) .Build(); strategies = deserializer.Deserialize>(data); @@ -44,9 +44,7 @@ public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializ VersionStrategies[] strategies = (VersionStrategies[])value!; var s = new SerializerBuilder() - .JsonCompatible() // <- Looks good - //.WithDefaultScalarStyle(ScalarStyle.DoubleQuoted) - //.WithNamingConvention(CamelCaseNamingConvention.Instance) + .JsonCompatible() .Build(); var data = s.Serialize(strategies); From b077623e8c29ef2c24c3990242935d3aed9f3a04 Mon Sep 17 00:00:00 2001 From: Stefan Schick Date: Mon, 3 Feb 2025 09:03:42 +0100 Subject: [PATCH 7/9] Add nullability annotation for public API --- src/GitVersion.Core/PublicAPI.Shipped.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitVersion.Core/PublicAPI.Shipped.txt b/src/GitVersion.Core/PublicAPI.Shipped.txt index ea6aeafe73..b1101b9c68 100644 --- a/src/GitVersion.Core/PublicAPI.Shipped.txt +++ b/src/GitVersion.Core/PublicAPI.Shipped.txt @@ -710,7 +710,7 @@ GitVersion.VersionCalculation.VersionStrategies.VersionInBranchName = 32 -> GitV GitVersion.VersionCalculation.VersionStrategiesConverter GitVersion.VersionCalculation.VersionStrategiesConverter.VersionStrategiesConverter() -> void static readonly GitVersion.VersionCalculation.VersionStrategiesConverter.Instance -> YamlDotNet.Serialization.IYamlTypeConverter! -GitVersion.VersionCalculation.VersionStrategiesConverter.Accepts(System.Type type) -> bool +GitVersion.VersionCalculation.VersionStrategiesConverter.Accepts(System.Type! type) -> bool GitVersion.VersionCalculation.VersionStrategiesConverter.ReadYaml(YamlDotNet.Core.IParser! parser, System.Type! type, YamlDotNet.Serialization.ObjectDeserializer! rootDeserializer) -> object? GitVersion.VersionCalculation.VersionStrategiesConverter.WriteYaml(YamlDotNet.Core.IEmitter! emitter, object? value, System.Type! type, YamlDotNet.Serialization.ObjectSerializer! serializer) -> void GitVersion.VersionCalculation.VersionStrategyModule From 1a0bace8eb86cb4c80f364ca9fa846886f6222bf Mon Sep 17 00:00:00 2001 From: Stefan Schick Date: Mon, 3 Feb 2025 09:12:53 +0100 Subject: [PATCH 8/9] Fix formatting issue in test --- src/GitVersion.App.Tests/ArgumentParserTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GitVersion.App.Tests/ArgumentParserTests.cs b/src/GitVersion.App.Tests/ArgumentParserTests.cs index f4c5d197e1..c9d678043a 100644 --- a/src/GitVersion.App.Tests/ArgumentParserTests.cs +++ b/src/GitVersion.App.Tests/ArgumentParserTests.cs @@ -535,9 +535,9 @@ private static IEnumerable OverrideConfigWithSingleOptionTestData( ); yield return new TestCaseData( "strategies=[\"None\",\"Mainline\"]", - new GitVersionConfiguration() + new GitVersionConfiguration { - VersionStrategies = [ VersionStrategies.None, VersionStrategies.Mainline] + VersionStrategies = [VersionStrategies.None, VersionStrategies.Mainline] } ); } From c81f5b77eb21234a32055eac45ddcb2ae20625d4 Mon Sep 17 00:00:00 2001 From: Stefan Schick Date: Tue, 4 Feb 2025 09:40:42 +0100 Subject: [PATCH 9/9] Move VersionStrategiesConverter to GitVersion.Configuration --- src/GitVersion.Configuration/ConfigurationSerializer.cs | 1 - .../VersionStrategiesConverter.cs | 5 +++-- src/GitVersion.Core/GitVersion.Core.csproj | 1 - src/GitVersion.Core/PublicAPI.Shipped.txt | 6 ------ 4 files changed, 3 insertions(+), 10 deletions(-) rename src/{GitVersion.Core/VersionCalculation => GitVersion.Configuration}/VersionStrategiesConverter.cs (91%) diff --git a/src/GitVersion.Configuration/ConfigurationSerializer.cs b/src/GitVersion.Configuration/ConfigurationSerializer.cs index bb87dce97a..9d66d0012c 100644 --- a/src/GitVersion.Configuration/ConfigurationSerializer.cs +++ b/src/GitVersion.Configuration/ConfigurationSerializer.cs @@ -1,4 +1,3 @@ -using GitVersion.VersionCalculation; using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; using YamlDotNet.Serialization.TypeInspectors; diff --git a/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs b/src/GitVersion.Configuration/VersionStrategiesConverter.cs similarity index 91% rename from src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs rename to src/GitVersion.Configuration/VersionStrategiesConverter.cs index 0718238ff7..7d790154cf 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionStrategiesConverter.cs +++ b/src/GitVersion.Configuration/VersionStrategiesConverter.cs @@ -1,11 +1,12 @@ +using GitVersion.VersionCalculation; using YamlDotNet.Core; using YamlDotNet.Core.Events; using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; -namespace GitVersion.VersionCalculation; +namespace GitVersion.Configuration; -public class VersionStrategiesConverter : IYamlTypeConverter +internal class VersionStrategiesConverter : IYamlTypeConverter { public static readonly IYamlTypeConverter Instance = new VersionStrategiesConverter(); diff --git a/src/GitVersion.Core/GitVersion.Core.csproj b/src/GitVersion.Core/GitVersion.Core.csproj index 5f82cba761..a11a076110 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/PublicAPI.Shipped.txt b/src/GitVersion.Core/PublicAPI.Shipped.txt index b1101b9c68..e8e7ed46a5 100644 --- a/src/GitVersion.Core/PublicAPI.Shipped.txt +++ b/src/GitVersion.Core/PublicAPI.Shipped.txt @@ -707,12 +707,6 @@ GitVersion.VersionCalculation.VersionStrategies.None = 0 -> GitVersion.VersionCa GitVersion.VersionCalculation.VersionStrategies.TaggedCommit = 8 -> GitVersion.VersionCalculation.VersionStrategies GitVersion.VersionCalculation.VersionStrategies.TrackReleaseBranches = 16 -> GitVersion.VersionCalculation.VersionStrategies GitVersion.VersionCalculation.VersionStrategies.VersionInBranchName = 32 -> GitVersion.VersionCalculation.VersionStrategies -GitVersion.VersionCalculation.VersionStrategiesConverter -GitVersion.VersionCalculation.VersionStrategiesConverter.VersionStrategiesConverter() -> void -static readonly GitVersion.VersionCalculation.VersionStrategiesConverter.Instance -> YamlDotNet.Serialization.IYamlTypeConverter! -GitVersion.VersionCalculation.VersionStrategiesConverter.Accepts(System.Type! type) -> bool -GitVersion.VersionCalculation.VersionStrategiesConverter.ReadYaml(YamlDotNet.Core.IParser! parser, System.Type! type, YamlDotNet.Serialization.ObjectDeserializer! rootDeserializer) -> object? -GitVersion.VersionCalculation.VersionStrategiesConverter.WriteYaml(YamlDotNet.Core.IEmitter! emitter, object? value, System.Type! type, YamlDotNet.Serialization.ObjectSerializer! serializer) -> void GitVersion.VersionCalculation.VersionStrategyModule GitVersion.VersionCalculation.VersionStrategyModule.RegisterTypes(Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> void GitVersion.VersionCalculation.VersionStrategyModule.VersionStrategyModule() -> void