|
| 1 | +import { dirname, basename, normalize, relative } from "path"; |
| 2 | +import { Discovery } from "../common/discovery"; |
| 3 | +import { CodeQLCliServer } from "../codeql-cli/cli"; |
| 4 | +import { pathExists } from "fs-extra"; |
| 5 | +import { |
| 6 | + Event, |
| 7 | + EventEmitter, |
| 8 | + RelativePattern, |
| 9 | + Uri, |
| 10 | + WorkspaceFolder, |
| 11 | +} from "vscode"; |
| 12 | +import { MultiFileSystemWatcher } from "../common/vscode/multi-file-system-watcher"; |
| 13 | +import { App } from "../common/app"; |
| 14 | +import { FileTreeDirectory, FileTreeLeaf } from "../common/file-tree-nodes"; |
| 15 | + |
| 16 | +/** |
| 17 | + * The results of discovering queries. |
| 18 | + */ |
| 19 | +interface QueryDiscoveryResults { |
| 20 | + /** |
| 21 | + * A tree of directories and query files. |
| 22 | + * May have multiple roots because of multiple workspaces. |
| 23 | + */ |
| 24 | + queries: FileTreeDirectory[]; |
| 25 | + |
| 26 | + /** |
| 27 | + * File system paths to watch. If any ql file changes in these directories |
| 28 | + * or any subdirectories, then this could signify a change in queries. |
| 29 | + */ |
| 30 | + watchPaths: Uri[]; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Discovers all query files contained in the QL packs in a given workspace folder. |
| 35 | + */ |
| 36 | +export class QueryDiscovery extends Discovery<QueryDiscoveryResults> { |
| 37 | + private results: QueryDiscoveryResults | undefined; |
| 38 | + |
| 39 | + private readonly onDidChangeQueriesEmitter = this.push( |
| 40 | + new EventEmitter<void>(), |
| 41 | + ); |
| 42 | + private readonly watcher: MultiFileSystemWatcher = this.push( |
| 43 | + new MultiFileSystemWatcher(), |
| 44 | + ); |
| 45 | + |
| 46 | + constructor( |
| 47 | + private readonly app: App, |
| 48 | + private readonly cliServer: CodeQLCliServer, |
| 49 | + ) { |
| 50 | + super("Query Discovery"); |
| 51 | + |
| 52 | + this.push(app.onDidChangeWorkspaceFolders(this.refresh.bind(this))); |
| 53 | + this.push(this.watcher.onDidChange(this.refresh.bind(this))); |
| 54 | + } |
| 55 | + |
| 56 | + public get queries(): FileTreeDirectory[] | undefined { |
| 57 | + return this.results?.queries; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Event to be fired when the set of discovered queries may have changed. |
| 62 | + */ |
| 63 | + public get onDidChangeQueries(): Event<void> { |
| 64 | + return this.onDidChangeQueriesEmitter.event; |
| 65 | + } |
| 66 | + |
| 67 | + protected async discover(): Promise<QueryDiscoveryResults> { |
| 68 | + const workspaceFolders = this.app.workspaceFolders; |
| 69 | + if (workspaceFolders === undefined || workspaceFolders.length === 0) { |
| 70 | + return { |
| 71 | + queries: [], |
| 72 | + watchPaths: [], |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + const queries = await this.discoverQueries(workspaceFolders); |
| 77 | + |
| 78 | + return { |
| 79 | + queries, |
| 80 | + watchPaths: workspaceFolders.map((f) => f.uri), |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + protected update(results: QueryDiscoveryResults): void { |
| 85 | + this.results = results; |
| 86 | + |
| 87 | + this.watcher.clear(); |
| 88 | + for (const watchPath of results.watchPaths) { |
| 89 | + // Watch for changes to any `.ql` file |
| 90 | + this.watcher.addWatch(new RelativePattern(watchPath, "**/*.{ql}")); |
| 91 | + // need to explicitly watch for changes to directories themselves. |
| 92 | + this.watcher.addWatch(new RelativePattern(watchPath, "**/")); |
| 93 | + } |
| 94 | + this.onDidChangeQueriesEmitter.fire(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Discover all queries in the specified directory and its subdirectories. |
| 99 | + * @returns A `QueryDirectory` object describing the contents of the directory, or `undefined` if |
| 100 | + * no queries were found. |
| 101 | + */ |
| 102 | + private async discoverQueries( |
| 103 | + workspaceFolders: readonly WorkspaceFolder[], |
| 104 | + ): Promise<FileTreeDirectory[]> { |
| 105 | + const rootDirectories = []; |
| 106 | + for (const workspaceFolder of workspaceFolders) { |
| 107 | + rootDirectories.push( |
| 108 | + await this.discoverQueriesInWorkspace(workspaceFolder), |
| 109 | + ); |
| 110 | + } |
| 111 | + return rootDirectories; |
| 112 | + } |
| 113 | + |
| 114 | + private async discoverQueriesInWorkspace( |
| 115 | + workspaceFolder: WorkspaceFolder, |
| 116 | + ): Promise<FileTreeDirectory> { |
| 117 | + const fullPath = workspaceFolder.uri.fsPath; |
| 118 | + const name = workspaceFolder.name; |
| 119 | + const rootDirectory = new FileTreeDirectory(fullPath, name); |
| 120 | + |
| 121 | + // Don't try discovery on workspace folders that don't exist on the filesystem |
| 122 | + if (await pathExists(fullPath)) { |
| 123 | + const resolvedQueries = await this.cliServer.resolveQueries(fullPath); |
| 124 | + for (const queryPath of resolvedQueries) { |
| 125 | + const relativePath = normalize(relative(fullPath, queryPath)); |
| 126 | + const dirName = dirname(relativePath); |
| 127 | + const parentDirectory = rootDirectory.createDirectory(dirName); |
| 128 | + parentDirectory.addChild( |
| 129 | + new FileTreeLeaf(queryPath, basename(queryPath)), |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + rootDirectory.finish(); |
| 134 | + } |
| 135 | + return rootDirectory; |
| 136 | + } |
| 137 | +} |
0 commit comments