Skip to content

Commit c42d3c2

Browse files
authored
Merge pull request #307 from PowerShell/daviwil/session-version-fixes
Improve PowerShell version and architecture gathering
2 parents 1ff1618 + 79231fc commit c42d3c2

File tree

3 files changed

+73
-24
lines changed

3 files changed

+73
-24
lines changed

src/PowerShellEditorServices.Protocol/LanguageServer/PowerShellVersionRequest.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,22 @@ public PowerShellVersionResponse()
3131

3232
public PowerShellVersionResponse(PowerShellVersionDetails versionDetails)
3333
{
34-
this.Version = versionDetails.Version.ToString();
35-
this.DisplayVersion = versionDetails.VersionString;
34+
this.Version = versionDetails.VersionString;
35+
this.DisplayVersion = $"{versionDetails.Version.Major}.{versionDetails.Version.Minor}";
3636
this.Edition = versionDetails.Edition;
37-
this.Architecture = versionDetails.Architecture;
37+
38+
switch (versionDetails.Architecture)
39+
{
40+
case PowerShellProcessArchitecture.X64:
41+
this.Architecture = "x64";
42+
break;
43+
case PowerShellProcessArchitecture.X86:
44+
this.Architecture = "x86";
45+
break;
46+
default:
47+
this.Architecture = "Architecture Unknown";
48+
break;
49+
}
3850
}
3951
}
4052
}

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,32 +257,52 @@ private PowerShellVersionDetails GetPowerShellVersion()
257257
Version powerShellVersion = new Version(5, 0);
258258
string versionString = null;
259259
string powerShellEdition = "Desktop";
260-
string architecture = "Unknown";
260+
var architecture = PowerShellProcessArchitecture.Unknown;
261261

262262
try
263263
{
264264
var psVersionTable = this.currentRunspace.SessionStateProxy.GetVariable("PSVersionTable") as Hashtable;
265265
if (psVersionTable != null)
266266
{
267-
var version = psVersionTable["PSVersion"] as Version;
268-
if (version != null)
269-
{
270-
powerShellVersion = version;
271-
}
272-
273267
var edition = psVersionTable["PSEdition"] as string;
274268
if (edition != null)
275269
{
276270
powerShellEdition = edition;
277271
}
278272

273+
// The PSVersion value will either be of Version or SemanticVersion.
274+
// In the former case, take the value directly. In the latter case,
275+
// generate a Version from its string representation.
276+
var version = psVersionTable["PSVersion"];
277+
if (version is Version)
278+
{
279+
powerShellVersion = (Version)version;
280+
}
281+
else if (string.Equals(powerShellEdition, "Core", StringComparison.CurrentCultureIgnoreCase))
282+
{
283+
// Expected version string format is 6.0.0-alpha so build a simpler version from that
284+
powerShellVersion = new Version(version.ToString().Split('-')[0]);
285+
}
286+
279287
var gitCommitId = psVersionTable["GitCommitId"] as string;
280288
if (gitCommitId != null)
281289
{
282290
versionString = gitCommitId;
283291
}
292+
else
293+
{
294+
versionString = powerShellVersion.ToString();
295+
}
284296

285-
architecture = this.currentRunspace.SessionStateProxy.GetVariable("env:PROCESSOR_ARCHITECTURE") as string;
297+
var arch = this.currentRunspace.SessionStateProxy.GetVariable("env:PROCESSOR_ARCHITECTURE") as string;
298+
if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase))
299+
{
300+
architecture = PowerShellProcessArchitecture.X64;
301+
}
302+
else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase))
303+
{
304+
architecture = PowerShellProcessArchitecture.X86;
305+
}
286306
}
287307
}
288308
catch (Exception ex)

src/PowerShellEditorServices/Session/PowerShellVersionDetails.cs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@
77

88
namespace Microsoft.PowerShell.EditorServices.Session
99
{
10+
/// <summary>
11+
/// Defines the possible enumeration values for the PowerShell process architecture.
12+
/// </summary>
13+
public enum PowerShellProcessArchitecture
14+
{
15+
/// <summary>
16+
/// The processor architecture is unknown or wasn't accessible.
17+
/// </summary>
18+
Unknown,
19+
20+
/// <summary>
21+
/// The processor architecture is 32-bit.
22+
/// </summary>
23+
X86,
24+
25+
/// <summary>
26+
/// The processor architecture is 64-bit.
27+
/// </summary>
28+
X64
29+
}
30+
1031
/// <summary>
1132
/// Provides details about the version of the PowerShell runtime.
1233
/// </summary>
@@ -29,31 +50,27 @@ public class PowerShellVersionDetails
2950
public string Edition { get; private set; }
3051

3152
/// <summary>
32-
/// Gets the architecture of the PowerShell process, either "x86" or
33-
/// "x64".
53+
/// Gets the architecture of the PowerShell process.
3454
/// </summary>
35-
public string Architecture { get; private set; }
55+
public PowerShellProcessArchitecture Architecture { get; private set; }
3656

3757
/// <summary>
3858
/// Creates an instance of the PowerShellVersionDetails class.
3959
/// </summary>
40-
/// <param name="powerShellVersion">The version of the PowerShell runtime.</param>
60+
/// <param name="version">The version of the PowerShell runtime.</param>
4161
/// <param name="versionString">A string representation of the PowerShell version.</param>
4262
/// <param name="editionString">The string representation of the PowerShell edition.</param>
43-
/// <param name="architectureString">The string representation of the processor architecture.</param>
63+
/// <param name="architecture">The processor architecture.</param>
4464
public PowerShellVersionDetails(
45-
Version powerShellVersion,
65+
Version version,
4666
string versionString,
4767
string editionString,
48-
string architectureString)
68+
PowerShellProcessArchitecture architecture)
4969
{
50-
this.Version = powerShellVersion;
51-
this.VersionString = versionString ?? $"{powerShellVersion.Major}.{powerShellVersion.Minor}";
70+
this.Version = version;
71+
this.VersionString = versionString;
5272
this.Edition = editionString;
53-
this.Architecture =
54-
string.Equals(architectureString, "AMD64", StringComparison.CurrentCultureIgnoreCase)
55-
? "x64"
56-
: architectureString;
73+
this.Architecture = architecture;
5774
}
5875
}
5976
}

0 commit comments

Comments
 (0)