|
| 1 | +/********************************************************************** |
| 2 | + * Copyright (c) 2025 Red Hat, Inc. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made |
| 5 | + * available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + ***********************************************************************/ |
| 10 | +/* eslint-disable header/header */ |
| 11 | + |
| 12 | +import * as path from '../../../base/common/path.js'; |
| 13 | +import { VSBuffer } from '../../../base/common/buffer.js'; |
| 14 | +import { Disposable } from '../../../base/common/lifecycle.js'; |
| 15 | +import { URI } from '../../../base/common/uri.js'; |
| 16 | +import { IFileService } from '../../../platform/files/common/files.js'; |
| 17 | +import { INativeServerExtensionManagementService } from '../../../platform/extensionManagement/node/extensionManagementService.js'; |
| 18 | +import { ILogService } from '../../../platform/log/common/log.js'; |
| 19 | +import { IUserDataProfilesService } from '../../../platform/userDataProfile/common/userDataProfile.js'; |
| 20 | + |
| 21 | +export class DefaultExtensionsInstaller extends Disposable { |
| 22 | + |
| 23 | + constructor( |
| 24 | + private readonly extensionManagementService: INativeServerExtensionManagementService, |
| 25 | + private readonly logService: ILogService, |
| 26 | + private readonly fileService: IFileService, |
| 27 | + private readonly userDataProfilesService: IUserDataProfilesService, |
| 28 | + ) { |
| 29 | + super(); |
| 30 | + this.initialize().catch(error => { |
| 31 | + this.logService.error('Failed to initialize default extensions installer', error); |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + private async initialize(): Promise<void> { |
| 36 | + const defaultExtensionsEnv = typeof process !== 'undefined' && process.env ? process.env['DEFAULT_EXTENSIONS'] : undefined; |
| 37 | + if (!defaultExtensionsEnv) { |
| 38 | + this.logService.debug('DefaultExtensionsInstaller: DEFAULT_EXTENSIONS not set, skipping installation'); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + const extensionPaths = defaultExtensionsEnv.split(';').filter(p => p.trim()); |
| 43 | + if (extensionPaths.length === 0) { |
| 44 | + this.logService.debug('DefaultExtensionsInstaller: No extensions to install'); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + this.logService.info(`DefaultExtensionsInstaller: Found ${extensionPaths.length} default extension(s) in DEFAULT_EXTENSIONS`); |
| 49 | + |
| 50 | + // Track installed extensions in a file to avoid reinstalling |
| 51 | + const storageFile = this.getStorageFile(); |
| 52 | + let installedPaths: string[] = []; |
| 53 | + try { |
| 54 | + const content = await this.fileService.readFile(storageFile); |
| 55 | + installedPaths = JSON.parse(content.value.toString()); |
| 56 | + } catch (e) { |
| 57 | + // File doesn't exist or is invalid, start fresh |
| 58 | + installedPaths = []; |
| 59 | + } |
| 60 | + |
| 61 | + const pathsToInstall = extensionPaths.filter(p => !installedPaths.includes(p.trim())); |
| 62 | + if (pathsToInstall.length === 0) { |
| 63 | + this.logService.debug('DefaultExtensionsInstaller: All default extensions already installed'); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + this.logService.info(`DefaultExtensionsInstaller: Installing ${pathsToInstall.length} new default extension(s)`); |
| 68 | + await this.installExtensions(pathsToInstall, storageFile, installedPaths); |
| 69 | + } |
| 70 | + |
| 71 | + private async installExtensions( |
| 72 | + pathsToInstall: string[], |
| 73 | + storageFile: URI, |
| 74 | + installedPaths: string[] |
| 75 | + ): Promise<void> { |
| 76 | + const successfullyInstalled: string[] = []; |
| 77 | + |
| 78 | + for (const extensionPath of pathsToInstall) { |
| 79 | + const trimmedPath = extensionPath.trim(); |
| 80 | + if (!trimmedPath) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + try { |
| 85 | + const vsixUri = URI.file(trimmedPath); |
| 86 | + this.logService.info(`DefaultExtensionsInstaller: Installing extension from ${trimmedPath}`); |
| 87 | + |
| 88 | + await this.extensionManagementService.install(vsixUri, { |
| 89 | + donotIncludePackAndDependencies: true, |
| 90 | + isDefault: true // Mark as default extension to bypass policy checks |
| 91 | + }); |
| 92 | + successfullyInstalled.push(trimmedPath); |
| 93 | + this.logService.info(`DefaultExtensionsInstaller: Successfully installed extension from ${trimmedPath}`); |
| 94 | + } catch (error) { |
| 95 | + this.logService.error(`DefaultExtensionsInstaller: Failed to install extension from ${trimmedPath}`, error); |
| 96 | + // Continue with other extensions even if one fails |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Update storage with successfully installed extensions |
| 101 | + if (successfullyInstalled.length > 0) { |
| 102 | + const updatedInstalled = [...installedPaths, ...successfullyInstalled]; |
| 103 | + await this.fileService.writeFile(storageFile, VSBuffer.fromString(JSON.stringify(updatedInstalled, null, 2))); |
| 104 | + this.logService.info(`DefaultExtensionsInstaller: Updated storage with ${successfullyInstalled.length} successfully installed extension(s)`); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private getStorageFile(): URI { |
| 109 | + // Store in the same directory as the extensions profile manifest |
| 110 | + return this.userDataProfilesService.defaultProfile.extensionsResource.with({ |
| 111 | + path: path.join(path.dirname(this.userDataProfilesService.defaultProfile.extensionsResource.path), '.default-extensions-installed.json') |
| 112 | + }); |
| 113 | + } |
| 114 | +} |
| 115 | + |
0 commit comments