|
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 readonly DotNet dotNet; |
| 18 | + public Runtime(DotNet dotNet) => this.dotNet = dotNet; |
| 19 | + |
| 20 | + private sealed class Version : IComparable<Version> |
| 21 | + { |
| 22 | + private readonly string Dir; |
| 23 | + public int Major { get; } |
| 24 | + public int Minor { get; } |
| 25 | + public int Patch { get; } |
| 26 | + |
| 27 | + |
| 28 | + public string FullPath => Path.Combine(Dir, this.ToString()); |
| 29 | + |
| 30 | + |
| 31 | + public Version(string version, string dir) |
| 32 | + { |
| 33 | + var parts = version.Split('.'); |
| 34 | + Major = int.Parse(parts[0]); |
| 35 | + Minor = int.Parse(parts[1]); |
| 36 | + Patch = int.Parse(parts[2]); |
| 37 | + Dir = dir; |
| 38 | + } |
| 39 | + |
| 40 | + public int CompareTo(Version? other) => |
| 41 | + other is null ? 1 : GetHashCode().CompareTo(other.GetHashCode()); |
| 42 | + |
| 43 | + public override bool Equals(object? obj) => |
| 44 | + obj is not null && obj is Version other && other.FullPath == FullPath; |
| 45 | + |
| 46 | + public override int GetHashCode() => |
| 47 | + (Major * 1000 + Minor) * 1000 + Patch; |
| 48 | + |
| 49 | + public override string ToString() => |
| 50 | + $"{Major}.{Minor}.{Patch}"; |
| 51 | + } |
| 52 | + |
15 | 53 | private static string ExecutingRuntime => RuntimeEnvironment.GetRuntimeDirectory();
|
16 | 54 |
|
| 55 | + private static readonly string NetCoreApp = "Microsoft.NETCore.App"; |
| 56 | + private static readonly string AspNetCoreApp = "Microsoft.AspNetCore.App"; |
| 57 | + |
| 58 | + private static void AddOrUpdate(Dictionary<string, Version> dict, string framework, Version version) |
| 59 | + { |
| 60 | + if (!dict.TryGetValue(framework, out var existing) || existing.CompareTo(version) < 0) |
| 61 | + { |
| 62 | + dict[framework] = version; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + [GeneratedRegex(@"^(\S+)\s(\d+\.\d+\.\d+)\s\[(\S+)\]$")] |
| 67 | + private static partial Regex RuntimeRegex(); |
| 68 | + |
17 | 69 | /// <summary>
|
18 |
| - /// Locates .NET Core Runtimes. |
| 70 | + /// Parses the output of `dotnet --list-runtimes` to get a map from a runtime to the location of |
| 71 | + /// the newest version of the runtime. |
| 72 | + /// It is assume that the format of a listed runtime is something like: |
| 73 | + /// Microsoft.NETCore.App 7.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App] |
19 | 74 | /// </summary>
|
20 |
| - private static IEnumerable<string> CoreRuntimes |
| 75 | + private static Dictionary<string, Version> ParseRuntimes(IList<string> listed) |
21 | 76 | {
|
22 |
| - get |
| 77 | + // Parse listed runtimes. |
| 78 | + var runtimes = new Dictionary<string, Version>(); |
| 79 | + listed.ForEach(r => |
23 | 80 | {
|
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")); |
29 |
| - |
30 |
| - var dir = coreDirs.FirstOrDefault(Directory.Exists); |
31 |
| - if (dir is not null) |
| 81 | + var match = RuntimeRegex().Match(r); |
| 82 | + if (match.Success) |
32 | 83 | {
|
33 |
| - return Directory.EnumerateDirectories(dir).OrderByDescending(Path.GetFileName); |
| 84 | + AddOrUpdate(runtimes, match.Groups[1].Value, new Version(match.Groups[2].Value, match.Groups[3].Value)); |
34 | 85 | }
|
| 86 | + }); |
35 | 87 |
|
36 |
| - return Enumerable.Empty<string>(); |
| 88 | + return runtimes; |
| 89 | + } |
| 90 | + |
| 91 | + private Dictionary<string, Version> GetNewestRuntimes() |
| 92 | + { |
| 93 | + try |
| 94 | + { |
| 95 | + var listed = dotNet.GetListedRuntimes(); |
| 96 | + return ParseRuntimes(listed); |
| 97 | + } |
| 98 | + catch (Exception ex) |
| 99 | + when (ex is System.ComponentModel.Win32Exception || ex is FileNotFoundException) |
| 100 | + { |
| 101 | + return new Dictionary<string, Version>(); |
37 | 102 | }
|
38 | 103 | }
|
39 | 104 |
|
| 105 | + |
40 | 106 | /// <summary>
|
41 | 107 | /// Locates .NET Desktop Runtimes.
|
42 | 108 | /// This includes Mono and Microsoft.NET.
|
@@ -69,24 +135,33 @@ private static IEnumerable<string> DesktopRuntimes
|
69 | 135 | }
|
70 | 136 | }
|
71 | 137 |
|
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 |
| 138 | + private IEnumerable<string> GetRuntimes() |
78 | 139 | {
|
79 |
| - get |
80 |
| - { |
81 |
| - foreach (var r in CoreRuntimes) |
82 |
| - yield return r; |
| 140 | + // Gets the newest version of the installed runtimes. |
| 141 | + var newestRuntimes = GetNewestRuntimes(); |
83 | 142 |
|
84 |
| - foreach (var r in DesktopRuntimes) |
85 |
| - yield return r; |
| 143 | + // Location of the newest .NET Core Runtime. |
| 144 | + if (newestRuntimes.TryGetValue(NetCoreApp, out var netCoreApp)) |
| 145 | + { |
| 146 | + yield return netCoreApp.FullPath; |
| 147 | + } |
86 | 148 |
|
87 |
| - // A bad choice if it's the self-contained runtime distributed in codeql dist. |
88 |
| - yield return ExecutingRuntime; |
| 149 | + // Location of the newest ASP.NET Core Runtime. |
| 150 | + if (newestRuntimes.TryGetValue(AspNetCoreApp, out var aspNetCoreApp)) |
| 151 | + { |
| 152 | + yield return aspNetCoreApp.FullPath; |
89 | 153 | }
|
| 154 | + |
| 155 | + foreach (var r in DesktopRuntimes) |
| 156 | + yield return r; |
| 157 | + |
| 158 | + // A bad choice if it's the self-contained runtime distributed in codeql dist. |
| 159 | + yield return ExecutingRuntime; |
90 | 160 | }
|
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// Gets the .NET runtime location to use for extraction |
| 164 | + /// </summary> |
| 165 | + public string GetRuntime(bool useSelfContained) => useSelfContained ? ExecutingRuntime : GetRuntimes().First(); |
91 | 166 | }
|
92 | 167 | }
|
0 commit comments