This repository was archived by the owner on Apr 27, 2023. It is now read-only.
generated from hbenl/vscode-example-test-adapter
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpowershellExtension.ts
68 lines (57 loc) · 2.54 KB
/
powershellExtension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Eventually something like this would go in an npm package
import * as vscode from 'vscode';
export interface IExternalPowerShellDetails {
exePath: string;
version: string;
displayName: string;
architecture: string;
}
export interface IPowerShellExtensionClient {
registerExternalExtension(id: string, apiVersion?: string): string;
unregisterExternalExtension(uuid: string): boolean;
getPowerShellVersionDetails(uuid: string): Promise<IExternalPowerShellDetails>;
}
export class PowerShellExtensionClient {
private internalPowerShellExtensionClient: IPowerShellExtensionClient
constructor() {
const powershellExtension = vscode.extensions.getExtension<IPowerShellExtensionClient>("ms-vscode.PowerShell-Preview") || vscode.extensions.getExtension<IPowerShellExtensionClient>("ms-vscode.PowerShell");
this.ExtensionPath = powershellExtension?.extensionPath ?? "";
this.internalPowerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
}
private _sessionId: string | undefined;
private get sessionId(): string | undefined {
if (!this._sessionId) {
throw new Error("Client is not registered. You must run client.RegisterExtension(extensionId) first before using any other APIs.");
}
return this._sessionId;
}
private set sessionId(id: string | undefined) {
this._sessionId = id;
}
public readonly ExtensionPath: string;
public get IsConnected() {
return this._sessionId != null;
}
/**
* RegisterExtension
* https://github.com/PowerShell/vscode-powershell/blob/2d30df76eec42a600f97f2cc28105a9793c9821b/src/features/ExternalApi.ts#L25-L38
*/
public RegisterExtension(extensionId: string) {
this.sessionId = this.internalPowerShellExtensionClient.registerExternalExtension(extensionId);
}
/**
* UnregisterExtension
* https://github.com/PowerShell/vscode-powershell/blob/2d30df76eec42a600f97f2cc28105a9793c9821b/src/features/ExternalApi.ts#L42-L54
*/
public UnregisterExtension() {
this.internalPowerShellExtensionClient.unregisterExternalExtension(this.sessionId as string);
this.sessionId = undefined;
}
/**
* GetVersionDetails
* https://github.com/PowerShell/vscode-powershell/blob/master/src/features/ExternalApi.ts#L58-L76
*/
public GetVersionDetails(): Thenable<IExternalPowerShellDetails> {
return this.internalPowerShellExtensionClient.getPowerShellVersionDetails(this.sessionId as string);
}
}