Skip to content
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

Use registry to find MSBuild location, fallback to old method. (see #324) #325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/OpenWrap/Build/PackageBuilders/MSBuildPackageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,47 @@ static string GetWinXbuildPath()
return Path.Combine(libPath, "mono\\4.0\\xbuild.exe");
}

private static IDictionary<string, string> GetMSBuildExecutionPathsUsingRegistry()
{
const string msbuildBaseRegKey = @"SOFTWARE\Microsoft\MSBuild\ToolsVersions";
const string msbuildToolsPathRegKey = @"MSBuildToolsPath";

IDictionary<string, string> msbuildVersions = new Dictionary<string, string>();

var baseKey = Registry.LocalMachine.OpenSubKey(msbuildBaseRegKey);
if (baseKey == null) return msbuildVersions;

foreach (string version in baseKey.GetSubKeyNames())
{
var key = baseKey.OpenSubKey(version);
if (key != null)
{
var value = key.GetValue(msbuildToolsPathRegKey);
if (value != null)
{
var fullPath = Path.Combine(value.ToString(), @"msbuild.exe");

if (File.Exists(fullPath)) msbuildVersions[version] = fullPath;
}
}
}

return msbuildVersions;
}

static string GetMSBuildExecutionPath()
{
string[] supportedMSBuildVersions = { "4.0", "3.5", "2.0" };

// find most recent registered version
var msbuildVersions = GetMSBuildExecutionPathsUsingRegistry();
var foundVersions = msbuildVersions.Keys;
var preferredVersion = supportedMSBuildVersions
.FirstOrDefault(v => foundVersions.Contains(v));

if (preferredVersion != null) return msbuildVersions[preferredVersion];

// fallback to default folders
var dotNetPath = Environment.ExpandEnvironmentVariables(@"%windir%\Microsoft.NET\Framework\");
if (!Directory.Exists(dotNetPath)) return null;
return (from versionFolder in Directory.GetDirectories(dotNetPath, "v*")
Expand Down