Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support version for installExtension, support uninstall cmd #14298

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { DiffService } from '@theia/workspace/lib/browser/diff-service';
import { inject, injectable, optional } from '@theia/core/shared/inversify';
import { Position } from '@theia/plugin-ext/lib/common/plugin-api-rpc';
import { URI } from '@theia/core/shared/vscode-uri';
import { PluginServer } from '@theia/plugin-ext/lib/common/plugin-protocol';
import { PluginDeployOptions, PluginIdentifiers, PluginServer } from '@theia/plugin-ext/lib/common/plugin-protocol';
import { TerminalFrontendContribution } from '@theia/terminal/lib/browser/terminal-frontend-contribution';
import { QuickOpenWorkspace } from '@theia/workspace/lib/browser/quick-open-workspace';
import { TerminalService } from '@theia/terminal/lib/browser/base/terminal-service';
Expand Down Expand Up @@ -109,6 +109,10 @@ export namespace VscodeCommands {
export const INSTALL_FROM_VSIX: Command = {
id: 'workbench.extensions.installExtension'
};

export const UNINSTALL_EXTENSION: Command = {
id: 'workbench.extensions.uninstallExtension'
};
}

// https://wicg.github.io/webusb/
Expand Down Expand Up @@ -370,16 +374,36 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
commands.registerCommand({ id: 'workbench.files.action.refreshFilesExplorer' }, {
execute: () => commands.executeCommand(FileNavigatorCommands.REFRESH_NAVIGATOR.id)
});
commands.registerCommand({ id: VscodeCommands.INSTALL_FROM_VSIX.id }, {
commands.registerCommand(VscodeCommands.INSTALL_FROM_VSIX, {
execute: async (vsixUriOrExtensionId: TheiaURI | UriComponents | string) => {
if (typeof vsixUriOrExtensionId === 'string') {
await this.pluginServer.deploy(VSCodeExtensionUri.fromId(vsixUriOrExtensionId).toString());
let extensionId = vsixUriOrExtensionId;
let opts: PluginDeployOptions | undefined;
if (PluginIdentifiers.isVersionedId(vsixUriOrExtensionId)) {
const idAndVersion = PluginIdentifiers.getIdAndVersion(vsixUriOrExtensionId);
extensionId = idAndVersion[0];
opts = { version: idAndVersion[1]!, ignoreOtherVersions: true };
}
await this.pluginServer.deploy(VSCodeExtensionUri.fromId(extensionId).toString(), undefined, opts);
} else {
const uriPath = isUriComponents(vsixUriOrExtensionId) ? URI.revive(vsixUriOrExtensionId).fsPath : await this.fileService.fsPath(vsixUriOrExtensionId);
await this.pluginServer.deploy(`local-file:${uriPath}`);
}
}
});
commands.registerCommand(VscodeCommands.UNINSTALL_EXTENSION, {
execute: async (id: string) => {
if (!id) {
throw new Error(nls.localizeByDefault('Extension id required.'));
}
if (!PluginIdentifiers.isVersionedId(id)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this makes sense from a user's perspective: while technically, we can have multiple versions of the same plugin installed, that's not how it's presented to the user: for them, you either have a plugin installed, or not, independent of the version, so we should probably pass a naked id here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PluginServer.uninstall() expects PluginIdentifiers.VersionedId parameter which is typed as ${string}.${string}@${string} that why it the code above makes sense to me.

that's not how it's presented to the user: for them, you either have a plugin installed, or not

The UNINSTALL_EXTENSION command is not presented to the user, but is used in theia/packages/vsx-registry/src/browser/vsx-extension.tsx where the versioned extension id is given.
I would prefer to keep require versioned id here.

throw new Error(`Invalid extension id: ${id}\nExpected format: <publisher>.<name>@<version>.`);
}
const idAndVersion = PluginIdentifiers.identifiersFromVersionedId(id);
const pluginId = PluginIdentifiers.componentsToVersionedId(idAndVersion!);
await this.pluginServer.uninstall(pluginId);
}
});
commands.registerCommand({ id: 'workbench.action.files.save', }, {
execute: (uri?: monaco.Uri) => {
if (uri) {
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-ext-vscode/src/common/plugin-vscode-uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// *****************************************************************************

import URI from '@theia/core/lib/common/uri';
import { PluginIdentifiers } from '@theia/plugin-ext';

/**
* Static methods for identifying a plugin as the target of the VSCode deployment system.
Expand All @@ -32,7 +33,7 @@ export namespace VSCodeExtensionUri {
}

export function fromVersionedId(versionedId: string): URI {
const versionAndId = versionedId.split('@');
const versionAndId = PluginIdentifiers.getIdAndVersion(versionedId);
return fromId(versionAndId[0], versionAndId[1]);
}

Expand Down
37 changes: 37 additions & 0 deletions packages/plugin-ext/src/common/plugin-identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,41 @@ export namespace PluginIdentifiers {
}
return { id: probablyId.slice(0, endOfName) as UnversionedId, version: probablyId.slice(endOfName + 1) };
}

/**
* Regular expression to match plugin identifiers.
*
* The pattern matches strings in the format: `vendor.name@version`.
*
* Expected matching strings examples:
* - `vendor.name@prerelease`
* - `[email protected]`
* - `[email protected]`
*/
const EXTENSION_IDENTIFIER_WITH_VERSION_REGEX = /^([^.]+\..+)@((prerelease)|(\d+\.\d+\.\d+(-.*)?))$/;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessarily restrictve, IMO: all we need to know here is what is before the '@' and what is after the '@'. Why introduce the added complexity? Also, regexes should always have a comment that describes what they are supposed to detect, Because no-one understands Regexes at first sight. If the id does not match the expression, you end up with the exact same result as before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same RegEx that is also used in vs-code, I thought it is a good idea to do the same.
I will add a comment to describe what is expected by this RegEx.


/**
* Extracts the extension identifier and version from a string.
* @param id The extension identifier
* @returns A tuple of the extension identifier and the version, if present.
*/
export function getIdAndVersion(id: string): [string, string | undefined] {
const matches = EXTENSION_IDENTIFIER_WITH_VERSION_REGEX.exec(id);
if (matches && matches[1]) {
return [matches[1], matches[2]];
}
return [id, undefined];
}

/**
* Checks if the extension identifier is in the format `<publisher>.<name>@<version>`.
* @param id The extension identifier
* @returns `true` if the extension identifier is in the format `<publisher>.<name>@<version>`.
*/
export function isVersionedId(id: string): boolean {
const matches = EXTENSION_IDENTIFIER_WITH_VERSION_REGEX.exec(id);
// eslint-disable-next-line no-null/no-null
return matches !== null && matches.length > 2;
}

}
7 changes: 5 additions & 2 deletions packages/vsx-registry/src/browser/vsx-extension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { TreeElement, TreeElementNode } from '@theia/core/lib/browser/source-tre
import { OpenerService, open, OpenerOptions } from '@theia/core/lib/browser/opener-service';
import { HostedPluginSupport } from '@theia/plugin-ext/lib/hosted/browser/hosted-plugin';
import { PluginServer, DeployedPlugin, PluginType, PluginIdentifiers, PluginDeployOptions } from '@theia/plugin-ext/lib/common/plugin-protocol';
import { VscodeCommands } from '@theia/plugin-ext-vscode/lib/browser/plugin-vscode-commands-contribution';
import { VSCodeExtensionUri } from '@theia/plugin-ext-vscode/lib/common/plugin-vscode-uri';
import { ProgressService } from '@theia/core/lib/common/progress-service';
import { Endpoint } from '@theia/core/lib/browser/endpoint';
Expand Down Expand Up @@ -324,8 +325,10 @@ export class VSXExtension implements VSXExtensionData, TreeElement {
if (plugin) {
await this.progressService.withProgress(
nls.localizeByDefault('Uninstalling {0}...', this.id), 'extensions',
() => this.pluginServer.uninstall(PluginIdentifiers.componentsToVersionedId(plugin.metadata.model))
);
async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why go through the command here? Why stringify/destringify the id?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why go through the command here? Why stringify/destringify the id?

The implementation is moved to a command to resolve #13796 , now calling the new command to reduce code duplicates.

const versionedId = PluginIdentifiers.componentsToVersionedId(plugin.metadata.model);
await this.commandRegistry.executeCommand(VscodeCommands.UNINSTALL_EXTENSION.id, versionedId);
});
}
} finally {
this._busy--;
Expand Down
Loading