|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.ComponentModel.Composition; |
| 5 | +using System.Globalization; |
| 6 | +using GitHub.VisualStudio; |
| 7 | +using Microsoft.VisualStudio; |
| 8 | +using Microsoft.VisualStudio.Shell.Interop; |
| 9 | +using System.Linq; |
| 10 | + |
| 11 | +namespace GitHub.Services |
| 12 | +{ |
| 13 | + [Export(typeof(IVSServices))] |
| 14 | + [PartCreationPolicy(CreationPolicy.Shared)] |
| 15 | + public class VSServices : IVSServices |
| 16 | + { |
| 17 | + readonly IUIProvider serviceProvider; |
| 18 | + |
| 19 | + [ImportingConstructor] |
| 20 | + public VSServices(IUIProvider serviceProvider) |
| 21 | + { |
| 22 | + this.serviceProvider = serviceProvider; |
| 23 | + } |
| 24 | + |
| 25 | + string vsVersion; |
| 26 | + public string VSVersion |
| 27 | + { |
| 28 | + get |
| 29 | + { |
| 30 | + if (vsVersion == null) |
| 31 | + vsVersion = GetVSVersion(); |
| 32 | + return vsVersion; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + public void ActivityLogMessage(string message) |
| 38 | + { |
| 39 | + var log = serviceProvider.GetActivityLog(); |
| 40 | + if (log != null) |
| 41 | + { |
| 42 | + if (!ErrorHandler.Succeeded(log.LogEntry((UInt32)__ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION, |
| 43 | + Info.ApplicationInfo.ApplicationSafeName, message))) |
| 44 | + Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "Could not log message to activity log: {0}", message)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public void ActivityLogError(string message) |
| 49 | + { |
| 50 | + var log = serviceProvider.GetActivityLog(); |
| 51 | + if (log != null) |
| 52 | + { |
| 53 | + |
| 54 | + if (!ErrorHandler.Succeeded(log.LogEntry((UInt32)__ACTIVITYLOG_ENTRYTYPE.ALE_ERROR, |
| 55 | + Info.ApplicationInfo.ApplicationSafeName, message))) |
| 56 | + Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "Could not log error to activity log: {0}", message)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public void ActivityLogWarning(string message) |
| 61 | + { |
| 62 | + var log = serviceProvider.GetActivityLog(); |
| 63 | + if (log != null) |
| 64 | + { |
| 65 | + if (!ErrorHandler.Succeeded(log.LogEntry((UInt32)__ACTIVITYLOG_ENTRYTYPE.ALE_WARNING, |
| 66 | + Info.ApplicationInfo.ApplicationSafeName, message))) |
| 67 | + Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "Could not log warning to activity log: {0}", message)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + const string RegistryRootKey = @"Software\Microsoft\VisualStudio"; |
| 72 | + const string EnvVersionKey = "EnvVersion"; |
| 73 | + string GetVSVersion() |
| 74 | + { |
| 75 | + var version = VisualStudio.Services.Dte.Version; |
| 76 | + var keyPath = String.Format(CultureInfo.InvariantCulture, "{0}\\{1}_Config\\SplashInfo", RegistryRootKey, version); |
| 77 | + try |
| 78 | + { |
| 79 | + using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keyPath)) |
| 80 | + { |
| 81 | + var value = (string)key.GetValue(EnvVersionKey, String.Empty); |
| 82 | + if (!String.IsNullOrEmpty(value)) |
| 83 | + return value; |
| 84 | + } |
| 85 | + var asm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName.StartsWith("Microsoft.VisualStudio.CommonIDE", StringComparison.OrdinalIgnoreCase)); |
| 86 | + if (asm != null) |
| 87 | + return asm.GetName().Version.ToString(); |
| 88 | + } |
| 89 | + catch(Exception ex) |
| 90 | + { |
| 91 | + VsOutputLogger.WriteLine(string.Format(CultureInfo.CurrentCulture, "Error getting the Visual Studio version '{0}'", ex)); |
| 92 | + } |
| 93 | + return version; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments