Add "pack install" and "pack download" commands

This commit is contained in:
shati-patel
2022-01-13 16:18:02 +00:00
committed by Shati Patel
parent 51906cbbda
commit 6d7b02583d
4 changed files with 181 additions and 0 deletions

View File

@@ -374,6 +374,14 @@
"command": "codeQL.clearCache",
"title": "CodeQL: Clear Cache"
},
{
"command": "codeQL.installPacks",
"title": "CodeQL: Install Packs"
},
{
"command": "codeQL.downloadPacks",
"title": "CodeQL: Download Packs"
},
{
"command": "codeQLDatabases.setCurrentDatabase",
"title": "Set Current Database"

View File

@@ -845,6 +845,14 @@ export class CodeQLCliServer implements Disposable {
);
}
/**
* Downloads a specified pack.
* @param pack The `<package-scope/name[@version]>` of the pack to download.
*/
async packDownload(pack: string) {
return this.runJsonCodeQlCliCommand(['pack', 'download'], [pack], 'Downloading packs');
}
async packInstall(dir: string) {
return this.runJsonCodeQlCliCommand(['pack', 'install'], [dir], 'Installing pack dependencies');
}

View File

@@ -83,6 +83,7 @@ import { RemoteQuery } from './remote-queries/remote-query';
import { URLSearchParams } from 'url';
import { RemoteQueriesInterfaceManager } from './remote-queries/remote-queries-interface';
import { sampleRemoteQuery, sampleRemoteQueryResult } from './remote-queries/sample-data';
import { handleDownloadPacks, handleInstallPacks } from './packaging';
/**
* extension.ts
@@ -922,6 +923,26 @@ async function activateWithInstalledDistribution(
}
}));
ctx.subscriptions.push(
commandRunnerWithProgress('codeQL.installPacks', async (
progress: ProgressCallback
) =>
await handleInstallPacks(cliServer, progress),
{
title: 'Installing packs',
}
));
ctx.subscriptions.push(
commandRunnerWithProgress('codeQL.downloadPacks', async (
progress: ProgressCallback
) =>
await handleDownloadPacks(cliServer, progress),
{
title: 'Downloading packs',
}
));
commands.registerCommand('codeQL.showLogs', () => {
logger.show();
});

View File

@@ -0,0 +1,144 @@
import { CodeQLCliServer } from './cli';
import * as fs from 'fs-extra';
import * as path from 'path';
import {
getOnDiskWorkspaceFolders,
showAndLogErrorMessage,
showAndLogInformationMessage,
} from './helpers';
import { window } from 'vscode';
import { ProgressCallback } from './commandRunner';
const CORE_PACKS = [
'codeql/cpp-all',
'codeql/csharp-all',
'codeql/go-all',
'codeql/java-all',
'codeql/javascript-all',
'codeql/python-all',
'codeql/ruby-all',
];
/**
* Lists all workspace folders that contain a qlpack.yml file.
*
* Note: This currently only finds packs at the root of a workspace folder.
* TODO: Add support for packs in subfolders.
*/
function getWorkspacePacks(): string[] {
const packs: string[] = [];
const workspaceFolders = getOnDiskWorkspaceFolders();
for (const folder of workspaceFolders) {
const qlpackYml = path.join(folder, 'qlpack.yml');
if (fs.pathExistsSync(qlpackYml)) {
packs.push(folder);
}
}
return packs;
}
/**
* Prompts user to choose packs to download, and downloads them.
*
* @param cliServer The CLI server.
* @param progress A progress callback.
*/
export async function handleDownloadPacks(
cliServer: CodeQLCliServer,
progress: ProgressCallback,
): Promise<void> {
progress({
message: 'Choose packs to download',
step: 1,
maxStep: 2,
});
let packsToDownload: string[] = [];
const corePackOption = 'Download core CodeQL packs';
const customPackOption = 'Download custom specified pack';
const quickpick = await window.showQuickPick(
[corePackOption, customPackOption],
{ ignoreFocusOut: true }
);
if (quickpick === corePackOption) {
packsToDownload = CORE_PACKS;
} else if (quickpick === customPackOption) {
const customPack = await window.showInputBox({
prompt:
'Enter the <package-scope/name[@version]> of the pack to download',
ignoreFocusOut: true,
});
if (customPack) {
packsToDownload.push(customPack);
} else {
void showAndLogErrorMessage('No pack specified.');
}
}
if (packsToDownload && packsToDownload.length > 0) {
progress({
message: `Downloading ${packsToDownload.join(', ')}`,
step: 2,
maxStep: 2,
});
for (const pack of packsToDownload) {
try {
await cliServer.packDownload(pack);
} catch (error) {
void showAndLogErrorMessage(`Unable to download pack ${pack}. See logs for more details.`);
}
}
void showAndLogInformationMessage('Finished downloading packs.');
}
}
/**
* Prompts user to choose packs to install, and installs them.
*
* @param cliServer The CLI server.
* @param progress A progress callback.
*/
export async function handleInstallPacks(
cliServer: CodeQLCliServer,
progress: ProgressCallback,
): Promise<void> {
progress({
message: 'Choose packs to install',
step: 1,
maxStep: 2,
});
let packsToInstall: string[] = [];
const workspacePackOption = 'Install workspace packs';
const customPackOption = 'Install custom specified pack';
const quickpick = await window.showQuickPick(
[workspacePackOption, customPackOption],
{ ignoreFocusOut: true }
);
if (quickpick === workspacePackOption) {
packsToInstall = getWorkspacePacks();
} else if (quickpick === customPackOption) {
const customPack = await window.showInputBox({
prompt:
'Enter the root directory of the pack to install (as an absolute path)',
ignoreFocusOut: true,
});
if (customPack) {
packsToInstall.push(customPack);
} else {
void showAndLogErrorMessage('No pack specified.');
}
}
if (packsToInstall && packsToInstall.length > 0) {
progress({
message: `Installing ${packsToInstall.join(', ')}`,
step: 2,
maxStep: 2,
});
for (const pack of packsToInstall) {
try {
await cliServer.packInstall(pack);
} catch (error) {
void showAndLogErrorMessage(`Unable to install pack ${pack}. See logs for more details.`);
}
}
void showAndLogInformationMessage('Finished installing packs.');
}
}