-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathFrameworkReferenceResolver.cs
More file actions
71 lines (59 loc) · 2.66 KB
/
FrameworkReferenceResolver.cs
File metadata and controls
71 lines (59 loc) · 2.66 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using Microsoft.Extensions.DependencyModel.Resolution;
namespace Microsoft.NET.Build.Tasks
{
internal class FrameworkReferenceResolver
{
private readonly Func<string, string> _getEnvironmentVariable;
/// <summary>
/// Creates an instance that reads environment variables from the process environment.
/// </summary>
public FrameworkReferenceResolver()
: this(Environment.GetEnvironmentVariable)
{
}
/// <summary>
/// Creates an instance that reads environment variables via the supplied delegate.
/// Use this from MSBuild tasks to route reads through TaskEnvironment.
/// </summary>
public FrameworkReferenceResolver(Func<string, string> getEnvironmentVariable)
{
_getEnvironmentVariable = getEnvironmentVariable ?? throw new ArgumentNullException(nameof(getEnvironmentVariable));
}
public string GetDefaultReferenceAssembliesPath()
{
// Allow setting the reference assemblies path via an environment variable.
// We read this directly instead of calling DotNetReferenceAssembliesPathResolver.Resolve()
// because that runtime method uses process-global Environment.GetEnvironmentVariable.
var referenceAssembliesPath = _getEnvironmentVariable(DotNetReferenceAssembliesPathResolver.DotNetReferenceAssembliesPathEnv);
if (!string.IsNullOrEmpty(referenceAssembliesPath))
{
return referenceAssembliesPath;
}
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// 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 = _getEnvironmentVariable("ProgramFiles(x86)");
if (string.IsNullOrEmpty(programFiles))
{
// On 32 bit machines they are in %ProgramFiles%
programFiles = _getEnvironmentVariable("ProgramFiles");
}
if (string.IsNullOrEmpty(programFiles))
{
// Reference assemblies aren't installed
return null;
}
return Path.Combine(
programFiles,
"Reference Assemblies", "Microsoft", "Framework");
}
}
}