C#: Use NuGetVersion instead of homemade version implementation.

This commit is contained in:
Michael Nebel
2025-12-04 14:26:27 +01:00
parent 1b84f70d1c
commit a3e545ddd5
3 changed files with 17 additions and 46 deletions

View File

@@ -1,24 +1,16 @@
using System; using System;
using System.IO; using System.IO;
using NuGet.Versioning;
namespace Semmle.Extraction.CSharp.DependencyFetching namespace Semmle.Extraction.CSharp.DependencyFetching
{ {
internal record DotNetVersion : IComparable<DotNetVersion> internal record DotNetVersion : IComparable<DotNetVersion>
{ {
private readonly string dir; private readonly string dir;
private readonly Version version; private readonly NuGetVersion version;
private readonly Version? preReleaseVersion;
private readonly string? preReleaseVersionType;
private bool IsPreRelease => preReleaseVersionType is not null && preReleaseVersion is not null;
private string FullVersion private string FullVersion =>
{ version.ToString();
get
{
var preRelease = IsPreRelease ? $"-{preReleaseVersionType}.{preReleaseVersion}" : "";
return this.version + preRelease;
}
}
public string FullPath => Path.Combine(dir, FullVersion); public string FullPath => Path.Combine(dir, FullVersion);
@@ -48,37 +40,14 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
} }
public DotNetVersion(string dir, string version, string preReleaseVersionType, string preReleaseVersion) public DotNetVersion(string dir, NuGetVersion version)
{ {
this.dir = dir; this.dir = dir;
this.version = Version.Parse(version); this.version = version;
if (!string.IsNullOrEmpty(preReleaseVersion) && !string.IsNullOrEmpty(preReleaseVersionType))
{
this.preReleaseVersionType = preReleaseVersionType;
this.preReleaseVersion = Version.Parse(preReleaseVersion);
}
} }
public int CompareTo(DotNetVersion? other) public int CompareTo(DotNetVersion? other) =>
{ version.CompareTo(other?.version);
var c = version.CompareTo(other?.version);
if (c == 0 && IsPreRelease)
{
if (!other!.IsPreRelease)
{
return -1;
}
// Both are pre-release like runtime versions.
// The pre-release version types are sorted alphabetically (e.g. alpha, beta, preview, rc)
// and the pre-release version types are more important that the pre-release version numbers.
return preReleaseVersionType != other!.preReleaseVersionType
? preReleaseVersionType!.CompareTo(other!.preReleaseVersionType)
: preReleaseVersion!.CompareTo(other!.preReleaseVersion);
}
return c;
}
public override string ToString() => FullPath; public override string ToString() => FullPath;
} }

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using NuGet.Versioning;
using Semmle.Util; using Semmle.Util;
using Semmle.Util.Logging; using Semmle.Util.Logging;
@@ -27,7 +28,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
this.newestRuntimes = new(GetNewestRuntimes); this.newestRuntimes = new(GetNewestRuntimes);
} }
[GeneratedRegex(@"^(\S+)\s(\d+\.\d+\.\d+)(-([a-z]+)\.(\d+\.\d+\.\d+))?\s\[(.+)\]$")] [GeneratedRegex(@"^(\S+)\s(\d+\.\d+\.\d+(-[a-z]+\.\d+\.\d+\.\d+)?)\s\[(.+)\]$")]
private static partial Regex RuntimeRegex(); private static partial Regex RuntimeRegex();
/// <summary> /// <summary>
@@ -44,9 +45,9 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
listed.ForEach(r => listed.ForEach(r =>
{ {
var match = regex.Match(r); var match = regex.Match(r);
if (match.Success) if (match.Success && NuGetVersion.TryParse(match.Groups[2].Value, out var version))
{ {
runtimes.AddOrUpdateToLatest(match.Groups[1].Value, new DotNetVersion(match.Groups[6].Value, match.Groups[2].Value, match.Groups[4].Value, match.Groups[5].Value)); runtimes.AddOrUpdateToLatest(match.Groups[1].Value, new DotNetVersion(match.Groups[4].Value, version));
} }
}); });

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using NuGet.Versioning;
using Semmle.Util; using Semmle.Util;
using Semmle.Util.Logging; using Semmle.Util.Logging;
@@ -27,7 +28,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
cscPath = new Lazy<string?>(GetCscPath); cscPath = new Lazy<string?>(GetCscPath);
} }
[GeneratedRegex(@"^(\d+\.\d+\.\d+)(-([a-z]+)\.(\d+\.\d+\.\d+))?\s\[(.+)\]$")] [GeneratedRegex(@"^(\d+\.\d+\.\d+(-[a-z]+\.\d+\.\d+\.\d+)?)\s\[(.+)\]$")]
private static partial Regex SdkRegex(); private static partial Regex SdkRegex();
private static HashSet<DotNetVersion> ParseSdks(IList<string> listed) private static HashSet<DotNetVersion> ParseSdks(IList<string> listed)
@@ -37,9 +38,9 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
listed.ForEach(r => listed.ForEach(r =>
{ {
var match = regex.Match(r); var match = regex.Match(r);
if (match.Success) if (match.Success && NuGetVersion.TryParse(match.Groups[1].Value, out var version))
{ {
sdks.Add(new DotNetVersion(match.Groups[5].Value, match.Groups[1].Value, match.Groups[3].Value, match.Groups[4].Value)); sdks.Add(new DotNetVersion(match.Groups[3].Value, version));
} }
}); });
@@ -73,4 +74,4 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
return path; return path;
} }
} }
} }