Skip to content

Commit 64fa257

Browse files
most of robs feedback
1 parent 53beb42 commit 64fa257

File tree

1 file changed

+66
-57
lines changed

1 file changed

+66
-57
lines changed

src/features/ExternalApi.ts

+66-57
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ import { IFeature } from "../feature";
88
import { Logger } from "../logging";
99
import { SessionManager } from "../session";
1010

11-
interface IExternalExtension {
12-
readonly id: string;
13-
readonly apiVersion: string;
14-
}
15-
1611
export interface IExternalPowerShellDetails {
1712
exePath: string;
1813
version: string;
@@ -41,33 +36,8 @@ export class ExternalApiFeature implements IFeature {
4136
RETURNS:
4237
string session uuid
4338
*/
44-
vscode.commands.registerCommand("PowerShell.RegisterExternalExtension", (id: string, apiVersion: string = 'v1'): string => {
45-
log.writeDiagnostic(`Registering extension '${id}' for use with API version '${apiVersion}'.`);
46-
47-
for (const [_, externalExtension] of ExternalApiFeature.registeredExternalExtension) {
48-
if (externalExtension.id === id) {
49-
const message = `The extension '${id}' is already registered.`;
50-
log.writeWarning(message);
51-
throw new Error(message);
52-
}
53-
}
54-
55-
if (!vscode.extensions.all.some(ext => ext.id === id)) {
56-
throw new Error(`No extension installed with id '${id}'. You must use a valid extension id.`);
57-
}
58-
59-
// If we're in development mode, we allow these to be used for testing purposes.
60-
if (!sessionManager.InDevelopmentMode && (id === "ms-vscode.PowerShell" || id === "ms-vscode.PowerShell-Preview")) {
61-
throw new Error("You can't use the PowerShell extension's id in this registration.");
62-
}
63-
64-
const uuid = uuidv4();
65-
ExternalApiFeature.registeredExternalExtension.set(uuid, {
66-
id,
67-
apiVersion
68-
});
69-
return uuid;
70-
}),
39+
vscode.commands.registerCommand("PowerShell.RegisterExternalExtension", (id: string, apiVersion: string = 'v1'): string =>
40+
this.registerExternalExtension(id, apiVersion)),
7141

7242
/*
7343
DESCRIPTION:
@@ -82,13 +52,8 @@ export class ExternalApiFeature implements IFeature {
8252
RETURNS:
8353
true if it worked, otherwise throws an error.
8454
*/
85-
vscode.commands.registerCommand("PowerShell.UnregisterExternalExtension", (uuid: string = ""): boolean => {
86-
log.writeDiagnostic(`Unregistering extension with session UUID: ${uuid}`);
87-
if (!ExternalApiFeature.registeredExternalExtension.delete(uuid)) {
88-
throw new Error(`No extension registered with session UUID: ${uuid}`);
89-
}
90-
return true;
91-
}),
55+
vscode.commands.registerCommand("PowerShell.UnregisterExternalExtension", (uuid: string = ""): boolean =>
56+
this.unregisterExternalExtension(uuid)),
9257

9358
/*
9459
DESCRIPTION:
@@ -109,27 +74,66 @@ export class ExternalApiFeature implements IFeature {
10974
architecture: string;
11075
}
11176
*/
112-
vscode.commands.registerCommand("PowerShell.GetPowerShellVersionDetails", async (uuid: string = ""): Promise<IExternalPowerShellDetails> => {
113-
if (!ExternalApiFeature.registeredExternalExtension.has(uuid)) {
114-
throw new Error(
115-
"UUID provided was invalid, make sure you execute the 'PowerShell.GetPowerShellVersionDetails' command and pass in the UUID that it returns to subsequent command executions.");
116-
}
77+
vscode.commands.registerCommand("PowerShell.GetPowerShellVersionDetails", (uuid: string = ""): Promise<IExternalPowerShellDetails> =>
78+
this.getPowerShellVersionDetails(uuid)),
79+
]
80+
}
11781

118-
// TODO: When we have more than one API version, make sure to include a check here.
119-
const extension = ExternalApiFeature.registeredExternalExtension.get(uuid);
120-
log.writeDiagnostic(`Extension '${extension.id}' used command 'PowerShell.GetPowerShellVersionDetails'.`);
82+
private registerExternalExtension(id: string, apiVersion: string = 'v1'): string {
83+
this.log.writeDiagnostic(`Registering extension '${id}' for use with API version '${apiVersion}'.`);
12184

122-
await sessionManager.waitUntilStarted();
123-
const versionDetails = sessionManager.getPowerShellVersionDetails();
85+
for (const [_, externalExtension] of ExternalApiFeature.registeredExternalExtension) {
86+
if (externalExtension.id === id) {
87+
const message = `The extension '${id}' is already registered.`;
88+
this.log.writeWarning(message);
89+
throw new Error(message);
90+
}
91+
}
12492

125-
return {
126-
exePath: sessionManager.PowerShellExeDetails.exePath,
127-
version: versionDetails.version,
128-
displayName: sessionManager.PowerShellExeDetails.displayName, // comes from the Session Menu
129-
architecture: versionDetails.architecture
130-
};
131-
}),
132-
]
93+
if (!vscode.extensions.all.some(ext => ext.id === id)) {
94+
throw new Error(`No extension installed with id '${id}'. You must use a valid extension id.`);
95+
}
96+
97+
// If we're in development mode, we allow these to be used for testing purposes.
98+
if (!this.sessionManager.InDevelopmentMode && (id === "ms-vscode.PowerShell" || id === "ms-vscode.PowerShell-Preview")) {
99+
throw new Error("You can't use the PowerShell extension's id in this registration.");
100+
}
101+
102+
const uuid = uuidv4();
103+
ExternalApiFeature.registeredExternalExtension.set(uuid, {
104+
id,
105+
apiVersion
106+
});
107+
return uuid;
108+
}
109+
110+
private unregisterExternalExtension(uuid: string = ""): boolean {
111+
this.log.writeDiagnostic(`Unregistering extension with session UUID: ${uuid}`);
112+
if (!ExternalApiFeature.registeredExternalExtension.delete(uuid)) {
113+
throw new Error(`No extension registered with session UUID: ${uuid}`);
114+
}
115+
return true;
116+
}
117+
118+
private async getPowerShellVersionDetails(uuid: string = ""): Promise<IExternalPowerShellDetails> {
119+
if (!ExternalApiFeature.registeredExternalExtension.has(uuid)) {
120+
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.");
122+
}
123+
124+
// TODO: When we have more than one API version, make sure to include a check here.
125+
const extension = ExternalApiFeature.registeredExternalExtension.get(uuid);
126+
this.log.writeDiagnostic(`Extension '${extension.id}' used command 'PowerShell.GetPowerShellVersionDetails'.`);
127+
128+
await this.sessionManager.waitUntilStarted();
129+
const versionDetails = this.sessionManager.getPowerShellVersionDetails();
130+
131+
return {
132+
exePath: this.sessionManager.PowerShellExeDetails.exePath,
133+
version: versionDetails.version,
134+
displayName: this.sessionManager.PowerShellExeDetails.displayName, // comes from the Session Menu
135+
architecture: versionDetails.architecture
136+
};
133137
}
134138

135139
public dispose() {
@@ -142,3 +146,8 @@ export class ExternalApiFeature implements IFeature {
142146
this.languageClient = languageclient;
143147
}
144148
}
149+
150+
interface IExternalExtension {
151+
readonly id: string;
152+
readonly apiVersion: string;
153+
}

0 commit comments

Comments
 (0)