-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
4 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/GitVersion.Configuration/VersionStrategiesConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<VersionStrategies> strategies = new(); | ||
|
||
if (parser.TryConsume<SequenceStart>(out var _)) | ||
{ | ||
while (!parser.TryConsume<SequenceEnd>(out var _)) | ||
{ | ||
string data = parser.Consume<Scalar>().Value; | ||
|
||
var strategy = Enum.Parse<VersionStrategies>(data); | ||
strategies.Add(strategy); | ||
} | ||
} | ||
else | ||
{ | ||
string data = parser.Consume<Scalar>().Value; | ||
|
||
var deserializer = new DeserializerBuilder() | ||
.WithNamingConvention(UnderscoredNamingConvention.Instance) | ||
.Build(); | ||
|
||
strategies = deserializer.Deserialize<List<VersionStrategies>>(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)); | ||
} | ||
} |