Skip to content

Commit 1e749ec

Browse files
committed
feat: move import folders out of original function, optimize logs
1 parent fb6fac8 commit 1e749ec

File tree

1 file changed

+65
-55
lines changed

1 file changed

+65
-55
lines changed

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

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,10 @@ export class DatabaseUI extends DisposableObject {
326326
}
327327

328328
private async chooseDatabaseFolder(
329-
subFolder: boolean,
330329
progress: ProgressCallback,
331330
): Promise<void> {
332331
try {
333-
await this.chooseAndSetDatabase(true, subFolder, progress);
332+
await this.chooseAndSetDatabase(true, progress);
334333
} catch (e) {
335334
void showAndLogExceptionWithTelemetry(
336335
this.app.logger,
@@ -345,7 +344,7 @@ export class DatabaseUI extends DisposableObject {
345344
private async handleChooseDatabaseFolder(): Promise<void> {
346345
return withProgress(
347346
async (progress) => {
348-
await this.chooseDatabaseFolder(false, progress);
347+
await this.chooseDatabaseFolder(progress);
349348
},
350349
{
351350
title: "Adding database from folder",
@@ -356,7 +355,7 @@ export class DatabaseUI extends DisposableObject {
356355
private async handleChooseDatabaseFolderFromPalette(): Promise<void> {
357356
return withProgress(
358357
async (progress) => {
359-
await this.chooseDatabaseFolder(false, progress);
358+
await this.chooseDatabaseFolder(progress);
360359
},
361360
{
362361
title: "Choose a Database from a Folder",
@@ -367,7 +366,7 @@ export class DatabaseUI extends DisposableObject {
367366
private async handleChooseMultipleDatabaseFolderFromPalette(): Promise<void> {
368367
return withProgress(
369368
async (progress) => {
370-
await this.chooseDatabaseFolder(true, progress);
369+
await this.chooseDatabasesParentFolder(progress);
371370
},
372371
{
373372
title: "Choose a Folder contains all Database Folders",
@@ -510,7 +509,7 @@ export class DatabaseUI extends DisposableObject {
510509
progress: ProgressCallback,
511510
): Promise<void> {
512511
try {
513-
await this.chooseAndSetDatabase(false, false, progress);
512+
await this.chooseAndSetDatabase(false, progress);
514513
} catch (e: unknown) {
515514
void showAndLogExceptionWithTelemetry(
516515
this.app.logger,
@@ -978,68 +977,79 @@ export class DatabaseUI extends DisposableObject {
978977
*/
979978
private async chooseAndSetDatabase(
980979
byFolder: boolean,
981-
subFolder: boolean,
982980
progress: ProgressCallback,
983981
): Promise<DatabaseItem[] | DatabaseItem | undefined> {
984982
const uri = await chooseDatabaseDir(byFolder);
985983
if (!uri) {
986984
return undefined;
987985
}
988986

989-
if (subFolder) {
990-
if (!byFolder) {
991-
return undefined;
992-
}
987+
if (byFolder && !uri.fsPath.endsWith("testproj")) {
988+
const fixedUri = await this.fixDbUri(uri);
989+
// we are selecting a database folder
990+
return await this.databaseManager.openDatabase(fixedUri, {
991+
type: "folder",
992+
});
993+
} else {
994+
// we are selecting a database archive or a testproj.
995+
// Unzip archives (if an archive) and copy into a workspace-controlled area
996+
// before importing.
997+
return await this.databaseFetcher.importLocalDatabase(
998+
uri.toString(true),
999+
progress,
1000+
);
1001+
}
1002+
}
9931003

994-
const databases: DatabaseItem[] = [];
995-
const failures: string[] = [];
996-
const entries = await workspace.fs.readDirectory(uri);
997-
for (const entry of entries) {
998-
if (entry[1] === FileType.Directory) {
999-
try {
1000-
const fixedUri = await this.fixDbUri(Uri.joinPath(uri, entry[0]));
1001-
const database = await this.databaseManager.openDatabase(fixedUri, {
1002-
type: "folder",
1003-
});
1004-
databases.push(database);
1005-
} catch (e) {
1006-
failures.push(entry[0]);
1007-
}
1008-
}
1009-
}
1004+
/**
1005+
* Ask the user for a parent directory that contains all databases.
1006+
* Returns all valid databases, or `undefined` if the operation was canceled.
1007+
*/
1008+
private async chooseDatabasesParentFolder(
1009+
progress: ProgressCallback,
1010+
): Promise<DatabaseItem[] | undefined> {
1011+
const uri = await chooseDatabaseDir(true);
1012+
if (!uri) {
1013+
return undefined;
1014+
}
10101015

1011-
if (failures.length) {
1012-
void showAndLogErrorMessage(
1013-
this.app.logger,
1014-
`Failed to import ${failures.length} database(s) (${failures.join(
1015-
", ",
1016-
)}), successfully imported ${databases.length} database(s).`,
1017-
);
1018-
} else {
1019-
void showAndLogInformationMessage(
1020-
this.app.logger,
1021-
`Successfully imported ${databases.length} database(s).`,
1022-
);
1016+
const databases: DatabaseItem[] = [];
1017+
const failures: string[] = [];
1018+
const entries = await workspace.fs.readDirectory(uri);
1019+
for (const [index, entry] of entries.entries()) {
1020+
progress({
1021+
step: index + 1,
1022+
maxStep: entries.length,
1023+
message: `Importing ${entry[0]}`,
1024+
});
1025+
1026+
if (entry[1] === FileType.Directory) {
1027+
try {
1028+
const fixedUri = await this.fixDbUri(Uri.joinPath(uri, entry[0]));
1029+
const database = await this.databaseManager.openDatabase(fixedUri, {
1030+
type: "folder",
1031+
});
1032+
databases.push(database);
1033+
} catch (e) {
1034+
failures.push(entry[0]);
1035+
}
10231036
}
1037+
}
10241038

1025-
return databases;
1039+
if (failures.length) {
1040+
void showAndLogErrorMessage(
1041+
this.app.logger,
1042+
`Failed to import ${failures.length} database(s), successfully imported ${databases.length} database(s).`,
1043+
{ fullMessage: `Failed imports: \n${failures.join("\n")}` },
1044+
);
10261045
} else {
1027-
if (byFolder && !uri.fsPath.endsWith("testproj")) {
1028-
const fixedUri = await this.fixDbUri(uri);
1029-
// we are selecting a database folder
1030-
return await this.databaseManager.openDatabase(fixedUri, {
1031-
type: "folder",
1032-
});
1033-
} else {
1034-
// we are selecting a database archive or a testproj.
1035-
// Unzip archives (if an archive) and copy into a workspace-controlled area
1036-
// before importing.
1037-
return await this.databaseFetcher.importLocalDatabase(
1038-
uri.toString(true),
1039-
progress,
1040-
);
1041-
}
1046+
void showAndLogInformationMessage(
1047+
this.app.logger,
1048+
`Successfully imported ${databases.length} database(s).`,
1049+
);
10421050
}
1051+
1052+
return databases;
10431053
}
10441054

10451055
/**

0 commit comments

Comments
 (0)