Path: blob/main/src/vs/workbench/services/extensions/common/extensionStorageMigration.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 { getErrorMessage } from '../../../../base/common/errors.js';6import { URI } from '../../../../base/common/uri.js';7import { IEnvironmentService } from '../../../../platform/environment/common/environment.js';8import { IExtensionStorageService } from '../../../../platform/extensionManagement/common/extensionStorage.js';9import { FileSystemProviderError, FileSystemProviderErrorCode, IFileService } from '../../../../platform/files/common/files.js';10import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';11import { ILogService } from '../../../../platform/log/common/log.js';12import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';13import { IUriIdentityService } from '../../../../platform/uriIdentity/common/uriIdentity.js';14import { IUserDataProfilesService } from '../../../../platform/userDataProfile/common/userDataProfile.js';15import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';1617/**18* An extension storage has following19* - State: Stored using storage service with extension id as key and state as value.20* - Resources: Stored under a location scoped to the extension.21*/22export async function migrateExtensionStorage(fromExtensionId: string, toExtensionId: string, global: boolean, instantionService: IInstantiationService): Promise<void> {23return instantionService.invokeFunction(async serviceAccessor => {24const environmentService = serviceAccessor.get(IEnvironmentService);25const userDataProfilesService = serviceAccessor.get(IUserDataProfilesService);26const extensionStorageService = serviceAccessor.get(IExtensionStorageService);27const storageService = serviceAccessor.get(IStorageService);28const uriIdentityService = serviceAccessor.get(IUriIdentityService);29const fileService = serviceAccessor.get(IFileService);30const workspaceContextService = serviceAccessor.get(IWorkspaceContextService);31const logService = serviceAccessor.get(ILogService);32const storageMigratedKey = `extensionStorage.migrate.${fromExtensionId}-${toExtensionId}`;33const migrateLowerCaseStorageKey = fromExtensionId.toLowerCase() === toExtensionId.toLowerCase() ? `extension.storage.migrateFromLowerCaseKey.${fromExtensionId.toLowerCase()}` : undefined;3435if (fromExtensionId === toExtensionId) {36return;37}3839const getExtensionStorageLocation = (extensionId: string, global: boolean): URI => {40if (global) {41return uriIdentityService.extUri.joinPath(userDataProfilesService.defaultProfile.globalStorageHome, extensionId.toLowerCase() /* Extension id is lower cased for global storage */);42}43return uriIdentityService.extUri.joinPath(environmentService.workspaceStorageHome, workspaceContextService.getWorkspace().id, extensionId);44};4546const storageScope = global ? StorageScope.PROFILE : StorageScope.WORKSPACE;47if (!storageService.getBoolean(storageMigratedKey, storageScope, false) && !(migrateLowerCaseStorageKey && storageService.getBoolean(migrateLowerCaseStorageKey, storageScope, false))) {48logService.info(`Migrating ${global ? 'global' : 'workspace'} extension storage from ${fromExtensionId} to ${toExtensionId}...`);49// Migrate state50const value = extensionStorageService.getExtensionState(fromExtensionId, global);51if (value) {52extensionStorageService.setExtensionState(toExtensionId, value, global);53extensionStorageService.setExtensionState(fromExtensionId, undefined, global);54}5556// Migrate stored files57const fromPath = getExtensionStorageLocation(fromExtensionId, global);58const toPath = getExtensionStorageLocation(toExtensionId, global);59if (!uriIdentityService.extUri.isEqual(fromPath, toPath)) {60try {61await fileService.move(fromPath, toPath, true);62} catch (error) {63if ((<FileSystemProviderError>error).code !== FileSystemProviderErrorCode.FileNotFound) {64logService.info(`Error while migrating ${global ? 'global' : 'workspace'} file storage from '${fromExtensionId}' to '${toExtensionId}'`, getErrorMessage(error));65}66}67}68logService.info(`Migrated ${global ? 'global' : 'workspace'} extension storage from ${fromExtensionId} to ${toExtensionId}`);69storageService.store(storageMigratedKey, true, storageScope, StorageTarget.MACHINE);70}71});72}737475