semver/README.md

82 lines
2.4 KiB
Markdown
Raw Permalink Normal View History

2026-04-05 08:23:42 +02:00
# `Geekeey.SemVer`
SemVer is a .NET library for parsing and comparing semantic version numbers. It provides a simple API for working with
semantic versioning, making it easy to manage version numbers in your .NET projects.
## Features
- **Parsing**: Parse semantic version strings into structured objects.
- **Comparison**: Compare semantic version objects to determine their order.
- **Validation**: Validate semantic version strings to ensure they conform to the SemVer specification.
- **Pre-release and Build Metadata**: Support for pre-release versions and build metadata as defined in the SemVer specification.
## Getting Started
### Install the NuGet package:
```shell
dotnet add package Geekeey.SemVer
```
You may need to add our NuGet feed to your `nuget.config` this can be done by running the following command:
```shell
dotnet nuget add source -n geekeey https://code.geekeey.de/api/packages/geekeey/nuget/index.json
```
### Usage
```csharp
using Geekeey.SemVer;
var version = SemanticVersion.Parse("1.2.3-beta+build.7");
var next = new SemanticVersion(1, 3, 0);
Console.WriteLine(version);
Console.WriteLine(version.Major);
Console.WriteLine(version.Prerelease);
Console.WriteLine(version.Metadata);
Console.WriteLine(version < next);
```
You can also use `TryParse` when you need to handle invalid input without exceptions.
```csharp
using Geekeey.SemVer;
if (SemanticVersion.TryParse("1.2.3-alpha", out var parsed))
{
Console.WriteLine(parsed);
}
```
Version ranges support npm-style and Maven-style syntax.
```csharp
using Geekeey.SemVer;
var npmRange = SemanticVersionRange.Parse("^1.2.3");
var mavenRange = SemanticVersionRange.Parse("[1.2.3,2.0.0)");
var candidate = SemanticVersion.Parse("1.4.2");
Console.WriteLine(npmRange.Contains(candidate));
Console.WriteLine(mavenRange.Contains(candidate));
Console.WriteLine(npmRange.ToString("m", null));
Console.WriteLine(mavenRange.ToString("ns", null));
```
Both `SemanticVersion` and `SemanticVersionRange` integrate with `System.Text.Json`.
```csharp
using System.Text.Json;
using Geekeey.SemVer;
var version = SemanticVersion.Parse("1.2.3-beta+build.7");
var range = SemanticVersionRange.Parse("^1.2.3");
var versionJson = JsonSerializer.Serialize(version);
var rangeJson = JsonSerializer.Serialize(range);
var roundTrippedVersion = JsonSerializer.Deserialize<SemanticVersion>(versionJson);
var roundTrippedRange = JsonSerializer.Deserialize<SemanticVersionRange>(rangeJson);
```