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