Skip to content

Commit 4270425

Browse files
committed
C#: Cater for preview versions.
1 parent bffdbbc commit 4270425

File tree

1 file changed

+35
-11
lines changed
  • csharp/extractor/Semmle.Extraction.CSharp.Standalone

1 file changed

+35
-11
lines changed

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

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,54 @@ internal partial class Runtime
2525
private sealed class RuntimeVersion : IComparable<RuntimeVersion>
2626
{
2727
private readonly string dir;
28-
public Version Version { get; }
28+
private Version Version { get; }
29+
private Version? PreviewVersion { get; } = null;
30+
private bool IsPreview => PreviewVersion is not null;
31+
public string FullPath
32+
{
33+
get
34+
{
35+
var preview = IsPreview ? $"-preview.{PreviewVersion}" : "";
36+
var version = Version + preview;
37+
return Path.Combine(dir, version);
38+
}
39+
}
2940

30-
public string FullPath => Path.Combine(dir, Version.ToString());
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+
}
3146

32-
// TODO: Also improve to account for preview versions.
33-
public RuntimeVersion(string version, string dir)
47+
public RuntimeVersion(string dir, string version, string previewVersion)
3448
{
35-
var parts = version.Split('.');
36-
Version = new Version(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]));
3749
this.dir = dir;
50+
Version = MakeVersion(version);
51+
if (!string.IsNullOrEmpty(previewVersion))
52+
{
53+
PreviewVersion = MakeVersion(previewVersion);
54+
}
3855
}
3956

40-
public int CompareTo(RuntimeVersion? other) => Version.CompareTo(other?.Version);
57+
public int CompareTo(RuntimeVersion? other)
58+
{
59+
var c = Version.CompareTo(other?.Version);
60+
if (c == 0 && IsPreview)
61+
{
62+
return other!.IsPreview ? PreviewVersion!.CompareTo(other!.PreviewVersion) : -1;
63+
}
64+
return c;
65+
}
4166

4267
public override bool Equals(object? obj) =>
4368
obj is not null && obj is RuntimeVersion other && other.FullPath == FullPath;
4469

45-
public override int GetHashCode() => Version.GetHashCode();
70+
public override int GetHashCode() => FullPath.GetHashCode();
4671

4772
public override string ToString() => FullPath;
4873
}
4974

50-
51-
[GeneratedRegex(@"^(\S+)\s(\d+\.\d+\.\d+)\s\[(\S+)\]$")]
75+
[GeneratedRegex(@"^(\S+)\s(\d+\.\d+\.\d+)(-preview\.(\d+\.\d+\.\d+))?\s\[(\S+)\]$")]
5276
private static partial Regex RuntimeRegex();
5377

5478
/// <summary>
@@ -66,7 +90,7 @@ private static Dictionary<string, RuntimeVersion> ParseRuntimes(IList<string> li
6690
var match = RuntimeRegex().Match(r);
6791
if (match.Success)
6892
{
69-
runtimes.AddOrUpdate(match.Groups[1].Value, new RuntimeVersion(match.Groups[2].Value, match.Groups[3].Value));
93+
runtimes.AddOrUpdate(match.Groups[1].Value, new RuntimeVersion(match.Groups[5].Value, match.Groups[2].Value, match.Groups[4].Value));
7094
}
7195
});
7296

0 commit comments

Comments
 (0)