-
Notifications
You must be signed in to change notification settings - Fork 419
/
Extensions.cs
96 lines (80 loc) · 3.29 KB
/
Extensions.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
using System.IO;
using Microsoft.Extensions.Logging;
namespace OmniSharp.MSBuild.Discovery
{
internal static class Extensions
{
public static void RegisterDefaultInstance(this IMSBuildLocator msbuildLocator, ILogger logger)
{
var bestInstanceFound = GetBestInstance(msbuildLocator, out var invalidVSFound);
if (bestInstanceFound != null)
{
// Did we end up choosing the standalone MSBuild because there was an invalid Visual Studio?
// If so, provide a helpful message to the user.
if (invalidVSFound && bestInstanceFound.DiscoveryType == DiscoveryType.StandAlone)
{
logger.LogWarning(
@"It looks like you have Visual Studio 2017 RTM installed.
Try updating Visual Studio 2017 to the most recent release to enable better MSBuild support."
);
}
msbuildLocator.RegisterInstance(bestInstanceFound);
}
else
{
logger.LogError("Could not locate MSBuild instance to register with OmniSharp");
}
}
public static bool HasDotNetSdksResolvers(this MSBuildInstance instance)
{
const string dotnetSdkResolver = "Microsoft.DotNet.MSBuildSdkResolver";
return File.Exists(
Path.Combine(
instance.MSBuildPath,
"SdkResolvers",
dotnetSdkResolver,
dotnetSdkResolver + ".dll"
)
);
}
/// <summary>
/// Checks if it is MSBuild from Visual Studio 2017 RTM that cannot be used.
/// </summary>
public static bool IsInvalidVisualStudio(this MSBuildInstance instance)
=> instance.Version.Major == 15
&& instance.Version.Minor == 0
&& (instance.DiscoveryType == DiscoveryType.DeveloperConsole
|| instance.DiscoveryType == DiscoveryType.VisualStudioSetup);
public static MSBuildInstance GetBestInstance(this IMSBuildLocator msbuildLocator, out bool invalidVSFound)
{
invalidVSFound = false;
MSBuildInstance bestMatchInstance = null;
var bestMatchScore = 0;
foreach (var instance in msbuildLocator.GetInstances())
{
var score = GetInstanceFeatureScore(instance);
invalidVSFound = invalidVSFound || instance.IsInvalidVisualStudio();
if (score > bestMatchScore
|| (score == bestMatchScore && instance.Version.Major > (bestMatchInstance?.Version.Major ?? 0)))
{
bestMatchInstance = instance;
bestMatchScore = score;
}
}
return bestMatchInstance;
}
private static int GetInstanceFeatureScore(MSBuildInstance i)
{
var score = 0;
if (i.HasDotNetSdksResolvers())
score++;
if (i.IsInvalidVisualStudio())
return int.MinValue;
else
score++;
if (i.DiscoveryType == DiscoveryType.StandAlone)
score--;
return score;
}
}
}