Skip to content

Commit 31c90a5

Browse files
committed
Remove import cycle for CLI server
1 parent 2ce6fd3 commit 31c90a5

File tree

3 files changed

+53
-48
lines changed

3 files changed

+53
-48
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { execFile } from "child_process";
2+
import { promisify } from "util";
3+
4+
import type { BaseLogger } from "../common/logging";
5+
import type { ProgressReporter } from "../common/logging/vscode";
6+
import { getChildProcessErrorMessage } from "../common/helpers-pure";
7+
8+
/**
9+
* Flags to pass to all cli commands.
10+
*/
11+
export const LOGGING_FLAGS = ["-v", "--log-to-stderr"];
12+
13+
/**
14+
* Runs a CodeQL CLI command without invoking the CLI server, returning the output as a string.
15+
* @param codeQlPath The path to the CLI.
16+
* @param command The `codeql` command to be run, provided as an array of command/subcommand names.
17+
* @param commandArgs The arguments to pass to the `codeql` command.
18+
* @param description Description of the action being run, to be shown in log and error messages.
19+
* @param logger Logger to write command log messages, e.g. to an output channel.
20+
* @param progressReporter Used to output progress messages, e.g. to the status bar.
21+
* @returns The contents of the command's stdout, if the command succeeded.
22+
*/
23+
export async function runCodeQlCliCommand(
24+
codeQlPath: string,
25+
command: string[],
26+
commandArgs: string[],
27+
description: string,
28+
logger: BaseLogger,
29+
progressReporter?: ProgressReporter,
30+
): Promise<string> {
31+
// Add logging arguments first, in case commandArgs contains positional parameters.
32+
const args = command.concat(LOGGING_FLAGS).concat(commandArgs);
33+
const argsString = args.join(" ");
34+
try {
35+
if (progressReporter !== undefined) {
36+
progressReporter.report({ message: description });
37+
}
38+
void logger.log(
39+
`${description} using CodeQL CLI: ${codeQlPath} ${argsString}...`,
40+
);
41+
const result = await promisify(execFile)(codeQlPath, args);
42+
void logger.log(result.stderr);
43+
void logger.log("CLI command succeeded.");
44+
return result.stdout;
45+
} catch (err) {
46+
throw new Error(
47+
`${description} failed: ${getChildProcessErrorMessage(err)}`,
48+
);
49+
}
50+
}

extensions/ql-vscode/src/codeql-cli/cli-version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SemVer } from "semver";
22
import { parse } from "semver";
3-
import { runCodeQlCliCommand } from "./cli";
3+
import { runCodeQlCliCommand } from "./cli-command";
44
import type { Logger } from "../common/logging";
55
import { getErrorMessage } from "../common/helpers-pure";
66

extensions/ql-vscode/src/codeql-cli/cli.ts

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { EOL } from "os";
22
import { spawn } from "child-process-promise";
33
import type { ChildProcessWithoutNullStreams } from "child_process";
4-
import { execFile, spawn as spawnChildProcess } from "child_process";
4+
import { spawn as spawnChildProcess } from "child_process";
55
import { readFile } from "fs-extra";
66
import { delimiter, dirname, join } from "path";
77
import type { Log } from "sarif";
88
import { SemVer } from "semver";
99
import type { Readable } from "stream";
1010
import tk from "tree-kill";
11-
import { promisify } from "util";
1211
import type { CancellationToken, Disposable, Uri } from "vscode";
1312

1413
import type {
@@ -21,7 +20,6 @@ import type { DistributionProvider } from "./distribution";
2120
import { FindDistributionResultKind } from "./distribution";
2221
import {
2322
assertNever,
24-
getChildProcessErrorMessage,
2523
getErrorMessage,
2624
getErrorStack,
2725
} from "../common/helpers-pure";
@@ -35,6 +33,7 @@ import type { App } from "../common/app";
3533
import { QueryLanguage } from "../common/query-language";
3634
import { LINE_ENDINGS, splitStreamAtSeparators } from "../common/split-stream";
3735
import type { Position } from "../query-server/messages";
36+
import { LOGGING_FLAGS } from "./cli-command";
3837

3938
/**
4039
* The version of the SARIF format that we are using.
@@ -46,11 +45,6 @@ const SARIF_FORMAT = "sarifv2.1.0";
4645
*/
4746
const CSV_FORMAT = "csv";
4847

49-
/**
50-
* Flags to pass to all cli commands.
51-
*/
52-
const LOGGING_FLAGS = ["-v", "--log-to-stderr"];
53-
5448
/**
5549
* The expected output of `codeql resolve queries --format bylanguage`.
5650
*/
@@ -1632,45 +1626,6 @@ export function spawnServer(
16321626
return child;
16331627
}
16341628

1635-
/**
1636-
* Runs a CodeQL CLI command without invoking the CLI server, returning the output as a string.
1637-
* @param codeQlPath The path to the CLI.
1638-
* @param command The `codeql` command to be run, provided as an array of command/subcommand names.
1639-
* @param commandArgs The arguments to pass to the `codeql` command.
1640-
* @param description Description of the action being run, to be shown in log and error messages.
1641-
* @param logger Logger to write command log messages, e.g. to an output channel.
1642-
* @param progressReporter Used to output progress messages, e.g. to the status bar.
1643-
* @returns The contents of the command's stdout, if the command succeeded.
1644-
*/
1645-
export async function runCodeQlCliCommand(
1646-
codeQlPath: string,
1647-
command: string[],
1648-
commandArgs: string[],
1649-
description: string,
1650-
logger: Logger,
1651-
progressReporter?: ProgressReporter,
1652-
): Promise<string> {
1653-
// Add logging arguments first, in case commandArgs contains positional parameters.
1654-
const args = command.concat(LOGGING_FLAGS).concat(commandArgs);
1655-
const argsString = args.join(" ");
1656-
try {
1657-
if (progressReporter !== undefined) {
1658-
progressReporter.report({ message: description });
1659-
}
1660-
void logger.log(
1661-
`${description} using CodeQL CLI: ${codeQlPath} ${argsString}...`,
1662-
);
1663-
const result = await promisify(execFile)(codeQlPath, args);
1664-
void logger.log(result.stderr);
1665-
void logger.log("CLI command succeeded.");
1666-
return result.stdout;
1667-
} catch (err) {
1668-
throw new Error(
1669-
`${description} failed: ${getChildProcessErrorMessage(err)}`,
1670-
);
1671-
}
1672-
}
1673-
16741629
/**
16751630
* Log a text stream to a `Logger` interface.
16761631
* @param stream The stream to log.

0 commit comments

Comments
 (0)