Skip to content

Commit e7fdca2

Browse files
refactor GetVersionDetails which had a couple references of ExecuteScriptAndGetItem
1 parent 16fa1d0 commit e7fdca2

File tree

3 files changed

+20
-67
lines changed

3 files changed

+20
-67
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,7 @@ public void Initialize(
329329
this.ConsoleReader = consoleHost as IHostInput;
330330

331331
// Get the PowerShell runtime version
332-
this.LocalPowerShellVersion =
333-
PowerShellVersionDetails.GetVersionDetails(
334-
initialRunspace,
335-
this.logger);
332+
this.LocalPowerShellVersion = PowerShellVersionDetails.GetVersionDetails();
336333

337334
this.powerShell = PowerShell.Create();
338335
this.powerShell.Runspace = initialRunspace;

src/PowerShellEditorServices/Services/PowerShellContext/Session/PowerShellVersionDetails.cs

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6-
using Microsoft.Extensions.Logging;
76
using System;
8-
using System.Collections;
9-
using System.Management.Automation.Runspaces;
7+
using Microsoft.PowerShell.EditorServices.Utility;
108

119
namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext
1210
{
@@ -89,76 +87,34 @@ public PowerShellVersionDetails(
8987
/// <summary>
9088
/// Gets the PowerShell version details for the given runspace.
9189
/// </summary>
92-
/// <param name="runspace">The runspace for which version details will be gathered.</param>
93-
/// <param name="logger">An ILogger implementation used for writing log messages.</param>
9490
/// <returns>A new PowerShellVersionDetails instance.</returns>
95-
public static PowerShellVersionDetails GetVersionDetails(Runspace runspace, ILogger logger)
91+
public static PowerShellVersionDetails GetVersionDetails()
9692
{
97-
Version powerShellVersion = new Version(5, 0);
98-
string versionString = null;
99-
string powerShellEdition = "Desktop";
93+
var arch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
10094
var architecture = PowerShellProcessArchitecture.Unknown;
101-
102-
try
95+
if (arch != null)
10396
{
104-
var psVersionTable = PowerShellContextService.ExecuteScriptAndGetItem<Hashtable>("$PSVersionTable", runspace);
105-
if (psVersionTable != null)
97+
if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase))
98+
{
99+
architecture = PowerShellProcessArchitecture.X64;
100+
}
101+
else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase))
106102
{
107-
var edition = psVersionTable["PSEdition"] as string;
108-
if (edition != null)
109-
{
110-
powerShellEdition = edition;
111-
}
112-
113-
// The PSVersion value will either be of Version or SemanticVersion.
114-
// In the former case, take the value directly. In the latter case,
115-
// generate a Version from its string representation.
116-
var version = psVersionTable["PSVersion"];
117-
if (version is Version)
118-
{
119-
powerShellVersion = (Version)version;
120-
}
121-
else if (version != null)
122-
{
123-
// Expected version string format is 6.0.0-alpha so build a simpler version from that
124-
powerShellVersion = new Version(version.ToString().Split('-')[0]);
125-
}
126-
127-
var gitCommitId = psVersionTable["GitCommitId"] as string;
128-
if (gitCommitId != null)
129-
{
130-
versionString = gitCommitId;
131-
}
132-
else
133-
{
134-
versionString = powerShellVersion.ToString();
135-
}
136-
137-
var arch = PowerShellContextService.ExecuteScriptAndGetItem<string>("$env:PROCESSOR_ARCHITECTURE", runspace);
138-
if (arch != null)
139-
{
140-
if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase))
141-
{
142-
architecture = PowerShellProcessArchitecture.X64;
143-
}
144-
else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase))
145-
{
146-
architecture = PowerShellProcessArchitecture.X86;
147-
}
148-
}
103+
architecture = PowerShellProcessArchitecture.X86;
149104
}
150105
}
151-
catch (Exception ex)
106+
107+
if (!VersionUtils.IsWindows)
152108
{
153-
logger.LogWarning(
154-
"Failed to look up PowerShell version, defaulting to version 5.\r\n\r\n" + ex.ToString());
109+
architecture = PowerShellProcessArchitecture.X64;
155110
}
156111

157112
return new PowerShellVersionDetails(
158-
powerShellVersion,
159-
versionString,
160-
powerShellEdition,
161-
architecture);
113+
VersionUtils.PSVersion,
114+
VersionUtils.PSVersionString,
115+
VersionUtils.PSEdition,
116+
architecture
117+
);
162118
}
163119

164120
#endregion

src/PowerShellEditorServices/Services/PowerShellContext/Session/RunspaceDetails.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ internal static RunspaceDetails CreateFromRunspace(
196196

197197
var runspaceLocation = RunspaceLocation.Local;
198198
var runspaceContext = RunspaceContext.Original;
199-
var versionDetails = PowerShellVersionDetails.GetVersionDetails(runspace, logger);
199+
var versionDetails = PowerShellVersionDetails.GetVersionDetails();
200200

201201
string connectionString = null;
202202

0 commit comments

Comments
 (0)