Skip to content

Commit 5d85da5

Browse files
committed
Refactor generateQueryPack
Creates a handful of new functions and adds documentation. This commit has no behavioural changes.
1 parent b3e642a commit 5d85da5

1 file changed

Lines changed: 103 additions & 53 deletions

File tree

extensions/ql-vscode/src/variant-analysis/run-remote-query.ts

Lines changed: 103 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -70,42 +70,24 @@ async function generateQueryPack(
7070
const originalPackRoot = await findPackRoot(queryFile);
7171
const packRelativePath = relative(originalPackRoot, queryFile);
7272
const targetQueryFileName = join(queryPackDir, packRelativePath);
73+
const workspaceFolders = getOnDiskWorkspaceFolders();
7374

7475
let language: string | undefined;
76+
77+
// Check if the query is already in a query pack.
78+
// If so, copy the entire query pack to the temporary directory.
79+
// Otherwise, copy only the query file to the temporary directory
80+
// and generate a synthetic query pack.
7581
if (await getQlPackPath(originalPackRoot)) {
7682
// don't include ql files. We only want the queryFile to be copied.
77-
const toCopy = await cliServer.packPacklist(originalPackRoot, false);
78-
79-
// also copy the lock file (either new name or old name) and the query file itself. These are not included in the packlist.
80-
[
81-
join(originalPackRoot, "qlpack.lock.yml"),
82-
join(originalPackRoot, "codeql-pack.lock.yml"),
83+
await copyExistingQueryPack(
84+
cliServer,
85+
originalPackRoot,
8386
queryFile,
84-
].forEach((absolutePath) => {
85-
if (absolutePath) {
86-
toCopy.push(absolutePath);
87-
}
88-
});
89-
90-
let copiedCount = 0;
91-
await copy(originalPackRoot, queryPackDir, {
92-
filter: (file: string) =>
93-
// copy file if it is in the packlist, or it is a parent directory of a file in the packlist
94-
!!toCopy.find((f) => {
95-
// Normalized paths ensure that Windows drive letters are capitalized consistently.
96-
const normalizedPath = Uri.file(f).fsPath;
97-
const matches =
98-
normalizedPath === file || normalizedPath.startsWith(file + sep);
99-
if (matches) {
100-
copiedCount++;
101-
}
102-
return matches;
103-
}),
104-
});
105-
106-
void extLogger.log(`Copied ${copiedCount} files to ${queryPackDir}`);
107-
108-
await fixPackFile(queryPackDir, packRelativePath);
87+
queryPackDir,
88+
packRelativePath,
89+
workspaceFolders,
90+
);
10991

11092
language = await findLanguage(cliServer, Uri.file(targetQueryFileName));
11193
} else {
@@ -114,20 +96,14 @@ async function generateQueryPack(
11496

11597
// copy only the query file to the query pack directory
11698
// and generate a synthetic query pack
117-
void extLogger.log(`Copying ${queryFile} to ${queryPackDir}`);
118-
await copy(queryFile, targetQueryFileName);
119-
void extLogger.log("Generating synthetic query pack");
120-
const syntheticQueryPack = {
121-
name: QUERY_PACK_NAME,
122-
version: "0.0.0",
123-
dependencies: {
124-
[`codeql/${language}-all`]: "*",
125-
},
126-
defaultSuite: generateDefaultSuite(packRelativePath),
127-
};
128-
await writeFile(
129-
join(queryPackDir, FALLBACK_QLPACK_FILENAME),
130-
dump(syntheticQueryPack),
99+
await createNewQueryPack(
100+
queryFile,
101+
queryPackDir,
102+
targetQueryFileName,
103+
language,
104+
packRelativePath,
105+
cliServer,
106+
workspaceFolders,
131107
);
132108
}
133109
if (!language) {
@@ -154,7 +130,6 @@ async function generateQueryPack(
154130
`Compiling and bundling query pack from ${queryPackDir} to ${bundlePath}. (This may take a while.)`,
155131
);
156132
await cliServer.packInstall(queryPackDir);
157-
const workspaceFolders = getOnDiskWorkspaceFolders();
158133
await cliServer.packBundle(
159134
queryPackDir,
160135
workspaceFolders,
@@ -168,6 +143,79 @@ async function generateQueryPack(
168143
};
169144
}
170145

146+
async function createNewQueryPack(
147+
queryFile: string,
148+
queryPackDir: string,
149+
targetQueryFileName: string,
150+
language: string | undefined,
151+
packRelativePath: string,
152+
cliServer: cli.CodeQLCliServer,
153+
workspaceFolders: string[],
154+
) {
155+
void extLogger.log(`Copying ${queryFile} to ${queryPackDir}`);
156+
await copy(queryFile, targetQueryFileName);
157+
void extLogger.log("Generating synthetic query pack");
158+
const syntheticQueryPack = {
159+
name: QUERY_PACK_NAME,
160+
version: "0.0.0",
161+
dependencies: {
162+
[`codeql/${language}-all`]: "*",
163+
},
164+
defaultSuite: generateDefaultSuite(packRelativePath),
165+
};
166+
await writeFile(
167+
join(queryPackDir, FALLBACK_QLPACK_FILENAME),
168+
dump(syntheticQueryPack),
169+
);
170+
}
171+
172+
async function copyExistingQueryPack(
173+
cliServer: cli.CodeQLCliServer,
174+
originalPackRoot: string,
175+
queryFile: string,
176+
queryPackDir: string,
177+
packRelativePath: string,
178+
workspaceFolders: string[],
179+
) {
180+
const toCopy = await cliServer.packPacklist(originalPackRoot, false);
181+
182+
// also copy the lock file (either new name or old name) and the query file itself. These are not included in the packlist.
183+
[
184+
join(originalPackRoot, "qlpack.lock.yml"),
185+
join(originalPackRoot, "codeql-pack.lock.yml"),
186+
queryFile,
187+
].forEach((absolutePath) => {
188+
if (absolutePath) {
189+
toCopy.push(absolutePath);
190+
}
191+
});
192+
193+
let copiedCount = 0;
194+
await copy(originalPackRoot, queryPackDir, {
195+
filter: (file: string) =>
196+
// copy file if it is in the packlist, or it is a parent directory of a file in the packlist
197+
!!toCopy.find((f) => {
198+
// Normalized paths ensure that Windows drive letters are capitalized consistently.
199+
const normalizedPath = Uri.file(f).fsPath;
200+
const matches =
201+
normalizedPath === file || normalizedPath.startsWith(file + sep);
202+
if (matches) {
203+
copiedCount++;
204+
}
205+
return matches;
206+
}),
207+
});
208+
209+
void extLogger.log(`Copied ${copiedCount} files to ${queryPackDir}`);
210+
211+
await fixPackFile(
212+
queryPackDir,
213+
packRelativePath,
214+
cliServer,
215+
workspaceFolders,
216+
);
217+
}
218+
171219
async function findPackRoot(queryFile: string): Promise<string> {
172220
// recursively find the directory containing qlpack.yml or codeql-pack.yml
173221
let dir = dirname(queryFile);
@@ -316,6 +364,8 @@ export async function prepareRemoteQueryRun(
316364
async function fixPackFile(
317365
queryPackDir: string,
318366
packRelativePath: string,
367+
cliServer: cli.CodeQLCliServer,
368+
workspaceFolders: string[],
319369
): Promise<void> {
320370
const packPath = await getQlPackPath(queryPackDir);
321371

@@ -329,19 +379,19 @@ async function fixPackFile(
329379
}
330380
const qlpack = load(await readFile(packPath, "utf8")) as QlPack;
331381

332-
// update pack name
333382
qlpack.name = QUERY_PACK_NAME;
334-
335-
// update default suite
336-
delete qlpack.defaultSuiteFile;
337-
qlpack.defaultSuite = generateDefaultSuite(packRelativePath);
338-
339-
// remove any ${workspace} version references
383+
updateDefaultSuite(qlpack, packRelativePath);
340384
removeWorkspaceRefs(qlpack);
341385

342386
await writeFile(packPath, dump(qlpack));
343387
}
344388

389+
390+
function updateDefaultSuite(qlpack: QlPack, packRelativePath: string) {
391+
delete qlpack.defaultSuiteFile;
392+
qlpack.defaultSuite = generateDefaultSuite(packRelativePath);
393+
}
394+
345395
function generateDefaultSuite(packRelativePath: string) {
346396
return [
347397
{

0 commit comments

Comments
 (0)