forked from OmniSharp/omnisharp-roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up MSBuild environment initialization and allow it to use Visua…
…l Studio 2017 if it's present Fixes dotnet/vscode-csharp#1368 This change ensures that OmniSharp will use the MSBuild tools installed with Visual Studio 2017 if they're present on the machine. This allows OmniSharp to properly handle VS 2017 projects where the targets/tasks aren't include with OmniSharp's local MSBuild, such as WebApplication projects as reported in the bug listed above. If VS 2017 is not on the machine, OmniSharp will continue to use its local MSBuild which has a fallback to the Microsoft Build Tools for targets/tasks that it can't find. I've also taken the opportunity to clean up a lot of the MSBuild environment initialized code and project file processing.
- Loading branch information
1 parent
27a9b89
commit 5f68289
Showing
9 changed files
with
260 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace OmniSharp.MSBuild | ||
{ | ||
internal static class Extensions | ||
{ | ||
public static void AddPropertyIfNeeded(this Dictionary<string, string> properties, string name, string userOptionValue, string environmentValue) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(userOptionValue)) | ||
{ | ||
// If the user set the option, we should use that. | ||
properties.Add(name, userOptionValue); | ||
} | ||
else if (!string.IsNullOrWhiteSpace(environmentValue)) | ||
{ | ||
// If we have a custom environment value, we should use that. | ||
properties.Add(name, environmentValue); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Text; | ||
using Microsoft.Build.Evaluation; | ||
using Microsoft.Build.Execution; | ||
using OmniSharp.Utilities; | ||
|
||
namespace OmniSharp.MSBuild | ||
{ | ||
public static class MSBuildHelpers | ||
{ | ||
private static Assembly s_MicrosoftBuildAssembly; | ||
|
||
private static Type s_BuildEnvironmentHelperType; | ||
private static Type s_BuildEnvironmentType; | ||
|
||
static MSBuildHelpers() | ||
{ | ||
s_MicrosoftBuildAssembly = Assembly.Load(new AssemblyName("Microsoft.Build")); | ||
|
||
s_BuildEnvironmentHelperType = s_MicrosoftBuildAssembly.GetType("Microsoft.Build.Shared.BuildEnvironmentHelper"); | ||
s_BuildEnvironmentType = s_MicrosoftBuildAssembly.GetType("Microsoft.Build.Shared.BuildEnvironment"); | ||
} | ||
|
||
public static string GetBuildEnvironmentInfo() | ||
{ | ||
var instanceProp = s_BuildEnvironmentHelperType.GetProperty("Instance"); | ||
var buildEnvironment = instanceProp.GetMethod.Invoke(null, null); | ||
|
||
return DumpBuildEnvironment(buildEnvironment); | ||
} | ||
|
||
private static string DumpBuildEnvironment(object buildEnvironment) | ||
{ | ||
var builder = new StringBuilder(); | ||
|
||
if (buildEnvironment != null) | ||
{ | ||
const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance; | ||
|
||
AppendPropertyValue(builder, "Mode", buildEnvironment, s_BuildEnvironmentType, flags); | ||
AppendPropertyValue(builder, "RunningTests", buildEnvironment, s_BuildEnvironmentType, flags); | ||
AppendPropertyValue(builder, "RunningInVisualStudio", buildEnvironment, s_BuildEnvironmentType, flags); | ||
AppendPropertyValue(builder, "MSBuildToolsDirectory32", buildEnvironment, s_BuildEnvironmentType, flags); | ||
AppendPropertyValue(builder, "MSBuildToolsDirectory64", buildEnvironment, s_BuildEnvironmentType, flags); | ||
AppendPropertyValue(builder, "MSBuildSDKsPath", buildEnvironment, s_BuildEnvironmentType, flags); | ||
AppendPropertyValue(builder, "CurrentMSBuildConfigurationFile", buildEnvironment, s_BuildEnvironmentType, flags); | ||
AppendPropertyValue(builder, "CurrentMSBuildExePath", buildEnvironment, s_BuildEnvironmentType, flags); | ||
AppendPropertyValue(builder, "CurrentMSBuildToolsDirectory", buildEnvironment, s_BuildEnvironmentType, flags); | ||
AppendPropertyValue(builder, "VisualStudioInstallRootDirectory", buildEnvironment, s_BuildEnvironmentType, flags); | ||
AppendPropertyValue(builder, "MSBuildExtensionsPath", buildEnvironment, s_BuildEnvironmentType, flags); | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
private static void AppendPropertyValue(StringBuilder builder, string name, object instance, Type type, BindingFlags bindingFlags) | ||
{ | ||
var propInfo = type.GetProperty(name, bindingFlags); | ||
var propValue = propInfo.GetMethod.Invoke(instance, null); | ||
builder.AppendLine($"{name}: {propValue}"); | ||
} | ||
|
||
public static bool TryGetVisualStudioBuildEnvironment() | ||
{ | ||
if (!PlatformHelper.IsWindows) | ||
{ | ||
return false; | ||
} | ||
|
||
// Call Microsoft.Build.Shared.BuildEnvironmentHelper.TryFromSetupApi(...), which attempts | ||
// to compute a build environment by looking for VS 2017. | ||
var tryFromSetupApiMethod = s_BuildEnvironmentHelperType.GetMethod("TryFromSetupApi", BindingFlags.NonPublic | BindingFlags.Static); | ||
var buildEnvironment = tryFromSetupApiMethod.Invoke(null, null); | ||
|
||
return buildEnvironment != null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.