From e7fdca2df46f5d5acc071aa2d74cb8e071edc0c5 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Wed, 22 Apr 2020 10:09:20 -0700 Subject: [PATCH] refactor GetVersionDetails which had a couple references of ExecuteScriptAndGetItem --- .../PowerShellContextService.cs | 5 +- .../Session/PowerShellVersionDetails.cs | 80 +++++-------------- .../Session/RunspaceDetails.cs | 2 +- 3 files changed, 20 insertions(+), 67 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index f8d3297741..f046257022 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -329,10 +329,7 @@ public void Initialize( this.ConsoleReader = consoleHost as IHostInput; // Get the PowerShell runtime version - this.LocalPowerShellVersion = - PowerShellVersionDetails.GetVersionDetails( - initialRunspace, - this.logger); + this.LocalPowerShellVersion = PowerShellVersionDetails.GetVersionDetails(); this.powerShell = PowerShell.Create(); this.powerShell.Runspace = initialRunspace; diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Session/PowerShellVersionDetails.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Session/PowerShellVersionDetails.cs index 72b8dc5bec..9d22070488 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Session/PowerShellVersionDetails.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Session/PowerShellVersionDetails.cs @@ -3,10 +3,8 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // -using Microsoft.Extensions.Logging; using System; -using System.Collections; -using System.Management.Automation.Runspaces; +using Microsoft.PowerShell.EditorServices.Utility; namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext { @@ -89,76 +87,34 @@ public PowerShellVersionDetails( /// /// Gets the PowerShell version details for the given runspace. /// - /// The runspace for which version details will be gathered. - /// An ILogger implementation used for writing log messages. /// A new PowerShellVersionDetails instance. - public static PowerShellVersionDetails GetVersionDetails(Runspace runspace, ILogger logger) + public static PowerShellVersionDetails GetVersionDetails() { - Version powerShellVersion = new Version(5, 0); - string versionString = null; - string powerShellEdition = "Desktop"; + var arch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"); var architecture = PowerShellProcessArchitecture.Unknown; - - try + if (arch != null) { - var psVersionTable = PowerShellContextService.ExecuteScriptAndGetItem("$PSVersionTable", runspace); - if (psVersionTable != null) + if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase)) + { + architecture = PowerShellProcessArchitecture.X64; + } + else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase)) { - var edition = psVersionTable["PSEdition"] as string; - if (edition != null) - { - powerShellEdition = edition; - } - - // The PSVersion value will either be of Version or SemanticVersion. - // In the former case, take the value directly. In the latter case, - // generate a Version from its string representation. - var version = psVersionTable["PSVersion"]; - if (version is Version) - { - powerShellVersion = (Version)version; - } - else if (version != null) - { - // Expected version string format is 6.0.0-alpha so build a simpler version from that - powerShellVersion = new Version(version.ToString().Split('-')[0]); - } - - var gitCommitId = psVersionTable["GitCommitId"] as string; - if (gitCommitId != null) - { - versionString = gitCommitId; - } - else - { - versionString = powerShellVersion.ToString(); - } - - var arch = PowerShellContextService.ExecuteScriptAndGetItem("$env:PROCESSOR_ARCHITECTURE", runspace); - if (arch != null) - { - if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase)) - { - architecture = PowerShellProcessArchitecture.X64; - } - else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase)) - { - architecture = PowerShellProcessArchitecture.X86; - } - } + architecture = PowerShellProcessArchitecture.X86; } } - catch (Exception ex) + + if (!VersionUtils.IsWindows) { - logger.LogWarning( - "Failed to look up PowerShell version, defaulting to version 5.\r\n\r\n" + ex.ToString()); + architecture = PowerShellProcessArchitecture.X64; } return new PowerShellVersionDetails( - powerShellVersion, - versionString, - powerShellEdition, - architecture); + VersionUtils.PSVersion, + VersionUtils.PSVersionString, + VersionUtils.PSEdition, + architecture + ); } #endregion diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Session/RunspaceDetails.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Session/RunspaceDetails.cs index d5013c9f9f..22eb2a37b8 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Session/RunspaceDetails.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Session/RunspaceDetails.cs @@ -196,7 +196,7 @@ internal static RunspaceDetails CreateFromRunspace( var runspaceLocation = RunspaceLocation.Local; var runspaceContext = RunspaceContext.Original; - var versionDetails = PowerShellVersionDetails.GetVersionDetails(runspace, logger); + var versionDetails = PowerShellVersionDetails.GetVersionDetails(); string connectionString = null;