|
| 1 | +import { window } from "vscode"; |
| 2 | +import { glob } from "glob"; |
| 3 | +import { basename } from "path"; |
| 4 | +import { load } from "js-yaml"; |
| 5 | +import { readFile } from "fs-extra"; |
| 6 | +import { getQlPackPath } from "../pure/ql"; |
| 7 | +import { CodeQLCliServer, QlpacksInfo } from "../codeql-cli/cli"; |
| 8 | +import { extLogger } from "../common"; |
| 9 | +import { getOnDiskWorkspaceFolders } from "../helpers"; |
| 10 | + |
| 11 | +export interface QlPacksForLanguage { |
| 12 | + /** The name of the pack containing the dbscheme. */ |
| 13 | + dbschemePack: string; |
| 14 | + /** `true` if `dbschemePack` is a library pack. */ |
| 15 | + dbschemePackIsLibraryPack: boolean; |
| 16 | + /** |
| 17 | + * The name of the corresponding standard query pack. |
| 18 | + * Only defined if `dbschemePack` is a library pack. |
| 19 | + */ |
| 20 | + queryPack?: string; |
| 21 | +} |
| 22 | + |
| 23 | +interface QlPackWithPath { |
| 24 | + packName: string; |
| 25 | + packDir: string | undefined; |
| 26 | +} |
| 27 | + |
| 28 | +async function findDbschemePack( |
| 29 | + packs: QlPackWithPath[], |
| 30 | + dbschemePath: string, |
| 31 | +): Promise<{ name: string; isLibraryPack: boolean }> { |
| 32 | + for (const { packDir, packName } of packs) { |
| 33 | + if (packDir !== undefined) { |
| 34 | + const qlpackPath = await getQlPackPath(packDir); |
| 35 | + |
| 36 | + if (qlpackPath !== undefined) { |
| 37 | + const qlpack = load(await readFile(qlpackPath, "utf8")) as { |
| 38 | + dbscheme?: string; |
| 39 | + library?: boolean; |
| 40 | + }; |
| 41 | + if ( |
| 42 | + qlpack.dbscheme !== undefined && |
| 43 | + basename(qlpack.dbscheme) === basename(dbschemePath) |
| 44 | + ) { |
| 45 | + return { |
| 46 | + name: packName, |
| 47 | + isLibraryPack: qlpack.library === true, |
| 48 | + }; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + throw new Error(`Could not find qlpack file for dbscheme ${dbschemePath}`); |
| 54 | +} |
| 55 | + |
| 56 | +function findStandardQueryPack( |
| 57 | + qlpacks: QlpacksInfo, |
| 58 | + dbschemePackName: string, |
| 59 | +): string | undefined { |
| 60 | + const matches = dbschemePackName.match(/^codeql\/(?<language>[a-z]+)-all$/); |
| 61 | + if (matches) { |
| 62 | + const queryPackName = `codeql/${matches.groups!.language}-queries`; |
| 63 | + if (qlpacks[queryPackName] !== undefined) { |
| 64 | + return queryPackName; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Either the dbscheme pack didn't look like one where the queries might be in the query pack, or |
| 69 | + // no query pack was found in the search path. Either is OK. |
| 70 | + return undefined; |
| 71 | +} |
| 72 | + |
| 73 | +export async function getQlPackForDbscheme( |
| 74 | + cliServer: Pick<CodeQLCliServer, "resolveQlpacks">, |
| 75 | + dbschemePath: string, |
| 76 | +): Promise<QlPacksForLanguage> { |
| 77 | + const qlpacks = await cliServer.resolveQlpacks(getOnDiskWorkspaceFolders()); |
| 78 | + const packs: QlPackWithPath[] = Object.entries(qlpacks).map( |
| 79 | + ([packName, dirs]) => { |
| 80 | + if (dirs.length < 1) { |
| 81 | + void extLogger.log( |
| 82 | + `In getQlPackFor ${dbschemePath}, qlpack ${packName} has no directories`, |
| 83 | + ); |
| 84 | + return { packName, packDir: undefined }; |
| 85 | + } |
| 86 | + if (dirs.length > 1) { |
| 87 | + void extLogger.log( |
| 88 | + `In getQlPackFor ${dbschemePath}, qlpack ${packName} has more than one directory; arbitrarily choosing the first`, |
| 89 | + ); |
| 90 | + } |
| 91 | + return { |
| 92 | + packName, |
| 93 | + packDir: dirs[0], |
| 94 | + }; |
| 95 | + }, |
| 96 | + ); |
| 97 | + const dbschemePack = await findDbschemePack(packs, dbschemePath); |
| 98 | + const queryPack = dbschemePack.isLibraryPack |
| 99 | + ? findStandardQueryPack(qlpacks, dbschemePack.name) |
| 100 | + : undefined; |
| 101 | + return { |
| 102 | + dbschemePack: dbschemePack.name, |
| 103 | + dbschemePackIsLibraryPack: dbschemePack.isLibraryPack, |
| 104 | + queryPack, |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +export async function getPrimaryDbscheme( |
| 109 | + datasetFolder: string, |
| 110 | +): Promise<string> { |
| 111 | + const dbschemes = await glob("*.dbscheme", { |
| 112 | + cwd: datasetFolder, |
| 113 | + }); |
| 114 | + |
| 115 | + if (dbschemes.length < 1) { |
| 116 | + throw new Error( |
| 117 | + `Can't find dbscheme for current database in ${datasetFolder}`, |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + dbschemes.sort(); |
| 122 | + const dbscheme = dbschemes[0]; |
| 123 | + |
| 124 | + if (dbschemes.length > 1) { |
| 125 | + void window.showErrorMessage( |
| 126 | + `Found multiple dbschemes in ${datasetFolder} during quick query; arbitrarily choosing the first, ${dbscheme}, to decide what library to use.`, |
| 127 | + ); |
| 128 | + } |
| 129 | + return dbscheme; |
| 130 | +} |
0 commit comments