From e603de41c1687e25991d3461ae926f035ed899c3 Mon Sep 17 00:00:00 2001 From: Koen Vlaswinkel Date: Mon, 20 Mar 2023 11:39:53 +0100 Subject: [PATCH] Convert local database commands to typed commands --- extensions/ql-vscode/src/common/commands.ts | 27 ++++ extensions/ql-vscode/src/extension.ts | 6 +- .../ql-vscode/src/local-databases-ui.ts | 135 ++++-------------- 3 files changed, 58 insertions(+), 110 deletions(-) diff --git a/extensions/ql-vscode/src/common/commands.ts b/extensions/ql-vscode/src/common/commands.ts index dcd175df0..5eaa5f733 100644 --- a/extensions/ql-vscode/src/common/commands.ts +++ b/extensions/ql-vscode/src/common/commands.ts @@ -1,5 +1,6 @@ import type { CommandManager } from "../packages/commands"; import type { Uri } from "vscode"; +import type { DatabaseItem } from "../local-databases"; import type { QueryHistoryInfo } from "../query-history/query-history-info"; // A command function matching the signature that VS Code calls when @@ -53,6 +54,31 @@ export type QueryHistoryCommands = { "codeQLQueryHistory.copyRepoList": SelectionCommandFunction; }; +// Commands used for the local databases panel +export type LocalDatabasesCommands = { + "codeQL.setCurrentDatabase": (uri: Uri) => Promise; + "codeQL.setDefaultTourDatabase": () => Promise; + "codeQL.upgradeCurrentDatabase": () => Promise; + "codeQL.clearCache": () => Promise; + + "codeQLDatabases.chooseDatabaseFolder": () => Promise; + "codeQLDatabases.chooseDatabaseArchive": () => Promise; + "codeQLDatabases.chooseDatabaseInternet": () => Promise; + "codeQLDatabases.chooseDatabaseGithub": () => Promise; + "codeQLDatabases.setCurrentDatabase": ( + databaseItem: DatabaseItem, + ) => Promise; + "codeQLDatabases.sortByName": () => Promise; + "codeQLDatabases.sortByDateAdded": () => Promise; + "codeQLDatabases.removeOrphanedDatabases": () => Promise; + + "codeQLDatabases.removeDatabase": SelectionCommandFunction; + "codeQLDatabases.upgradeDatabase": SelectionCommandFunction; + "codeQLDatabases.renameDatabase": SelectionCommandFunction; + "codeQLDatabases.openDatabaseFolder": SelectionCommandFunction; + "codeQLDatabases.addDatabaseSource": SelectionCommandFunction; +}; + // Commands tied to variant analysis export type VariantAnalysisCommands = { "codeQL.openVariantAnalysisLogs": ( @@ -64,6 +90,7 @@ export type VariantAnalysisCommands = { export type AllCommands = BaseCommands & QueryHistoryCommands & + LocalDatabasesCommands & VariantAnalysisCommands; export type AppCommandManager = CommandManager; diff --git a/extensions/ql-vscode/src/extension.ts b/extensions/ql-vscode/src/extension.ts index 67fcb49ee..03c90dceb 100644 --- a/extensions/ql-vscode/src/extension.ts +++ b/extensions/ql-vscode/src/extension.ts @@ -35,7 +35,6 @@ import { CodeQLCliServer } from "./cli"; import { CliConfigListener, DistributionConfigListener, - isCanary, joinOrderWarningThreshold, MAX_QUERIES, QueryHistoryConfigListener, @@ -642,7 +641,6 @@ async function activateWithInstalledDistribution( getContextStoragePath(ctx), ctx.extensionPath, ); - databaseUI.init(); ctx.subscriptions.push(databaseUI); void extLogger.log("Initializing evaluator log viewer."); @@ -1096,6 +1094,7 @@ async function activateWithInstalledDistribution( ...getCommands(), ...qhm.getCommands(), ...variantAnalysisManager.getCommands(), + ...databaseUI.getCommands(), }; for (const [commandName, command] of Object.entries(allCommands)) { @@ -1290,8 +1289,7 @@ async function activateWithInstalledDistribution( commandRunnerWithProgress( "codeQL.chooseDatabaseGithub", async (progress: ProgressCallback, token: CancellationToken) => { - const credentials = isCanary() ? app.credentials : undefined; - await databaseUI.chooseDatabaseGithub(credentials, progress, token); + await databaseUI.chooseDatabaseGithub(progress, token); }, { title: "Adding database from GitHub", diff --git a/extensions/ql-vscode/src/local-databases-ui.ts b/extensions/ql-vscode/src/local-databases-ui.ts index 22fd7e071..95b98d4fe 100644 --- a/extensions/ql-vscode/src/local-databases-ui.ts +++ b/extensions/ql-vscode/src/local-databases-ui.ts @@ -21,7 +21,7 @@ import { DatabaseItem, DatabaseManager, } from "./local-databases"; -import { commandRunner, ProgressCallback, withProgress } from "./commandRunner"; +import { ProgressCallback, withProgress } from "./commandRunner"; import { isLikelyDatabaseRoot, isLikelyDbLanguageFolder, @@ -38,8 +38,8 @@ import { asError, asyncFilter, getErrorMessage } from "./pure/helpers-pure"; import { QueryRunner } from "./queryRunner"; import { isCanary } from "./config"; import { App } from "./common/app"; -import { Credentials } from "./common/authentication"; import { redactableError } from "./pure/errors"; +import { LocalDatabasesCommands } from "./common/commands"; enum SortOrder { NameAsc = "NameAsc", @@ -206,108 +206,34 @@ export class DatabaseUI extends DisposableObject { ); } - init() { - void extLogger.log("Registering database panel commands."); - this.push( - commandRunner( - "codeQL.setCurrentDatabase", - this.handleSetCurrentDatabase.bind(this), - ), - ); - this.push( - commandRunner( - "codeQL.setDefaultTourDatabase", + public getCommands(): LocalDatabasesCommands { + return { + "codeQL.setCurrentDatabase": this.handleSetCurrentDatabase.bind(this), + "codeQL.setDefaultTourDatabase": this.handleSetDefaultTourDatabase.bind(this), - ), - ); - this.push( - commandRunner( - "codeQL.upgradeCurrentDatabase", + "codeQL.upgradeCurrentDatabase": this.handleUpgradeCurrentDatabase.bind(this), - ), - ); - this.push( - commandRunner("codeQL.clearCache", this.handleClearCache.bind(this)), - ); - - this.push( - commandRunner( - "codeQLDatabases.chooseDatabaseFolder", + "codeQL.clearCache": this.handleClearCache.bind(this), + "codeQLDatabases.chooseDatabaseFolder": this.handleChooseDatabaseFolder.bind(this), - ), - ); - this.push( - commandRunner( - "codeQLDatabases.chooseDatabaseArchive", + "codeQLDatabases.chooseDatabaseArchive": this.handleChooseDatabaseArchive.bind(this), - ), - ); - this.push( - commandRunner( - "codeQLDatabases.chooseDatabaseInternet", + "codeQLDatabases.chooseDatabaseInternet": this.handleChooseDatabaseInternet.bind(this), - ), - ); - this.push( - commandRunner("codeQLDatabases.chooseDatabaseGithub", async () => { - const credentials = isCanary() ? this.app.credentials : undefined; - await this.handleChooseDatabaseGithub(credentials); - }), - ); - this.push( - commandRunner( - "codeQLDatabases.setCurrentDatabase", + "codeQLDatabases.chooseDatabaseGithub": + this.handleChooseDatabaseGithub.bind(this), + "codeQLDatabases.setCurrentDatabase": this.handleMakeCurrentDatabase.bind(this), - ), - ); - this.push( - commandRunner( - "codeQLDatabases.sortByName", - this.handleSortByName.bind(this), - ), - ); - this.push( - commandRunner( - "codeQLDatabases.sortByDateAdded", - this.handleSortByDateAdded.bind(this), - ), - ); - this.push( - commandRunner( - "codeQLDatabases.removeDatabase", - this.handleRemoveDatabase.bind(this), - ), - ); - this.push( - commandRunner( - "codeQLDatabases.upgradeDatabase", - this.handleUpgradeDatabase.bind(this), - ), - ); - this.push( - commandRunner( - "codeQLDatabases.renameDatabase", - this.handleRenameDatabase.bind(this), - ), - ); - this.push( - commandRunner( - "codeQLDatabases.openDatabaseFolder", - this.handleOpenFolder.bind(this), - ), - ); - this.push( - commandRunner( - "codeQLDatabases.addDatabaseSource", - this.handleAddSource.bind(this), - ), - ); - this.push( - commandRunner( - "codeQLDatabases.removeOrphanedDatabases", + "codeQLDatabases.sortByName": this.handleSortByName.bind(this), + "codeQLDatabases.sortByDateAdded": this.handleSortByDateAdded.bind(this), + "codeQLDatabases.removeDatabase": this.handleRemoveDatabase.bind(this), + "codeQLDatabases.upgradeDatabase": this.handleUpgradeDatabase.bind(this), + "codeQLDatabases.renameDatabase": this.handleRenameDatabase.bind(this), + "codeQLDatabases.openDatabaseFolder": this.handleOpenFolder.bind(this), + "codeQLDatabases.addDatabaseSource": this.handleAddSource.bind(this), + "codeQLDatabases.removeOrphanedDatabases": this.handleRemoveOrphanedDatabases.bind(this), - ), - ); + }; } private async handleMakeCurrentDatabase( @@ -505,12 +431,10 @@ export class DatabaseUI extends DisposableObject { ); } - private async handleChooseDatabaseInternet(): Promise< - DatabaseItem | undefined - > { + private async handleChooseDatabaseInternet(): Promise { return withProgress( async (progress, token) => { - return await this.chooseDatabaseInternet(progress, token); + await this.chooseDatabaseInternet(progress, token); }, { title: "Adding database from URL", @@ -519,10 +443,11 @@ export class DatabaseUI extends DisposableObject { } public async chooseDatabaseGithub( - credentials: Credentials | undefined, progress: ProgressCallback, token: CancellationToken, ): Promise { + const credentials = isCanary() ? this.app.credentials : undefined; + return await promptImportGithubDatabase( this.databaseManager, this.storagePath, @@ -533,12 +458,10 @@ export class DatabaseUI extends DisposableObject { ); } - private async handleChooseDatabaseGithub( - credentials: Credentials | undefined, - ): Promise { + private async handleChooseDatabaseGithub(): Promise { return withProgress( async (progress, token) => { - return await this.chooseDatabaseGithub(credentials, progress, token); + await this.chooseDatabaseGithub(progress, token); }, { title: "Adding database from GitHub",