Skip to content

Commit 7e01187

Browse files
committed
Add support for database registration
1 parent d28a449 commit 7e01187

7 files changed

Lines changed: 235 additions & 64 deletions

File tree

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 19 additions & 14 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,15 +182,15 @@ 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));
188-
databasesManager.setCurrentDatabaseItem(item);
192+
const item = await databasesManager.openDatabase(progress, token, Uri.file(dbPath));
193+
databasesManager.setCurrentDatabaseItem(progress, token, item);
189194
return item;
190195
} else {
191196
throw new Error('Database not found in archive.');

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,14 @@ export class DatabaseUI extends DisposableObject {
300300
})
301301
);
302302
this.push(
303-
commandRunner(
303+
commandRunnerWithProgress(
304304
'codeQLDatabases.setCurrentDatabase',
305-
this.handleMakeCurrentDatabase
305+
this.handleMakeCurrentDatabase,
306+
{
307+
title: 'Set current database',
308+
cancellable: false
309+
}
310+
306311
)
307312
);
308313
this.push(
@@ -318,9 +323,13 @@ export class DatabaseUI extends DisposableObject {
318323
)
319324
);
320325
this.push(
321-
commandRunner(
326+
commandRunnerWithProgress(
322327
'codeQLDatabases.removeDatabase',
323-
this.handleRemoveDatabase
328+
this.handleRemoveDatabase,
329+
{
330+
title: 'Removing database',
331+
cancellable: false
332+
}
324333
)
325334
);
326335
this.push(
@@ -354,9 +363,11 @@ export class DatabaseUI extends DisposableObject {
354363
}
355364

356365
private handleMakeCurrentDatabase = async (
366+
progress: ProgressCallback,
367+
token: CancellationToken,
357368
databaseItem: DatabaseItem
358369
): Promise<void> => {
359-
await this.databaseManager.setCurrentDatabaseItem(databaseItem);
370+
await this.databaseManager.setCurrentDatabaseItem(progress, token, databaseItem);
360371
};
361372

362373
handleChooseDatabaseFolder = async (
@@ -580,7 +591,7 @@ export class DatabaseUI extends DisposableObject {
580591
token
581592
);
582593
} else {
583-
await this.setCurrentDatabase(uri);
594+
await this.setCurrentDatabase(progress, token, uri);
584595
}
585596
} catch (e) {
586597
// rethrow and let this be handled by default error handling.
@@ -593,15 +604,17 @@ export class DatabaseUI extends DisposableObject {
593604
};
594605

595606
private handleRemoveDatabase = async (
607+
progress: ProgressCallback,
608+
token: CancellationToken,
596609
databaseItem: DatabaseItem,
597610
multiSelect: DatabaseItem[] | undefined
598611
): Promise<void> => {
599612
if (multiSelect?.length) {
600-
multiSelect.forEach((dbItem) =>
601-
this.databaseManager.removeDatabaseItem(dbItem)
602-
);
613+
Promise.all(multiSelect.map((dbItem) =>
614+
this.databaseManager.removeDatabaseItem(progress, token, dbItem)
615+
));
603616
} else {
604-
this.databaseManager.removeDatabaseItem(databaseItem);
617+
await this.databaseManager.removeDatabaseItem(progress, token, databaseItem);
605618
}
606619
};
607620

@@ -651,13 +664,15 @@ export class DatabaseUI extends DisposableObject {
651664
}
652665

653666
private async setCurrentDatabase(
667+
progress: ProgressCallback,
668+
token: CancellationToken,
654669
uri: Uri
655670
): Promise<DatabaseItem | undefined> {
656671
let databaseItem = this.databaseManager.findDatabaseItem(uri);
657672
if (databaseItem === undefined) {
658-
databaseItem = await this.databaseManager.openDatabase(uri);
673+
databaseItem = await this.databaseManager.openDatabase(progress, token, uri);
659674
}
660-
await this.databaseManager.setCurrentDatabaseItem(databaseItem);
675+
await this.databaseManager.setCurrentDatabaseItem(progress, token, databaseItem);
661676

662677
return databaseItem;
663678
}
@@ -680,7 +695,7 @@ export class DatabaseUI extends DisposableObject {
680695
if (byFolder) {
681696
const fixedUri = await this.fixDbUri(uri);
682697
// we are selecting a database folder
683-
return await this.setCurrentDatabase(fixedUri);
698+
return await this.setCurrentDatabase(progress, token, fixedUri);
684699
} else {
685700
// we are selecting a database archive. Must unzip into a workspace-controlled area
686701
// before importing.

0 commit comments

Comments
 (0)