|
| 1 | +import { relative, sep } from "path"; |
| 2 | +import { window } from "vscode"; |
| 3 | +import { CodeQLCliServer } from "../cli"; |
| 4 | +import { getOnDiskWorkspaceFolders, showAndLogErrorMessage } from "../helpers"; |
| 5 | +import { ProgressCallback } from "../progress"; |
| 6 | + |
| 7 | +const maxStep = 3; |
| 8 | + |
| 9 | +export async function pickExtensionPackModelFile( |
| 10 | + cliServer: Pick<CodeQLCliServer, "resolveQlpacks" | "resolveExtensions">, |
| 11 | + progress: ProgressCallback, |
| 12 | +): Promise<string | undefined> { |
| 13 | + const extensionPackPath = await pickExtensionPack(cliServer, progress); |
| 14 | + if (!extensionPackPath) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const modelFile = await pickModelFile(cliServer, progress, extensionPackPath); |
| 19 | + if (!modelFile) { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + return modelFile; |
| 24 | +} |
| 25 | + |
| 26 | +async function pickExtensionPack( |
| 27 | + cliServer: Pick<CodeQLCliServer, "resolveQlpacks">, |
| 28 | + progress: ProgressCallback, |
| 29 | +): Promise<string | undefined> { |
| 30 | + progress({ |
| 31 | + message: "Resolving extension packs...", |
| 32 | + step: 1, |
| 33 | + maxStep, |
| 34 | + }); |
| 35 | + |
| 36 | + // Get all existing extension packs in the workspace |
| 37 | + const additionalPacks = getOnDiskWorkspaceFolders(); |
| 38 | + const extensionPacks = await cliServer.resolveQlpacks(additionalPacks, true); |
| 39 | + const options = Object.keys(extensionPacks).map((pack) => ({ |
| 40 | + label: pack, |
| 41 | + extensionPack: pack, |
| 42 | + })); |
| 43 | + |
| 44 | + progress({ |
| 45 | + message: "Choosing extension pack...", |
| 46 | + step: 2, |
| 47 | + maxStep, |
| 48 | + }); |
| 49 | + |
| 50 | + const extensionPackOption = await window.showQuickPick(options, { |
| 51 | + title: "Select extension pack to use", |
| 52 | + }); |
| 53 | + if (!extensionPackOption) { |
| 54 | + return undefined; |
| 55 | + } |
| 56 | + |
| 57 | + const extensionPackPaths = extensionPacks[extensionPackOption.extensionPack]; |
| 58 | + if (extensionPackPaths.length !== 1) { |
| 59 | + void showAndLogErrorMessage( |
| 60 | + `Extension pack ${extensionPackOption.extensionPack} could not be resolved to a single location`, |
| 61 | + { |
| 62 | + fullMessage: `Extension pack ${ |
| 63 | + extensionPackOption.extensionPack |
| 64 | + } could not be resolved to a single location. Found ${ |
| 65 | + extensionPackPaths.length |
| 66 | + } locations: ${extensionPackPaths.join(", ")}.`, |
| 67 | + }, |
| 68 | + ); |
| 69 | + return undefined; |
| 70 | + } |
| 71 | + |
| 72 | + return extensionPackPaths[0]; |
| 73 | +} |
| 74 | + |
| 75 | +async function pickModelFile( |
| 76 | + cliServer: Pick<CodeQLCliServer, "resolveExtensions">, |
| 77 | + progress: ProgressCallback, |
| 78 | + extensionPackPath: string, |
| 79 | +): Promise<string | undefined> { |
| 80 | + // Find the existing model files in the extension pack |
| 81 | + const additionalPacks = getOnDiskWorkspaceFolders(); |
| 82 | + const extensions = await cliServer.resolveExtensions( |
| 83 | + extensionPackPath, |
| 84 | + additionalPacks, |
| 85 | + ); |
| 86 | + |
| 87 | + const modelFiles = new Set<string>(); |
| 88 | + |
| 89 | + if (extensionPackPath in extensions.data) { |
| 90 | + for (const extension of extensions.data[extensionPackPath]) { |
| 91 | + modelFiles.add(extension.file); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + const fileOptions: Array<{ label: string; file: string }> = []; |
| 96 | + for (const file of modelFiles) { |
| 97 | + fileOptions.push({ |
| 98 | + label: relative(extensionPackPath, file).replaceAll(sep, "/"), |
| 99 | + file, |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + progress({ |
| 104 | + message: "Choosing model file...", |
| 105 | + step: 3, |
| 106 | + maxStep, |
| 107 | + }); |
| 108 | + |
| 109 | + const fileOption = await window.showQuickPick(fileOptions, { |
| 110 | + title: "Select model file to use", |
| 111 | + }); |
| 112 | + |
| 113 | + if (!fileOption) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + return fileOption.file; |
| 118 | +} |
0 commit comments