|
3 | 3 | using System.Runtime.InteropServices;
|
4 | 4 | using System.IO;
|
5 | 5 | using System.Linq;
|
| 6 | +using System.Text.RegularExpressions; |
| 7 | +using Semmle.BuildAnalyser; |
6 | 8 | using Semmle.Util;
|
7 | 9 |
|
8 | 10 | namespace Semmle.Extraction.CSharp.Standalone
|
9 | 11 | {
|
10 | 12 | /// <summary>
|
11 | 13 | /// Locates .NET Runtimes.
|
12 | 14 | /// </summary>
|
13 |
| - internal static class Runtime |
| 15 | + internal partial class Runtime |
14 | 16 | {
|
| 17 | + private const string netCoreApp = "Microsoft.NETCore.App"; |
| 18 | + private const string aspNetCoreApp = "Microsoft.AspNetCore.App"; |
| 19 | + |
| 20 | + private readonly IDotNet dotNet; |
15 | 21 | private static string ExecutingRuntime => RuntimeEnvironment.GetRuntimeDirectory();
|
16 | 22 |
|
17 |
| - /// <summary> |
18 |
| - /// Locates .NET Core Runtimes. |
19 |
| - /// </summary> |
20 |
| - private static IEnumerable<string> CoreRuntimes |
| 23 | + public Runtime(IDotNet dotNet) => this.dotNet = dotNet; |
| 24 | + |
| 25 | + internal sealed class RuntimeVersion : IComparable<RuntimeVersion> |
21 | 26 | {
|
22 |
| - get |
| 27 | + private readonly string dir; |
| 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; |
| 32 | + public string FullPath |
23 | 33 | {
|
24 |
| - var dotnetPath = FileUtils.FindProgramOnPath(Win32.IsWindows() ? "dotnet.exe" : "dotnet"); |
25 |
| - var dotnetDirs = dotnetPath is not null |
26 |
| - ? new[] { dotnetPath } |
27 |
| - : new[] { "/usr/share/dotnet", @"C:\Program Files\dotnet" }; |
28 |
| - var coreDirs = dotnetDirs.Select(d => Path.Combine(d, "shared", "Microsoft.NETCore.App")); |
| 34 | + get |
| 35 | + { |
| 36 | + var preRelease = IsPreRelease ? $"-{preReleaseVersionType}.{preReleaseVersion}" : ""; |
| 37 | + var version = this.version + preRelease; |
| 38 | + return Path.Combine(dir, version); |
| 39 | + } |
| 40 | + } |
29 | 41 |
|
30 |
| - var dir = coreDirs.FirstOrDefault(Directory.Exists); |
31 |
| - if (dir is not null) |
| 42 | + public RuntimeVersion(string dir, string version, string preReleaseVersionType, string preReleaseVersion) |
| 43 | + { |
| 44 | + this.dir = dir; |
| 45 | + this.version = Version.Parse(version); |
| 46 | + if (!string.IsNullOrEmpty(preReleaseVersion) && !string.IsNullOrEmpty(preReleaseVersionType)) |
32 | 47 | {
|
33 |
| - return Directory.EnumerateDirectories(dir).OrderByDescending(Path.GetFileName); |
| 48 | + this.preReleaseVersionType = preReleaseVersionType; |
| 49 | + this.preReleaseVersion = Version.Parse(preReleaseVersion); |
34 | 50 | }
|
| 51 | + } |
35 | 52 |
|
36 |
| - return Enumerable.Empty<string>(); |
| 53 | + public int CompareTo(RuntimeVersion? other) |
| 54 | + { |
| 55 | + var c = version.CompareTo(other?.version); |
| 56 | + if (c == 0 && IsPreRelease) |
| 57 | + { |
| 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); |
| 69 | + } |
| 70 | + |
| 71 | + return c; |
37 | 72 | }
|
| 73 | + |
| 74 | + public override bool Equals(object? obj) => |
| 75 | + obj is not null && obj is RuntimeVersion other && other.FullPath == FullPath; |
| 76 | + |
| 77 | + public override int GetHashCode() => FullPath.GetHashCode(); |
| 78 | + |
| 79 | + public override string ToString() => FullPath; |
| 80 | + } |
| 81 | + |
| 82 | + [GeneratedRegex(@"^(\S+)\s(\d+\.\d+\.\d+)(-([a-z]+)\.(\d+\.\d+\.\d+))?\s\[(\S+)\]$")] |
| 83 | + private static partial Regex RuntimeRegex(); |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Parses the output of `dotnet --list-runtimes` to get a map from a runtime to the location of |
| 87 | + /// the newest version of the runtime. |
| 88 | + /// It is assume that the format of a listed runtime is something like: |
| 89 | + /// Microsoft.NETCore.App 7.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App] |
| 90 | + /// </summary> |
| 91 | + private static Dictionary<string, RuntimeVersion> ParseRuntimes(IList<string> listed) |
| 92 | + { |
| 93 | + // Parse listed runtimes. |
| 94 | + var runtimes = new Dictionary<string, RuntimeVersion>(); |
| 95 | + listed.ForEach(r => |
| 96 | + { |
| 97 | + var match = RuntimeRegex().Match(r); |
| 98 | + if (match.Success) |
| 99 | + { |
| 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)); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + return runtimes; |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Returns a dictionary mapping runtimes to their newest version. |
| 109 | + /// </summary> |
| 110 | + internal Dictionary<string, RuntimeVersion> GetNewestRuntimes() |
| 111 | + { |
| 112 | + var listed = dotNet.GetListedRuntimes(); |
| 113 | + return ParseRuntimes(listed); |
38 | 114 | }
|
39 | 115 |
|
40 | 116 | /// <summary>
|
@@ -69,24 +145,33 @@ private static IEnumerable<string> DesktopRuntimes
|
69 | 145 | }
|
70 | 146 | }
|
71 | 147 |
|
72 |
| - /// <summary> |
73 |
| - /// Gets the .NET runtime location to use for extraction |
74 |
| - /// </summary> |
75 |
| - public static string GetRuntime(bool useSelfContained) => useSelfContained ? ExecutingRuntime : Runtimes.First(); |
76 |
| - |
77 |
| - private static IEnumerable<string> Runtimes |
| 148 | + private IEnumerable<string> GetRuntimes() |
78 | 149 | {
|
79 |
| - get |
80 |
| - { |
81 |
| - foreach (var r in CoreRuntimes) |
82 |
| - yield return r; |
| 150 | + // Gets the newest version of the installed runtimes. |
| 151 | + var newestRuntimes = GetNewestRuntimes(); |
83 | 152 |
|
84 |
| - foreach (var r in DesktopRuntimes) |
85 |
| - yield return r; |
| 153 | + // Location of the newest .NET Core Runtime. |
| 154 | + if (newestRuntimes.TryGetValue(netCoreApp, out var netCoreVersion)) |
| 155 | + { |
| 156 | + yield return netCoreVersion.FullPath; |
| 157 | + } |
86 | 158 |
|
87 |
| - // A bad choice if it's the self-contained runtime distributed in codeql dist. |
88 |
| - yield return ExecutingRuntime; |
| 159 | + // Location of the newest ASP.NET Core Runtime. |
| 160 | + if (newestRuntimes.TryGetValue(aspNetCoreApp, out var aspNetCoreVersion)) |
| 161 | + { |
| 162 | + yield return aspNetCoreVersion.FullPath; |
89 | 163 | }
|
| 164 | + |
| 165 | + foreach (var r in DesktopRuntimes) |
| 166 | + yield return r; |
| 167 | + |
| 168 | + // A bad choice if it's the self-contained runtime distributed in codeql dist. |
| 169 | + yield return ExecutingRuntime; |
90 | 170 | }
|
| 171 | + |
| 172 | + /// <summary> |
| 173 | + /// Gets the .NET runtime location to use for extraction |
| 174 | + /// </summary> |
| 175 | + public string GetRuntime(bool useSelfContained) => useSelfContained ? ExecutingRuntime : GetRuntimes().First(); |
91 | 176 | }
|
92 | 177 | }
|
0 commit comments