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