|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +// Implementation inspired by https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/c864f15af264f0f456a6d8a83290b5c940715349/client/vscode/src/settings/uninstall.ts#L2 |
| 7 | + |
| 8 | +import * as vscode from 'vscode' |
| 9 | +import { join } from 'path' |
| 10 | +import { getLogger } from './logger/logger' |
| 11 | +import { telemetry } from './telemetry' |
| 12 | +import { VSCODE_EXTENSION_ID } from './extensions' |
| 13 | +import { extensionVersion } from './vscode/env' |
| 14 | + |
| 15 | +/** |
| 16 | + * Checks if the extension has been uninstalled by reading the .obsolete file |
| 17 | + * and comparing the number of obsolete extensions with the installed extensions. |
| 18 | + * |
| 19 | + * @param {string} extensionName - The name of the extension. |
| 20 | + * @param {string} extensionsDirPath - The path to the extensions directory. |
| 21 | + * @param {string} obsoleteFilePath - The path to the .obsolete file. |
| 22 | + * @param {function} callback - Action performed when extension is uninstalled. |
| 23 | + * @returns {void} |
| 24 | + */ |
| 25 | +async function checkExtensionUninstall( |
| 26 | + extensionName: typeof VSCODE_EXTENSION_ID.awstoolkit | typeof VSCODE_EXTENSION_ID.amazonq, |
| 27 | + extensionsDirPath: string, |
| 28 | + obsoleteFilePath: string, |
| 29 | + callback: () => Promise<void> |
| 30 | +): Promise<void> { |
| 31 | + /** |
| 32 | + * Users can have multiple profiles with different versions of the extensions. |
| 33 | + * |
| 34 | + * This makes sure the callback is triggered only when an explicit extension with specific version is uninstalled. |
| 35 | + */ |
| 36 | + const extension = `${extensionName}-${extensionVersion}` |
| 37 | + try { |
| 38 | + const [obsoleteFileContent, extensionsDirContent] = await Promise.all([ |
| 39 | + vscode.workspace.fs.readFile(vscode.Uri.file(obsoleteFilePath)), |
| 40 | + vscode.workspace.fs.readDirectory(vscode.Uri.file(extensionsDirPath)), |
| 41 | + ]) |
| 42 | + |
| 43 | + const installedExtensionsCount = extensionsDirContent |
| 44 | + .map(([name]) => name) |
| 45 | + .filter((name) => name.includes(extension)).length |
| 46 | + |
| 47 | + const obsoleteExtensions = JSON.parse(obsoleteFileContent.toString()) |
| 48 | + const obsoleteExtensionsCount = Object.keys(obsoleteExtensions).filter((id) => id.includes(extension)).length |
| 49 | + |
| 50 | + if (installedExtensionsCount === obsoleteExtensionsCount) { |
| 51 | + await callback() |
| 52 | + telemetry.aws_extensionUninstalled.run((span) => { |
| 53 | + span.record({}) |
| 54 | + }) |
| 55 | + getLogger().info(`UninstallExtension: ${extension} uninstalled successfully`) |
| 56 | + } |
| 57 | + } catch (error) { |
| 58 | + getLogger().error(`UninstallExtension: Failed to check .obsolete: ${error}`) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Sets up a file system watcher to monitor the .obsolete file for changes and handle |
| 64 | + * extension un-installation if the extension is marked as obsolete. |
| 65 | + * |
| 66 | + * @param {string} extensionName - The name of the extension. |
| 67 | + * @param {vscode.ExtensionContext} context - The extension context. |
| 68 | + * @param {function} callback - Action performed when extension is uninstalled. |
| 69 | + * @returns {void} |
| 70 | + */ |
| 71 | +export function setupUninstallHandler( |
| 72 | + extensionName: typeof VSCODE_EXTENSION_ID.awstoolkit | typeof VSCODE_EXTENSION_ID.amazonq, |
| 73 | + context: vscode.ExtensionContext, |
| 74 | + callback: () => Promise<void> = async () => {} |
| 75 | +): void { |
| 76 | + try { |
| 77 | + const extensionPath = context.extensionPath |
| 78 | + const pathComponents = extensionPath.split('/').slice(0, -1) |
| 79 | + const extensionsDirPath = pathComponents.join('/') |
| 80 | + |
| 81 | + const obsoleteFilePath = join(extensionsDirPath, '.obsolete') |
| 82 | + |
| 83 | + if (extensionsDirPath && obsoleteFilePath) { |
| 84 | + const watchPattern = new vscode.RelativePattern(extensionsDirPath, '.obsolete') |
| 85 | + const fileWatcher = vscode.workspace.createFileSystemWatcher(watchPattern) |
| 86 | + |
| 87 | + const checkUninstallHandler = () => |
| 88 | + checkExtensionUninstall(extensionName, extensionsDirPath, obsoleteFilePath, callback) |
| 89 | + fileWatcher.onDidCreate(checkUninstallHandler) |
| 90 | + fileWatcher.onDidChange(checkUninstallHandler) |
| 91 | + |
| 92 | + context.subscriptions.push(fileWatcher) |
| 93 | + } |
| 94 | + } catch (error) { |
| 95 | + getLogger().error(`UninstallExtension: Failed to register un-installation: ${error}`) |
| 96 | + } |
| 97 | +} |
0 commit comments