/
DotNetCorePathFinder.cs
334 lines (301 loc) · 10.2 KB
/
DotNetCorePathFinder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright (c) 2018 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using ICSharpCode.Decompiler.Util;
using LightJson.Serialization;
namespace ICSharpCode.Decompiler.Metadata
{
public class DotNetCorePathFinder
{
class DotNetCorePackageInfo
{
public readonly string Name;
public readonly string Version;
public readonly string Type;
public readonly string Path;
public readonly string[] RuntimeComponents;
public DotNetCorePackageInfo(string fullName, string type, string path, string[] runtimeComponents)
{
var parts = fullName.Split('/');
this.Name = parts[0];
if (parts.Length > 1)
{
this.Version = parts[1];
}
else
{
this.Version = "<UNKNOWN>";
}
this.Type = type;
this.Path = path;
this.RuntimeComponents = runtimeComponents ?? Empty<string>.Array;
}
}
static readonly string[] LookupPaths = new string[] {
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages")
};
static readonly string[] RuntimePacks = new[] {
"Microsoft.NETCore.App",
"Microsoft.WindowsDesktop.App",
"Microsoft.AspNetCore.App",
"Microsoft.AspNetCore.All"
};
readonly DotNetCorePackageInfo[] packages;
readonly List<string> searchPaths = new List<string>();
readonly List<string> packageBasePaths = new List<string>();
readonly Version targetFrameworkVersion;
readonly string dotnetBasePath = FindDotNetExeDirectory();
readonly string preferredRuntimePack;
public DotNetCorePathFinder(TargetFrameworkIdentifier targetFramework, Version targetFrameworkVersion,
string preferredRuntimePack)
{
this.targetFrameworkVersion = targetFrameworkVersion;
this.preferredRuntimePack = preferredRuntimePack;
if (targetFramework == TargetFrameworkIdentifier.NETStandard)
{
// .NET Standard 2.1 is implemented by .NET Core 3.0 or higher
if (targetFrameworkVersion.Major == 2 && targetFrameworkVersion.Minor == 1)
{
this.targetFrameworkVersion = new Version(3, 0, 0);
}
}
}
public DotNetCorePathFinder(string parentAssemblyFileName, string targetFrameworkIdString, string preferredRuntimePack,
TargetFrameworkIdentifier targetFramework, Version targetFrameworkVersion, ReferenceLoadInfo loadInfo = null)
: this(targetFramework, targetFrameworkVersion, preferredRuntimePack)
{
string assemblyName = Path.GetFileNameWithoutExtension(parentAssemblyFileName);
string basePath = Path.GetDirectoryName(parentAssemblyFileName);
searchPaths.Add(basePath);
var depsJsonFileName = Path.Combine(basePath, $"{assemblyName}.deps.json");
if (File.Exists(depsJsonFileName))
{
packages = LoadPackageInfos(depsJsonFileName, targetFrameworkIdString).ToArray();
foreach (var path in LookupPaths)
{
foreach (var p in packages)
{
foreach (var item in p.RuntimeComponents)
{
var itemPath = Path.GetDirectoryName(item);
var fullPath = Path.Combine(path, p.Name, p.Version, itemPath).ToLowerInvariant();
if (Directory.Exists(fullPath))
packageBasePaths.Add(fullPath);
}
}
}
}
else
{
loadInfo?.AddMessage(assemblyName, MessageKind.Warning, $"{assemblyName}.deps.json could not be found!");
}
}
public void AddSearchDirectory(string path)
{
this.searchPaths.Add(path);
}
public void RemoveSearchDirectory(string path)
{
this.searchPaths.Remove(path);
}
public string TryResolveDotNetCore(IAssemblyReference name)
{
foreach (var basePath in searchPaths.Concat(packageBasePaths))
{
if (File.Exists(Path.Combine(basePath, name.Name + ".dll")))
{
return Path.Combine(basePath, name.Name + ".dll");
}
else if (File.Exists(Path.Combine(basePath, name.Name + ".exe")))
{
return Path.Combine(basePath, name.Name + ".exe");
}
}
return TryResolveDotNetCoreShared(name, out _);
}
internal string GetReferenceAssemblyPath(string targetFramework)
{
var (tfi, version) = UniversalAssemblyResolver.ParseTargetFramework(targetFramework);
string identifier, identifierExt;
switch (tfi)
{
case TargetFrameworkIdentifier.NETCoreApp:
identifier = "Microsoft.NETCore.App";
identifierExt = "netcoreapp" + version.Major + "." + version.Minor;
break;
case TargetFrameworkIdentifier.NETStandard:
identifier = "NETStandard.Library";
identifierExt = "netstandard" + version.Major + "." + version.Minor;
break;
case TargetFrameworkIdentifier.NET:
identifier = "Microsoft.NETCore.App";
identifierExt = "net" + version.Major + "." + version.Minor;
break;
default:
throw new NotSupportedException();
}
return Path.Combine(dotnetBasePath, "packs", identifier + ".Ref", version.ToString(), "ref", identifierExt);
}
static IEnumerable<DotNetCorePackageInfo> LoadPackageInfos(string depsJsonFileName, string targetFramework)
{
var dependencies = JsonReader.Parse(File.ReadAllText(depsJsonFileName));
var runtimeInfos = dependencies["targets"][targetFramework].AsJsonObject;
var libraries = dependencies["libraries"].AsJsonObject;
if (runtimeInfos == null || libraries == null)
yield break;
foreach (var library in libraries)
{
var type = library.Value["type"].AsString;
var path = library.Value["path"].AsString;
var runtimeInfo = runtimeInfos[library.Key].AsJsonObject?["runtime"].AsJsonObject;
string[] components = new string[runtimeInfo?.Count ?? 0];
if (runtimeInfo != null)
{
int i = 0;
foreach (var component in runtimeInfo)
{
components[i] = component.Key;
i++;
}
}
yield return new DotNetCorePackageInfo(library.Key, type, path, components);
}
}
public string TryResolveDotNetCoreShared(IAssemblyReference name, out string runtimePack)
{
if (dotnetBasePath == null)
{
runtimePack = null;
return null;
}
IEnumerable<string> runtimePacks = RuntimePacks;
if (preferredRuntimePack != null)
{
runtimePacks = new[] { preferredRuntimePack }.Concat(runtimePacks);
}
foreach (string pack in runtimePacks)
{
runtimePack = pack;
string basePath = Path.Combine(dotnetBasePath, "shared", pack);
if (!Directory.Exists(basePath))
continue;
var closestVersion = GetClosestVersionFolder(basePath, targetFrameworkVersion);
if (File.Exists(Path.Combine(basePath, closestVersion, name.Name + ".dll")))
{
return Path.Combine(basePath, closestVersion, name.Name + ".dll");
}
else if (File.Exists(Path.Combine(basePath, closestVersion, name.Name + ".exe")))
{
return Path.Combine(basePath, closestVersion, name.Name + ".exe");
}
}
runtimePack = null;
return null;
}
static string GetClosestVersionFolder(string basePath, Version version)
{
var foundVersions = new DirectoryInfo(basePath).GetDirectories()
.Select(d => ConvertToVersion(d.Name))
.Where(v => v.version != null);
foreach (var folder in foundVersions.OrderBy(v => v.Item1))
{
if (folder.version >= version)
return folder.directoryName;
}
return version.ToString();
}
internal static (Version version, string directoryName) ConvertToVersion(string name)
{
string RemoveTrailingVersionInfo()
{
string shortName = name;
int dashIndex = shortName.IndexOf('-');
if (dashIndex > 0)
{
shortName = shortName.Remove(dashIndex);
}
return shortName;
}
try
{
return (new Version(RemoveTrailingVersionInfo()), name);
}
catch (Exception ex)
{
Trace.TraceWarning(ex.ToString());
return (null, null);
}
}
public static string FindDotNetExeDirectory()
{
string dotnetExeName = (Environment.OSVersion.Platform == PlatformID.Unix) ? "dotnet" : "dotnet.exe";
foreach (var item in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator))
{
try
{
string fileName = Path.Combine(item, dotnetExeName);
if (!File.Exists(fileName))
continue;
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
if ((new FileInfo(fileName).Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
{
fileName = GetRealPath(fileName, Encoding.Default);
if (!File.Exists(fileName))
continue;
}
}
return Path.GetDirectoryName(fileName);
}
catch (ArgumentException) { }
}
return null;
}
static unsafe string GetRealPath(string path, Encoding encoding)
{
var bytes = encoding.GetBytes(path);
fixed (byte* input = bytes)
{
byte* output = GetRealPath(input, null);
if (output == null)
{
return null;
}
int len = 0;
for (byte* c = output; *c != 0; c++)
{
len++;
}
byte[] result = new byte[len];
Marshal.Copy((IntPtr)output, result, 0, result.Length);
Free(output);
return encoding.GetString(result);
}
}
[DllImport("libc", EntryPoint = "realpath")]
static extern unsafe byte* GetRealPath(byte* path, byte* resolvedPath);
[DllImport("libc", EntryPoint = "free")]
static extern unsafe void Free(void* ptr);
}
}