diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 4876e85a..11fe6968 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -15,11 +15,10 @@ internal static class DotNetSdkLocationHelper { private static readonly Regex DotNetBasePathRegex = new Regex("Base Path:(.*)$", RegexOptions.Multiline); private static readonly Regex VersionRegex = new Regex(@"^(\d+)\.(\d+)\.(\d+)", RegexOptions.Multiline); + private static readonly Regex SdkRegex = new Regex(@"(\S+) \[(.*?)]$", RegexOptions.Multiline); - public static VisualStudioInstance GetInstance(string workingDirectory) - { - string dotNetSdkPath = GetDotNetBasePath(workingDirectory); - + public static VisualStudioInstance GetInstance(string dotNetSdkPath) + { if (string.IsNullOrWhiteSpace(dotNetSdkPath)) { return null; @@ -58,10 +57,20 @@ public static VisualStudioInstance GetInstance(string workingDirectory) discoveryType: DiscoveryType.DotNetSdk); } - private static string GetDotNetBasePath(string workingDirectory) + public static IEnumerable GetInstances(string workingDirectory) + { + foreach (var basePath in GetDotNetBasePaths(workingDirectory)) + { + var dotnetSdk = GetInstance(basePath); + if (dotnetSdk != null) + yield return dotnetSdk; + } + } + + private static IEnumerable GetDotNetBasePaths(string workingDirectory) { const string DOTNET_CLI_UI_LANGUAGE = nameof(DOTNET_CLI_UI_LANGUAGE); - + Process process; try { @@ -83,12 +92,12 @@ private static string GetDotNetBasePath(string workingDirectory) catch { // when error running dotnet command, consider dotnet as not available - return null; + yield break; } if (process.HasExited) { - return null; + yield break; } var lines = new List(); @@ -109,10 +118,37 @@ private static string GetDotNetBasePath(string workingDirectory) var matched = DotNetBasePathRegex.Match(outputString); if (!matched.Success) { - return null; + yield break; } + + var basePath = matched.Groups[1].Value.Trim(); - return matched.Groups[1].Value.Trim(); + yield return basePath; // We return the version in use at the front of the list in order to ensure FirstOrDefault always returns the version in use. + + var lineSdkIndex = lines.FindIndex(line => line.Contains("SDKs installed")); + + if (lineSdkIndex != -1) + { + lineSdkIndex++; + + while (lineSdkIndex < lines.Count && !string.IsNullOrEmpty(lines[lineSdkIndex])) + { + var sdkMatch = SdkRegex.Match(lines[lineSdkIndex]); + + if (!sdkMatch.Success) + break; + + var version = sdkMatch.Groups[1].Value.Trim(); + var path = sdkMatch.Groups[2].Value.Trim(); + + path = Path.Combine(path, version) + Path.DirectorySeparatorChar; + + if (!path.Equals(basePath)) + yield return path; + + lineSdkIndex++; + } + } } } } diff --git a/src/MSBuildLocator/MSBuildLocator.cs b/src/MSBuildLocator/MSBuildLocator.cs index 3279f14a..8f893dd2 100644 --- a/src/MSBuildLocator/MSBuildLocator.cs +++ b/src/MSBuildLocator/MSBuildLocator.cs @@ -324,8 +324,7 @@ private static IEnumerable GetInstances(VisualStudioInstan #endif #if NETCOREAPP - var dotnetSdk = DotNetSdkLocationHelper.GetInstance(options.WorkingDirectory); - if (dotnetSdk != null) + foreach (var dotnetSdk in DotNetSdkLocationHelper.GetInstances(options.WorkingDirectory)) yield return dotnetSdk; #endif } diff --git a/version.json b/version.json index 709282c6..d4dd49c2 100644 --- a/version.json +++ b/version.json @@ -1,5 +1,5 @@ { - "version": "1.2", + "version": "1.3", "assemblyVersion": "1.0.0.0", "publicReleaseRefSpec": [ "^refs/heads/release/.*"