@@ -14,54 +14,39 @@ namespace Semmle.Extraction.CSharp.Standalone
14
14
/// </summary>
15
15
internal partial class Runtime
16
16
{
17
+ private const string netCoreApp = "Microsoft.NETCore.App" ;
18
+ private const string aspNetCoreApp = "Microsoft.AspNetCore.App" ;
19
+
17
20
private readonly DotNet dotNet ;
21
+ private static string ExecutingRuntime => RuntimeEnvironment . GetRuntimeDirectory ( ) ;
22
+
18
23
public Runtime ( DotNet dotNet ) => this . dotNet = dotNet ;
19
24
20
- private sealed class Version : IComparable < Version >
25
+ private sealed class RuntimeVersion : IComparable < RuntimeVersion >
21
26
{
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 ; }
29
29
30
+ public string FullPath => Path . Combine ( dir , Version . ToString ( ) ) ;
30
31
31
- public Version ( string version , string dir )
32
+ // TODO: Also improve to account for preview versions.
33
+ public RuntimeVersion ( string version , string dir )
32
34
{
33
35
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 ;
38
38
}
39
39
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 ) ;
42
41
43
42
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 ;
45
44
46
- public override int GetHashCode ( ) =>
47
- ( Major * 1000 + Minor ) * 1000 + Patch ;
45
+ public override int GetHashCode ( ) => Version . GetHashCode ( ) ;
48
46
49
- public override string ToString ( ) =>
50
- $ "{ Major } .{ Minor } .{ Patch } ";
47
+ public override string ToString ( ) => FullPath ;
51
48
}
52
49
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
- }
65
50
66
51
[ GeneratedRegex ( @"^(\S+)\s(\d+\.\d+\.\d+)\s\[(\S+)\]$" ) ]
67
52
private static partial Regex RuntimeRegex ( ) ;
@@ -72,37 +57,28 @@ private static void AddOrUpdate(Dictionary<string, Version> dict, string framewo
72
57
/// It is assume that the format of a listed runtime is something like:
73
58
/// Microsoft.NETCore.App 7.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
74
59
/// </summary>
75
- private static Dictionary < string , Version > ParseRuntimes ( IList < string > listed )
60
+ private static Dictionary < string , RuntimeVersion > ParseRuntimes ( IList < string > listed )
76
61
{
77
62
// Parse listed runtimes.
78
- var runtimes = new Dictionary < string , Version > ( ) ;
63
+ var runtimes = new Dictionary < string , RuntimeVersion > ( ) ;
79
64
listed . ForEach ( r =>
80
65
{
81
66
var match = RuntimeRegex ( ) . Match ( r ) ;
82
67
if ( match . Success )
83
68
{
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 ) ) ;
85
70
}
86
71
} ) ;
87
72
88
73
return runtimes ;
89
74
}
90
75
91
- private Dictionary < string , Version > GetNewestRuntimes ( )
76
+ private Dictionary < string , RuntimeVersion > GetNewestRuntimes ( )
92
77
{
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 ) ;
103
80
}
104
81
105
-
106
82
/// <summary>
107
83
/// Locates .NET Desktop Runtimes.
108
84
/// This includes Mono and Microsoft.NET.
@@ -141,15 +117,15 @@ private IEnumerable<string> GetRuntimes()
141
117
var newestRuntimes = GetNewestRuntimes ( ) ;
142
118
143
119
// Location of the newest .NET Core Runtime.
144
- if ( newestRuntimes . TryGetValue ( NetCoreApp , out var netCoreApp ) )
120
+ if ( newestRuntimes . TryGetValue ( netCoreApp , out var netCoreVersion ) )
145
121
{
146
- yield return netCoreApp . FullPath ;
122
+ yield return netCoreVersion . FullPath ;
147
123
}
148
124
149
125
// Location of the newest ASP.NET Core Runtime.
150
- if ( newestRuntimes . TryGetValue ( AspNetCoreApp , out var aspNetCoreApp ) )
126
+ if ( newestRuntimes . TryGetValue ( aspNetCoreApp , out var aspNetCoreVersion ) )
151
127
{
152
- yield return aspNetCoreApp . FullPath ;
128
+ yield return aspNetCoreVersion . FullPath ;
153
129
}
154
130
155
131
foreach ( var r in DesktopRuntimes )
0 commit comments