-
Notifications
You must be signed in to change notification settings - Fork 418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up MSBuild environment initialization and allow it to use Visual Studio 2017 if it's present #818
Merged
DustinCampbell
merged 1 commit into
OmniSharp:dev
from
DustinCampbell:improve-msbuild-support
Apr 9, 2017
Merged
Clean up MSBuild environment initialization and allow it to use Visual Studio 2017 if it's present #818
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice