|
1 | 1 | using System.CommandLine;
|
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
2 | 4 | using System.Threading.Tasks;
|
| 5 | +using Microsoft.Build.Locator; |
3 | 6 |
|
4 | 7 | namespace Microsoft.OData.Cli
|
5 | 8 | {
|
6 | 9 | class Program
|
7 | 10 | {
|
8 | 11 | static async Task Main(string[] args)
|
9 | 12 | {
|
10 |
| - Build.Locator.MSBuildLocator.RegisterDefaults(); |
| 13 | + RegisterMsBuild(); |
11 | 14 | GenerateCommand generateCommand = new GenerateCommand();
|
12 | 15 | RootCommand app = new RootCommand {
|
13 | 16 | generateCommand
|
14 | 17 | };
|
15 | 18 | await app.InvokeAsync(args);
|
16 | 19 | }
|
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Tries to register MSBuild from Visual Studio install folder. If not available, register defaults. |
| 23 | + /// </summary> |
| 24 | + private static void RegisterMsBuild() |
| 25 | + { |
| 26 | + const string defaultInstallDirOfVisualStudio = @"C:\Program Files\Microsoft Visual Studio\"; |
| 27 | + var installDirOfLatestVisualStudio = Directory.GetDirectories(defaultInstallDirOfVisualStudio, "????", SearchOption.TopDirectoryOnly) |
| 28 | + .Where(x => Path.GetFileName(x).All(char.IsDigit)) |
| 29 | + .MaxBy(x => Path.GetFileName(x)); |
| 30 | + |
| 31 | + string pathToMsBuildExeInLatestVisualStudioVersion = Path.Combine( |
| 32 | + Directory.GetDirectories(installDirOfLatestVisualStudio, "*", SearchOption.TopDirectoryOnly).FirstOrDefault() ?? string.Empty, |
| 33 | + "MSBuild", "Current", "Bin", "MSBuild.exe"); |
| 34 | + |
| 35 | + if (File.Exists(pathToMsBuildExeInLatestVisualStudioVersion)) |
| 36 | + { |
| 37 | + MSBuildLocator.RegisterMSBuildPath(Path.GetDirectoryName(pathToMsBuildExeInLatestVisualStudioVersion)); |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + MSBuildLocator.RegisterDefaults(); |
| 42 | + } |
| 43 | + } |
17 | 44 | }
|
18 | 45 | }
|
0 commit comments