|
| 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 { Disposable, DisposableStore, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle'; |
| 7 | +import { URI } from 'vs/base/common/uri'; |
| 8 | +import { IExtensionGalleryService, IExtensionIdentifier, IGlobalExtensionEnablementService, DidUninstallExtensionEvent, InstallExtensionResult, UninstallOptions } from 'vs/platform/extensionManagement/common/extensionManagement'; |
| 9 | +import { getIdAndVersion } from 'vs/platform/extensionManagement/common/extensionManagementUtil'; |
| 10 | +import { IExtensionsProfileScannerService } from 'vs/platform/extensionManagement/common/extensionsProfileScannerService'; |
| 11 | +import { ExtensionStorageService, IExtensionStorageService } from 'vs/platform/extensionManagement/common/extensionStorage'; |
| 12 | +import { migrateUnsupportedExtensions } from 'vs/platform/extensionManagement/common/unsupportedExtensionsMigration'; |
| 13 | +import { INativeServerExtensionManagementService } from 'vs/platform/extensionManagement/node/extensionManagementService'; |
| 14 | +import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions'; |
| 15 | +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; |
| 16 | +import { ILogService } from 'vs/platform/log/common/log'; |
| 17 | +import { IStorageService } from 'vs/platform/storage/common/storage'; |
| 18 | +import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity'; |
| 19 | +import { DidChangeProfilesEvent, IUserDataProfilesService } from 'vs/platform/userDataProfile/common/userDataProfile'; |
| 20 | + |
| 21 | +const uninstalOptions: UninstallOptions = { versionOnly: true, donotIncludePack: true, donotCheckDependents: true }; |
| 22 | + |
| 23 | +export class ExtensionsCleaner extends Disposable { |
| 24 | + |
| 25 | + constructor( |
| 26 | + @INativeServerExtensionManagementService extensionManagementService: INativeServerExtensionManagementService, |
| 27 | + @IUserDataProfilesService private readonly userDataProfilesService: IUserDataProfilesService, |
| 28 | + @IExtensionGalleryService extensionGalleryService: IExtensionGalleryService, |
| 29 | + @IExtensionStorageService extensionStorageService: IExtensionStorageService, |
| 30 | + @IGlobalExtensionEnablementService extensionEnablementService: IGlobalExtensionEnablementService, |
| 31 | + @IInstantiationService instantiationService: IInstantiationService, |
| 32 | + @IStorageService storageService: IStorageService, |
| 33 | + @ILogService logService: ILogService, |
| 34 | + ) { |
| 35 | + super(); |
| 36 | + |
| 37 | + extensionManagementService.removeUninstalledExtensions(this.userDataProfilesService.profiles.length === 1); |
| 38 | + migrateUnsupportedExtensions(extensionManagementService, extensionGalleryService, extensionStorageService, extensionEnablementService, logService); |
| 39 | + ExtensionStorageService.removeOutdatedExtensionVersions(extensionManagementService, storageService); |
| 40 | + this._register(instantiationService.createInstance(ProfileExtensionsCleaner)); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +class ProfileExtensionsCleaner extends Disposable { |
| 45 | + |
| 46 | + private profileExtensionsLocations = new Map<string, URI[]>; |
| 47 | + |
| 48 | + private readonly profileModeDisposables = this._register(new MutableDisposable<DisposableStore>()); |
| 49 | + |
| 50 | + constructor( |
| 51 | + @INativeServerExtensionManagementService private readonly extensionManagementService: INativeServerExtensionManagementService, |
| 52 | + @IUserDataProfilesService private readonly userDataProfilesService: IUserDataProfilesService, |
| 53 | + @IExtensionsProfileScannerService private readonly extensionsProfileScannerService: IExtensionsProfileScannerService, |
| 54 | + @IUriIdentityService private readonly uriIdentityService: IUriIdentityService, |
| 55 | + @ILogService private readonly logService: ILogService, |
| 56 | + ) { |
| 57 | + super(); |
| 58 | + this.onDidChangeProfiles({ added: this.userDataProfilesService.profiles, removed: [], all: this.userDataProfilesService.profiles }); |
| 59 | + } |
| 60 | + |
| 61 | + private async onDidChangeProfiles({ added, removed, all }: Omit<DidChangeProfilesEvent, 'updated'>): Promise<void> { |
| 62 | + try { |
| 63 | + await Promise.all(removed.map(profile => profile.extensionsResource ? this.removeExtensionsFromProfile(profile.extensionsResource) : Promise.resolve())); |
| 64 | + } catch (error) { |
| 65 | + this.logService.error(error); |
| 66 | + } |
| 67 | + |
| 68 | + if (all.length === 1) { |
| 69 | + // Exit profile mode |
| 70 | + this.profileModeDisposables.clear(); |
| 71 | + // Listen for entering into profile mode |
| 72 | + const disposable = this._register(this.userDataProfilesService.onDidChangeProfiles(() => { |
| 73 | + disposable.dispose(); |
| 74 | + this.onDidChangeProfiles({ added: this.userDataProfilesService.profiles, removed: [], all: this.userDataProfilesService.profiles }); |
| 75 | + })); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + if (added.length) { |
| 81 | + await Promise.all(added.map(profile => profile.extensionsResource ? this.populateExtensionsFromProfile(profile.extensionsResource) : Promise.resolve())); |
| 82 | + // Enter profile mode |
| 83 | + if (!this.profileModeDisposables.value) { |
| 84 | + this.profileModeDisposables.value = new DisposableStore(); |
| 85 | + this.profileModeDisposables.value.add(toDisposable(() => this.profileExtensionsLocations.clear())); |
| 86 | + this.profileModeDisposables.value.add(this.userDataProfilesService.onDidChangeProfiles(e => this.onDidChangeProfiles(e))); |
| 87 | + this.profileModeDisposables.value.add(this.extensionManagementService.onDidInstallExtensions(e => this.onDidInstallExtensions(e))); |
| 88 | + this.profileModeDisposables.value.add(this.extensionManagementService.onDidUninstallExtension(e => this.onDidUninstallExtension(e))); |
| 89 | + await this.uninstallExtensionsNotInProfiles(); |
| 90 | + } |
| 91 | + } |
| 92 | + } catch (error) { |
| 93 | + this.logService.error(error); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private async uninstallExtensionsNotInProfiles(): Promise<void> { |
| 98 | + const installed = await this.extensionManagementService.getAllUserInstalled(); |
| 99 | + const toUninstall = installed.filter(installedExtension => installedExtension.installedTimestamp /* Installed by VS Code */ && !this.profileExtensionsLocations.has(this.getKey(installedExtension.identifier, installedExtension.manifest.version))); |
| 100 | + if (toUninstall.length) { |
| 101 | + await Promise.all(toUninstall.map(extension => this.extensionManagementService.uninstall(extension, uninstalOptions))); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private async onDidInstallExtensions(installedExtensions: readonly InstallExtensionResult[]): Promise<void> { |
| 106 | + for (const { local, profileLocation } of installedExtensions) { |
| 107 | + if (!local || !profileLocation) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + this.addExtensionWithKey(this.getKey(local.identifier, local.manifest.version), profileLocation); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private async onDidUninstallExtension(e: DidUninstallExtensionEvent): Promise<void> { |
| 115 | + if (!e.profileLocation || !e.version) { |
| 116 | + return; |
| 117 | + } |
| 118 | + if (this.removeExtensionWithKey(this.getKey(e.identifier, e.version), e.profileLocation)) { |
| 119 | + await this.uninstallExtensions([{ identifier: e.identifier, version: e.version }]); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private async populateExtensionsFromProfile(extensionsProfileLocation: URI): Promise<void> { |
| 124 | + const extensions = await this.extensionsProfileScannerService.scanProfileExtensions(extensionsProfileLocation); |
| 125 | + for (const extension of extensions) { |
| 126 | + this.addExtensionWithKey(this.getKey(extension.identifier, extension.version), extensionsProfileLocation); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private async removeExtensionsFromProfile(removedProfile: URI): Promise<void> { |
| 131 | + const extensionsToRemove: { identifier: IExtensionIdentifier; version: string }[] = []; |
| 132 | + for (const key of [...this.profileExtensionsLocations.keys()]) { |
| 133 | + if (!this.removeExtensionWithKey(key, removedProfile)) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + const extensionToRemove = this.fromKey(key); |
| 137 | + if (extensionToRemove) { |
| 138 | + extensionsToRemove.push(extensionToRemove); |
| 139 | + } |
| 140 | + } |
| 141 | + if (extensionsToRemove.length) { |
| 142 | + await this.uninstallExtensions(extensionsToRemove); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private addExtensionWithKey(key: string, extensionsProfileLocation: URI): void { |
| 147 | + let locations = this.profileExtensionsLocations.get(key); |
| 148 | + if (!locations) { |
| 149 | + locations = []; |
| 150 | + this.profileExtensionsLocations.set(key, locations); |
| 151 | + } |
| 152 | + locations.push(extensionsProfileLocation); |
| 153 | + } |
| 154 | + |
| 155 | + private removeExtensionWithKey(key: string, profileLocation: URI): boolean { |
| 156 | + const profiles = this.profileExtensionsLocations.get(key); |
| 157 | + if (profiles) { |
| 158 | + const index = profiles.findIndex(profile => this.uriIdentityService.extUri.isEqual(profile, profileLocation)); |
| 159 | + if (index > -1) { |
| 160 | + profiles.splice(index, 1); |
| 161 | + } |
| 162 | + } |
| 163 | + if (!profiles?.length) { |
| 164 | + this.profileExtensionsLocations.delete(key); |
| 165 | + return true; |
| 166 | + } |
| 167 | + return false; |
| 168 | + } |
| 169 | + |
| 170 | + private async uninstallExtensions(extensionsToRemove: { identifier: IExtensionIdentifier; version: string }[]): Promise<void> { |
| 171 | + const installed = await this.extensionManagementService.getAllUserInstalled(); |
| 172 | + const toUninstall = installed.filter(installedExtension => installedExtension.installedTimestamp /* Installed by VS Code */ && extensionsToRemove.some(e => this.getKey(installedExtension.identifier, installedExtension.manifest.version) === this.getKey(e.identifier, e.version))); |
| 173 | + if (toUninstall.length) { |
| 174 | + await Promise.all(toUninstall.map(extension => this.extensionManagementService.uninstall(extension, uninstalOptions))); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private getKey(identifier: IExtensionIdentifier, version: string): string { |
| 179 | + return `${ExtensionIdentifier.toKey(identifier.id)}@${version}`; |
| 180 | + } |
| 181 | + |
| 182 | + private fromKey(key: string): { identifier: IExtensionIdentifier; version: string } | undefined { |
| 183 | + const [id, version] = getIdAndVersion(key); |
| 184 | + return version ? { identifier: { id }, version } : undefined; |
| 185 | + } |
| 186 | +} |
0 commit comments