-
Notifications
You must be signed in to change notification settings - Fork 226
Split helpers.ts file #2504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Split helpers.ts file #2504
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
706c6b8
Move qlpacks helpers to separate file
koesie10 b0c18b3
Move unused languageToDbScheme
koesie10 754fa67
Move db contents heuristics to separate file
koesie10 a4e4c67
Move getInitialQueryContents to separate file
koesie10 8c98401
Move isQueryLanguage to query language file
koesie10 bfead07
Move walkDirectory to pure files file
koesie10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
extensions/ql-vscode/src/databases/local-databases/db-contents-heuristics.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { pathExists } from "fs-extra"; | ||
| import { basename, join } from "path"; | ||
| import { glob } from "glob"; | ||
|
|
||
| /** | ||
| * The following functions al heuristically determine metadata about databases. | ||
| */ | ||
|
|
||
| /** | ||
| * Heuristically determines if the directory passed in corresponds | ||
| * to a database root. A database root is a directory that contains | ||
| * a codeql-database.yml or (historically) a .dbinfo file. It also | ||
| * contains a folder starting with `db-`. | ||
| */ | ||
| export async function isLikelyDatabaseRoot(maybeRoot: string) { | ||
| const [a, b, c] = await Promise.all([ | ||
| // databases can have either .dbinfo or codeql-database.yml. | ||
| pathExists(join(maybeRoot, ".dbinfo")), | ||
| pathExists(join(maybeRoot, "codeql-database.yml")), | ||
|
|
||
| // they *must* have a db-{language} folder | ||
| glob("db-*/", { cwd: maybeRoot }), | ||
| ]); | ||
|
|
||
| return (a || b) && c.length > 0; | ||
| } | ||
|
|
||
| /** | ||
| * A language folder is any folder starting with `db-` that is itself not a database root. | ||
| */ | ||
| export async function isLikelyDbLanguageFolder(dbPath: string) { | ||
| return ( | ||
| basename(dbPath).startsWith("db-") && !(await isLikelyDatabaseRoot(dbPath)) | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { window } from "vscode"; | ||
| import { glob } from "glob"; | ||
| import { basename } from "path"; | ||
| import { load } from "js-yaml"; | ||
| import { readFile } from "fs-extra"; | ||
| import { getQlPackPath } from "../pure/ql"; | ||
| import { CodeQLCliServer, QlpacksInfo } from "../codeql-cli/cli"; | ||
| import { extLogger } from "../common"; | ||
| import { getOnDiskWorkspaceFolders } from "../helpers"; | ||
|
|
||
| export interface QlPacksForLanguage { | ||
| /** The name of the pack containing the dbscheme. */ | ||
| dbschemePack: string; | ||
| /** `true` if `dbschemePack` is a library pack. */ | ||
| dbschemePackIsLibraryPack: boolean; | ||
| /** | ||
| * The name of the corresponding standard query pack. | ||
| * Only defined if `dbschemePack` is a library pack. | ||
| */ | ||
| queryPack?: string; | ||
| } | ||
|
|
||
| interface QlPackWithPath { | ||
| packName: string; | ||
| packDir: string | undefined; | ||
| } | ||
|
|
||
| async function findDbschemePack( | ||
| packs: QlPackWithPath[], | ||
| dbschemePath: string, | ||
| ): Promise<{ name: string; isLibraryPack: boolean }> { | ||
| for (const { packDir, packName } of packs) { | ||
| if (packDir !== undefined) { | ||
| const qlpackPath = await getQlPackPath(packDir); | ||
|
|
||
| if (qlpackPath !== undefined) { | ||
| const qlpack = load(await readFile(qlpackPath, "utf8")) as { | ||
| dbscheme?: string; | ||
| library?: boolean; | ||
| }; | ||
| if ( | ||
| qlpack.dbscheme !== undefined && | ||
| basename(qlpack.dbscheme) === basename(dbschemePath) | ||
| ) { | ||
| return { | ||
| name: packName, | ||
| isLibraryPack: qlpack.library === true, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| throw new Error(`Could not find qlpack file for dbscheme ${dbschemePath}`); | ||
| } | ||
|
|
||
| function findStandardQueryPack( | ||
| qlpacks: QlpacksInfo, | ||
| dbschemePackName: string, | ||
| ): string | undefined { | ||
| const matches = dbschemePackName.match(/^codeql\/(?<language>[a-z]+)-all$/); | ||
| if (matches) { | ||
| const queryPackName = `codeql/${matches.groups!.language}-queries`; | ||
| if (qlpacks[queryPackName] !== undefined) { | ||
| return queryPackName; | ||
| } | ||
| } | ||
|
|
||
| // Either the dbscheme pack didn't look like one where the queries might be in the query pack, or | ||
| // no query pack was found in the search path. Either is OK. | ||
| return undefined; | ||
| } | ||
|
|
||
| export async function getQlPackForDbscheme( | ||
| cliServer: Pick<CodeQLCliServer, "resolveQlpacks">, | ||
| dbschemePath: string, | ||
| ): Promise<QlPacksForLanguage> { | ||
| const qlpacks = await cliServer.resolveQlpacks(getOnDiskWorkspaceFolders()); | ||
| const packs: QlPackWithPath[] = Object.entries(qlpacks).map( | ||
| ([packName, dirs]) => { | ||
| if (dirs.length < 1) { | ||
| void extLogger.log( | ||
| `In getQlPackFor ${dbschemePath}, qlpack ${packName} has no directories`, | ||
| ); | ||
| return { packName, packDir: undefined }; | ||
| } | ||
| if (dirs.length > 1) { | ||
| void extLogger.log( | ||
| `In getQlPackFor ${dbschemePath}, qlpack ${packName} has more than one directory; arbitrarily choosing the first`, | ||
| ); | ||
| } | ||
| return { | ||
| packName, | ||
| packDir: dirs[0], | ||
| }; | ||
| }, | ||
| ); | ||
| const dbschemePack = await findDbschemePack(packs, dbschemePath); | ||
| const queryPack = dbschemePack.isLibraryPack | ||
| ? findStandardQueryPack(qlpacks, dbschemePack.name) | ||
| : undefined; | ||
| return { | ||
| dbschemePack: dbschemePack.name, | ||
| dbschemePackIsLibraryPack: dbschemePack.isLibraryPack, | ||
| queryPack, | ||
| }; | ||
| } | ||
|
|
||
| export async function getPrimaryDbscheme( | ||
| datasetFolder: string, | ||
| ): Promise<string> { | ||
| const dbschemes = await glob("*.dbscheme", { | ||
| cwd: datasetFolder, | ||
| }); | ||
|
|
||
| if (dbschemes.length < 1) { | ||
| throw new Error( | ||
| `Can't find dbscheme for current database in ${datasetFolder}`, | ||
| ); | ||
| } | ||
|
|
||
| dbschemes.sort(); | ||
| const dbscheme = dbschemes[0]; | ||
|
|
||
| if (dbschemes.length > 1) { | ||
| void window.showErrorMessage( | ||
| `Found multiple dbschemes in ${datasetFolder} during quick query; arbitrarily choosing the first, ${dbscheme}, to decide what library to use.`, | ||
| ); | ||
| } | ||
| return dbscheme; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems this is only used for the quick query and tests? Can we move it somewhere more related to that? Even though it works with the db scheme I think it's fair to move it outside of the databases folder, e.g. the local-queries folder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also used in the query resolver (in
language-support/contextual/query-resolver.ts), so I think it makes most sense to keep it in thedatabasesfolder. I don't think it's related tolanguage-supportorlocal-queries, but is about the database itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok