Skip to content

Commit

Permalink
refactor GetVersionDetails which had a couple references of ExecuteSc…
Browse files Browse the repository at this point in the history
…riptAndGetItem
  • Loading branch information
TylerLeonhardt committed Apr 22, 2020
1 parent 16fa1d0 commit e7fdca2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -89,76 +87,34 @@ public PowerShellVersionDetails(
/// <summary>
/// Gets the PowerShell version details for the given runspace.
/// </summary>
/// <param name="runspace">The runspace for which version details will be gathered.</param>
/// <param name="logger">An ILogger implementation used for writing log messages.</param>
/// <returns>A new PowerShellVersionDetails instance.</returns>
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<Hashtable>("$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<string>("$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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit e7fdca2

Please sign in to comment.