Skip to content

Commit

Permalink
Draft fix for OData#324
Browse files Browse the repository at this point in the history
  • Loading branch information
pbru87 committed Jan 29, 2024
1 parent 745bfa6 commit 50ee737
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/Microsoft.OData.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
using System.CommandLine;
using System;
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 50ee737

Please sign in to comment.