3
3
*--------------------------------------------------------*/
4
4
import * as vscode from "vscode" ;
5
5
import { v4 as uuidv4 } from 'uuid' ;
6
- import { LanguageClient } from "vscode-languageclient" ;
7
6
import { LanguageClientConsumer } from "../languageClientConsumer" ;
8
7
import { Logger } from "../logging" ;
9
8
import { SessionManager } from "../session" ;
@@ -15,71 +14,44 @@ export interface IExternalPowerShellDetails {
15
14
architecture : string ;
16
15
}
17
16
18
- export class ExternalApiFeature extends LanguageClientConsumer {
19
- private commands : vscode . Disposable [ ] ;
17
+ export interface IPowerShellExtensionClient {
18
+ registerExternalExtension ( id : string , apiVersion ?: string ) : string ;
19
+ unregisterExternalExtension ( uuid : string ) : boolean ;
20
+ getPowerShellVersionDetails ( uuid : string ) : Promise < IExternalPowerShellDetails > ;
21
+ }
22
+
23
+ /*
24
+ In order to use this in a Visual Studio Code extension, you can do the following:
25
+
26
+ const powershellExtension = vscode.extensions.getExtension<IPowerShellExtensionClient>("ms-vscode.PowerShell-Preview");
27
+ const powerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
28
+
29
+ NOTE: At some point, we should release a helper npm package that wraps the API and does:
30
+ * Discovery of what extension they have installed: PowerShell or PowerShell Preview
31
+ * Manages session id for you
32
+
33
+ */
34
+ export class ExternalApiFeature extends LanguageClientConsumer implements IPowerShellExtensionClient {
20
35
private static readonly registeredExternalExtension : Map < string , IExternalExtension > = new Map < string , IExternalExtension > ( ) ;
21
36
22
37
constructor ( private sessionManager : SessionManager , private log : Logger ) {
23
38
super ( ) ;
24
- this . commands = [
25
- /*
26
- DESCRIPTION:
27
- Registers your extension to allow usage of the external API. The returns
28
- a session UUID that will need to be passed in to subsequent API calls.
29
-
30
- USAGE:
31
- vscode.commands.executeCommand(
32
- "PowerShell.RegisterExternalExtension",
33
- "ms-vscode.PesterTestExplorer" // the name of the extension using us
34
- "v1"); // API Version.
35
-
36
- RETURNS:
37
- string session uuid
38
- */
39
- vscode . commands . registerCommand ( "PowerShell.RegisterExternalExtension" , ( id : string , apiVersion : string = 'v1' ) : string =>
40
- this . registerExternalExtension ( id , apiVersion ) ) ,
41
-
42
- /*
43
- DESCRIPTION:
44
- Unregisters a session that an extension has. This returns
45
- true if it succeeds or throws if it fails.
46
-
47
- USAGE:
48
- vscode.commands.executeCommand(
49
- "PowerShell.UnregisterExternalExtension",
50
- "uuid"); // the uuid from above for tracking purposes
51
-
52
- RETURNS:
53
- true if it worked, otherwise throws an error.
54
- */
55
- vscode . commands . registerCommand ( "PowerShell.UnregisterExternalExtension" , ( uuid : string = "" ) : boolean =>
56
- this . unregisterExternalExtension ( uuid ) ) ,
57
-
58
- /*
59
- DESCRIPTION:
60
- This will fetch the version details of the PowerShell used to start
61
- PowerShell Editor Services in the PowerShell extension.
62
-
63
- USAGE:
64
- vscode.commands.executeCommand(
65
- "PowerShell.GetPowerShellVersionDetails",
66
- "uuid"); // the uuid from above for tracking purposes
67
-
68
- RETURNS:
69
- An IPowerShellVersionDetails which consists of:
70
- {
71
- version: string;
72
- displayVersion: string;
73
- edition: string;
74
- architecture: string;
75
- }
76
- */
77
- vscode . commands . registerCommand ( "PowerShell.GetPowerShellVersionDetails" , ( uuid : string = "" ) : Promise < IExternalPowerShellDetails > =>
78
- this . getPowerShellVersionDetails ( uuid ) ) ,
79
- ]
80
39
}
81
40
82
- private registerExternalExtension ( id : string , apiVersion : string = 'v1' ) : string {
41
+ /*
42
+ DESCRIPTION:
43
+ Registers your extension to allow usage of the external API. The returns
44
+ a session UUID that will need to be passed in to subsequent API calls.
45
+
46
+ USAGE:
47
+ powerShellExtensionClient.registerExternalExtension(
48
+ "ms-vscode.PesterTestExplorer" // the name of the extension using us
49
+ "v1"); // API Version.
50
+
51
+ RETURNS:
52
+ string session uuid
53
+ */
54
+ public registerExternalExtension ( id : string , apiVersion : string = 'v1' ) : string {
83
55
this . log . writeDiagnostic ( `Registering extension '${ id } ' for use with API version '${ apiVersion } '.` ) ;
84
56
85
57
for ( const [ _ , externalExtension ] of ExternalApiFeature . registeredExternalExtension ) {
@@ -107,18 +79,48 @@ export class ExternalApiFeature extends LanguageClientConsumer {
107
79
return uuid ;
108
80
}
109
81
110
- private unregisterExternalExtension ( uuid : string = "" ) : boolean {
82
+ /*
83
+ DESCRIPTION:
84
+ Unregisters a session that an extension has. This returns
85
+ true if it succeeds or throws if it fails.
86
+
87
+ USAGE:
88
+ powerShellExtensionClient.unregisterExternalExtension(
89
+ "uuid"); // the uuid from above for tracking purposes
90
+
91
+ RETURNS:
92
+ true if it worked, otherwise throws an error.
93
+ */
94
+ public unregisterExternalExtension ( uuid : string = "" ) : boolean {
111
95
this . log . writeDiagnostic ( `Unregistering extension with session UUID: ${ uuid } ` ) ;
112
96
if ( ! ExternalApiFeature . registeredExternalExtension . delete ( uuid ) ) {
113
97
throw new Error ( `No extension registered with session UUID: ${ uuid } ` ) ;
114
98
}
115
99
return true ;
116
100
}
117
101
118
- private async getPowerShellVersionDetails ( uuid : string = "" ) : Promise < IExternalPowerShellDetails > {
102
+ /*
103
+ DESCRIPTION:
104
+ This will fetch the version details of the PowerShell used to start
105
+ PowerShell Editor Services in the PowerShell extension.
106
+
107
+ USAGE:
108
+ powerShellExtensionClient.getPowerShellVersionDetails(
109
+ "uuid"); // the uuid from above for tracking purposes
110
+
111
+ RETURNS:
112
+ An IPowerShellVersionDetails which consists of:
113
+ {
114
+ version: string;
115
+ displayVersion: string;
116
+ edition: string;
117
+ architecture: string;
118
+ }
119
+ */
120
+ public async getPowerShellVersionDetails ( uuid : string = "" ) : Promise < IExternalPowerShellDetails > {
119
121
if ( ! ExternalApiFeature . registeredExternalExtension . has ( uuid ) ) {
120
122
throw new Error (
121
- "UUID provided was invalid, make sure you execute the 'PowerShell.GetPowerShellVersionDetails' command and pass in the UUID that it returns to subsequent command executions ." ) ;
123
+ "UUID provided was invalid, make sure you ran the 'powershellExtensionClient.registerExternalExtension(extensionId)' method and pass in the UUID that it returns to subsequent methods ." ) ;
122
124
}
123
125
124
126
// TODO: When we have more than one API version, make sure to include a check here.
@@ -137,9 +139,7 @@ export class ExternalApiFeature extends LanguageClientConsumer {
137
139
}
138
140
139
141
public dispose ( ) {
140
- for ( const command of this . commands ) {
141
- command . dispose ( ) ;
142
- }
142
+ // Nothing to dispose.
143
143
}
144
144
}
145
145
0 commit comments