Skip to content

Commit

Permalink
Use registry to find MSBuild location, fallback to old method. (see O…
Browse files Browse the repository at this point in the history
  • Loading branch information
rvdginste committed May 26, 2012
1 parent 5d13754 commit 33e0e2a
Showing 1 changed file with 39 additions and 0 deletions.
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

0 comments on commit 33e0e2a

Please sign in to comment.