Path: blob/main/src/vs/platform/extensionManagement/common/unsupportedExtensionsMigration.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 { CancellationToken } from '../../../base/common/cancellation.js';6import { EXTENSION_INSTALL_SKIP_PUBLISHER_TRUST_CONTEXT, IExtensionGalleryService, IExtensionManagementService, IGlobalExtensionEnablementService, InstallOperation } from './extensionManagement.js';7import { areSameExtensions, getExtensionId } from './extensionManagementUtil.js';8import { IExtensionStorageService } from './extensionStorage.js';9import { ExtensionType } from '../../extensions/common/extensions.js';10import { ILogService } from '../../log/common/log.js';11import * as semver from '../../../base/common/semver/semver.js';1213/**14* Migrates the installed unsupported nightly extension to a supported pre-release extension. It includes following:15* - Uninstall the Unsupported extension16* - Install (with optional storage migration) the Pre-release extension only if17* - the extension is not installed18* - or it is a release version and the unsupported extension is enabled.19*/20export async function migrateUnsupportedExtensions(extensionManagementService: IExtensionManagementService, galleryService: IExtensionGalleryService, extensionStorageService: IExtensionStorageService, extensionEnablementService: IGlobalExtensionEnablementService, logService: ILogService): Promise<void> {21try {22const extensionsControlManifest = await extensionManagementService.getExtensionsControlManifest();23if (!extensionsControlManifest.deprecated) {24return;25}26const installed = await extensionManagementService.getInstalled(ExtensionType.User);27for (const [unsupportedExtensionId, deprecated] of Object.entries(extensionsControlManifest.deprecated)) {28if (!deprecated?.extension) {29continue;30}31const { id: preReleaseExtensionId, autoMigrate, preRelease } = deprecated.extension;32if (!autoMigrate) {33continue;34}35const unsupportedExtension = installed.find(i => areSameExtensions(i.identifier, { id: unsupportedExtensionId }));36// Unsupported Extension is not installed37if (!unsupportedExtension) {38continue;39}4041const gallery = (await galleryService.getExtensions([{ id: preReleaseExtensionId, preRelease }], { targetPlatform: await extensionManagementService.getTargetPlatform(), compatible: true }, CancellationToken.None))[0];42if (!gallery) {43logService.info(`Skipping migrating '${unsupportedExtension.identifier.id}' extension because, the comaptible target '${preReleaseExtensionId}' extension is not found`);44continue;45}4647try {48logService.info(`Migrating '${unsupportedExtension.identifier.id}' extension to '${preReleaseExtensionId}' extension...`);4950const isUnsupportedExtensionEnabled = !extensionEnablementService.getDisabledExtensions().some(e => areSameExtensions(e, unsupportedExtension.identifier));51await extensionManagementService.uninstall(unsupportedExtension);52logService.info(`Uninstalled the unsupported extension '${unsupportedExtension.identifier.id}'`);5354let preReleaseExtension = installed.find(i => areSameExtensions(i.identifier, { id: preReleaseExtensionId }));55if (!preReleaseExtension || (!preReleaseExtension.isPreReleaseVersion && isUnsupportedExtensionEnabled)) {56preReleaseExtension = await extensionManagementService.installFromGallery(gallery, { installPreReleaseVersion: true, isMachineScoped: unsupportedExtension.isMachineScoped, operation: InstallOperation.Migrate, context: { [EXTENSION_INSTALL_SKIP_PUBLISHER_TRUST_CONTEXT]: true } });57logService.info(`Installed the pre-release extension '${preReleaseExtension.identifier.id}'`);58if (!isUnsupportedExtensionEnabled) {59await extensionEnablementService.disableExtension(preReleaseExtension.identifier);60logService.info(`Disabled the pre-release extension '${preReleaseExtension.identifier.id}' because the unsupported extension '${unsupportedExtension.identifier.id}' is disabled`);61}62if (autoMigrate.storage) {63extensionStorageService.addToMigrationList(getExtensionId(unsupportedExtension.manifest.publisher, unsupportedExtension.manifest.name), getExtensionId(preReleaseExtension.manifest.publisher, preReleaseExtension.manifest.name));64logService.info(`Added pre-release extension to the storage migration list`);65}66}67logService.info(`Migrated '${unsupportedExtension.identifier.id}' extension to '${preReleaseExtensionId}' extension.`);68} catch (error) {69logService.error(error);70}71}7273if (extensionsControlManifest.autoUpdate) {74for (const [extensionId, version] of Object.entries(extensionsControlManifest.autoUpdate)) {75try {76const extensionToAutoUpdate = installed.find(i => areSameExtensions(i.identifier, { id: extensionId }) && semver.lte(i.manifest.version, version));77if (!extensionToAutoUpdate) {78continue;79}8081const gallery = (await galleryService.getExtensions([{ id: extensionId, preRelease: extensionToAutoUpdate.preRelease }], { targetPlatform: await extensionManagementService.getTargetPlatform(), compatible: true }, CancellationToken.None))[0];82if (!gallery) {83logService.info(`Skipping updating '${extensionToAutoUpdate.identifier.id}' extension because, the compatible target '${extensionId}' extension is not found`);84continue;85}8687await extensionManagementService.installFromGallery(gallery, { installPreReleaseVersion: extensionToAutoUpdate.preRelease, isMachineScoped: extensionToAutoUpdate.isMachineScoped, operation: InstallOperation.Update, context: { [EXTENSION_INSTALL_SKIP_PUBLISHER_TRUST_CONTEXT]: true } });88logService.info(`Autoupdated '${extensionToAutoUpdate.identifier.id}' extension to '${gallery.version}' extension.`);89} catch (error) {90logService.error(error);91}92}93}9495} catch (error) {96logService.error(error);97}98}99100101