private static string GetMSBuildPath() { // Dev console, probably the best case var vsinstalldir = Environment.GetEnvironmentVariable("VSINSTALLDIR"); if (!string.IsNullOrEmpty(vsinstalldir)) { var path = Path.Combine(vsinstalldir, "MSBuild", "15.0", "Bin"); Console.WriteLine($"Found VS from VSINSTALLDIR (Dev Console): {path}"); return path; } var instances = VisualStudioLocationHelper.GetInstances(); if (instances.Count == 0) { throw new Exception("Couldn't find MSBuild"); } if (instances.Count > 1) { Console.WriteLine($"WARNING: Found {instances.Count} instances of VS: {string.Join(", ", instances.Select(i => i.Path))}"); } string instancePath = instances[0].Path; Console.WriteLine($"Used VS from Setup API: {instancePath}"); return Path.Combine(instancePath, "MSBuild", "15.0", "Bin"); }
private static IEnumerable <VisualStudioInstance> GetInstances(VisualStudioInstanceQueryOptions options) { #if NET46 var devConsole = GetDevConsoleInstance(); if (devConsole != null) { yield return(devConsole); } #if FEATURE_VISUALSTUDIOSETUP foreach (var instance in VisualStudioLocationHelper.GetInstances()) { yield return(instance); } #endif #endif #if NETCOREAPP foreach (var dotnetSdk in DotNetSdkLocationHelper.GetInstances(options.WorkingDirectory)) { yield return(dotnetSdk); } #endif }
/// <summary> /// Gathers toolset data from the registry + configuration file + VS setup API, if any. /// NOTE: this method is internal for unit testing purposes only. /// </summary> internal static string ReadAllToolsets ( Dictionary <string, Toolset> toolsets, #if FEATURE_WIN32_REGISTRY ToolsetRegistryReader registryReader, #endif #if FEATURE_SYSTEM_CONFIGURATION ToolsetConfigurationReader configurationReader, #endif PropertyDictionary <ProjectPropertyInstance> environmentProperties, PropertyDictionary <ProjectPropertyInstance> globalProperties, ToolsetDefinitionLocations locations ) { var defaultVer = ReadAllToolsetsNative ( toolsets, #if FEATURE_WIN32_REGISTRY registryReader, #endif #if FEATURE_SYSTEM_CONFIGURATION configurationReader, #endif environmentProperties, globalProperties, locations ); const string _CUR = MSBuildConstants.CurrentToolsVersion; string currentDir = BuildEnvironmentHelper.Instance.CurrentMSBuildToolsDirectory.TrimEnd(Path.DirectorySeparatorChar); foreach (var vs in VisualStudioLocationHelper.GetInstances()) { string vstr = $"{vs.Version.Major}.0"; string toolbin = FileUtilities.CombinePaths ( vs.Path, "MSBuild", vs.Version.Major >= 16 ? _CUR : vstr, "Bin" ); if (IntPtr.Size == 8) { string toolbin64 = FileUtilities.CombinePaths(toolbin, "amd64"); if (Directory.Exists(toolbin64)) { toolbin = toolbin64; } } if (!toolsets.ContainsKey(vstr)) { // For Mono var buildProperties = CreateStandardProperties(globalProperties, vstr, NativeMethodsShared.FrameworkBasePath, toolbin); toolsets.Add ( vstr, new Toolset ( vstr, toolbin, buildProperties, environmentProperties, globalProperties, null, currentDir, string.Empty ) ); } } if (!toolsets.ContainsKey(_CUR)) { var actual = toolsets.GetMostActual(); if (actual != null) { toolsets.Add(_CUR, actual); } else { toolsets.Add ( _CUR, new Toolset ( _CUR, currentDir, new PropertyDictionary <ProjectPropertyInstance>(), new PropertyDictionary <ProjectPropertyInstance>(), currentDir, string.Empty ) ); } } return(defaultVer); }