Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/browser/mainThreadCLICommands.ts
5225 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Schemas } from '../../../base/common/network.js';
7
import { isWeb } from '../../../base/common/platform.js';
8
import { isString } from '../../../base/common/types.js';
9
import { URI, UriComponents } from '../../../base/common/uri.js';
10
import { localize } from '../../../nls.js';
11
import { CommandsRegistry, ICommandService } from '../../../platform/commands/common/commands.js';
12
import { IExtensionGalleryService, IExtensionManagementService } from '../../../platform/extensionManagement/common/extensionManagement.js';
13
import { ExtensionManagementCLI } from '../../../platform/extensionManagement/common/extensionManagementCLI.js';
14
import { getExtensionId } from '../../../platform/extensionManagement/common/extensionManagementUtil.js';
15
import { IExtensionManifest } from '../../../platform/extensions/common/extensions.js';
16
import { IInstantiationService, ServicesAccessor } from '../../../platform/instantiation/common/instantiation.js';
17
import { ServiceCollection } from '../../../platform/instantiation/common/serviceCollection.js';
18
import { ILabelService } from '../../../platform/label/common/label.js';
19
import { AbstractMessageLogger, ILogger, LogLevel } from '../../../platform/log/common/log.js';
20
import { IOpenerService } from '../../../platform/opener/common/opener.js';
21
import { IProductService } from '../../../platform/product/common/productService.js';
22
import { IOpenWindowOptions, IWindowOpenable } from '../../../platform/window/common/window.js';
23
import { IWorkbenchEnvironmentService } from '../../services/environment/common/environmentService.js';
24
import { IExtensionManagementServerService } from '../../services/extensionManagement/common/extensionManagement.js';
25
import { IExtensionManifestPropertiesService } from '../../services/extensions/common/extensionManifestPropertiesService.js';
26
27
28
// this class contains the commands that the CLI server is reying on
29
30
CommandsRegistry.registerCommand('_remoteCLI.openExternal', function (accessor: ServicesAccessor, uri: UriComponents | string): Promise<boolean> {
31
const openerService = accessor.get(IOpenerService);
32
return openerService.open(isString(uri) ? uri : URI.revive(uri), { openExternal: true, allowTunneling: true });
33
});
34
35
CommandsRegistry.registerCommand('_remoteCLI.windowOpen', function (accessor: ServicesAccessor, toOpen: IWindowOpenable[], options: IOpenWindowOptions) {
36
const commandService = accessor.get(ICommandService);
37
if (!toOpen.length) {
38
return commandService.executeCommand('_files.newWindow', options);
39
}
40
return commandService.executeCommand('_files.windowOpen', toOpen, options);
41
});
42
43
CommandsRegistry.registerCommand('_remoteCLI.getSystemStatus', function (accessor: ServicesAccessor): Promise<string | undefined> {
44
const commandService = accessor.get(ICommandService);
45
return commandService.executeCommand<string>('_issues.getSystemStatus');
46
});
47
48
interface ManageExtensionsArgs {
49
list?: { showVersions?: boolean; category?: string };
50
install?: (string | URI)[];
51
uninstall?: string[];
52
force?: boolean;
53
}
54
55
CommandsRegistry.registerCommand('_remoteCLI.manageExtensions', async function (accessor: ServicesAccessor, args: ManageExtensionsArgs): Promise<string | undefined> {
56
const instantiationService = accessor.get(IInstantiationService);
57
const extensionManagementServerService = accessor.get(IExtensionManagementServerService);
58
const remoteExtensionManagementService = extensionManagementServerService.remoteExtensionManagementServer?.extensionManagementService;
59
if (!remoteExtensionManagementService) {
60
return;
61
}
62
63
const lines: string[] = [];
64
const logger = new class extends AbstractMessageLogger {
65
protected override log(level: LogLevel, message: string): void {
66
lines.push(message);
67
}
68
}();
69
const childInstantiationService = instantiationService.createChild(new ServiceCollection([IExtensionManagementService, remoteExtensionManagementService]));
70
try {
71
const cliService = childInstantiationService.createInstance(RemoteExtensionManagementCLI, logger);
72
73
if (args.list) {
74
await cliService.listExtensions(!!args.list.showVersions, args.list.category, undefined);
75
} else {
76
const revive = (inputs: (string | UriComponents)[]) => inputs.map(input => isString(input) ? input : URI.revive(input));
77
if (Array.isArray(args.install) && args.install.length) {
78
try {
79
await cliService.installExtensions(revive(args.install), [], { isMachineScoped: true }, !!args.force);
80
} catch (e) {
81
lines.push(e.message);
82
}
83
}
84
if (Array.isArray(args.uninstall) && args.uninstall.length) {
85
try {
86
await cliService.uninstallExtensions(revive(args.uninstall), !!args.force, undefined);
87
} catch (e) {
88
lines.push(e.message);
89
}
90
}
91
}
92
return lines.join('\n');
93
} finally {
94
childInstantiationService.dispose();
95
}
96
97
});
98
99
class RemoteExtensionManagementCLI extends ExtensionManagementCLI {
100
101
private _location: string | undefined;
102
103
constructor(
104
logger: ILogger,
105
@IExtensionManagementService extensionManagementService: IExtensionManagementService,
106
@IExtensionGalleryService extensionGalleryService: IExtensionGalleryService,
107
@ILabelService labelService: ILabelService,
108
@IWorkbenchEnvironmentService envService: IWorkbenchEnvironmentService,
109
@IExtensionManifestPropertiesService private readonly _extensionManifestPropertiesService: IExtensionManifestPropertiesService,
110
@IProductService productService: IProductService,
111
) {
112
super([], logger, extensionManagementService, extensionGalleryService, productService);
113
114
const remoteAuthority = envService.remoteAuthority;
115
this._location = remoteAuthority ? labelService.getHostLabel(Schemas.vscodeRemote, remoteAuthority) : undefined;
116
}
117
118
protected override get location(): string | undefined {
119
return this._location;
120
}
121
122
protected override validateExtensionKind(manifest: IExtensionManifest): boolean {
123
if (!this._extensionManifestPropertiesService.canExecuteOnWorkspace(manifest)
124
// Web extensions installed on remote can be run in web worker extension host
125
&& !(isWeb && this._extensionManifestPropertiesService.canExecuteOnWeb(manifest))) {
126
this.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)));
127
return false;
128
}
129
return true;
130
}
131
}
132
133