From 7d57e5c8d50b23ed72ddaebc11ca945c3a7d19fc Mon Sep 17 00:00:00 2001 From: kurtgronbech Date: Fri, 29 Nov 2019 20:49:51 +0100 Subject: [PATCH 01/13] Return all installed DotNetSDK instances --- samples/BuilderApp/BuilderApp.csproj | 2 +- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 54 ++++++++++++++++--- src/MSBuildLocator/MSBuildLocator.cs | 7 ++- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/samples/BuilderApp/BuilderApp.csproj b/samples/BuilderApp/BuilderApp.csproj index 504d378f..79705a2a 100644 --- a/samples/BuilderApp/BuilderApp.csproj +++ b/samples/BuilderApp/BuilderApp.csproj @@ -2,7 +2,7 @@ Exe - net471;netcoreapp2.1 + net472;netcoreapp2.1 false false diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 4876e85a..42b19427 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(@"(\d+\.\d+\.\d+) \[(.*)]$", RegexOptions.Multiline); - public static VisualStudioInstance GetInstance(string workingDirectory) - { - string dotNetSdkPath = GetDotNetBasePath(workingDirectory); - + public static VisualStudioInstance GetInstance(string workingDirectory, string dotNetSdkPath) + { if (string.IsNullOrWhiteSpace(dotNetSdkPath)) { return null; @@ -58,10 +57,22 @@ public static VisualStudioInstance GetInstance(string workingDirectory) discoveryType: DiscoveryType.DotNetSdk); } - private static string GetDotNetBasePath(string workingDirectory) + public static IEnumerable GetInstances(string workingDirectory) + { + var basePaths = GetDotNetBasePaths(workingDirectory); + + foreach (var basePath in basePaths) + { + yield return GetInstance(workingDirectory, basePath); + } + } + + private static List GetDotNetBasePaths(string workingDirectory) { const string DOTNET_CLI_UI_LANGUAGE = nameof(DOTNET_CLI_UI_LANGUAGE); + List basePaths = new List(); + Process process; try { @@ -106,13 +117,44 @@ private static string GetDotNetBasePath(string workingDirectory) var outputString = string.Join(Environment.NewLine, lines); + var lineSdkIndex = lines.FindIndex(line => line.Contains(".NET Core SDKs installed")); + + if (lineSdkIndex > 0) + { + lineSdkIndex++; + + while (lineSdkIndex < lines.Count && !lines[lineSdkIndex].Contains(".NET Core runtimes installed")) + { + var ma = SdkRegex.Match(lines[lineSdkIndex]); + + if (!ma.Success) + break; + + var version = ma.Groups[1].Value.Trim(); + var path = ma.Groups[2].Value.Trim(); + + path = Path.Combine(path, version) + "\\"; + + //basePaths.Add(path); + + // We insert at index 0 so that instance list will be sorted descending so that instances.FirstOrDefault() + // will always return the latest installed version of dotnet SDK + basePaths.Insert(0, path); + + lineSdkIndex++; + } + } + var matched = DotNetBasePathRegex.Match(outputString); if (!matched.Success) { return null; } - return matched.Groups[1].Value.Trim(); + //basePaths.Add(matched.Groups[1].Value.Trim()); + basePaths.Insert(0, matched.Groups[1].Value.Trim()); // Insert at index 0 to not break FirstOrDefault() returning the latest SDK + + return basePaths; } } } diff --git a/src/MSBuildLocator/MSBuildLocator.cs b/src/MSBuildLocator/MSBuildLocator.cs index e050ee79..8953edc0 100644 --- a/src/MSBuildLocator/MSBuildLocator.cs +++ b/src/MSBuildLocator/MSBuildLocator.cs @@ -310,15 +310,14 @@ private static IEnumerable GetInstances(VisualStudioInstan if (devConsole != null) yield return devConsole; - #if FEATURE_VISUALSTUDIOSETUP +#if FEATURE_VISUALSTUDIOSETUP foreach (var instance in VisualStudioLocationHelper.GetInstances()) yield return instance; - #endif +#endif #endif #if NETCOREAPP - var dotnetSdk = DotNetSdkLocationHelper.GetInstance(options.WorkingDirectory); - if (dotnetSdk != null) + foreach (var dotnetSdk in DotNetSdkLocationHelper.GetInstances(options.WorkingDirectory)) yield return dotnetSdk; #endif } From 36ca39dce3092c728d2f1ed2739cf9716a4fa1dd Mon Sep 17 00:00:00 2001 From: kurtgronbech Date: Fri, 29 Nov 2019 22:19:17 +0100 Subject: [PATCH 02/13] Correctly check the index returned by FindIndex --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 42b19427..64522839 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -119,7 +119,7 @@ private static List GetDotNetBasePaths(string workingDirectory) var lineSdkIndex = lines.FindIndex(line => line.Contains(".NET Core SDKs installed")); - if (lineSdkIndex > 0) + if (lineSdkIndex != -1) { lineSdkIndex++; From da342773b2005ed88bf906415f958371986e37f0 Mon Sep 17 00:00:00 2001 From: "Kurt I. Gronbech" Date: Thu, 19 Nov 2020 23:37:58 +0100 Subject: [PATCH 03/13] Resolved feedback from JoeRobich. Also added support for .NET 5.0. --- samples/BuilderApp/BuilderApp.csproj | 2 +- samples/BuilderApp/Program.cs | 2 +- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 35 ++++++++----------- src/MSBuildLocator/MSBuildLocator.cs | 11 +++--- .../Microsoft.Build.Locator.csproj | 2 +- 5 files changed, 25 insertions(+), 27 deletions(-) diff --git a/samples/BuilderApp/BuilderApp.csproj b/samples/BuilderApp/BuilderApp.csproj index 79705a2a..504d378f 100644 --- a/samples/BuilderApp/BuilderApp.csproj +++ b/samples/BuilderApp/BuilderApp.csproj @@ -2,7 +2,7 @@ Exe - net472;netcoreapp2.1 + net471;netcoreapp2.1 false false diff --git a/samples/BuilderApp/Program.cs b/samples/BuilderApp/Program.cs index 0ec2f0bc..3904749e 100644 --- a/samples/BuilderApp/Program.cs +++ b/samples/BuilderApp/Program.cs @@ -16,7 +16,7 @@ namespace BuilderApp internal class Program { private static void Main(string[] args) - { + { Header(); var projectFilePath = Args(args); diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 64522839..caeeeb42 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -117,25 +117,29 @@ private static List GetDotNetBasePaths(string workingDirectory) var outputString = string.Join(Environment.NewLine, lines); - var lineSdkIndex = lines.FindIndex(line => line.Contains(".NET Core SDKs installed")); + var matched = DotNetBasePathRegex.Match(outputString); + if (!matched.Success) + { + return null; + } + + var lineSdkIndex = lines.FindIndex(line => line.Contains(".NET Core SDKs installed") || line.Contains(".NET SDKs installed")); if (lineSdkIndex != -1) { lineSdkIndex++; - while (lineSdkIndex < lines.Count && !lines[lineSdkIndex].Contains(".NET Core runtimes installed")) + while (lineSdkIndex < lines.Count && (!lines[lineSdkIndex].Contains(".NET Core runtimes installed") && !lines[lineSdkIndex].Contains(".NET runtimes installed"))) { - var ma = SdkRegex.Match(lines[lineSdkIndex]); + var sdkMatch = SdkRegex.Match(lines[lineSdkIndex]); - if (!ma.Success) + if (!sdkMatch.Success) break; - var version = ma.Groups[1].Value.Trim(); - var path = ma.Groups[2].Value.Trim(); + var version = sdkMatch.Groups[1].Value.Trim(); + var path = sdkMatch.Groups[2].Value.Trim(); - path = Path.Combine(path, version) + "\\"; - - //basePaths.Add(path); + path = Path.Combine(path, version) + "\\"; // We insert at index 0 so that instance list will be sorted descending so that instances.FirstOrDefault() // will always return the latest installed version of dotnet SDK @@ -143,17 +147,8 @@ private static List GetDotNetBasePaths(string workingDirectory) lineSdkIndex++; } - } - - var matched = DotNetBasePathRegex.Match(outputString); - if (!matched.Success) - { - return null; - } - - //basePaths.Add(matched.Groups[1].Value.Trim()); - basePaths.Insert(0, matched.Groups[1].Value.Trim()); // Insert at index 0 to not break FirstOrDefault() returning the latest SDK - + } + return basePaths; } } diff --git a/src/MSBuildLocator/MSBuildLocator.cs b/src/MSBuildLocator/MSBuildLocator.cs index 8953edc0..d07e8ebf 100644 --- a/src/MSBuildLocator/MSBuildLocator.cs +++ b/src/MSBuildLocator/MSBuildLocator.cs @@ -22,9 +22,12 @@ public static class MSBuildLocator private static readonly string[] s_msBuildAssemblies = { "Microsoft.Build", + "Microsoft.Build.Engine", "Microsoft.Build.Framework", "Microsoft.Build.Tasks.Core", - "Microsoft.Build.Utilities.Core" + "Microsoft.Build.Utilities.Core", + "System.Runtime.CompilerServices.Unsafe", + "System.Numerics.Vectors" }; #if NET46 @@ -81,7 +84,7 @@ internal static IEnumerable QueryVisualStudioInstances( IEnumerable instances, VisualStudioInstanceQueryOptions options) { - return instances.Where(i => options.DiscoveryTypes.HasFlag(i.DiscoveryType)); + return instances.Where(i => i != null && options.DiscoveryTypes.HasFlag(i.DiscoveryType)); } /// @@ -310,10 +313,10 @@ private static IEnumerable GetInstances(VisualStudioInstan if (devConsole != null) yield return devConsole; -#if FEATURE_VISUALSTUDIOSETUP + #if FEATURE_VISUALSTUDIOSETUP foreach (var instance in VisualStudioLocationHelper.GetInstances()) yield return instance; -#endif + #endif #endif #if NETCOREAPP diff --git a/src/MSBuildLocator/Microsoft.Build.Locator.csproj b/src/MSBuildLocator/Microsoft.Build.Locator.csproj index b31c961e..f36dba61 100644 --- a/src/MSBuildLocator/Microsoft.Build.Locator.csproj +++ b/src/MSBuildLocator/Microsoft.Build.Locator.csproj @@ -19,7 +19,7 @@ $(DefineConstants);FEATURE_VISUALSTUDIOSETUP - + From 1eea571193beecabbdecc7034b32fe5ba467e178 Mon Sep 17 00:00:00 2001 From: "Kurt I. Gronbech" Date: Thu, 19 Nov 2020 23:46:49 +0100 Subject: [PATCH 04/13] Cleaned some whitespace --- samples/BuilderApp/Program.cs | 2 +- src/MSBuildLocator/Microsoft.Build.Locator.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/BuilderApp/Program.cs b/samples/BuilderApp/Program.cs index 3904749e..0ec2f0bc 100644 --- a/samples/BuilderApp/Program.cs +++ b/samples/BuilderApp/Program.cs @@ -16,7 +16,7 @@ namespace BuilderApp internal class Program { private static void Main(string[] args) - { + { Header(); var projectFilePath = Args(args); diff --git a/src/MSBuildLocator/Microsoft.Build.Locator.csproj b/src/MSBuildLocator/Microsoft.Build.Locator.csproj index f36dba61..b31c961e 100644 --- a/src/MSBuildLocator/Microsoft.Build.Locator.csproj +++ b/src/MSBuildLocator/Microsoft.Build.Locator.csproj @@ -19,7 +19,7 @@ $(DefineConstants);FEATURE_VISUALSTUDIOSETUP - + From b6353cb6a96dff279c2a72f4ab0a37f829f7a731 Mon Sep 17 00:00:00 2001 From: "Kurt I. Gronbech" Date: Tue, 8 Dec 2020 12:18:44 +0100 Subject: [PATCH 05/13] use --list-sdks, fixed instance null-check, removed workingdirectory --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 46 +++++++------------ src/MSBuildLocator/MSBuildLocator.cs | 2 +- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index caeeeb42..e9319837 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -12,12 +12,11 @@ namespace Microsoft.Build.Locator { 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(@"(\d+\.\d+\.\d+) \[(.*)]$", RegexOptions.Multiline); - public static VisualStudioInstance GetInstance(string workingDirectory, string dotNetSdkPath) + public static VisualStudioInstance GetInstance(string dotNetSdkPath) { if (string.IsNullOrWhiteSpace(dotNetSdkPath)) { @@ -63,7 +62,9 @@ public static IEnumerable GetInstances(string workingDirec foreach (var basePath in basePaths) { - yield return GetInstance(workingDirectory, basePath); + var dotnetSdk = GetInstance(basePath); + if (dotnetSdk != null) + yield return dotnetSdk; } } @@ -76,7 +77,7 @@ private static List GetDotNetBasePaths(string workingDirectory) Process process; try { - var startInfo = new ProcessStartInfo("dotnet", "--info") + var startInfo = new ProcessStartInfo("dotnet", "--list-sdks") { WorkingDirectory = workingDirectory, CreateNoWindow = true, @@ -117,37 +118,22 @@ private static List GetDotNetBasePaths(string workingDirectory) var outputString = string.Join(Environment.NewLine, lines); - var matched = DotNetBasePathRegex.Match(outputString); - if (!matched.Success) + foreach (var line in lines) { - return null; - } + var sdkMatch = SdkRegex.Match(line); - var lineSdkIndex = lines.FindIndex(line => line.Contains(".NET Core SDKs installed") || line.Contains(".NET SDKs installed")); + if (!sdkMatch.Success) + break; - if (lineSdkIndex != -1) - { - lineSdkIndex++; - - while (lineSdkIndex < lines.Count && (!lines[lineSdkIndex].Contains(".NET Core runtimes installed") && !lines[lineSdkIndex].Contains(".NET runtimes installed"))) - { - var sdkMatch = SdkRegex.Match(lines[lineSdkIndex]); + var version = sdkMatch.Groups[1].Value.Trim(); + var path = sdkMatch.Groups[2].Value.Trim(); - if (!sdkMatch.Success) - break; + path = Path.Combine(path, version) + Path.DirectorySeparatorChar; - var version = sdkMatch.Groups[1].Value.Trim(); - var path = sdkMatch.Groups[2].Value.Trim(); - - path = Path.Combine(path, version) + "\\"; - - // We insert at index 0 so that instance list will be sorted descending so that instances.FirstOrDefault() - // will always return the latest installed version of dotnet SDK - basePaths.Insert(0, path); + basePaths.Add(path); + } - lineSdkIndex++; - } - } + basePaths.Reverse(); // Reverse the list in order to preserve FirstOrDefault to always return the latest installed SDK for the time being return basePaths; } diff --git a/src/MSBuildLocator/MSBuildLocator.cs b/src/MSBuildLocator/MSBuildLocator.cs index a4c23beb..8f893dd2 100644 --- a/src/MSBuildLocator/MSBuildLocator.cs +++ b/src/MSBuildLocator/MSBuildLocator.cs @@ -84,7 +84,7 @@ internal static IEnumerable QueryVisualStudioInstances( IEnumerable instances, VisualStudioInstanceQueryOptions options) { - return instances.Where(i => i != null && options.DiscoveryTypes.HasFlag(i.DiscoveryType)); + return instances.Where(i => options.DiscoveryTypes.HasFlag(i.DiscoveryType)); } /// From 80691393c47bff783ed7a7655b9281453b740379 Mon Sep 17 00:00:00 2001 From: "Kurt I. Gronbech" Date: Wed, 9 Dec 2020 08:37:20 +0100 Subject: [PATCH 06/13] Go back to using --info and also get the base path --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index e9319837..77c10110 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -12,7 +12,8 @@ namespace Microsoft.Build.Locator { 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(@"(\d+\.\d+\.\d+) \[(.*)]$", RegexOptions.Multiline); @@ -77,7 +78,7 @@ private static List GetDotNetBasePaths(string workingDirectory) Process process; try { - var startInfo = new ProcessStartInfo("dotnet", "--list-sdks") + var startInfo = new ProcessStartInfo("dotnet", "--info") { WorkingDirectory = workingDirectory, CreateNoWindow = true, @@ -118,22 +119,45 @@ private static List GetDotNetBasePaths(string workingDirectory) var outputString = string.Join(Environment.NewLine, lines); - foreach (var line in lines) + var matched = DotNetBasePathRegex.Match(outputString); + if (!matched.Success) { - var sdkMatch = SdkRegex.Match(line); + return null; + } + + var basePath = matched.Groups[1].Value.Trim(); - if (!sdkMatch.Success) - break; + var lineSdkIndex = lines.FindIndex(line => line.Contains("SDKs installed")); - var version = sdkMatch.Groups[1].Value.Trim(); - var path = sdkMatch.Groups[2].Value.Trim(); + if (lineSdkIndex != -1) + { + lineSdkIndex++; - path = Path.Combine(path, version) + Path.DirectorySeparatorChar; + while (lineSdkIndex < lines.Count && !string.IsNullOrEmpty(lines[lineSdkIndex])) + { + var sdkMatch = SdkRegex.Match(lines[lineSdkIndex]); - basePaths.Add(path); - } + if (!sdkMatch.Success) + break; - basePaths.Reverse(); // Reverse the list in order to preserve FirstOrDefault to always return the latest installed SDK for the time being + 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)) + { + // We insert the version in use at the top of the list in order to preserve FirstOrDefault to always return the version in use + basePaths.Insert(0, path); + } + else + { + basePaths.Add(path); + } + + lineSdkIndex++; + } + } return basePaths; } From 9e91f9e02517c2049fdf7ca08378a5a9b625216f Mon Sep 17 00:00:00 2001 From: "Kurt I. Gronbech" Date: Wed, 9 Dec 2020 22:53:39 +0100 Subject: [PATCH 07/13] Indentation fix and clearer comment on why we insert basePath at the front of the list --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 77c10110..7cc15f39 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -142,16 +142,17 @@ private static List GetDotNetBasePaths(string workingDirectory) var version = sdkMatch.Groups[1].Value.Trim(); var path = sdkMatch.Groups[2].Value.Trim(); - - path = Path.Combine(path, version) + Path.DirectorySeparatorChar; + var test = Path.Combine(path, version); + //path = Path.Combine(path, version) + Path.DirectorySeparatorChar; + path = Path.Combine(path, version); if (path.Equals(basePath)) - { - // We insert the version in use at the top of the list in order to preserve FirstOrDefault to always return the version in use + { + // We insert the version in use at the front of the list in order to ensure FirstOrDefault always returns the version in use. basePaths.Insert(0, path); } else - { + { basePaths.Add(path); } From 47e602b69e2e1bcd8f57c72c421ae896e4aca478 Mon Sep 17 00:00:00 2001 From: "Kurt I. Gronbech" Date: Wed, 9 Dec 2020 22:57:03 +0100 Subject: [PATCH 08/13] Removed a couple of test lines which had snuck in --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 7cc15f39..19832102 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -142,10 +142,9 @@ private static List GetDotNetBasePaths(string workingDirectory) var version = sdkMatch.Groups[1].Value.Trim(); var path = sdkMatch.Groups[2].Value.Trim(); - var test = Path.Combine(path, version); - //path = Path.Combine(path, version) + Path.DirectorySeparatorChar; - path = Path.Combine(path, version); - + + path = Path.Combine(path, version) + Path.DirectorySeparatorChar; + if (path.Equals(basePath)) { // We insert the version in use at the front of the list in order to ensure FirstOrDefault always returns the version in use. From f7573bce5b1fb549f4f02f3ec813f41c65444f54 Mon Sep 17 00:00:00 2001 From: "Kurt I. Gronbech" Date: Thu, 10 Dec 2020 18:31:51 +0100 Subject: [PATCH 09/13] Updated pattern for SdkRegex --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 19832102..191e5960 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -15,7 +15,7 @@ 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(@"(\d+\.\d+\.\d+) \[(.*)]$", RegexOptions.Multiline); + private static readonly Regex SdkRegex = new Regex(@"(\S+) \[(.*)]$", RegexOptions.Multiline); public static VisualStudioInstance GetInstance(string dotNetSdkPath) { From afe8a1d67d0f708829a62f03aa2ad8c3af93f5a4 Mon Sep 17 00:00:00 2001 From: "Kurt I. Gronbech" Date: Fri, 11 Dec 2020 00:35:36 +0100 Subject: [PATCH 10/13] Changed GetDotNetBasePath to be an iterator --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 39 +++++++------------ 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 191e5960..f6f76279 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -58,10 +58,8 @@ public static VisualStudioInstance GetInstance(string dotNetSdkPath) } public static IEnumerable GetInstances(string workingDirectory) - { - var basePaths = GetDotNetBasePaths(workingDirectory); - - foreach (var basePath in basePaths) + { + foreach (var basePath in GetDotNetBasePaths(workingDirectory)) { var dotnetSdk = GetInstance(basePath); if (dotnetSdk != null) @@ -69,12 +67,10 @@ public static IEnumerable GetInstances(string workingDirec } } - private static List GetDotNetBasePaths(string workingDirectory) + private static IEnumerable GetDotNetBasePaths(string workingDirectory) { const string DOTNET_CLI_UI_LANGUAGE = nameof(DOTNET_CLI_UI_LANGUAGE); - - List basePaths = new List(); - + Process process; try { @@ -96,12 +92,12 @@ private static List GetDotNetBasePaths(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(); @@ -121,12 +117,14 @@ private static List GetDotNetBasePaths(string workingDirectory) var matched = DotNetBasePathRegex.Match(outputString); if (!matched.Success) - { - return null; + { + yield break; } var basePath = 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) @@ -144,22 +142,13 @@ private static List GetDotNetBasePaths(string workingDirectory) var path = sdkMatch.Groups[2].Value.Trim(); path = Path.Combine(path, version) + Path.DirectorySeparatorChar; - - if (path.Equals(basePath)) - { - // We insert the version in use at the front of the list in order to ensure FirstOrDefault always returns the version in use. - basePaths.Insert(0, path); - } - else - { - basePaths.Add(path); - } + if (!path.Equals(basePath)) + yield return path; + lineSdkIndex++; } } - - return basePaths; } } } From 32a6f0b576d55d6c3ba37064bb94b9075cf4f65b Mon Sep 17 00:00:00 2001 From: "Kurt I. Gronbech" Date: Fri, 11 Dec 2020 00:41:23 +0100 Subject: [PATCH 11/13] cleaned some whitespace --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index f6f76279..e89316e5 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -96,7 +96,7 @@ private static IEnumerable GetDotNetBasePaths(string workingDirectory) } if (process.HasExited) - { + { yield break; } @@ -117,7 +117,7 @@ private static IEnumerable GetDotNetBasePaths(string workingDirectory) var matched = DotNetBasePathRegex.Match(outputString); if (!matched.Success) - { + { yield break; } From 5546230d203c759071f90c0ae2f29df79ae157a9 Mon Sep 17 00:00:00 2001 From: Rainer Sigwald Date: Mon, 14 Dec 2020 16:04:20 -0600 Subject: [PATCH 12/13] Bump to v1.3 for new feature (getting all installed .NET SDKs) --- version.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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/.*" From 7ab4ced612ca01367b31e4e9c64a7772831447e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kurt=2E=20I=2E=20Gr=C3=B8nbech?= <49025150+kurtcodemander@users.noreply.github.com> Date: Tue, 15 Dec 2020 05:11:26 +0100 Subject: [PATCH 13/13] Update src/MSBuildLocator/DotNetSdkLocationHelper.cs Co-authored-by: Rainer Sigwald --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index e89316e5..11fe6968 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -15,7 +15,7 @@ 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); + private static readonly Regex SdkRegex = new Regex(@"(\S+) \[(.*?)]$", RegexOptions.Multiline); public static VisualStudioInstance GetInstance(string dotNetSdkPath) {