Skip to content

Commit e70d3b2

Browse files
authored
Merge pull request #303 from PowerShell/daviwil/version-details
Add PowerShellVersionDetails class and PowerShellVersionRequest
2 parents ea101b3 + 6ffdae1 commit e70d3b2

File tree

8 files changed

+158
-7
lines changed

8 files changed

+158
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
7+
using Microsoft.PowerShell.EditorServices.Session;
8+
9+
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
10+
{
11+
public class PowerShellVersionRequest
12+
{
13+
public static readonly
14+
RequestType<object, PowerShellVersionResponse> Type =
15+
RequestType<object, PowerShellVersionResponse>.Create("powerShell/getVersion");
16+
}
17+
18+
public class PowerShellVersionResponse
19+
{
20+
public string Version { get; set; }
21+
22+
public string DisplayVersion { get; set; }
23+
24+
public string Edition { get; set; }
25+
26+
public string Architecture { get; set; }
27+
28+
public PowerShellVersionResponse()
29+
{
30+
}
31+
32+
public PowerShellVersionResponse(PowerShellVersionDetails versionDetails)
33+
{
34+
this.Version = versionDetails.Version.ToString();
35+
this.DisplayVersion = versionDetails.VersionString;
36+
this.Edition = versionDetails.Edition;
37+
this.Architecture = versionDetails.Architecture;
38+
}
39+
}
40+
}

src/PowerShellEditorServices.Protocol/PowerShellEditorServices.Protocol.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Compile Include="LanguageServer\EditorCommands.cs" />
5959
<Compile Include="LanguageServer\FindModuleRequest.cs" />
6060
<Compile Include="LanguageServer\InstallModuleRequest.cs" />
61+
<Compile Include="LanguageServer\PowerShellVersionRequest.cs" />
6162
<Compile Include="MessageProtocol\Channel\NamedPipeClientChannel.cs" />
6263
<Compile Include="MessageProtocol\Channel\NamedPipeServerChannel.cs" />
6364
<Compile Include="MessageProtocol\Channel\TcpSocketClientChannel.cs" />

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+11
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ protected override void Initialize()
101101

102102
this.SetRequestHandler(InvokeExtensionCommandRequest.Type, this.HandleInvokeExtensionCommandRequest);
103103

104+
this.SetRequestHandler(PowerShellVersionRequest.Type, this.HandlePowerShellVersionRequest);
105+
104106
this.SetRequestHandler(DebugAdapterMessages.EvaluateRequest.Type, this.HandleEvaluateRequest);
105107

106108
// Initialize the extension service
@@ -799,6 +801,15 @@ protected async Task HandleWorkspaceSymbolRequest(
799801
await requestContext.SendResult(symbols.ToArray());
800802
}
801803

