Skip to content

Commit d193198

Browse files
committed
Add support for database registration
1 parent f4624f3 commit d193198

7 files changed

Lines changed: 266 additions & 70 deletions

File tree

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function promptImportInternetDatabase(
2727
databasesManager: DatabaseManager,
2828
storagePath: string,
2929
progress: ProgressCallback,
30-
_: CancellationToken,
30+
token: CancellationToken,
3131
): Promise<DatabaseItem | undefined> {
3232
const databaseUrl = await window.showInputBox({
3333
prompt: 'Enter URL of zipfile of database to download',
@@ -42,7 +42,8 @@ export async function promptImportInternetDatabase(
4242
databaseUrl,
4343
databasesManager,
4444
storagePath,
45-
progress
45+
progress,
46+
token
4647
);
4748

4849
if (item) {
@@ -65,7 +66,7 @@ export async function promptImportLgtmDatabase(
6566
databasesManager: DatabaseManager,
6667
storagePath: string,
6768
progress: ProgressCallback,
68-
_: CancellationToken
69+
token: CancellationToken
6970
): Promise<DatabaseItem | undefined> {
7071
const lgtmUrl = await window.showInputBox({
7172
prompt:
@@ -82,7 +83,8 @@ export async function promptImportLgtmDatabase(
8283
databaseUrl,
8384
databasesManager,
8485
storagePath,
85-
progress
86+
progress,
87+
token
8688
);
8789
if (item) {
8890
commands.executeCommand('codeQLDatabases.focus');
@@ -108,14 +110,15 @@ export async function importArchiveDatabase(
108110
databasesManager: DatabaseManager,
109111
storagePath: string,
110112
progress: ProgressCallback,
111-
_: CancellationToken,
113+
token: CancellationToken,
112114
): Promise<DatabaseItem | undefined> {
113115
try {
114116
const item = await databaseArchiveFetcher(
115117
databaseUrl,
116118
databasesManager,
117119
storagePath,
118-
progress
120+
progress,
121+
token
119122
);
120123
if (item) {
121124
commands.executeCommand('codeQLDatabases.focus');
@@ -139,15 +142,17 @@ export async function importArchiveDatabase(
139142
* @param databaseUrl URL from which to grab the database
140143
* @param databasesManager the DatabaseManager
141144
* @param storagePath where to store the unzipped database.
142-
* @param progressCallback optional callback to send progress messages to
145+
* @param progress callback to send progress messages to
146+
* @param token cancellation token
143147
*/
144148
async function databaseArchiveFetcher(
145149
databaseUrl: string,
146150
databasesManager: DatabaseManager,
147151
storagePath: string,
148-
progressCallback?: ProgressCallback
152+
progress: ProgressCallback,
153+
token: CancellationToken
149154
): Promise<DatabaseItem> {
150-
progressCallback?.({
155+
progress({
151156
message: 'Getting database',
152157
step: 1,
153158
maxStep: 4,
@@ -161,10 +166,10 @@ async function databaseArchiveFetcher(
161166
if (isFile(databaseUrl)) {
162167
await readAndUnzip(databaseUrl, unzipPath);
163168
} else {
164-
await fetchAndUnzip(databaseUrl, unzipPath, progressCallback);
169+
await fetchAndUnzip(databaseUrl, unzipPath, progress);
165170
}
166171

167-
progressCallback?.({
172+
progress({
168173
message: 'Opening database',
169174
step: 3,
170175
maxStep: 4,
@@ -177,14 +182,14 @@ async function databaseArchiveFetcher(
177182
'codeql-database.yml'
178183
);
179184
if (dbPath) {
180-
progressCallback?.({
185+
progress({
181186
message: 'Validating and fixing source location',
182187
step: 4,
183188
maxStep: 4,
184189
});
185190
await ensureZippedSourceLocation(dbPath);
186191

187-
const item = await databasesManager.openDatabase(Uri.file(dbPath));
192+
const item = await databasesManager.openDatabase(progress, token, Uri.file(dbPath));
188193
databasesManager.setCurrentDatabaseItem(item);
189194
return item;
190195
} else {

extensions/ql-vscode/src/databases-ui.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,13 @@ export class DatabaseUI extends DisposableObject {
318318
)
319319
);
320320
this.push(
321-
commandRunner(
321+
commandRunnerWithProgress(
322322
'codeQLDatabases.removeDatabase',
323-
this.handleRemoveDatabase
323+
this.handleRemoveDatabase,
324+
{
325+
title: 'Removing database',
326+
cancellable: false
327+
}
324328
)
325329
);
326330
this.push(
@@ -580,7 +584,7 @@ export class DatabaseUI extends DisposableObject {
580584
token
581585
);
582586
} else {
583-
await this.setCurrentDatabase(uri);
587+
await this.setCurrentDatabase(progress, token, uri);
584588
}
585589
} catch (e) {
586590
// rethrow and let this be handled by default error handling.
@@ -593,15 +597,17 @@ export class DatabaseUI extends DisposableObject {
593597
};
594598

595599
private handleRemoveDatabase = async (
600+
progress: ProgressCallback,
601+
token: CancellationToken,
596602
databaseItem: DatabaseItem,
597603
multiSelect: DatabaseItem[] | undefined
598604
): Promise<void> => {
599605
if (multiSelect?.length) {
600-
multiSelect.forEach((dbItem) =>
601-
this.databaseManager.removeDatabaseItem(dbItem)
602-
);
606+
Promise.all(multiSelect.map((dbItem) =>
607+
this.databaseManager.removeDatabaseItem(progress, token, dbItem)
608+
));
603609
} else {
604-
this.databaseManager.removeDatabaseItem(databaseItem);
610+
await this.databaseManager.removeDatabaseItem(progress, token, databaseItem);
605611
}
606612
};
607613

@@ -651,11 +657,13 @@ export class DatabaseUI extends DisposableObject {
651657
}
652658

653659
private async setCurrentDatabase(
660+
progress: ProgressCallback,
661+
token: CancellationToken,
654662
uri: Uri
655663
): Promise<DatabaseItem | undefined> {
656664
let databaseItem = this.databaseManager.findDatabaseItem(uri);
657665
if (databaseItem === undefined) {
658-
databaseItem = await this.databaseManager.openDatabase(uri);
666+
databaseItem = await this.databaseManager.openDatabase(progress, token, uri);
659667
}
660668
await this.databaseManager.setCurrentDatabaseItem(databaseItem);
661669

@@ -680,7 +688,7 @@ export class DatabaseUI extends DisposableObject {
680688
if (byFolder) {
681689
const fixedUri = await this.fixDbUri(uri);
682690
// we are selecting a database folder
683-
return await this.setCurrentDatabase(fixedUri);
691+
return await this.setCurrentDatabase(progress, token, fixedUri);
684692
} else {
685693
// we are selecting a database archive. Must unzip into a workspace-controlled area
686694
// before importing.

0 commit comments

Comments
 (0)