Skip to content

Commit

Permalink
Use "findLanguage" in relevant places
Browse files Browse the repository at this point in the history
- When running on multiple databases
- When running a remote query
  • Loading branch information
shati-patel committed Jul 28, 2021
1 parent af7bc8e commit b8a90b7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 32 deletions.
13 changes: 13 additions & 0 deletions extensions/ql-vscode/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export interface UpgradesInfo {
*/
export type QlpacksInfo = { [name: string]: string[] };

/**
* The expected output of `codeql resolve languages`.
*/
export type LanguagesInfo = { [name: string]: string[] };

/**
* The expected output of `codeql resolve qlref`.
*/
Expand Down Expand Up @@ -748,6 +753,14 @@ export class CodeQLCliServer implements Disposable {
);
}

/**
* Gets information about the available languages.
* @returns A dictionary mapping language name to the directory it comes from
*/
async resolveLanguages(): Promise<LanguagesInfo> {
return await this.runJsonCodeQlCliCommand<LanguagesInfo>(['resolve', 'languages'], [], 'Resolving languages');
}

/**
* Gets information about queries in a query suite.
* @param suite The suite to resolve.
Expand Down
36 changes: 9 additions & 27 deletions extensions/ql-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import {
import { CodeQlStatusBarHandler } from './status-bar';

import { Credentials } from './authentication';
import runRemoteQuery from './run-remote-query';
import { runRemoteQuery, findLanguage } from './run-remote-query';

/**
* extension.ts
Expand Down Expand Up @@ -559,30 +559,6 @@ async function activateWithInstalledDistribution(
}
)
);
/**
* Finds the language that a query targets.
* If it can't be autodetected, prompt the user to specify the language manually.
*/
async function findLanguage(
queryUri: Uri
): Promise<string> {
let language = '';
try {
const queryInfo = await cliServer.resolveQueryByLanguage(helpers.getOnDiskWorkspaceFolders(), queryUri || window.activeTextEditor?.document.uri);
language = (Object.keys(queryInfo.byLanguage))[0];
} catch (e) {
// Add an option to manually specify the language, in case automatic language detection fails.
void logger.log('Could not autodetect query language. Select language manually.');
language = await window.showQuickPick(
['cpp', 'csharp', 'go', 'java', 'javascript', 'ruby', 'python'],
{ placeHolder: 'Select target language for your query', ignoreFocusOut: true }
) || '';
}
if (language === '') {
throw new Error('Language not found');
}
return language;
}
interface DatabaseQuickPickItem extends QuickPickItem {
databaseItem: DatabaseItem;
}
Expand All @@ -594,7 +570,13 @@ async function activateWithInstalledDistribution(
token: CancellationToken,
uri: Uri | undefined
) => {
const quickPickItems = dbm.databaseItems.map<DatabaseQuickPickItem>(dbItem => (
const queryLanguage = await findLanguage(cliServer, uri);
const filteredDBs = dbm.databaseItems.filter(db => db.language === queryLanguage);
if (filteredDBs.length === 0) {
void helpers.showAndLogErrorMessage(`No databases found for language ${queryLanguage}`);
return;
}
const quickPickItems = filteredDBs.map<DatabaseQuickPickItem>(dbItem => (
{
databaseItem: dbItem,
label: dbItem.name,
Expand Down Expand Up @@ -731,7 +713,7 @@ async function activateWithInstalledDistribution(
) => {
if (isCanary()) {
const credentials = await Credentials.initialize(ctx);
await runRemoteQuery(credentials, uri || window.activeTextEditor?.document.uri);
await runRemoteQuery(cliServer, credentials, uri || window.activeTextEditor?.document.uri);
}
})
);
Expand Down
41 changes: 36 additions & 5 deletions extensions/ql-vscode/src/run-remote-query.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
import { Uri } from 'vscode';
import { Uri, window } from 'vscode';
import * as yaml from 'js-yaml';
import * as fs from 'fs-extra';
import { showAndLogErrorMessage, showAndLogInformationMessage } from './helpers';
import { getOnDiskWorkspaceFolders, showAndLogErrorMessage, showAndLogInformationMessage } from './helpers';
import { Credentials } from './authentication';
import * as cli from './cli';
import { logger } from './logging';

interface Config {
repositories: string[];
ref?: string;
language: string;
language?: string;
}

// Test "controller" repository and workflow.
const OWNER = 'dsp-testing';
const REPO = 'qc-controller';

export default async function runRemoteQuery(credentials: Credentials, uri?: Uri) {
/**
* Finds the language that a query targets.
* If it can't be autodetected, prompt the user to specify the language manually.
*/
export async function findLanguage(
cliServer: cli.CodeQLCliServer,
queryUri: Uri | undefined
): Promise<string> {
const uri = queryUri || window.activeTextEditor?.document.uri;
if (uri !== undefined) {
try {
const queryInfo = await cliServer.resolveQueryByLanguage(getOnDiskWorkspaceFolders(), uri);
return (Object.keys(queryInfo.byLanguage))[0];
} catch (e) {
void logger.log('Could not autodetect query language. Select language manually.');
}
}
const availableLanguages = Object.keys(await cliServer.resolveLanguages());
const language = await window.showQuickPick(
availableLanguages,
{ placeHolder: 'Select target language for your query', ignoreFocusOut: true }
) || '';
if (language === '') {
// This only happens if the user cancels the quick pick.
void showAndLogErrorMessage('Language not found. Language must be specified manually.');
}
return language;
}

export async function runRemoteQuery(cliServer: cli.CodeQLCliServer, credentials: Credentials, uri?: Uri) {
if (!uri?.fsPath.endsWith('.ql')) {
return;
}
Expand All @@ -34,7 +65,7 @@ export default async function runRemoteQuery(credentials: Credentials, uri?: Uri
const config = yaml.safeLoad(await fs.readFile(repositoriesFile, 'utf8')) as Config;

const ref = config.ref || 'main';
const language = config.language;
const language = config.language || await findLanguage(cliServer, uri);
const repositories = config.repositories;

try {
Expand Down

0 comments on commit b8a90b7

Please sign in to comment.