Path: blob/main/src/vs/workbench/api/browser/mainThreadCLICommands.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Schemas } from '../../../base/common/network.js';6import { isWeb } from '../../../base/common/platform.js';7import { isString } from '../../../base/common/types.js';8import { URI, UriComponents } from '../../../base/common/uri.js';9import { localize } from '../../../nls.js';10import { CommandsRegistry, ICommandService } from '../../../platform/commands/common/commands.js';11import { IExtensionGalleryService, IExtensionManagementService } from '../../../platform/extensionManagement/common/extensionManagement.js';12import { ExtensionManagementCLI } from '../../../platform/extensionManagement/common/extensionManagementCLI.js';13import { getExtensionId } from '../../../platform/extensionManagement/common/extensionManagementUtil.js';14import { IExtensionManifest } from '../../../platform/extensions/common/extensions.js';15import { IInstantiationService, ServicesAccessor } from '../../../platform/instantiation/common/instantiation.js';16import { ServiceCollection } from '../../../platform/instantiation/common/serviceCollection.js';17import { ILabelService } from '../../../platform/label/common/label.js';18import { AbstractMessageLogger, ILogger, LogLevel } from '../../../platform/log/common/log.js';19import { IOpenerService } from '../../../platform/opener/common/opener.js';20import { IOpenWindowOptions, IWindowOpenable } from '../../../platform/window/common/window.js';21import { IWorkbenchEnvironmentService } from '../../services/environment/common/environmentService.js';22import { IExtensionManagementServerService } from '../../services/extensionManagement/common/extensionManagement.js';23import { IExtensionManifestPropertiesService } from '../../services/extensions/common/extensionManifestPropertiesService.js';242526// this class contains the commands that the CLI server is reying on2728CommandsRegistry.registerCommand('_remoteCLI.openExternal', function (accessor: ServicesAccessor, uri: UriComponents | string): Promise<boolean> {29const openerService = accessor.get(IOpenerService);30return openerService.open(isString(uri) ? uri : URI.revive(uri), { openExternal: true, allowTunneling: true });31});3233CommandsRegistry.registerCommand('_remoteCLI.windowOpen', function (accessor: ServicesAccessor, toOpen: IWindowOpenable[], options: IOpenWindowOptions) {34const commandService = accessor.get(ICommandService);35if (!toOpen.length) {36return commandService.executeCommand('_files.newWindow', options);37}38return commandService.executeCommand('_files.windowOpen', toOpen, options);39});4041CommandsRegistry.registerCommand('_remoteCLI.getSystemStatus', function (accessor: ServicesAccessor): Promise<string | undefined> {42const commandService = accessor.get(ICommandService);43return commandService.executeCommand<string>('_issues.getSystemStatus');44});4546interface ManageExtensionsArgs {47list?: { showVersions?: boolean; category?: string };48install?: (string | URI)[];49uninstall?: string[];50force?: boolean;51}5253CommandsRegistry.registerCommand('_remoteCLI.manageExtensions', async function (accessor: ServicesAccessor, args: ManageExtensionsArgs): Promise<string | undefined> {54const instantiationService = accessor.get(IInstantiationService);55const extensionManagementServerService = accessor.get(IExtensionManagementServerService);56const remoteExtensionManagementService = extensionManagementServerService.remoteExtensionManagementServer?.extensionManagementService;57if (!remoteExtensionManagementService) {58return;59}6061const lines: string[] = [];62const logger = new class extends AbstractMessageLogger {63protected override log(level: LogLevel, message: string): void {64lines.push(message);65}66}();67const childInstantiationService = instantiationService.createChild(new ServiceCollection([IExtensionManagementService, remoteExtensionManagementService]));68try {69const cliService = childInstantiationService.createInstance(RemoteExtensionManagementCLI, logger);7071if (args.list) {72await cliService.listExtensions(!!args.list.showVersions, args.list.category, undefined);73} else {74const revive = (inputs: (string | UriComponents)[]) => inputs.map(input => isString(input) ? input : URI.revive(input));75if (Array.isArray(args.install) && args.install.length) {76try {77await cliService.installExtensions(revive(args.install), [], { isMachineScoped: true }, !!args.force);78} catch (e) {79lines.push(e.message);80}81}82if (Array.isArray(args.uninstall) && args.uninstall.length) {83try {84await cliService.uninstallExtensions(revive(args.uninstall), !!args.force, undefined);85} catch (e) {86lines.push(e.message);87}88}89}90return lines.join('\n');91} finally {92childInstantiationService.dispose();93}9495});9697class RemoteExtensionManagementCLI extends ExtensionManagementCLI {9899private _location: string | undefined;100101constructor(102logger: ILogger,103@IExtensionManagementService extensionManagementService: IExtensionManagementService,104@IExtensionGalleryService extensionGalleryService: IExtensionGalleryService,105@ILabelService labelService: ILabelService,106@IWorkbenchEnvironmentService envService: IWorkbenchEnvironmentService,107@IExtensionManifestPropertiesService private readonly _extensionManifestPropertiesService: IExtensionManifestPropertiesService,108) {109super(logger, extensionManagementService, extensionGalleryService);110111const remoteAuthority = envService.remoteAuthority;112this._location = remoteAuthority ? labelService.getHostLabel(Schemas.vscodeRemote, remoteAuthority) : undefined;113}114115protected override get location(): string | undefined {116return this._location;117}118119protected override validateExtensionKind(manifest: IExtensionManifest): boolean {120if (!this._extensionManifestPropertiesService.canExecuteOnWorkspace(manifest)121// Web extensions installed on remote can be run in web worker extension host122&& !(isWeb && this._extensionManifestPropertiesService.canExecuteOnWeb(manifest))) {123this.logger.info(localize('cannot be installed', "Cannot install the '{0}' extension because it is declared to not run in this setup.", getExtensionId(manifest.publisher, manifest.name)));124return false;125}126return true;127}128}129130131