Skip to content

Commit e4aaa43

Browse files
committed
C#: Also support alpha, beta, rc candidates.
1 parent f065ba9 commit e4aaa43

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Runtime.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,49 @@ internal partial class Runtime
2525
internal sealed class RuntimeVersion : IComparable<RuntimeVersion>
2626
{
2727
private readonly string dir;
28-
private Version Version { get; }
29-
private Version? PreviewVersion { get; } = null;
30-
private bool IsPreview => PreviewVersion is not null;
28+
private readonly Version version;
29+
private readonly Version? preReleaseVersion;
30+
private readonly string? preReleaseVersionType;
31+
private bool IsPreRelease => preReleaseVersionType is not null && preReleaseVersion is not null;
3132
public string FullPath
3233
{
3334
get
3435
{
35-
var preview = IsPreview ? $"-preview.{PreviewVersion}" : "";
36-
var version = Version + preview;
36+
var preRelease = IsPreRelease ? $"-{preReleaseVersionType}.{preReleaseVersion}" : "";
37+
var version = this.version + preRelease;
3738
return Path.Combine(dir, version);
3839
}
3940
}
4041

41-
private static Version MakeVersion(string version)
42-
{
43-
var versionParts = version.Split('.');
44-
return new Version(int.Parse(versionParts[0]), int.Parse(versionParts[1]), int.Parse(versionParts[2]));
45-
}
46-
47-
public RuntimeVersion(string dir, string version, string previewVersion)
42+
public RuntimeVersion(string dir, string version, string preReleaseVersionType, string preReleaseVersion)
4843
{
4944
this.dir = dir;
50-
Version = MakeVersion(version);
51-
if (!string.IsNullOrEmpty(previewVersion))
45+
this.version = Version.Parse(version);
46+
if (!string.IsNullOrEmpty(preReleaseVersion) && !string.IsNullOrEmpty(preReleaseVersionType))
5247
{
53-
PreviewVersion = MakeVersion(previewVersion);
48+
this.preReleaseVersionType = preReleaseVersionType;
49+
this.preReleaseVersion = Version.Parse(preReleaseVersion);
5450
}
5551
}
5652

5753
public int CompareTo(RuntimeVersion? other)
5854
{
59-
var c = Version.CompareTo(other?.Version);
60-
if (c == 0 && IsPreview)
55+
var c = version.CompareTo(other?.version);
56+
if (c == 0 && IsPreRelease)
6157
{
62-
return other!.IsPreview ? PreviewVersion!.CompareTo(other!.PreviewVersion) : -1;
58+
if (!other!.IsPreRelease)
59+
{
60+
return -1;
61+
}
62+
63+
// Both are pre-release like runtime versions.
64+
// The pre-release version types are sorted alphabetically (e.g. alpha, beta, preview, rc)
65+
// and the pre-release version types are more important that the pre-release version numbers.
66+
return preReleaseVersionType != other!.preReleaseVersionType
67+
? preReleaseVersionType!.CompareTo(other!.preReleaseVersionType)
68+
: preReleaseVersion!.CompareTo(other!.preReleaseVersion);
6369
}
70+
6471
return c;
6572
}
6673

@@ -72,7 +79,7 @@ public override bool Equals(object? obj) =>
7279
public override string ToString() => FullPath;
7380
}
7481

75-
[GeneratedRegex(@"^(\S+)\s(\d+\.\d+\.\d+)(-preview\.(\d+\.\d+\.\d+))?\s\[(\S+)\]$")]
82+
[GeneratedRegex(@"^(\S+)\s(\d+\.\d+\.\d+)(-([a-z]+)\.(\d+\.\d+\.\d+))?\s\[(\S+)\]$")]
7683
private static partial Regex RuntimeRegex();
7784

7885
/// <summary>
@@ -90,7 +97,7 @@ private static Dictionary<string, RuntimeVersion> ParseRuntimes(IList<string> li
9097
var match = RuntimeRegex().Match(r);
9198
if (match.Success)
9299
{
93-
runtimes.AddOrUpdate(match.Groups[1].Value, new RuntimeVersion(match.Groups[5].Value, match.Groups[2].Value, match.Groups[4].Value));
100+
runtimes.AddOrUpdate(match.Groups[1].Value, new RuntimeVersion(match.Groups[6].Value, match.Groups[2].Value, match.Groups[4].Value, match.Groups[5].Value));
94101
}
95102
});
96103

csharp/extractor/Semmle.Extraction.Tests/Runtime.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public void TestRuntime2()
5959
var listedRuntimes = new List<string>
6060
{
6161
"Microsoft.NETCore.App 7.0.2 [/path/dotnet/shared/Microsoft.NETCore.App]",
62-
"Microsoft.NETCore.App 8.0.0-preview.5.23280.8 [/path/dotnet/shared/Microsoft.NETCore.App]",
63-
"Microsoft.NETCore.App 8.0.0-preview.5.43280.8 [/path/dotnet/shared/Microsoft.NETCore.App]"
62+
"Microsoft.NETCore.App 8.0.0-preview.5.43280.8 [/path/dotnet/shared/Microsoft.NETCore.App]",
63+
"Microsoft.NETCore.App 8.0.0-preview.5.23280.8 [/path/dotnet/shared/Microsoft.NETCore.App]"
6464
};
6565
var dotnet = new DotNetStub(listedRuntimes);
6666
var runtime = new Runtime(dotnet);
@@ -74,5 +74,29 @@ public void TestRuntime2()
7474
Assert.True(runtimes.TryGetValue("Microsoft.NETCore.App", out var netCoreApp));
7575
Assert.Equal("/path/dotnet/shared/Microsoft.NETCore.App/8.0.0-preview.5.43280.8", netCoreApp.FullPath);
7676
}
77+
78+
[Fact]
79+
public void TestRuntime3()
80+
{
81+
// Setup
82+
var listedRuntimes = new List<string>
83+
{
84+
"Microsoft.NETCore.App 7.0.2 [/path/dotnet/shared/Microsoft.NETCore.App]",
85+
"Microsoft.NETCore.App 8.0.0-rc.4.43280.8 [/path/dotnet/shared/Microsoft.NETCore.App]",
86+
"Microsoft.NETCore.App 8.0.0-preview.5.23280.8 [/path/dotnet/shared/Microsoft.NETCore.App]"
87+
};
88+
var dotnet = new DotNetStub(listedRuntimes);
89+
var runtime = new Runtime(dotnet);
90+
91+
// Execute
92+
var runtimes = runtime.GetNewestRuntimes();
93+
94+
// Verify
95+
Assert.Single(runtimes);
96+
97+
Assert.True(runtimes.TryGetValue("Microsoft.NETCore.App", out var netCoreApp));
98+
Assert.Equal("/path/dotnet/shared/Microsoft.NETCore.App/8.0.0-rc.4.43280.8", netCoreApp.FullPath);
99+
}
100+
77101
}
78102
}

0 commit comments

Comments
 (0)