From a3e545ddd5b5d636f76ac6fefd930fc8890cfa13 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Thu, 4 Dec 2025 14:26:27 +0100 Subject: [PATCH] C#: Use NuGetVersion instead of homemade version implementation. --- .../DotNetVersion.cs | 47 ++++--------------- .../Runtime.cs | 7 +-- .../Sdk.cs | 9 ++-- 3 files changed, 17 insertions(+), 46 deletions(-) diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetVersion.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetVersion.cs index b4273d02b77..31a4ac2292d 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetVersion.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetVersion.cs @@ -1,24 +1,16 @@ using System; using System.IO; +using NuGet.Versioning; namespace Semmle.Extraction.CSharp.DependencyFetching { internal record DotNetVersion : IComparable { private readonly string dir; - private readonly Version version; - private readonly Version? preReleaseVersion; - private readonly string? preReleaseVersionType; - private bool IsPreRelease => preReleaseVersionType is not null && preReleaseVersion is not null; + private readonly NuGetVersion version; - private string FullVersion - { - get - { - var preRelease = IsPreRelease ? $"-{preReleaseVersionType}.{preReleaseVersion}" : ""; - return this.version + preRelease; - } - } + private string FullVersion => + version.ToString(); 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.version = Version.Parse(version); - if (!string.IsNullOrEmpty(preReleaseVersion) && !string.IsNullOrEmpty(preReleaseVersionType)) - { - this.preReleaseVersionType = preReleaseVersionType; - this.preReleaseVersion = Version.Parse(preReleaseVersion); - } + this.version = version; } - public int CompareTo(DotNetVersion? other) - { - 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 int CompareTo(DotNetVersion? other) => + version.CompareTo(other?.version); public override string ToString() => FullPath; } diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Runtime.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Runtime.cs index a489ec504c4..64c835d27fc 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Runtime.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Runtime.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text.RegularExpressions; +using NuGet.Versioning; using Semmle.Util; using Semmle.Util.Logging; @@ -27,7 +28,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching 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(); /// @@ -44,9 +45,9 @@ namespace Semmle.Extraction.CSharp.DependencyFetching listed.ForEach(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)); } }); diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Sdk.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Sdk.cs index 848350bce98..c4d1ba9ac08 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Sdk.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Sdk.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; +using NuGet.Versioning; using Semmle.Util; using Semmle.Util.Logging; @@ -27,7 +28,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching cscPath = new Lazy(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 HashSet ParseSdks(IList listed) @@ -37,9 +38,9 @@ namespace Semmle.Extraction.CSharp.DependencyFetching listed.ForEach(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; } } -} \ No newline at end of file +}