-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ReferenceAssemblyPathResolver.cs
140 lines (120 loc) · 5.09 KB
/
ReferenceAssemblyPathResolver.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
namespace Microsoft.Extensions.DependencyModel.Resolution
{
public class ReferenceAssemblyPathResolver: ICompilationAssemblyResolver
{
private readonly IFileSystem _fileSystem;
private readonly string _defaultReferenceAssembliesPath;
private readonly string[] _fallbackSearchPaths;
public ReferenceAssemblyPathResolver()
: this(FileSystemWrapper.Default, EnvironmentWrapper.Default)
{
}
public ReferenceAssemblyPathResolver(string defaultReferenceAssembliesPath, string[] fallbackSearchPaths)
: this(FileSystemWrapper.Default, defaultReferenceAssembliesPath, fallbackSearchPaths)
{
}
internal ReferenceAssemblyPathResolver(IFileSystem fileSystem, IEnvironment environment)
: this(fileSystem,
GetDefaultReferenceAssembliesPath(fileSystem, environment),
GetFallbackSearchPaths(fileSystem, environment))
{
}
internal ReferenceAssemblyPathResolver(IFileSystem fileSystem, string defaultReferenceAssembliesPath, string[] fallbackSearchPaths)
{
_fileSystem = fileSystem;
_defaultReferenceAssembliesPath = defaultReferenceAssembliesPath;
_fallbackSearchPaths = fallbackSearchPaths;
}
public bool TryResolveAssemblyPaths(CompilationLibrary library, List<string> assemblies)
{
if (!string.Equals(library.Type, "referenceassembly", StringComparison.OrdinalIgnoreCase))
{
return false;
}
foreach (var assembly in library.Assemblies)
{
string fullName;
if (!TryResolveReferenceAssembly(assembly, out fullName))
{
throw new InvalidOperationException($"Cannot find reference assembly '{assembly}' file for package {library.Name}");
}
assemblies.Add(fullName);
}
return true;
}
private bool TryResolveReferenceAssembly(string path, out string fullPath)
{
fullPath = null;
if (_defaultReferenceAssembliesPath != null)
{
var relativeToReferenceAssemblies = Path.Combine(_defaultReferenceAssembliesPath, path);
if (_fileSystem.File.Exists(relativeToReferenceAssemblies))
{
fullPath = relativeToReferenceAssemblies;
return true;
}
}
var name = Path.GetFileName(path);
foreach (var fallbackPath in _fallbackSearchPaths)
{
var fallbackFile = Path.Combine(fallbackPath, name);
if (_fileSystem.File.Exists(fallbackFile))
{
fullPath = fallbackFile;
return true;
}
}
return false;
}
internal static string[] GetFallbackSearchPaths(IFileSystem fileSystem, IEnvironment environment)
{
if (!environment.IsWindows())
{
return new string[0];
}
var net20Dir = Path.Combine(environment.GetEnvironmentVariable("WINDIR"), "Microsoft.NET", "Framework", "v2.0.50727");
if (!fileSystem.Directory.Exists(net20Dir))
{
return new string[0];
}
return new[] { net20Dir };
}
internal static string GetDefaultReferenceAssembliesPath(IFileSystem fileSystem, IEnvironment environment)
{
// Allow setting the reference assemblies path via an environment variable
var referenceAssembliesPath = DotNetReferenceAssembliesPathResolver.Resolve(environment, fileSystem);
if (!string.IsNullOrEmpty(referenceAssembliesPath))
{
return referenceAssembliesPath;
}
if (!environment.IsWindows())
{
// There is no reference assemblies path outside of windows
// The environment variable can be used to specify one
return null;
}
// References assemblies are in %ProgramFiles(x86)% on
// 64 bit machines
var programFiles = environment.GetEnvironmentVariable("ProgramFiles(x86)");
if (string.IsNullOrEmpty(programFiles))
{
// On 32 bit machines they are in %ProgramFiles%
programFiles = environment.GetEnvironmentVariable("ProgramFiles");
}
if (string.IsNullOrEmpty(programFiles))
{
// Reference assemblies aren't installed
return null;
}
return Path.Combine(
programFiles,
"Reference Assemblies", "Microsoft", "Framework");
}
}
}