| 36 | |
| 37 | */ |
| 38 | export class ExternalApiFeature implements IPowerShellExtensionClient { |
| 39 | private static readonly registeredExternalExtension: Map< |
| 40 | string, |
| 41 | IExternalExtension |
| 42 | > = new Map<string, IExternalExtension>(); |
| 43 | |
| 44 | constructor( |
| 45 | private extensionContext: vscode.ExtensionContext, |
| 46 | private sessionManager: SessionManager, |
| 47 | private logger: ILogger, |
| 48 | ) {} |
| 49 | |
| 50 | /* |
| 51 | DESCRIPTION: |
| 52 | Registers your extension to allow usage of the external API. The returns |
| 53 | a session UUID that will need to be passed in to subsequent API calls. |
| 54 | |
| 55 | USAGE: |
| 56 | powerShellExtensionClient.registerExternalExtension( |
| 57 | "ms-vscode.PesterTestExplorer" // the name of the extension using us |
| 58 | "v1"); // API Version. |
| 59 | |
| 60 | RETURNS: |
| 61 | string session uuid |
| 62 | */ |
| 63 | public registerExternalExtension(id: string, apiVersion = "v1"): string { |
| 64 | this.logger.writeDebug( |
| 65 | `Registering extension '${id}' for use with API version '${apiVersion}'.`, |
| 66 | ); |
| 67 | |
| 68 | for (const [ |
| 69 | _name, |
| 70 | externalExtension, |
| 71 | ] of ExternalApiFeature.registeredExternalExtension) { |
| 72 | if (externalExtension.id === id) { |
| 73 | const message = `The extension '${id}' is already registered.`; |
| 74 | this.logger.writeWarning(message); |
| 75 | throw new Error(message); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (!vscode.extensions.all.some((ext) => ext.id === id)) { |
| 80 | throw new Error( |
| 81 | `No extension installed with id '${id}'. You must use a valid extension id.`, |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | // Our ID is only only allowed to be used in our unit tests. |
| 86 | if ( |
| 87 | id === "ms-vscode.powershell" && |
| 88 | !(this.extensionContext.extensionMode === vscode.ExtensionMode.Test) |
| 89 | ) { |
| 90 | throw new Error( |
| 91 | "You can't use the PowerShell extension's id in this registration.", |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | const uuid = uuidv4(); |
nothing calls this directly
no outgoing calls
no test coverage detected