Skip to content

Commit a663ef8

Browse files
committed
Remove CompareSemVer dup and refactor a bit
1 parent 1e58d30 commit a663ef8

File tree

2 files changed

+26
-87
lines changed

2 files changed

+26
-87
lines changed

dev-proxy-abstractions/ProxyUtils.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -548,38 +548,47 @@ public static void ValidateSchemaVersion(string schemaUrl, ILogger logger)
548548
/// </summary>
549549
/// <param name="a">ver1</param>
550550
/// <param name="b">ver2</param>
551-
/// <returns>Returns 0 if the versions are equal, -1 if a is less than b, and 1 if a is greater than b.</returns>
552-
private static int CompareSemVer(string? a, string? b)
551+
/// <returns>
552+
/// Returns 0 if the versions are equal, -1 if a is less than b, and 1 if a is greater than b.
553+
/// An invalid argument is "rounded" to a minimal version.
554+
/// </returns>
555+
public static int CompareSemVer(string? a, string? b)
553556
{
554-
if (a == null && b == null)
557+
if (string.IsNullOrWhiteSpace(a) && string.IsNullOrWhiteSpace(b))
555558
{
556559
return 0;
557560
}
558-
else if (a == null)
561+
else if (string.IsNullOrWhiteSpace(a))
559562
{
560563
return -1;
561564
}
562-
else if (b == null)
565+
else if (string.IsNullOrWhiteSpace(b))
563566
{
564567
return 1;
565568
}
566569

567-
if (a.StartsWith('v'))
568-
{
569-
a = a[1..];
570-
}
571-
if (b.StartsWith('v'))
572-
{
573-
b = b[1..];
574-
}
570+
a = a.TrimStart('v');
571+
b = b.TrimStart('v');
575572

576573
var aParts = a.Split('-');
577574
var bParts = b.Split('-');
578575

579-
var aVersion = new Version(aParts[0]);
580-
var bVersion = new Version(bParts[0]);
576+
var aParsed = Version.TryParse(aParts[0], out var aVersion);
577+
var bParsed = Version.TryParse(bParts[0], out var bVersion);
578+
if (!aParsed && !bParsed)
579+
{
580+
return 0;
581+
}
582+
else if (!aParsed)
583+
{
584+
return -1;
585+
}
586+
else if (!bParsed)
587+
{
588+
return 1;
589+
}
581590

582-
var compare = aVersion.CompareTo(bVersion);
591+
var compare = aVersion!.CompareTo(bVersion);
583592
if (compare != 0)
584593
{
585594
// if the versions are different, return the comparison result

dev-proxy/UpdateNotification.cs

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal static class UpdateNotification
4545
// -1 = latest release is greater
4646
// 0 = versions are equal
4747
// 1 = current version is greater
48-
if (CompareSemVer(currentVersion, latestReleaseVersion) < 0)
48+
if (ProxyUtils.CompareSemVer(currentVersion, latestReleaseVersion) < 0)
4949
{
5050
return latestRelease;
5151
}
@@ -60,76 +60,6 @@ internal static class UpdateNotification
6060
}
6161
}
6262

63-
/// <summary>
64-
/// Compares two semantic versions strings.
65-
/// </summary>
66-
/// <param name="a">ver1</param>
67-
/// <param name="b">ver2</param>
68-
/// <returns>Returns 0 if the versions are equal, -1 if a is less than b, and 1 if a is greater than b.</returns>
69-
private static int CompareSemVer(string? a, string? b)
70-
{
71-
if (a == null && b == null)
72-
{
73-
return 0;
74-
}
75-
else if (a == null)
76-
{
77-
return -1;
78-
}
79-
else if (b == null)
80-
{
81-
return 1;
82-
}
83-
84-
if (a.StartsWith('v'))
85-
{
86-
a = a[1..];
87-
}
88-
if (b.StartsWith('v'))
89-
{
90-
b = b[1..];
91-
}
92-
93-
var aParts = a.Split('-');
94-
var bParts = b.Split('-');
95-
96-
var aVersion = new Version(aParts[0]);
97-
var bVersion = new Version(bParts[0]);
98-
99-
var compare = aVersion.CompareTo(bVersion);
100-
if (compare != 0)
101-
{
102-
// if the versions are different, return the comparison result
103-
return compare;
104-
}
105-
106-
// if the versions are the same, compare the prerelease tags
107-
if (aParts.Length == 1 && bParts.Length == 1)
108-
{
109-
// if both versions are stable, they are equal
110-
return 0;
111-
}
112-
else if (aParts.Length == 1)
113-
{
114-
// if a is stable and b is not, a is greater
115-
return 1;
116-
}
117-
else if (bParts.Length == 1)
118-
{
119-
// if b is stable and a is not, b is greater
120-
return -1;
121-
}
122-
else if (aParts[1] == bParts[1])
123-
{
124-
// if both versions are prerelease and the tags are the same, they are equal
125-
return 0;
126-
}
127-
else {
128-
// if both versions are prerelease, b is greater
129-
return -1;
130-
}
131-
}
132-
13363
private static async Task<ReleaseInfo?> GetLatestReleaseAsync(ReleaseType releaseType)
13464
{
13565
var http = new HttpClient();

0 commit comments

Comments
 (0)