|
| 1 | +/*! |
| 2 | + * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as path from 'path' |
| 7 | +import * as nls from 'vscode-nls' |
| 8 | +import * as vscode from 'vscode' |
| 9 | +import * as AdmZip from 'adm-zip' |
| 10 | +import got from 'got' |
| 11 | +import globals from '../shared/extensionGlobals' |
| 12 | +import { getLogger } from '../shared/logger' |
| 13 | +import { VSCODE_EXTENSION_ID } from '../shared/extensions' |
| 14 | +import { makeTemporaryToolkitFolder } from '../shared/filesystemUtilities' |
| 15 | +import { reloadWindowPrompt } from '../shared/utilities/vsCodeUtils' |
| 16 | +import { isUserCancelledError, ToolkitError } from '../shared/errors' |
| 17 | +import { SystemUtilities } from '../shared/systemUtilities' |
| 18 | +import { telemetry } from '../shared/telemetry/telemetry' |
| 19 | +import { cast } from '../shared/utilities/typeConstructors' |
| 20 | +import { CancellationError } from '../shared/utilities/timeoutUtils' |
| 21 | + |
| 22 | +const localize = nls.loadMessageBundle() |
| 23 | + |
| 24 | +const downloadIntervalMs = 1000 * 60 * 60 * 24 // A day in milliseconds |
| 25 | +const betaToolkitKey = 'dev.beta' |
| 26 | + |
| 27 | +interface BetaToolkit { |
| 28 | + readonly needUpdate: boolean |
| 29 | + readonly lastCheck: number |
| 30 | +} |
| 31 | + |
| 32 | +function getBetaToolkitData(vsixUrl: string): BetaToolkit | undefined { |
| 33 | + return globals.context.globalState.get<Record<string, BetaToolkit>>(betaToolkitKey, {})[vsixUrl] |
| 34 | +} |
| 35 | + |
| 36 | +async function updateBetaToolkitData(vsixUrl: string, data: BetaToolkit) { |
| 37 | + await globals.context.globalState.update(betaToolkitKey, { |
| 38 | + ...globals.context.globalState.get<Record<string, BetaToolkit>>(betaToolkitKey, {}), |
| 39 | + [vsixUrl]: data, |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Watch the beta VSIX daily for changes. |
| 45 | + * If this is the first time we are watching the beta version or if its been 24 hours since it was last checked then try to prompt for update |
| 46 | + */ |
| 47 | +export function watchBetaVSIX(vsixUrl: string): vscode.Disposable { |
| 48 | + getLogger().info(`dev: watching ${vsixUrl} for beta artifacts`) |
| 49 | + |
| 50 | + const toolkit = getBetaToolkitData(vsixUrl) |
| 51 | + if (!toolkit || toolkit.needUpdate || Date.now() - toolkit.lastCheck > downloadIntervalMs) { |
| 52 | + runAutoUpdate(vsixUrl) |
| 53 | + } |
| 54 | + |
| 55 | + const interval = globals.clock.setInterval(() => runAutoUpdate(vsixUrl), downloadIntervalMs) |
| 56 | + return { dispose: () => clearInterval(interval) } |
| 57 | +} |
| 58 | + |
| 59 | +async function runAutoUpdate(vsixUrl: string) { |
| 60 | + getLogger().debug(`dev: checking ${vsixUrl} for a new version`) |
| 61 | + |
| 62 | + try { |
| 63 | + await telemetry.aws_autoUpdateBeta.run(() => checkBetaUrl(vsixUrl)) |
| 64 | + } catch (e) { |
| 65 | + if (!isUserCancelledError(e)) { |
| 66 | + getLogger().warn(`dev: beta extension auto-update failed: %s`, e) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Prompt to update the beta extension when required |
| 73 | + */ |
| 74 | +async function checkBetaUrl(vsixUrl: string): Promise<void> { |
| 75 | + const resp = await got(vsixUrl).buffer() |
| 76 | + const latestBetaInfo = await getExtensionInfo(resp) |
| 77 | + if (VSCODE_EXTENSION_ID.awstoolkit !== `${latestBetaInfo.publisher}.${latestBetaInfo.name}`) { |
| 78 | + throw new ToolkitError('URL does not point to an AWS Toolkit artifact', { code: 'InvalidExtensionName' }) |
| 79 | + } |
| 80 | + |
| 81 | + const currentVersion = vscode.extensions.getExtension(VSCODE_EXTENSION_ID.awstoolkit)?.packageJSON.version |
| 82 | + if (latestBetaInfo.version !== currentVersion) { |
| 83 | + const tmpFolder = await makeTemporaryToolkitFolder() |
| 84 | + const betaPath = vscode.Uri.joinPath(vscode.Uri.file(tmpFolder), path.basename(vsixUrl)) |
| 85 | + await SystemUtilities.writeFile(betaPath, resp) |
| 86 | + |
| 87 | + try { |
| 88 | + await promptInstallToolkit(betaPath, latestBetaInfo.version, vsixUrl) |
| 89 | + } finally { |
| 90 | + await SystemUtilities.remove(tmpFolder) |
| 91 | + } |
| 92 | + } else { |
| 93 | + await updateBetaToolkitData(vsixUrl, { |
| 94 | + lastCheck: Date.now(), |
| 95 | + needUpdate: false, |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +interface ExtensionInfo { |
| 101 | + readonly name: string |
| 102 | + readonly version: string |
| 103 | + readonly publisher: string |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Get information about the extension or error if no version could be found |
| 108 | + * |
| 109 | + * @param extension The URI of the extension on disk or the raw data |
| 110 | + * @returns The version + name of the extension |
| 111 | + * @throws Error if the extension manifest could not be found or parsed |
| 112 | + */ |
| 113 | +async function getExtensionInfo(extension: Buffer): Promise<ExtensionInfo> |
| 114 | +async function getExtensionInfo(extensionLocation: vscode.Uri): Promise<ExtensionInfo> |
| 115 | +async function getExtensionInfo(extensionOrLocation: vscode.Uri | Buffer): Promise<ExtensionInfo> { |
| 116 | + const fileNameOrData = extensionOrLocation instanceof vscode.Uri ? extensionOrLocation.fsPath : extensionOrLocation |
| 117 | + const packageFile = new AdmZip(fileNameOrData).getEntry('extension/package.json') |
| 118 | + const packageJSON = packageFile?.getData().toString() |
| 119 | + if (!packageJSON) { |
| 120 | + throw new ToolkitError('Extension does not have a `package.json`', { code: 'NoPackageJson' }) |
| 121 | + } |
| 122 | + |
| 123 | + try { |
| 124 | + const data = JSON.parse(packageJSON) |
| 125 | + |
| 126 | + return { |
| 127 | + name: cast(data.name, String), |
| 128 | + version: cast(data.version, String), |
| 129 | + publisher: cast(data.publisher, String), |
| 130 | + } |
| 131 | + } catch (e) { |
| 132 | + throw ToolkitError.chain(e, 'Unable to parse extension data', { code: 'BadParse' }) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +async function promptInstallToolkit(pluginPath: vscode.Uri, newVersion: string, vsixUrl: string): Promise<void> { |
| 137 | + const vsixName = path.basename(pluginPath.fsPath) |
| 138 | + const installBtn = localize('AWS.missingExtension.install', 'Install...') |
| 139 | + |
| 140 | + const response = await vscode.window.showInformationMessage( |
| 141 | + localize( |
| 142 | + 'AWS.dev.beta.updatePrompt', |
| 143 | + `New version of AWS Toolkit is available at the [beta URL]({0}). Install the new version "{1}" to continue using the beta.`, |
| 144 | + vsixUrl, |
| 145 | + newVersion |
| 146 | + ), |
| 147 | + installBtn |
| 148 | + ) |
| 149 | + |
| 150 | + switch (response) { |
| 151 | + case installBtn: |
| 152 | + try { |
| 153 | + getLogger().info(`dev: installing artifact ${vsixName}`) |
| 154 | + await vscode.commands.executeCommand('workbench.extensions.installExtension', pluginPath) |
| 155 | + await updateBetaToolkitData(vsixUrl, { |
| 156 | + lastCheck: Date.now(), |
| 157 | + needUpdate: false, |
| 158 | + }) |
| 159 | + reloadWindowPrompt(localize('AWS.dev.beta.reloadPrompt', 'Reload now to use the new beta AWS Toolkit.')) |
| 160 | + } catch (e) { |
| 161 | + throw ToolkitError.chain(e, `Failed to install ${vsixName}`, { code: 'FailedExtensionInstall' }) |
| 162 | + } |
| 163 | + break |
| 164 | + case undefined: |
| 165 | + await updateBetaToolkitData(vsixUrl, { |
| 166 | + lastCheck: Date.now(), |
| 167 | + needUpdate: true, |
| 168 | + }) |
| 169 | + throw new CancellationError('user') |
| 170 | + } |
| 171 | +} |
0 commit comments