Move storagePath calculation to extension.ts
This commit is contained in:
committed by
Andrew Eisenberg
parent
926ab92dfe
commit
2f92477bd9
@@ -1,12 +1,12 @@
|
||||
import * as fetch from "node-fetch";
|
||||
import * as unzipper from "unzipper";
|
||||
import { ExtensionContext, Uri, ProgressOptions, ProgressLocation, commands, window } from "vscode";
|
||||
import { Uri, ProgressOptions, ProgressLocation, commands, window } from "vscode";
|
||||
import * as fs from "fs-extra";
|
||||
import * as path from "path";
|
||||
import { DatabaseManager } from "./databases";
|
||||
import { ProgressCallback, showAndLogErrorMessage, withProgress } from "./helpers";
|
||||
|
||||
export default async function promptFetchDatabase(dbm: DatabaseManager, ctx: ExtensionContext) {
|
||||
export default async function promptFetchDatabase(dbm: DatabaseManager, storagePath: string) {
|
||||
try {
|
||||
const databaseUrl = await window.showInputBox({
|
||||
prompt: 'Enter URL of zipfile of database to download'
|
||||
@@ -20,7 +20,7 @@ export default async function promptFetchDatabase(dbm: DatabaseManager, ctx: Ext
|
||||
title: 'Adding database from URL',
|
||||
cancellable: false,
|
||||
};
|
||||
await withProgress(progressOptions, async progress => await databaseFetcher(databaseUrl, dbm, ctx, progress));
|
||||
await withProgress(progressOptions, async progress => await databaseFetcher(databaseUrl, dbm, storagePath, progress));
|
||||
commands.executeCommand('codeQLDatabases.focus');
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -31,7 +31,7 @@ export default async function promptFetchDatabase(dbm: DatabaseManager, ctx: Ext
|
||||
async function databaseFetcher(
|
||||
databaseUrl: string,
|
||||
databasesManager: DatabaseManager,
|
||||
ctx: ExtensionContext,
|
||||
storagePath: string,
|
||||
progressCallback: ProgressCallback
|
||||
): Promise<void> {
|
||||
progressCallback({
|
||||
@@ -39,7 +39,6 @@ async function databaseFetcher(
|
||||
message: 'Downloading database',
|
||||
step: 1
|
||||
});
|
||||
const storagePath = ctx.storagePath || ctx.globalStoragePath;
|
||||
if (!storagePath) {
|
||||
throw new Error("No storage path specified.");
|
||||
}
|
||||
|
||||
@@ -109,8 +109,9 @@ async function findDataset(parentDirectory: string): Promise<vscode.Uri> {
|
||||
return vscode.Uri.file(dbAbsolutePath);
|
||||
}
|
||||
|
||||
async function findSourceArchive(databasePath: string, silent = false):
|
||||
Promise<vscode.Uri | undefined> {
|
||||
async function findSourceArchive(
|
||||
databasePath: string, silent = false
|
||||
): Promise<vscode.Uri | undefined> {
|
||||
|
||||
const relativePaths = ['src', 'output/src_archive']
|
||||
|
||||
|
||||
@@ -335,7 +335,7 @@ async function activateWithInstalledDistribution(ctx: ExtensionContext, distribu
|
||||
await qs.restartQueryServer();
|
||||
helpers.showAndLogInformationMessage('CodeQL Query Server restarted.', { outputLogger: queryServerLogger });
|
||||
}));
|
||||
ctx.subscriptions.push(commands.registerCommand('codeQL.downloadDatabase', () => promptFetchDatabase(dbm, ctx)));
|
||||
ctx.subscriptions.push(commands.registerCommand('codeQL.downloadDatabase', () => promptFetchDatabase(dbm, getContextStoragePath(ctx))));
|
||||
|
||||
ctx.subscriptions.push(client.start());
|
||||
|
||||
@@ -351,10 +351,15 @@ async function activateWithInstalledDistribution(ctx: ExtensionContext, distribu
|
||||
}
|
||||
}
|
||||
|
||||
function getContextStoragePath(ctx: ExtensionContext) {
|
||||
return ctx.storagePath || ctx.globalStoragePath;
|
||||
}
|
||||
|
||||
function initializeLogging(ctx: ExtensionContext): void {
|
||||
logger.init(ctx);
|
||||
queryServerLogger.init(ctx);
|
||||
ideServerLogger.init(ctx);
|
||||
const storagePath = getContextStoragePath(ctx);
|
||||
logger.init(storagePath);
|
||||
queryServerLogger.init(storagePath);
|
||||
ideServerLogger.init(storagePath);
|
||||
ctx.subscriptions.push(logger);
|
||||
ctx.subscriptions.push(queryServerLogger);
|
||||
ctx.subscriptions.push(ideServerLogger);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { window as Window, OutputChannel, Progress, ExtensionContext, Disposable } from 'vscode';
|
||||
import { window as Window, OutputChannel, Progress, Disposable } from 'vscode';
|
||||
import { DisposableObject } from 'semmle-vscode-utils';
|
||||
import * as fs from 'fs-extra';
|
||||
import * as path from 'path';
|
||||
@@ -47,8 +47,8 @@ export class OutputChannelLogger extends DisposableObject implements Logger {
|
||||
this.push(this.outputChannel);
|
||||
}
|
||||
|
||||
init(ctx: ExtensionContext): void {
|
||||
this.additionalLogLocationPath = path.join(ctx.storagePath || ctx.globalStoragePath, this.title);
|
||||
init(storagePath: string): void {
|
||||
this.additionalLogLocationPath = path.join(storagePath, this.title);
|
||||
|
||||
// clear out any old state from previous runs
|
||||
fs.remove(this.additionalLogLocationPath);
|
||||
|
||||
@@ -48,18 +48,7 @@ describe('OutputChannelLogger tests', () => {
|
||||
});
|
||||
|
||||
it('should create a side log in the workspace area', async () => {
|
||||
await sideLogTest('storagePath', 'globalStoragePath');
|
||||
});
|
||||
|
||||
it('should create a side log in the global area', async () => {
|
||||
await sideLogTest('globalStoragePath', 'storagePath');
|
||||
});
|
||||
|
||||
async function sideLogTest(expectedArea: string, otherArea: string): Promise<void> {
|
||||
logger.init({
|
||||
[expectedArea]: tempFolders[expectedArea].name,
|
||||
[otherArea]: undefined
|
||||
});
|
||||
logger.init(tempFolders.storagePath.name);
|
||||
|
||||
await logger.log('xxx', { additionalLogLocation: 'first' });
|
||||
await logger.log('yyy', { additionalLogLocation: 'second' });
|
||||
@@ -67,19 +56,16 @@ describe('OutputChannelLogger tests', () => {
|
||||
await logger.log('aaa');
|
||||
|
||||
// expect 2 side logs
|
||||
const testLoggerFolder = path.join(tempFolders[expectedArea].name, 'test-logger');
|
||||
const testLoggerFolder = path.join(tempFolders.storagePath.name, 'test-logger');
|
||||
expect(fs.readdirSync(testLoggerFolder).length).to.equal(2);
|
||||
expect(fs.readdirSync(tempFolders[otherArea].name).length).to.equal(0);
|
||||
|
||||
// contents
|
||||
expect(fs.readFileSync(path.join(testLoggerFolder, 'first'), 'utf8')).to.equal('xxx\nzzz');
|
||||
expect(fs.readFileSync(path.join(testLoggerFolder, 'second'), 'utf8')).to.equal('yyy\n');
|
||||
}
|
||||
});
|
||||
|
||||
it('should delete side logs on dispose', async () => {
|
||||
logger.init({
|
||||
storagePath: tempFolders.storagePath.name
|
||||
});
|
||||
logger.init(tempFolders.storagePath.name);
|
||||
await logger.log('xxx', { additionalLogLocation: 'first' });
|
||||
await logger.log('yyy', { additionalLogLocation: 'second' });
|
||||
|
||||
@@ -94,9 +80,7 @@ describe('OutputChannelLogger tests', () => {
|
||||
});
|
||||
|
||||
it('should remove an additional log location', async () => {
|
||||
logger.init({
|
||||
storagePath: tempFolders.storagePath.name
|
||||
});
|
||||
logger.init(tempFolders.storagePath.name);
|
||||
await logger.log('xxx', { additionalLogLocation: 'first' });
|
||||
await logger.log('yyy', { additionalLogLocation: 'second' });
|
||||
|
||||
@@ -112,9 +96,7 @@ describe('OutputChannelLogger tests', () => {
|
||||
|
||||
it('should delete an existing folder on init', async () => {
|
||||
fs.createFileSync(path.join(tempFolders.storagePath.name, 'test-logger', 'xxx'));
|
||||
logger.init({
|
||||
storagePath: tempFolders.storagePath.name
|
||||
});
|
||||
logger.init(tempFolders.storagePath.name);
|
||||
// should be empty dir
|
||||
|
||||
const testLoggerFolder = path.join(tempFolders.storagePath.name, 'test-logger');
|
||||
|
||||
Reference in New Issue
Block a user