804+
protected async Task HandlePowerShellVersionRequest(
805+
object noParams,
806+
RequestContext<PowerShellVersionResponse> requestContext)
807+
{
808+
await requestContext.SendResult(
809+
new PowerShellVersionResponse(
810+
this.editorSession.PowerShellContext.PowerShellVersionDetails));
811+
}
812+
802813
private bool IsQueryMatch(string query, string symbolName)
803814
{
804815
return symbolName.IndexOf(query, StringComparison.OrdinalIgnoreCase) >= 0;

src/PowerShellEditorServices/Nano.PowerShellEditorServices.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<Compile Include="Debugging\BreakpointDetailsBase.cs" />
5252
<Compile Include="Debugging\DebugService.cs" />
5353
<Compile Include="Debugging\CommandBreakpointDetails.cs" />
54+
<Compile Include="Debugging\InvalidPowerShellExpressionException.cs" />
5455
<Compile Include="Debugging\StackFrameDetails.cs" />
5556
<Compile Include="Debugging\VariableDetails.cs" />
5657
<Compile Include="Debugging\VariableDetailsBase.cs" />
@@ -96,6 +97,7 @@
9697
<Compile Include="Session\PowerShellExecutionResult.cs" />
9798
<Compile Include="Session\PowerShellContext.cs" />
9899
<Compile Include="Session\PowerShellContextState.cs" />
100+
<Compile Include="Session\PowerShellVersionDetails.cs" />
99101
<Compile Include="Session\ProfilePaths.cs" />
100102
<Compile Include="Session\ProgressDetails.cs" />
101103
<Compile Include="Session\RunspaceHandle.cs" />
@@ -136,6 +138,7 @@
136138
<Compile Include="..\PowerShellEditorServices.Protocol\LanguageServer\EditorCommands.cs" />
137139
<Compile Include="..\PowerShellEditorServices.Protocol\LanguageServer\FindModuleRequest.cs" />
138140
<Compile Include="..\PowerShellEditorServices.Protocol\LanguageServer\InstallModuleRequest.cs" />
141+
<Compile Include="..\PowerShellEditorServices.Protocol\LanguageServer\PowerShellVersionRequest.cs" />
139142
<Compile Include="..\PowerShellEditorServices.Protocol\MessageProtocol\Channel\NamedPipeClientChannel.cs" />
140143
<Compile Include="..\PowerShellEditorServices.Protocol\MessageProtocol\Channel\NamedPipeServerChannel.cs" />
141144
<Compile Include="..\PowerShellEditorServices.Protocol\MessageProtocol\Channel\TcpSocketClientChannel.cs" />
@@ -158,6 +161,7 @@
158161
<Compile Include="..\PowerShellEditorServices.Protocol\DebugAdapter\Scope.cs" />
159162
<Compile Include="..\PowerShellEditorServices.Protocol\DebugAdapter\ScopesRequest.cs" />
160163
<Compile Include="..\PowerShellEditorServices.Protocol\DebugAdapter\SetBreakpointsRequest.cs" />
164+
<Compile Include="..\PowerShellEditorServices.Protocol\DebugAdapter\SetVariableRequest.cs" />
161165
<Compile Include="..\PowerShellEditorServices.Protocol\DebugAdapter\SetExceptionBreakpointsRequest.cs" />
162166
<Compile Include="..\PowerShellEditorServices.Protocol\DebugAdapter\Source.cs" />
163167
<Compile Include="..\PowerShellEditorServices.Protocol\DebugAdapter\SourceRequest.cs" />

src/PowerShellEditorServices/PowerShellEditorServices.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<Compile Include="Session\PowerShellExecutionResult.cs" />
118118
<Compile Include="Session\PowerShellContext.cs" />
119119
<Compile Include="Session\PowerShellContextState.cs" />
120+
<Compile Include="Session\PowerShellVersionDetails.cs" />
120121
<Compile Include="Session\ProfilePaths.cs" />
121122
<Compile Include="Session\ProgressDetails.cs" />
122123
<Compile Include="Session\RunspaceHandle.cs" />

src/PowerShellEditorServices/Session/PowerShellContext.cs

+27-7
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,28 @@ public PowerShellContextState SessionState
7575
private set;
7676
}
7777

78+
/// <summary>
79+
/// Gets the PowerShell version details for the current runspace.
80+
/// </summary>
81+
public PowerShellVersionDetails PowerShellVersionDetails
82+
{
83+
get; private set;
84+
}
85+
7886
/// <summary>
7987
/// Gets the PowerShell version of the current runspace.
8088
/// </summary>
8189
public Version PowerShellVersion
8290
{
83-
get; private set;
91+
get { return this.PowerShellVersionDetails.Version; }
8492
}
8593

8694
/// <summary>
8795
/// Gets the PowerShell edition of the current runspace.
8896
/// </summary>
8997
public string PowerShellEdition
9098
{
91-
get; private set;
99+
get { return this.PowerShellVersionDetails.Edition; }
92100
}
93101

94102
/// <summary>
@@ -188,9 +196,7 @@ private void Initialize(ProfilePaths profilePaths, Runspace initialRunspace)
188196
this.powerShell.Runspace = this.currentRunspace;
189197

190198
// Get the PowerShell runtime version
191-
Tuple<Version, string> versionEditionTuple = GetPowerShellVersion();
192-
this.PowerShellVersion = versionEditionTuple.Item1;
193-
this.PowerShellEdition = versionEditionTuple.Item2;
199+
this.PowerShellVersionDetails = GetPowerShellVersion();
194200

195201
// Write out the PowerShell version for tracking purposes
196202
Logger.Write(
@@ -246,10 +252,12 @@ private void Initialize(ProfilePaths profilePaths, Runspace initialRunspace)
246252
this.runspaceWaitQueue.EnqueueAsync(runspaceHandle).Wait();
247253
}
248254

