-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
WorkloadSdkResolver.cs
116 lines (87 loc) · 4.1 KB
/
WorkloadSdkResolver.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Build.Framework;
using Microsoft.NET.Sdk.WorkloadManifestReader;
using Microsoft.DotNet.Configurer;
#if NET
using Microsoft.DotNet.Cli;
#else
using Microsoft.DotNet.DotNetSdkResolver;
#endif
#nullable disable
namespace Microsoft.NET.Sdk.WorkloadMSBuildSdkResolver
{
// This SdkResolver is used by the .NET SDK version of MSBuild. Workload resolution logic which
// is shared with Full Framework / Visual Studio MSBuild is in CachingWorkloadResolver.
public class WorkloadSdkResolver : SdkResolver
{
public override string Name => "Microsoft.DotNet.MSBuildWorkloadSdkResolver";
public override int Priority => 4000;
private class CachedState
{
public string DotnetRootPath { get; init; }
public string SdkVersion { get; init; }
public string GlobalJsonPath { get; init; }
public CachingWorkloadResolver WorkloadResolver { get; init; }
}
public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext resolverContext, SdkResultFactory factory)
{
CachedState cachedState = null;
if (resolverContext.State is CachedState resolverContextState)
{
cachedState = resolverContextState;
}
if (cachedState == null)
{
var dotnetRootPath = GetDotNetRoot(resolverContext);
var sdkDirectory = GetSdkDirectory(resolverContext);
// The SDK version is the name of the SDK directory (ie dotnet\sdk\5.0.100)
var sdkVersion = Path.GetFileName(sdkDirectory);
var globalJsonPath = SdkDirectoryWorkloadManifestProvider.GetGlobalJsonPath(GetGlobalJsonStartDir(resolverContext));
cachedState = new CachedState()
{
DotnetRootPath = dotnetRootPath,
SdkVersion = sdkVersion,
GlobalJsonPath = globalJsonPath,
WorkloadResolver = new CachingWorkloadResolver()
};
resolverContext.State = cachedState;
}
string userProfileDir = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPath();
var result = cachedState.WorkloadResolver.Resolve(sdkReference.Name, cachedState.DotnetRootPath, cachedState.SdkVersion, userProfileDir, cachedState.GlobalJsonPath);
return result.ToSdkResult(sdkReference, factory);
}
private string GetSdkDirectory(SdkResolverContext context)
{
#if NET
var sdkDirectory = Path.GetDirectoryName(typeof(DotnetFiles).Assembly.Location);
return sdkDirectory;
#else
string dotnetExeDir = EnvironmentProvider.GetDotnetExeDirectory();
string globalJsonStartDir = Path.GetDirectoryName(context.SolutionFilePath ?? context.ProjectFilePath);
var sdkResolutionResult = _sdkResolver.ResolveNETCoreSdkDirectory(globalJsonStartDir, context.MSBuildVersion, context.IsRunningInVisualStudio, dotnetExeDir);
return sdkResolutionResult.ResolvedSdkDirectory;
#endif
}
private string GetDotNetRoot(SdkResolverContext context)
{
var sdkDirectory = GetSdkDirectory(context);
var dotnetRoot = Directory.GetParent(sdkDirectory).Parent.FullName;
return dotnetRoot;
}
// Duplicated logic from DotNetMSBuildSdkResolver
private static string GetGlobalJsonStartDir(SdkResolverContext context)
{
string startDir = Environment.CurrentDirectory;
if (!string.IsNullOrWhiteSpace(context.SolutionFilePath))
{
startDir = Path.GetDirectoryName(context.SolutionFilePath);
}
else if (!string.IsNullOrWhiteSpace(context.ProjectFilePath))
{
startDir = Path.GetDirectoryName(context.ProjectFilePath);
}
return startDir;
}
}
}