McSherry.SemanticVersioning
McSherry.SemanticVersioning copied to clipboard
Converter for System.Version
I just ran into an issue where I have to try handing 1.2.3.4
, 1.2.3 hash
and 1.2.3.4 hash
(info versions and regular versions) to Semantic Version. It would be useful if there was a implicit conversion that converted (major.minor.build.revision) to a semantic version (major.minor.build-revision)
I'd considered adding conversion operators for System.Version
in the past, but I decided against this because they are semantically incompatible. Plus, implicit operators should only be used where there would be no loss of information. It isn't possible to preserve all information System.Version <-> SemanticVersion
.
Yeah, I agree. We just needed it and have added support for different use cases.
[Theory]
[InlineData(null, null)]
[InlineData("1", "1.0.0")]
[InlineData("1.2", "1.2.0")]
[InlineData("1.2 7ab3b4da18", "1.2.0")]
[InlineData("1.2.3", "1.2.3")]
[InlineData("1.2.3 7ab3b4da18", "1.2.3")]
[InlineData("1.2.3-beta2", "1.2.3-beta2")]
[InlineData("1.2.3.*", "1.2.3")]
[InlineData("1.2.3.0", "1.2.3-0")]
[InlineData("1.2.3.0*", "1.2.3-0")]
[InlineData("1.2.3*.0", "1.2.3-0")]
[InlineData("1.2.*.0", "1.2.0")]
[InlineData("1.2.*", "1.2.0")]
[InlineData("1.2.3.4", "1.2.3-4")]
[InlineData("1.2.3.4 7ab3b4da18", "1.2.3-4")]
public async Task SemanticVersionTests(string input, string expected) {
var actual = await _parser.ParseAsync(input);
Assert.Equal(expected, actual?.ToString());
}
@niemyjski I plan to introduce a "greedy" parse mode (see #12). It might be useful in your application.
I've decided to reopen this issue.
Although semantic versions and System.Version
aren't completely compatible, I think including a converter apart from the main API (maybe under a McSherry.SemanticVersioning.Extensions
namespace or similar) would be useful while avoiding cluttering the main namespaces.