249-
private Tuple<Version, string> GetPowerShellVersion()
255+
private PowerShellVersionDetails GetPowerShellVersion()
250256
{
251257
Version powerShellVersion = new Version(5, 0);
258+
string versionString = null;
252259
string powerShellEdition = "Desktop";
260+
string architecture = "Unknown";
253261

254262
try
255263
{
@@ -267,14 +275,26 @@ private Tuple<Version, string> GetPowerShellVersion()
267275
{
268276
powerShellEdition = edition;
269277
}
278+
279+
var gitCommitId = psVersionTable["GitCommitId"] as string;
280+
if (gitCommitId != null)
281+
{
282+
versionString = gitCommitId;
283+
}
284+
285+
architecture = this.currentRunspace.SessionStateProxy.GetVariable("env:PROCESSOR_ARCHITECTURE") as string;
270286
}
271287
}
272288
catch (Exception ex)
273289
{
274290
Logger.Write(LogLevel.Warning, "Failed to look up PowerShell version. Defaulting to version 5. " + ex.Message);
275291
}
276292

277-
return new Tuple<Version, string>(powerShellVersion, powerShellEdition);
293+
return new PowerShellVersionDetails(
294+
powerShellVersion,
295+
versionString,
296+
powerShellEdition,
297+
architecture);
278298
}
279299

280300
#endregion
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Session
9+
{
10+
/// <summary>
11+
/// Provides details about the version of the PowerShell runtime.
12+
/// </summary>
13+
public class PowerShellVersionDetails
14+
{
15+
/// <summary>
16+
/// Gets the version of the PowerShell runtime.
17+
/// </summary>
18+
public Version Version { get; private set; }
19+
20+
/// <summary>
21+
/// Gets the full version string, either the ToString of the Version
22+
/// property or the GitCommitId for open-source PowerShell releases.
23+
/// </summary>
24+
public string VersionString { get; private set; }
25+
26+
/// <summary>
27+
/// Gets the PowerShell edition (generally Desktop or Core).
28+
/// </summary>
29+
public string Edition { get; private set; }
30+
31+
/// <summary>
32+
/// Gets the architecture of the PowerShell process, either "x86" or
33+
/// "x64".
34+
/// </summary>
35+
public string Architecture { get; private set; }
36+
37+
/// <summary>
38+
/// Creates an instance of the PowerShellVersionDetails class.
39+
/// </summary>
40+
/// <param name="powerShellVersion">The version of the PowerShell runtime.</param>
41+
/// <param name="versionString">A string representation of the PowerShell version.</param>
42+
/// <param name="editionString">The string representation of the PowerShell edition.</param>
43+
/// <param name="architectureString">The string representation of the processor architecture.</param>
44+
public PowerShellVersionDetails(
45+
Version powerShellVersion,
46+
string versionString,
47+
string editionString,
48+
string architectureString)
49+
{
50+
this.Version = powerShellVersion;
51+
this.VersionString = versionString ?? $"{powerShellVersion.Major}.{powerShellVersion.Minor}";
52+
this.Edition = editionString;
53+
this.Architecture =
54+
string.Equals(architectureString, "AMD64", StringComparison.CurrentCultureIgnoreCase)
55+
? "x64"
56+
: architectureString;
57+
}
58+
}
59+
}

test/PowerShellEditorServices.Test.Host/LanguageServerTests.cs

+15
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,21 @@ await this.languageServiceClient.SendEvent(
702702
Assert.Equal("PROFILE: True", outputString);
703703
}
704704

705+
[Fact]
706+
public async Task ServiceReturnsPowerShellVersionDetails()
707+
{
708+
PowerShellVersionResponse versionDetails =
709+
await this.SendRequest(
710+
PowerShellVersionRequest.Type,
711+
new PowerShellVersionRequest());
712+
713+
// TODO: This should be more robust and predictable.
714+
Assert.StartsWith("5.", versionDetails.Version);
715+
Assert.StartsWith("5.", versionDetails.DisplayVersion);
716+
Assert.Equal("Desktop", versionDetails.Edition);
717+
Assert.Equal("x86", versionDetails.Architecture);
718+
}
719+
705720
private async Task SendOpenFileEvent(string filePath, bool waitForDiagnostics = true)
706721
{
707722
string fileContents = string.Join(Environment.NewLine, File.ReadAllLines(filePath));

0 commit comments

Comments
 (0)