@@ -14,54 +14,39 @@ namespace Semmle.Extraction.CSharp.Standalone
1414 /// </summary>
1515 internal partial class Runtime
1616 {
17+ private const string netCoreApp = "Microsoft.NETCore.App" ;
18+ private const string aspNetCoreApp = "Microsoft.AspNetCore.App" ;
19+
1720 private readonly DotNet dotNet ;
21+ private static string ExecutingRuntime => RuntimeEnvironment . GetRuntimeDirectory ( ) ;
22+
1823 public Runtime ( DotNet dotNet ) => this . dotNet = dotNet ;
1924
20- private sealed class Version : IComparable < Version >
25+ private sealed class RuntimeVersion : IComparable < RuntimeVersion >
2126 {
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 ( ) ) ;
27+ private readonly string dir ;
28+ public Version Version { get ; }
2929
30+ public string FullPath => Path . Combine ( dir , Version . ToString ( ) ) ;
3031
31- public Version ( string version , string dir )
32+ // TODO: Also improve to account for preview versions.
33+ public RuntimeVersion ( string version , string dir )
3234 {
3335 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 ;
36+ Version = new Version ( int . Parse ( parts [ 0 ] ) , int . Parse ( parts [ 1 ] ) , int . Parse ( parts [ 2 ] ) ) ;
37+ this . dir = dir ;
3838 }
3939
40- public int CompareTo ( Version ? other ) =>
41- other is null ? 1 : GetHashCode ( ) . CompareTo ( other . GetHashCode ( ) ) ;
40+ public int CompareTo ( RuntimeVersion ? other ) => Version . CompareTo ( other ? . Version ) ;
4241
4342 public override bool Equals ( object ? obj ) =>
44- obj is not null && obj is Version other && other . FullPath == FullPath ;
43+ obj is not null && obj is RuntimeVersion other && other . FullPath == FullPath ;
4544
46- public override int GetHashCode ( ) =>
47- ( Major * 1000 + Minor ) * 1000 + Patch ;
45+ public override int GetHashCode ( ) => Version . GetHashCode ( ) ;
4846
49- public override string ToString ( ) =>
50- $ "{ Major } .{ Minor } .{ Patch } ";
47+ public override string ToString ( ) => FullPath ;
5148 }
5249
53- private static string ExecutingRuntime => RuntimeEnvironment . GetRuntimeDirectory ( ) ;
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- }
6550
6651 [ GeneratedRegex ( @"^(\S+)\s(\d+\.\d+\.\d+)\s\[(\S+)\]$" ) ]
6752 private static partial Regex RuntimeRegex ( ) ;
@@ -72,37 +57,28 @@ private static void AddOrUpdate(Dictionary<string, Version> dict, string framewo
7257 /// It is assume that the format of a listed runtime is something like:
7358 /// Microsoft.NETCore.App 7.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
7459 /// </summary>
75- private static Dictionary < string , Version > ParseRuntimes ( IList < string > listed )
60+ private static Dictionary < string , RuntimeVersion > ParseRuntimes ( IList < string > listed )
7661 {
7762 // Parse listed runtimes.
78- var runtimes = new Dictionary < string , Version > ( ) ;
63+ var runtimes = new Dictionary < string , RuntimeVersion > ( ) ;
7964 listed . ForEach ( r =>
8065 {
8166 var match = RuntimeRegex ( ) . Match ( r ) ;
8267 if ( match . Success )
8368 {
84- AddOrUpdate ( runtimes , match . Groups [ 1 ] . Value , new Version ( match . Groups [ 2 ] . Value , match . Groups [ 3 ] . Value ) ) ;
69+ runtimes . AddOrUpdate ( match . Groups [ 1 ] . Value , new RuntimeVersion ( match . Groups [ 2 ] . Value , match . Groups [ 3 ] . Value ) ) ;
8570 }
8671 } ) ;
8772
8873 return runtimes ;
8974 }
9075
91- private Dictionary < string , Version > GetNewestRuntimes ( )
76+ private Dictionary < string , RuntimeVersion > GetNewestRuntimes ( )
9277 {
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 > ( ) ;
102- }
78+ var listed = dotNet . GetListedRuntimes ( ) ;
79+ return ParseRuntimes ( listed ) ;
10380 }
10481
105-
10682 /// <summary>
10783 /// Locates .NET Desktop Runtimes.
10884 /// This includes Mono and Microsoft.NET.
@@ -141,15 +117,15 @@ private IEnumerable<string> GetRuntimes()
141117 var newestRuntimes = GetNewestRuntimes ( ) ;
142118
143119 // Location of the newest .NET Core Runtime.
144- if ( newestRuntimes . TryGetValue ( NetCoreApp , out var netCoreApp ) )
120+ if ( newestRuntimes . TryGetValue ( netCoreApp , out var netCoreVersion ) )
145121 {
146- yield return netCoreApp . FullPath ;
122+ yield return netCoreVersion . FullPath ;
147123 }
148124
149125 // Location of the newest ASP.NET Core Runtime.
150- if ( newestRuntimes . TryGetValue ( AspNetCoreApp , out var aspNetCoreApp ) )
126+ if ( newestRuntimes . TryGetValue ( aspNetCoreApp , out var aspNetCoreVersion ) )
151127 {
152- yield return aspNetCoreApp . FullPath ;
128+ yield return aspNetCoreVersion . FullPath ;
153129 }
154130
155131 foreach ( var r in DesktopRuntimes )
0 commit comments