Skip to content

Commit

Permalink
Fixes OData#324 partially by extending the MSBuild registration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pbru87 committed Aug 31, 2023
1 parent de654bc commit d009dcf
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Microsoft.OData.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
using System.CommandLine;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Build.Locator;

namespace Microsoft.OData.Cli
{
class Program
{
static async Task Main(string[] args)
{
Build.Locator.MSBuildLocator.RegisterDefaults();
RegisterMsBuild();
GenerateCommand generateCommand = new GenerateCommand();
RootCommand app = new RootCommand {
generateCommand
};
await app.InvokeAsync(args);
}

/// <summary>
/// Tries to register MSBuild from Visual Studio install folder. If not available, register defaults.
/// </summary>
private static void RegisterMsBuild()
{
const string defaultInstallDirOfVisualStudio = @"C:\Program Files\Microsoft Visual Studio\";
var installDirOfLatestVisualStudio = Directory.GetDirectories(defaultInstallDirOfVisualStudio, "????", SearchOption.TopDirectoryOnly)
.Where(x => Path.GetFileName(x).All(char.IsDigit))
.MaxBy(x => Path.GetFileName(x));

string pathToMsBuildExeInLatestVisualStudioVersion = Path.Combine(
Directory.GetDirectories(installDirOfLatestVisualStudio, "*", SearchOption.TopDirectoryOnly).FirstOrDefault() ?? string.Empty,
"MSBuild", "Current", "Bin", "MSBuild.exe");

if (File.Exists(pathToMsBuildExeInLatestVisualStudioVersion))
{
MSBuildLocator.RegisterMSBuildPath(Path.GetDirectoryName(pathToMsBuildExeInLatestVisualStudioVersion));
}
else
{
MSBuildLocator.RegisterDefaults();
}
}
}
}

0 comments on commit d009dcf

Please sign in to comment.