-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRubyVersion.cs
More file actions
69 lines (54 loc) · 2.49 KB
/
Copy pathRubyVersion.cs
File metadata and controls
69 lines (54 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Text.RegularExpressions;
namespace SqlcGenRuby;
public record RubyVersion(short Major, short? Minor, short? Patch = null) : IComparable<RubyVersion>
{
public int CompareTo(RubyVersion? other)
{
if (other is null) return 1;
var majorComparison = Major.CompareTo(other.Major);
if (majorComparison != 0) return majorComparison;
if (Minor == null)
return majorComparison;
var minorComparison = Minor.Value.CompareTo(other.Minor);
if (minorComparison != 0) return minorComparison;
return Patch?.CompareTo(other.Patch) ?? minorComparison;
}
public bool AtLeast(RubyVersion other) => CompareTo(other) >= 0;
public bool AtMost(RubyVersion other) => CompareTo(other) <= 0;
}
public static partial class RubyVersionExtensions
{
private static readonly RubyVersion MinSupportedVersion = new(3, 1);
private static readonly RubyVersion MaxSupportedVersion = new(3, 3);
public static RubyVersion ParseName(string versionPattern)
{
var rubyVersion = ParseSemanticVersion(versionPattern);
if (!rubyVersion.AtLeast(MinSupportedVersion))
throw new ArgumentException($"Provided version {rubyVersion} exceeds min version {MinSupportedVersion}");
if (!rubyVersion.AtMost(MaxSupportedVersion))
throw new ArgumentException($"Provided version {rubyVersion} exceeds max version {MaxSupportedVersion}");
return rubyVersion;
}
private static RubyVersion ParseSemanticVersion(string versionPattern)
{
var semanticVersionRegex = SemanticVersionRegex();
var match = semanticVersionRegex.Match(versionPattern);
if (!match.Success)
throw new ArgumentException($"version {versionPattern} can't be parsed to a semantic version");
var major = short.Parse(match.Groups[1].Value);
short? minor = null;
if (match.Groups[2].Success)
minor = short.Parse(match.Groups[2].Value);
short? patch = null;
if (match.Groups[3].Success)
patch = short.Parse(match.Groups[3].Value);
return new RubyVersion(major, minor, patch);
}
public static bool ImmutableDataSupported(this RubyVersion me)
{
return me.AtLeast(new RubyVersion(3, 2));
}
[GeneratedRegex(@"^\s*(0|[1-9][0-9]*)(?:\.(0|[1-9][0-9]*))?(?:\.(0|[1-9][0-9]*))?\s*$")]
private static partial Regex SemanticVersionRegex();
}