From ee759abea911e66bf7aa259f36cfa4a547e862ad Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 14 Mar 2023 17:40:39 +0000 Subject: [PATCH] Move installOrUpdateThenTryActivate to top level --- extensions/ql-vscode/src/extension.ts | 138 +++++++++++++++----------- 1 file changed, 80 insertions(+), 58 deletions(-) diff --git a/extensions/ql-vscode/src/extension.ts b/extensions/ql-vscode/src/extension.ts index f4c150e63..31fe6be34 100644 --- a/extensions/ql-vscode/src/extension.ts +++ b/extensions/ql-vscode/src/extension.ts @@ -274,64 +274,32 @@ export async function activate( // Checking the vscode version should not block extension activation. void assertVSCodeVersionGreaterThan(MIN_VERSION, ctx); - async function installOrUpdateThenTryActivate( - config: DistributionUpdateConfig, - ): Promise> { - await installOrUpdateDistribution(ctx, distributionManager, config); - - // Display the warnings even if the extension has already activated. - const distributionResult = - await getDistributionDisplayingDistributionWarnings(distributionManager); - let extensionInterface: CodeQLExtensionInterface | Record = - {}; - if ( - !beganMainExtensionActivation && - distributionResult.kind !== FindDistributionResultKind.NoDistribution - ) { - extensionInterface = await activateWithInstalledDistribution( + ctx.subscriptions.push( + distributionConfigListener.onDidChangeConfiguration(() => + installOrUpdateThenTryActivate( ctx, distributionManager, distributionConfigListener, - ); - } else if ( - distributionResult.kind === FindDistributionResultKind.NoDistribution - ) { - registerErrorStubs([checkForUpdatesCommand], (command) => async () => { - const installActionName = "Install CodeQL CLI"; - const chosenAction = await showAndLogErrorMessage( - `Can't execute ${command}: missing CodeQL CLI.`, - { - items: [installActionName], - }, - ); - if (chosenAction === installActionName) { - await installOrUpdateThenTryActivate({ - isUserInitiated: true, - shouldDisplayMessageWhenNoUpdates: false, - allowAutoUpdating: true, - }); - } - }); - } - return extensionInterface; - } - - ctx.subscriptions.push( - distributionConfigListener.onDidChangeConfiguration(() => - installOrUpdateThenTryActivate({ - isUserInitiated: true, - shouldDisplayMessageWhenNoUpdates: false, - allowAutoUpdating: true, - }), + { + isUserInitiated: true, + shouldDisplayMessageWhenNoUpdates: false, + allowAutoUpdating: true, + }, + ), ), ); ctx.subscriptions.push( commandRunner(checkForUpdatesCommand, () => - installOrUpdateThenTryActivate({ - isUserInitiated: true, - shouldDisplayMessageWhenNoUpdates: true, - allowAutoUpdating: true, - }), + installOrUpdateThenTryActivate( + ctx, + distributionManager, + distributionConfigListener, + { + isUserInitiated: true, + shouldDisplayMessageWhenNoUpdates: true, + allowAutoUpdating: true, + }, + ), ), ); @@ -341,14 +309,19 @@ export async function activate( variantAnalysisViewSerializer, ); - const codeQlExtension = await installOrUpdateThenTryActivate({ - isUserInitiated: !!ctx.globalState.get(shouldUpdateOnNextActivationKey), - shouldDisplayMessageWhenNoUpdates: false, + const codeQlExtension = await installOrUpdateThenTryActivate( + ctx, + distributionManager, + distributionConfigListener, + { + isUserInitiated: !!ctx.globalState.get(shouldUpdateOnNextActivationKey), + shouldDisplayMessageWhenNoUpdates: false, - // only auto update on startup if the user has previously requested an update - // otherwise, ask user to accept the update - allowAutoUpdating: !!ctx.globalState.get(shouldUpdateOnNextActivationKey), - }); + // only auto update on startup if the user has previously requested an update + // otherwise, ask user to accept the update + allowAutoUpdating: !!ctx.globalState.get(shouldUpdateOnNextActivationKey), + }, + ); variantAnalysisViewSerializer.onExtensionLoaded( codeQlExtension.variantAnalysisManager, @@ -533,6 +506,55 @@ async function getDistributionDisplayingDistributionWarnings( return result; } +async function installOrUpdateThenTryActivate( + ctx: ExtensionContext, + distributionManager: DistributionManager, + distributionConfigListener: DistributionConfigListener, + config: DistributionUpdateConfig, +): Promise> { + await installOrUpdateDistribution(ctx, distributionManager, config); + + // Display the warnings even if the extension has already activated. + const distributionResult = + await getDistributionDisplayingDistributionWarnings(distributionManager); + let extensionInterface: CodeQLExtensionInterface | Record = {}; + if ( + !beganMainExtensionActivation && + distributionResult.kind !== FindDistributionResultKind.NoDistribution + ) { + extensionInterface = await activateWithInstalledDistribution( + ctx, + distributionManager, + distributionConfigListener, + ); + } else if ( + distributionResult.kind === FindDistributionResultKind.NoDistribution + ) { + registerErrorStubs([checkForUpdatesCommand], (command) => async () => { + const installActionName = "Install CodeQL CLI"; + const chosenAction = await showAndLogErrorMessage( + `Can't execute ${command}: missing CodeQL CLI.`, + { + items: [installActionName], + }, + ); + if (chosenAction === installActionName) { + await installOrUpdateThenTryActivate( + ctx, + distributionManager, + distributionConfigListener, + { + isUserInitiated: true, + shouldDisplayMessageWhenNoUpdates: false, + allowAutoUpdating: true, + }, + ); + } + }); + } + return extensionInterface; +} + const PACK_GLOBS = [ "**/codeql-pack.yml", "**/qlpack.yml",