From 6acc99c69f7762cbae39aa09f07d02c9eb1acb73 Mon Sep 17 00:00:00 2001 From: Andy Jordan Date: Fri, 2 Dec 2022 09:52:02 -0800 Subject: [PATCH] Remove unnecessary `PowerShellProcessArchitecture` This simply wasn't being used and was overly complicated. The only time we want the architecture is when queried via LSP so that the VS Code client can determine which installer to download for its auto-update feature. If we can, we should deduplicate version logic in the loader and the `VersionUtils` class. --- .../EditorServicesLoader.cs | 1 + .../Context/PowerShellVersionDetails.cs | 73 ++----------------- .../PowerShell/Handlers/GetVersionHandler.cs | 25 +------ .../PowerShell/Handlers/IGetVersionHandler.cs | 29 ++------ .../Utility/VersionUtils.cs | 24 ++++++ 5 files changed, 36 insertions(+), 116 deletions(-) diff --git a/src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs b/src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs index 924619283..2f9b8f8a6 100644 --- a/src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs +++ b/src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs @@ -354,6 +354,7 @@ private void LogOperatingSystemDetails() "); } + // TODO: Deduplicate this with VersionUtils. private static string GetOSArchitecture() { #if CoreCLR diff --git a/src/PowerShellEditorServices/Services/PowerShell/Context/PowerShellVersionDetails.cs b/src/PowerShellEditorServices/Services/PowerShell/Context/PowerShellVersionDetails.cs index 4bbdd47e1..e84f235b9 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Context/PowerShellVersionDetails.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Context/PowerShellVersionDetails.cs @@ -11,34 +11,11 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Context { using System.Management.Automation; - /// - /// Defines the possible enumeration values for the PowerShell process architecture. - /// - internal enum PowerShellProcessArchitecture - { - /// - /// The processor architecture is unknown or wasn't accessible. - /// - Unknown, - - /// - /// The processor architecture is 32-bit. - /// - X86, - - /// - /// The processor architecture is 64-bit. - /// - X64 - } - /// /// Provides details about the version of the PowerShell runtime. /// internal class PowerShellVersionDetails { - #region Properties - /// /// Gets the version of the PowerShell runtime. /// @@ -55,40 +32,26 @@ internal class PowerShellVersionDetails /// public string Edition { get; } - /// - /// Gets the architecture of the PowerShell process. - /// - public PowerShellProcessArchitecture Architecture { get; } - - #endregion - - #region Constructors - /// /// Creates an instance of the PowerShellVersionDetails class. /// /// The version of the PowerShell runtime. /// A string representation of the PowerShell version. /// The string representation of the PowerShell edition. - /// The processor architecture. public PowerShellVersionDetails( Version version, string versionString, - string editionString, - PowerShellProcessArchitecture architecture) + string editionString) { Version = version; VersionString = versionString; Edition = editionString; - Architecture = architecture; } - #endregion - - #region Public Methods - /// - /// Gets the PowerShell version details for the given runspace. + /// Gets the PowerShell version details for the given runspace. This doesn't use + /// VersionUtils because we may be remoting, and therefore want the remote runspace's + /// version, not the local process. /// /// An ILogger implementation used for writing log messages. /// The PowerShell instance for which to get the version. @@ -98,7 +61,6 @@ public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerSh Version powerShellVersion = new(5, 0); string versionString = null; string powerShellEdition = "Desktop"; - PowerShellProcessArchitecture architecture = PowerShellProcessArchitecture.Unknown; try { @@ -129,25 +91,6 @@ public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerSh } versionString = psVersionTable["GitCommitId"] is string gitCommitId ? gitCommitId : powerShellVersion.ToString(); - - PSCommand procArchCommand = new PSCommand().AddScript("$env:PROCESSOR_ARCHITECTURE", useLocalScope: true); - - string arch = pwsh - .AddScript("$env:PROCESSOR_ARCHITECTURE", useLocalScope: true) - .InvokeAndClear() - .FirstOrDefault(); - - if (arch != null) - { - if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase)) - { - architecture = PowerShellProcessArchitecture.X64; - } - else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase)) - { - architecture = PowerShellProcessArchitecture.X86; - } - } } } catch (Exception ex) @@ -156,13 +99,7 @@ public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerSh "Failed to look up PowerShell version, defaulting to version 5.\r\n\r\n" + ex.ToString()); } - return new PowerShellVersionDetails( - powerShellVersion, - versionString, - powerShellEdition, - architecture); + return new PowerShellVersionDetails(powerShellVersion, versionString, powerShellEdition); } - - #endregion } } diff --git a/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs b/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs index b500b7c1e..2490c8ad8 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -using System; using System.Threading; using System.Threading.Tasks; using Microsoft.PowerShell.EditorServices.Services.PowerShell; @@ -13,35 +12,13 @@ internal class GetVersionHandler : IGetVersionHandler { public async Task Handle(GetVersionParams request, CancellationToken cancellationToken) { - PowerShellProcessArchitecture architecture = PowerShellProcessArchitecture.Unknown; - // This should be changed to using a .NET call sometime in the future... but it's just for logging purposes. - string arch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"); - if (arch != null) - { - if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase)) - { - architecture = PowerShellProcessArchitecture.X64; - } - else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase)) - { - architecture = PowerShellProcessArchitecture.X86; - } - } - return new PowerShellVersion { Version = VersionUtils.PSVersionString, Edition = VersionUtils.PSEdition, DisplayVersion = VersionUtils.PSVersion.ToString(2), - Architecture = architecture.ToString() + Architecture = VersionUtils.Architecture }; } - - private enum PowerShellProcessArchitecture - { - Unknown, - X86, - X64 - } } } diff --git a/src/PowerShellEditorServices/Services/PowerShell/Handlers/IGetVersionHandler.cs b/src/PowerShellEditorServices/Services/PowerShell/Handlers/IGetVersionHandler.cs index 66e66eab6..c2b4977bd 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Handlers/IGetVersionHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Handlers/IGetVersionHandler.cs @@ -2,7 +2,6 @@ // Licensed under the MIT License. using MediatR; -using Microsoft.PowerShell.EditorServices.Services.PowerShell.Context; using OmniSharp.Extensions.JsonRpc; namespace Microsoft.PowerShell.EditorServices.Services.PowerShell @@ -12,29 +11,11 @@ internal interface IGetVersionHandler : IJsonRpcRequestHandler { } - internal class PowerShellVersion + internal record PowerShellVersion { - public string Version { get; set; } - public string DisplayVersion { get; set; } - public string Edition { get; set; } - public string Architecture { get; set; } - - public PowerShellVersion() - { - } - - public PowerShellVersion(PowerShellVersionDetails versionDetails) - { - Version = versionDetails.VersionString; - DisplayVersion = $"{versionDetails.Version.Major}.{versionDetails.Version.Minor}"; - Edition = versionDetails.Edition; - - Architecture = versionDetails.Architecture switch - { - PowerShellProcessArchitecture.X64 => "x64", - PowerShellProcessArchitecture.X86 => "x86", - _ => "Architecture Unknown", - }; - } + public string Version { get; init; } + public string DisplayVersion { get; init; } + public string Edition { get; init; } + public string Architecture { get; init; } } } diff --git a/src/PowerShellEditorServices/Utility/VersionUtils.cs b/src/PowerShellEditorServices/Utility/VersionUtils.cs index a814b73a6..287b7277e 100644 --- a/src/PowerShellEditorServices/Utility/VersionUtils.cs +++ b/src/PowerShellEditorServices/Utility/VersionUtils.cs @@ -61,6 +61,11 @@ internal static class VersionUtils /// True if we are running on Linux, false otherwise. /// public static bool IsLinux { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + + /// + /// The .NET Architecture as a string. + /// + public static string Architecture { get; } = PowerShellReflectionUtils.GetOSArchitecture(); } internal static class PowerShellReflectionUtils @@ -96,5 +101,24 @@ internal static class PowerShellReflectionUtils public static string PSVersionString { get; } = s_psCurrentVersionProperty != null ? s_psCurrentVersionProperty.GetValue(null).ToString() : s_psVersionProperty.GetValue(null).ToString(); + + public static string GetOSArchitecture() + { +#if CoreCLR + if (Environment.OSVersion.Platform != PlatformID.Win32NT) + { + return RuntimeInformation.OSArchitecture.ToString(); + } +#endif + // If on win7 (version 6.1.x), avoid System.Runtime.InteropServices.RuntimeInformation + if (Environment.OSVersion.Version < new Version(6, 2)) + { + return Environment.Is64BitProcess + ? "X64" + : "X86"; + } + + return RuntimeInformation.OSArchitecture.ToString(); + } } }