From f6eccb4c1ff93a6ebc58c88610509b54596ec779 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Wed, 24 Jan 2018 15:55:00 -0500 Subject: [PATCH 1/2] Correctly detecting VisualStudio version on 2017 and forward --- src/GitHub.Exports/Services/VSServices.cs | 44 ++++++++++++++++++----- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/GitHub.Exports/Services/VSServices.cs b/src/GitHub.Exports/Services/VSServices.cs index 39f13da5a8..bc56dded0d 100644 --- a/src/GitHub.Exports/Services/VSServices.cs +++ b/src/GitHub.Exports/Services/VSServices.cs @@ -1,8 +1,10 @@ using System; using System.ComponentModel.Composition; +using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; +using System.Text.RegularExpressions; using GitHub.Logging; using GitHub.VisualStudio; using Microsoft.VisualStudio; @@ -151,16 +153,42 @@ string GetVSVersion() var keyPath = String.Format(CultureInfo.InvariantCulture, "{0}\\{1}.{2}_Config\\SplashInfo", RegistryRootKey, version.Major, version.Minor); try { - using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keyPath)) + if (version.Major == 14) { - var value = (string)key.GetValue(EnvVersionKey, String.Empty); - if (!String.IsNullOrEmpty(value)) - return value; + using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keyPath)) + { + var value = (string)key.GetValue(EnvVersionKey, String.Empty); + if (!String.IsNullOrEmpty(value)) + return value; + } + } + else + { + var os = serviceProvider.TryGetService(); + var devenv = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); + + // C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances + var pathToInstallationData = Path.Combine(os.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData), + "Microsoft", "VisualStudio", "Packages", "_Instances"); + + var regexVersion = new Regex(@"""productDisplayVersion"":""([^""]+)"""); + var regexPath = new Regex(@"""installationPath"":""([^""]+)"""); + + foreach (var dir in os.Directory.EnumerateDirectories(pathToInstallationData)) + { + var data = os.File.ReadAllText(Path.Combine(dir, "state.json"), System.Text.Encoding.UTF8); + if (regexPath.IsMatch(data) && regexVersion.IsMatch(data)) + { + var path = regexPath.Match(data).Groups[1].Value; + path = path.Replace("\\\\", "\\"); + if (devenv.StartsWith(path, StringComparison.OrdinalIgnoreCase)) + { + var value = regexVersion.Match(data).Groups[1].Value; + return value; + } + } + } } - // fallback to poking the CommonIDE assembly, which most closely follows the advertised version. - var asm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName.StartsWith("Microsoft.VisualStudio.CommonIDE", StringComparison.OrdinalIgnoreCase)); - if (asm != null) - return asm.GetName().Version.ToString(); } catch(Exception ex) { From 7c2b7952f808561054a63a3604feea0c971374b5 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 25 Jan 2018 09:13:41 -0500 Subject: [PATCH 2/2] Using SetupConfiguration to get the version in VisualStudio 15 and above --- src/GitHub.Exports/GitHub.Exports.csproj | 4 +++ src/GitHub.Exports/Services/VSServices.cs | 30 +++++------------------ src/GitHub.Exports/packages.config | 1 + 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/GitHub.Exports/GitHub.Exports.csproj b/src/GitHub.Exports/GitHub.Exports.csproj index dc0c9d224c..9d98b9c612 100644 --- a/src/GitHub.Exports/GitHub.Exports.csproj +++ b/src/GitHub.Exports/GitHub.Exports.csproj @@ -63,6 +63,10 @@ ..\..\packages\Microsoft.VisualStudio.ComponentModelHost.14.0.25424\lib\net45\Microsoft.VisualStudio.ComponentModelHost.dll True + + ..\..\packages\Microsoft.VisualStudio.Setup.Configuration.Interop.1.15.103\lib\net35\Microsoft.VisualStudio.Setup.Configuration.Interop.dll + True + ..\..\packages\Microsoft.VisualStudio.Shell.14.0.14.3.25407\lib\Microsoft.VisualStudio.Shell.14.0.dll True diff --git a/src/GitHub.Exports/Services/VSServices.cs b/src/GitHub.Exports/Services/VSServices.cs index bc56dded0d..4b580a5eaf 100644 --- a/src/GitHub.Exports/Services/VSServices.cs +++ b/src/GitHub.Exports/Services/VSServices.cs @@ -8,6 +8,8 @@ using GitHub.Logging; using GitHub.VisualStudio; using Microsoft.VisualStudio; +using Microsoft.VisualStudio.Setup.Configuration; +using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; using Rothko; using Serilog; @@ -147,6 +149,7 @@ void TryCleanupSolutionUserFiles(IOperatingSystem os, string repoPath, string sl const string RegistryRootKey = @"Software\Microsoft\VisualStudio"; const string EnvVersionKey = "EnvVersion"; + const string InstallationNamePrefix = "VisualStudio/"; string GetVSVersion() { var version = typeof(Microsoft.VisualStudio.Shell.ActivityLog).Assembly.GetName().Version; @@ -164,30 +167,9 @@ string GetVSVersion() } else { - var os = serviceProvider.TryGetService(); - var devenv = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); - - // C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances - var pathToInstallationData = Path.Combine(os.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData), - "Microsoft", "VisualStudio", "Packages", "_Instances"); - - var regexVersion = new Regex(@"""productDisplayVersion"":""([^""]+)"""); - var regexPath = new Regex(@"""installationPath"":""([^""]+)"""); - - foreach (var dir in os.Directory.EnumerateDirectories(pathToInstallationData)) - { - var data = os.File.ReadAllText(Path.Combine(dir, "state.json"), System.Text.Encoding.UTF8); - if (regexPath.IsMatch(data) && regexVersion.IsMatch(data)) - { - var path = regexPath.Match(data).Groups[1].Value; - path = path.Replace("\\\\", "\\"); - if (devenv.StartsWith(path, StringComparison.OrdinalIgnoreCase)) - { - var value = regexVersion.Match(data).Groups[1].Value; - return value; - } - } - } + var setupConfiguration = new SetupConfiguration(); + var setupInstance = setupConfiguration.GetInstanceForCurrentProcess(); + return setupInstance.GetInstallationName().TrimPrefix(InstallationNamePrefix); } } catch(Exception ex) diff --git a/src/GitHub.Exports/packages.config b/src/GitHub.Exports/packages.config index 6f97115759..bd610fe67e 100644 --- a/src/GitHub.Exports/packages.config +++ b/src/GitHub.Exports/packages.config @@ -4,6 +4,7 @@ +