Store QL pack in workspace instead of VSCode storage

We're checking that the skeleton QL pack doesn't exist as a workspace
folder, so we should be creating this folder in the workspace as well.

Initially this was being created in VSCode's local storage.
This commit is contained in:
Elena Tanasoiu
2023-02-14 12:41:22 +00:00
parent ce3e19a2d7
commit 24eb8fd307
3 changed files with 50 additions and 18 deletions

View File

@@ -669,6 +669,7 @@ export class DatabaseManager extends DisposableObject {
folderName,
databaseItem.language as QueryLanguage,
this.cli,
this.ctx.storageUri?.fsPath,
);
await qlPackGenerator.generate();
} catch (e: unknown) {

View File

@@ -1,6 +1,7 @@
import { mkdir, writeFile } from "fs-extra";
import { writeFile } from "fs-extra";
import { dump } from "js-yaml";
import { join } from "path";
import { Uri, workspace } from "vscode";
import { CodeQLCliServer } from "./cli";
export type QueryLanguage =
@@ -18,21 +19,28 @@ export class QlPackGenerator {
private readonly qlpackVersion: string;
private readonly header: string;
private readonly qlpackFileName: string;
private readonly folderUri: Uri;
constructor(
private readonly folderName: string,
private readonly queryLanguage: QueryLanguage,
private readonly cliServer: CodeQLCliServer,
private readonly storagePath: string | undefined,
) {
if (this.storagePath === undefined) {
throw new Error("Workspace storage path is undefined");
}
this.qlpackName = `getting-started/codeql-extra-queries-${this.queryLanguage}`;
this.qlpackVersion = "1.0.0";
this.header = "# This is an automatically generated file.\n\n";
this.qlpackFileName = "qlpack.yml";
this.folderUri = Uri.parse(join(this.storagePath, this.folderName));
}
public async generate() {
await mkdir(this.folderName);
// create QL pack folder and add to workspace
await this.createWorkspaceFolder();
// create qlpack.yml
await this.createQlPackYaml();
@@ -44,8 +52,19 @@ export class QlPackGenerator {
await this.createCodeqlPackLockYaml();
}
private async createWorkspaceFolder() {
await workspace.fs.createDirectory(this.folderUri);
const end = (workspace.workspaceFolders || []).length;
await workspace.updateWorkspaceFolders(end, 0, {
name: this.folderName,
uri: this.folderUri,
});
}
private async createQlPackYaml() {
const qlPackFile = join(this.folderName, this.qlpackFileName);
const qlPackFilePath = join(this.folderUri.path, this.qlpackFileName);
const qlPackYml = {
name: this.qlpackName,
@@ -55,11 +74,11 @@ export class QlPackGenerator {
},
};
await writeFile(qlPackFile, this.header + dump(qlPackYml), "utf8");
await writeFile(qlPackFilePath, this.header + dump(qlPackYml), "utf8");
}
private async createExampleQlFile() {
const exampleQlFile = join(this.folderName, "example.ql");
const exampleQlFilePath = join(this.folderUri.path, "example.ql");
const exampleQl = `
/**
@@ -75,10 +94,10 @@ import ${this.queryLanguage}
select "Hello, world!"
`.trim();
await writeFile(exampleQlFile, exampleQl, "utf8");
await writeFile(exampleQlFilePath, exampleQl, "utf8");
}
private async createCodeqlPackLockYaml() {
await this.cliServer.packAdd(this.folderName, this.queryLanguage);
await this.cliServer.packAdd(this.folderUri.path, this.queryLanguage);
}
}

View File

@@ -1,10 +1,14 @@
import { join } from "path";
import { existsSync, rmdirSync } from "fs";
import { existsSync, rmSync } from "fs";
import { QlPackGenerator, QueryLanguage } from "../../../src/qlpack-generator";
import { CodeQLCliServer } from "../../../src/cli";
import { isFolderAlreadyInWorkspace } from "../../../src/helpers";
import { workspace } from "vscode";
import { getErrorMessage } from "../../../src/pure/helpers-pure";
describe("QlPackGenerator", () => {
let packfolderName: string;
let packFolderName: string;
let packFolderPath: string;
let qlPackYamlFilePath: string;
let exampleQlFilePath: string;
let language: string;
@@ -13,9 +17,11 @@ describe("QlPackGenerator", () => {
beforeEach(async () => {
language = "ruby";
packfolderName = `test-ql-pack-${language}`;
qlPackYamlFilePath = join(packfolderName, "qlpack.yml");
exampleQlFilePath = join(packfolderName, "example.ql");
packFolderName = `test-ql-pack-${language}`;
packFolderPath = join(__dirname, packFolderName);
qlPackYamlFilePath = join(packFolderPath, "qlpack.yml");
exampleQlFilePath = join(packFolderPath, "example.ql");
packAddSpy = jest.fn();
const mockCli = {
@@ -23,31 +29,37 @@ describe("QlPackGenerator", () => {
} as unknown as CodeQLCliServer;
generator = new QlPackGenerator(
packfolderName,
packFolderName,
language as QueryLanguage,
mockCli,
__dirname,
);
});
afterEach(async () => {
try {
rmdirSync(packfolderName, { recursive: true });
rmSync(packFolderPath, { recursive: true });
const end = (workspace.workspaceFolders || []).length;
workspace.updateWorkspaceFolders(end - 1, 1);
} catch (e) {
// ignore
console.log(
`Could not remove folder from workspace: ${getErrorMessage(e)}`,
);
}
});
it("should generate a QL pack", async () => {
expect(existsSync(packfolderName)).toBe(false);
expect(isFolderAlreadyInWorkspace(packFolderName)).toBe(false);
expect(existsSync(qlPackYamlFilePath)).toBe(false);
expect(existsSync(exampleQlFilePath)).toBe(false);
await generator.generate();
expect(existsSync(packfolderName)).toBe(true);
expect(isFolderAlreadyInWorkspace(packFolderName)).toBe(true);
expect(existsSync(qlPackYamlFilePath)).toBe(true);
expect(existsSync(exampleQlFilePath)).toBe(true);
expect(packAddSpy).toHaveBeenCalledWith(packfolderName, language);
expect(packAddSpy).toHaveBeenCalledWith(packFolderPath, language);
});
});