diff --git a/src/GitVersion.App.Tests/ArgumentParserTests.cs b/src/GitVersion.App.Tests/ArgumentParserTests.cs index 6569742bb3..c9d678043a 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.Configuration/ConfigurationSerializer.cs b/src/GitVersion.Configuration/ConfigurationSerializer.cs index df18446214..9d66d0012c 100644 --- a/src/GitVersion.Configuration/ConfigurationSerializer.cs +++ b/src/GitVersion.Configuration/ConfigurationSerializer.cs @@ -8,6 +8,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.Configuration/VersionStrategiesConverter.cs b/src/GitVersion.Configuration/VersionStrategiesConverter.cs new file mode 100644 index 0000000000..7d790154cf --- /dev/null +++ b/src/GitVersion.Configuration/VersionStrategiesConverter.cs @@ -0,0 +1,54 @@ +using GitVersion.VersionCalculation; +using YamlDotNet.Core; +using YamlDotNet.Core.Events; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace GitVersion.Configuration; + +internal class VersionStrategiesConverter : IYamlTypeConverter +{ + public static readonly IYamlTypeConverter Instance = new VersionStrategiesConverter(); + + public bool Accepts(Type type) => type == typeof(VersionStrategies[]); + + public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) + { + 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) + .Build(); + + strategies = deserializer.Deserialize>(data); + } + + return strategies.ToArray(); + } + + public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) + { + VersionStrategies[] strategies = (VersionStrategies[])value!; + + var s = new SerializerBuilder() + .JsonCompatible() + .Build(); + var data = s.Serialize(strategies); + + emitter.Emit(new Scalar(data)); + } +}