Skip to content

Commit

Permalink
Add support for database registration
Browse files Browse the repository at this point in the history
  • Loading branch information
aeisenberg committed Nov 30, 2020
1 parent d28a449 commit 6fbfa82
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 70 deletions.
31 changes: 18 additions & 13 deletions extensions/ql-vscode/src/databaseFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function promptImportInternetDatabase(
databasesManager: DatabaseManager,
storagePath: string,
progress: ProgressCallback,
_: CancellationToken,
token: CancellationToken,
): Promise<DatabaseItem | undefined> {
const databaseUrl = await window.showInputBox({
prompt: 'Enter URL of zipfile of database to download',
Expand All @@ -42,7 +42,8 @@ export async function promptImportInternetDatabase(
databaseUrl,
databasesManager,
storagePath,
progress
progress,
token
);

if (item) {
Expand All @@ -65,7 +66,7 @@ export async function promptImportLgtmDatabase(
databasesManager: DatabaseManager,
storagePath: string,
progress: ProgressCallback,
_: CancellationToken
token: CancellationToken
): Promise<DatabaseItem | undefined> {
const lgtmUrl = await window.showInputBox({
prompt:
Expand All @@ -82,7 +83,8 @@ export async function promptImportLgtmDatabase(
databaseUrl,
databasesManager,
storagePath,
progress
progress,
token
);
if (item) {
commands.executeCommand('codeQLDatabases.focus');
Expand All @@ -108,14 +110,15 @@ export async function importArchiveDatabase(
databasesManager: DatabaseManager,
storagePath: string,
progress: ProgressCallback,
_: CancellationToken,
token: CancellationToken,
): Promise<DatabaseItem | undefined> {
try {
const item = await databaseArchiveFetcher(
databaseUrl,
databasesManager,
storagePath,
progress
progress,
token
);
if (item) {
commands.executeCommand('codeQLDatabases.focus');
Expand All @@ -139,15 +142,17 @@ export async function importArchiveDatabase(
* @param databaseUrl URL from which to grab the database
* @param databasesManager the DatabaseManager
* @param storagePath where to store the unzipped database.
* @param progressCallback optional callback to send progress messages to
* @param progress callback to send progress messages to
* @param token cancellation token
*/
async function databaseArchiveFetcher(
databaseUrl: string,
databasesManager: DatabaseManager,
storagePath: string,
progressCallback?: ProgressCallback
progress: ProgressCallback,
token: CancellationToken
): Promise<DatabaseItem> {
progressCallback?.({
progress({
message: 'Getting database',
step: 1,
maxStep: 4,
Expand All @@ -161,10 +166,10 @@ async function databaseArchiveFetcher(
if (isFile(databaseUrl)) {
await readAndUnzip(databaseUrl, unzipPath);
} else {
await fetchAndUnzip(databaseUrl, unzipPath, progressCallback);
await fetchAndUnzip(databaseUrl, unzipPath, progress);
}

progressCallback?.({
progress({
message: 'Opening database',
step: 3,
maxStep: 4,
Expand All @@ -177,14 +182,14 @@ async function databaseArchiveFetcher(
'codeql-database.yml'
);
if (dbPath) {
progressCallback?.({
progress({
message: 'Validating and fixing source location',
step: 4,
maxStep: 4,
});
await ensureZippedSourceLocation(dbPath);

const item = await databasesManager.openDatabase(Uri.file(dbPath));
const item = await databasesManager.openDatabase(progress, token, Uri.file(dbPath));
databasesManager.setCurrentDatabaseItem(item);
return item;
} else {
Expand Down
26 changes: 17 additions & 9 deletions extensions/ql-vscode/src/databases-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,13 @@ export class DatabaseUI extends DisposableObject {
)
);
this.push(
commandRunner(
commandRunnerWithProgress(
'codeQLDatabases.removeDatabase',
this.handleRemoveDatabase
this.handleRemoveDatabase,
{
title: 'Removing database',
cancellable: false
}
)
);
this.push(
Expand Down Expand Up @@ -580,7 +584,7 @@ export class DatabaseUI extends DisposableObject {
token
);
} else {
await this.setCurrentDatabase(uri);
await this.setCurrentDatabase(progress, token, uri);
}
} catch (e) {
// rethrow and let this be handled by default error handling.
Expand All @@ -593,15 +597,17 @@ export class DatabaseUI extends DisposableObject {
};

private handleRemoveDatabase = async (
progress: ProgressCallback,
token: CancellationToken,
databaseItem: DatabaseItem,
multiSelect: DatabaseItem[] | undefined
): Promise<void> => {
if (multiSelect?.length) {
multiSelect.forEach((dbItem) =>
this.databaseManager.removeDatabaseItem(dbItem)
);
Promise.all(multiSelect.map((dbItem) =>
this.databaseManager.removeDatabaseItem(progress, token, dbItem)
));
} else {
this.databaseManager.removeDatabaseItem(databaseItem);
await this.databaseManager.removeDatabaseItem(progress, token, databaseItem);
}
};

Expand Down Expand Up @@ -651,11 +657,13 @@ export class DatabaseUI extends DisposableObject {
}

private async setCurrentDatabase(
progress: ProgressCallback,
token: CancellationToken,
uri: Uri
): Promise<DatabaseItem | undefined> {
let databaseItem = this.databaseManager.findDatabaseItem(uri);
if (databaseItem === undefined) {
databaseItem = await this.databaseManager.openDatabase(uri);
databaseItem = await this.databaseManager.openDatabase(progress, token, uri);
}
await this.databaseManager.setCurrentDatabaseItem(databaseItem);

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

0 comments on commit 6fbfa82

Please sign in to comment.