Skip to content

Commit 2c31335

Browse files
committed
Merge branch 'koesie10/pick-extension-model-file' into koesie10/create-extension-model-file
2 parents 59c0610 + 5cdf7ed commit 2c31335

File tree

4 files changed

+309
-35
lines changed

4 files changed

+309
-35
lines changed

extensions/ql-vscode/src/cli.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,18 @@ export type MlModelInfo = {
107107
/** The expected output of `codeql resolve ml-models`. */
108108
export type MlModelsInfo = { models: MlModelInfo[] };
109109

110-
export type DataExtensionInfo = {
110+
/** Information about a data extension predicate, as resolved by `codeql resolve extensions`. */
111+
export type DataExtensionResult = {
111112
predicate: string;
112113
file: string;
113114
index: number;
114115
};
115116

116117
/** The expected output of `codeql resolve extensions`. */
117-
export type ExtensionsInfo = {
118+
export type ResolveExtensionsResult = {
118119
models: MlModelInfo[];
119120
data: {
120-
[filename: string]: DataExtensionInfo[];
121+
[path: string]: DataExtensionResult[];
121122
};
122123
};
123124

@@ -1215,11 +1216,11 @@ export class CodeQLCliServer implements Disposable {
12151216
async resolveExtensions(
12161217
suite: string,
12171218
additionalPacks: string[],
1218-
): Promise<ExtensionsInfo> {
1219+
): Promise<ResolveExtensionsResult> {
12191220
const args = this.getAdditionalPacksArg(additionalPacks);
12201221
args.push(suite);
12211222

1222-
return this.runJsonCodeQlCliCommand<ExtensionsInfo>(
1223+
return this.runJsonCodeQlCliCommand<ResolveExtensionsResult>(
12231224
["resolve", "extensions"],
12241225
args,
12251226
"Resolving extensions",

extensions/ql-vscode/src/data-extensions-editor/data-extensions-editor-module.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { join } from "path";
99
import { App } from "../common/app";
1010
import { showAndLogErrorMessage } from "../helpers";
1111
import { withProgress } from "../progress";
12-
import { pickExtensionPack, pickModelFile } from "./extension-packs";
12+
import { pickExtensionPackModelFile } from "./extension-pack-picker";
1313

1414
export class DataExtensionsEditorModule {
1515
private readonly queryStorageDir: string;
@@ -67,20 +67,11 @@ export class DataExtensionsEditorModule {
6767
return;
6868
}
6969

70-
const extensionPackPath = await pickExtensionPack(
71-
this.cliServer,
72-
progress,
73-
);
74-
if (!extensionPackPath) {
75-
return;
76-
}
77-
78-
const modelFile = await pickModelFile(
70+
const modelFile = await pickExtensionPackModelFile(
7971
this.cliServer,
72+
db,
8073
progress,
8174
token,
82-
db,
83-
extensionPackPath,
8475
);
8576
if (!modelFile) {
8677
return;

extensions/ql-vscode/src/data-extensions-editor/extension-packs.ts renamed to extensions/ql-vscode/src/data-extensions-editor/extension-pack-picker.ts

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { relative, resolve } from "path";
1+
import { relative, resolve, sep } from "path";
22
import { pathExists, readFile } from "fs-extra";
33
import { load as loadYaml } from "js-yaml";
44
import { minimatch } from "minimatch";
@@ -9,14 +9,42 @@ import { ProgressCallback } from "../progress";
99
import { DatabaseItem } from "../local-databases";
1010
import { getQlPackPath, QLPACK_FILENAMES } from "../pure/ql";
1111

12-
export async function pickExtensionPack(
13-
cliServer: CodeQLCliServer,
12+
const maxStep = 3;
13+
14+
export async function pickExtensionPackModelFile(
15+
cliServer: Pick<CodeQLCliServer, "resolveQlpacks" | "resolveExtensions">,
16+
databaseItem: Pick<DatabaseItem, "name">,
17+
progress: ProgressCallback,
18+
token: CancellationToken,
19+
): Promise<string | undefined> {
20+
const extensionPackPath = await pickExtensionPack(cliServer, progress, token);
21+
if (!extensionPackPath) {
22+
return;
23+
}
24+
25+
const modelFile = await pickModelFile(
26+
cliServer,
27+
databaseItem,
28+
extensionPackPath,
29+
progress,
30+
token,
31+
);
32+
if (!modelFile) {
33+
return;
34+
}
35+
36+
return modelFile;
37+
}
38+
39+
async function pickExtensionPack(
40+
cliServer: Pick<CodeQLCliServer, "resolveQlpacks">,
1441
progress: ProgressCallback,
42+
token: CancellationToken,
1543
): Promise<string | undefined> {
1644
progress({
1745
message: "Resolving extension packs...",
1846
step: 1,
19-
maxStep: 3,
47+
maxStep,
2048
});
2149

2250
// Get all existing extension packs in the workspace
@@ -30,12 +58,16 @@ export async function pickExtensionPack(
3058
progress({
3159
message: "Choosing extension pack...",
3260
step: 2,
33-
maxStep: 3,
61+
maxStep,
3462
});
3563

36-
const extensionPackOption = await window.showQuickPick(options, {
37-
title: "Select extension pack to use",
38-
});
64+
const extensionPackOption = await window.showQuickPick(
65+
options,
66+
{
67+
title: "Select extension pack to use",
68+
},
69+
token,
70+
);
3971
if (!extensionPackOption) {
4072
return undefined;
4173
}
@@ -44,19 +76,26 @@ export async function pickExtensionPack(
4476
if (extensionPackPaths.length !== 1) {
4577
void showAndLogErrorMessage(
4678
`Extension pack ${extensionPackOption.extensionPack} could not be resolved to a single location`,
79+
{
80+
fullMessage: `Extension pack ${
81+
extensionPackOption.extensionPack
82+
} could not be resolved to a single location. Found ${
83+
extensionPackPaths.length
84+
} locations: ${extensionPackPaths.join(", ")}.`,
85+
},
4786
);
4887
return undefined;
4988
}
5089

5190
return extensionPackPaths[0];
5291
}
5392

54-
export async function pickModelFile(
55-
cliServer: CodeQLCliServer,
93+
async function pickModelFile(
94+
cliServer: Pick<CodeQLCliServer, "resolveExtensions">,
95+
databaseItem: Pick<DatabaseItem, "name">,
96+
extensionPackPath: string,
5697
progress: ProgressCallback,
5798
token: CancellationToken,
58-
databaseItem: DatabaseItem,
59-
extensionPackPath: string,
6099
): Promise<string | undefined> {
61100
// Find the existing model files in the extension pack
62101
const additionalPacks = getOnDiskWorkspaceFolders();
@@ -74,13 +113,13 @@ export async function pickModelFile(
74113
}
75114

76115
if (modelFiles.size === 0) {
77-
return pickNewModelFile(token, databaseItem, extensionPackPath);
116+
return pickNewModelFile(databaseItem, extensionPackPath, token);
78117
}
79118

80119
const fileOptions: Array<{ label: string; file: string | null }> = [];
81120
for (const file of modelFiles) {
82121
fileOptions.push({
83-
label: relative(extensionPackPath, file),
122+
label: relative(extensionPackPath, file).replaceAll(sep, "/"),
84123
file,
85124
});
86125
}
@@ -92,7 +131,7 @@ export async function pickModelFile(
92131
progress({
93132
message: "Choosing model file...",
94133
step: 3,
95-
maxStep: 3,
134+
maxStep,
96135
});
97136

98137
const fileOption = await window.showQuickPick(
@@ -111,13 +150,13 @@ export async function pickModelFile(
111150
return fileOption.file;
112151
}
113152

114-
return pickNewModelFile(token, databaseItem, extensionPackPath);
153+
return pickNewModelFile(databaseItem, extensionPackPath, token);
115154
}
116155

117156
async function pickNewModelFile(
118-
token: CancellationToken,
119-
databaseItem: DatabaseItem,
157+
databaseItem: Pick<DatabaseItem, "name">,
120158
extensionPackPath: string,
159+
token: CancellationToken,
121160
) {
122161
const qlpackPath = await getQlPackPath(extensionPackPath);
123162
if (!qlpackPath) {

0 commit comments

Comments
 (0)