|
| 1 | +import { CodeQLCliServer } from './cli'; |
| 2 | +import * as fs from 'fs-extra'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { |
| 5 | + getOnDiskWorkspaceFolders, |
| 6 | + showAndLogErrorMessage, |
| 7 | + showAndLogInformationMessage, |
| 8 | +} from './helpers'; |
| 9 | +import { window } from 'vscode'; |
| 10 | +import { ProgressCallback } from './commandRunner'; |
| 11 | + |
| 12 | +const CORE_PACKS = [ |
| 13 | + 'codeql/cpp-all', |
| 14 | + 'codeql/csharp-all', |
| 15 | + 'codeql/go-all', |
| 16 | + 'codeql/java-all', |
| 17 | + 'codeql/javascript-all', |
| 18 | + 'codeql/python-all', |
| 19 | + 'codeql/ruby-all', |
| 20 | +]; |
| 21 | + |
| 22 | +/** |
| 23 | + * Lists all workspace folders that contain a qlpack.yml file. |
| 24 | + * |
| 25 | + * Note: This currently only finds packs at the root of a workspace folder. |
| 26 | + * TODO: Add support for packs in subfolders. |
| 27 | + */ |
| 28 | +function getWorkspacePacks(): string[] { |
| 29 | + const packs: string[] = []; |
| 30 | + const workspaceFolders = getOnDiskWorkspaceFolders(); |
| 31 | + for (const folder of workspaceFolders) { |
| 32 | + const qlpackYml = path.join(folder, 'qlpack.yml'); |
| 33 | + if (fs.pathExistsSync(qlpackYml)) { |
| 34 | + packs.push(folder); |
| 35 | + } |
| 36 | + } |
| 37 | + return packs; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Prompts user to choose packs to download, and downloads them. |
| 42 | + * |
| 43 | + * @param cliServer The CLI server. |
| 44 | + * @param progress A progress callback. |
| 45 | + */ |
| 46 | +export async function handleDownloadPacks( |
| 47 | + cliServer: CodeQLCliServer, |
| 48 | + progress: ProgressCallback, |
| 49 | +): Promise<void> { |
| 50 | + progress({ |
| 51 | + message: 'Choose packs to download', |
| 52 | + step: 1, |
| 53 | + maxStep: 2, |
| 54 | + }); |
| 55 | + let packsToDownload: string[] = []; |
| 56 | + const corePackOption = 'Download core CodeQL packs'; |
| 57 | + const customPackOption = 'Download custom specified pack'; |
| 58 | + const quickpick = await window.showQuickPick( |
| 59 | + [corePackOption, customPackOption], |
| 60 | + { ignoreFocusOut: true } |
| 61 | + ); |
| 62 | + if (quickpick === corePackOption) { |
| 63 | + packsToDownload = CORE_PACKS; |
| 64 | + } else if (quickpick === customPackOption) { |
| 65 | + const customPack = await window.showInputBox({ |
| 66 | + prompt: |
| 67 | + 'Enter the <package-scope/name[@version]> of the pack to download', |
| 68 | + ignoreFocusOut: true, |
| 69 | + }); |
| 70 | + if (customPack) { |
| 71 | + packsToDownload.push(customPack); |
| 72 | + } else { |
| 73 | + void showAndLogErrorMessage('No pack specified.'); |
| 74 | + } |
| 75 | + } |
| 76 | + if (packsToDownload && packsToDownload.length > 0) { |
| 77 | + progress({ |
| 78 | + message: `Downloading ${packsToDownload.join(', ')}`, |
| 79 | + step: 2, |
| 80 | + maxStep: 2, |
| 81 | + }); |
| 82 | + for (const pack of packsToDownload) { |
| 83 | + try { |
| 84 | + await cliServer.packDownload(pack); |
| 85 | + } catch (error) { |
| 86 | + void showAndLogErrorMessage(`Unable to download pack ${pack}. See logs for more details.`); |
| 87 | + } |
| 88 | + } |
| 89 | + void showAndLogInformationMessage('Finished downloading packs.'); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Prompts user to choose packs to install, and installs them. |
| 95 | + * |
| 96 | + * @param cliServer The CLI server. |
| 97 | + * @param progress A progress callback. |
| 98 | + */ |
| 99 | +export async function handleInstallPacks( |
| 100 | + cliServer: CodeQLCliServer, |
| 101 | + progress: ProgressCallback, |
| 102 | +): Promise<void> { |
| 103 | + progress({ |
| 104 | + message: 'Choose packs to install', |
| 105 | + step: 1, |
| 106 | + maxStep: 2, |
| 107 | + }); |
| 108 | + let packsToInstall: string[] = []; |
| 109 | + const workspacePackOption = 'Install workspace packs'; |
| 110 | + const customPackOption = 'Install custom specified pack'; |
| 111 | + const quickpick = await window.showQuickPick( |
| 112 | + [workspacePackOption, customPackOption], |
| 113 | + { ignoreFocusOut: true } |
| 114 | + ); |
| 115 | + if (quickpick === workspacePackOption) { |
| 116 | + packsToInstall = getWorkspacePacks(); |
| 117 | + } else if (quickpick === customPackOption) { |
| 118 | + const customPack = await window.showInputBox({ |
| 119 | + prompt: |
| 120 | + 'Enter the root directory of the pack to install (as an absolute path)', |
| 121 | + ignoreFocusOut: true, |
| 122 | + }); |
| 123 | + if (customPack) { |
| 124 | + packsToInstall.push(customPack); |
| 125 | + } else { |
| 126 | + void showAndLogErrorMessage('No pack specified.'); |
| 127 | + } |
| 128 | + } |
| 129 | + if (packsToInstall && packsToInstall.length > 0) { |
| 130 | + progress({ |
| 131 | + message: `Installing ${packsToInstall.join(', ')}`, |
| 132 | + step: 2, |
| 133 | + maxStep: 2, |
| 134 | + }); |
| 135 | + for (const pack of packsToInstall) { |
| 136 | + try { |
| 137 | + await cliServer.packInstall(pack); |
| 138 | + } catch (error) { |
| 139 | + void showAndLogErrorMessage(`Unable to install pack ${pack}. See logs for more details.`); |
| 140 | + } |
| 141 | + } |
| 142 | + void showAndLogInformationMessage('Finished installing packs.'); |
| 143 | + } |
| 144 | +} |
0 commit comments