Skip to content

Commit e388108

Browse files
committed
wip working dynamic args
1 parent 44ce9a4 commit e388108

3 files changed

Lines changed: 53 additions & 42 deletions

File tree

src/extension.ts

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as vscode from 'vscode';
22
import * as cp from 'child_process';
33
import * as path from "path";
4-
import * as os from "os";
54
import * as xml2js from 'xml2js';
65

76
import { runScript } from './util/scripts';
7+
import { resolvePath } from './util/path';
88

99
enum SeverityNumber {
1010
Info = 0,
@@ -59,37 +59,9 @@ function parseMinSeverity(str: string): SeverityNumber {
5959
}
6060
}
6161

62-
export function resolvePath(argPath: string): string {
63-
const folders = vscode.workspace.workspaceFolders;
64-
const workspaceRoot = folders && folders.length > 0
65-
? folders[0].uri.fsPath
66-
: process.cwd();
67-
68-
// Expand ${workspaceFolder}
69-
if (argPath.includes("${workspaceFolder}")) {
70-
argPath = argPath.replace("${workspaceFolder}", workspaceRoot);
71-
}
72-
73-
// Expand tilde (~) to home directory
74-
if (argPath.startsWith("~")) {
75-
argPath = path.join(os.homedir(), argPath.slice(1));
76-
}
77-
78-
// Expand ./ or ../ relative paths (relative to workspace root if available)
79-
if (argPath.startsWith("./") || argPath.startsWith("../")) {
80-
argPath = path.resolve(workspaceRoot, argPath);
81-
}
82-
83-
// If still not absolute, treat it as relative to workspace root
84-
if (!path.isAbsolute(argPath)) {
85-
argPath = path.join(workspaceRoot, argPath);
86-
}
87-
return argPath;
88-
}
89-
9062
// This method is called when your extension is activated.
9163
// Your extension is activated the very first time the command is executed.
92-
export function activate(context: vscode.ExtensionContext) {
64+
export async function activate(context: vscode.ExtensionContext) {
9365

9466
// Create a diagnostic collection.
9567
const diagnosticCollection = vscode.languages.createDiagnosticCollection("Cppcheck");
@@ -98,17 +70,21 @@ export function activate(context: vscode.ExtensionContext) {
9870
// If an argument requires us to run any scripts we do it here
9971
const config = vscode.workspace.getConfiguration();
10072
const args = config.get<string>("cppcheck-official.arguments", "");
101-
const argsWithScripts = args.split(" ").filter((arg) => arg.includes('§{'));
102-
argsWithScripts.forEach(async (arg) => {
73+
const argsWithScripts = args.split("--").filter((arg) => arg.includes('${'));
74+
for (const arg of argsWithScripts) {
10375
// argType will look like e.g. --project
10476
const argType = arg.split("=")[0];
10577
const argValue = arg.split("=")[1];
10678
// Remove ${ from the beginning and slice } away from the end of argValue
10779
const scriptCommand = argValue.split("{")[1].slice(0, - 1);
10880
const scriptOutput = await runScript(scriptCommand);
109-
dynamicArgs.push(`${argType}=${scriptOutput}`);
110-
});
111-
81+
console.log('scriptOutput', scriptOutput);
82+
// We expect the script output that we are to set the argument to will be wrapped with ${}
83+
const scriptOutputPath = scriptOutput.split("${")[1].split("}")[0];
84+
dynamicArgs.push(`${argType}=${scriptOutputPath}`);
85+
console.log('dynamic args', dynamicArgs);
86+
};
87+
11288
// set up a map of timers per document URI for debounce for continuous analysis triggers
11389
// I.e. document has been changed -> DEBOUNCE_MS time passed since last change -> run cppcheck
11490
const debounceTimers: Map<string, NodeJS.Timeout> = new Map();
@@ -218,14 +194,14 @@ async function runCppcheckOnFileXML(
218194
const minSevNum = parseMinSeverity(minSevString);
219195

220196
// Arguments specified with scripts are replaced with script output (dynamicArgs)
221-
const staticArgs = extraArgs.split(" ").filter((arg) => !arg.includes("${"));
197+
const staticArgs = extraArgs.split("--").filter((arg) => !arg.includes("${"));
222198
const allArgs = staticArgs.concat(dynamicArgs);
223-
199+
console.log('all args', allArgs);
224200
// Resolve paths for arguments where applicable
225201
const extraArgsParsed = allArgs.map((arg) => {
226-
if (arg.startsWith('--project')) {
202+
if (arg.startsWith('project')) {
227203
const splitArg = arg.split('=');
228-
return `${splitArg[0]}=${resolvePath(splitArg[1])}`;
204+
return `--${splitArg[0]}=${resolvePath(splitArg[1])}`;
229205
}
230206
return arg;
231207
});

src/util/path.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as path from "path";
2+
import * as os from "os";
3+
import * as vscode from 'vscode';
4+
5+
export function resolvePath(argPath: string): string {
6+
const folders = vscode.workspace.workspaceFolders;
7+
const workspaceRoot = folders && folders.length > 0
8+
? folders[0].uri.fsPath
9+
: process.cwd();
10+
11+
// Expand ${workspaceFolder}
12+
if (argPath.includes("${workspaceFolder}")) {
13+
argPath = argPath.replace("${workspaceFolder}", workspaceRoot);
14+
}
15+
16+
// Expand tilde (~) to home directory
17+
if (argPath.startsWith("~")) {
18+
argPath = path.join(os.homedir(), argPath.slice(1));
19+
}
20+
21+
// Expand ./ or ../ relative paths (relative to workspace root if available)
22+
if (argPath.startsWith("./") || argPath.startsWith("../")) {
23+
argPath = path.resolve(workspaceRoot, argPath);
24+
}
25+
26+
// If still not absolute, treat it as relative to workspace root
27+
if (!path.isAbsolute(argPath)) {
28+
argPath = path.join(workspaceRoot, argPath);
29+
}
30+
return argPath;
31+
}

src/util/scripts.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { execFile } from "child_process";
2+
import { resolvePath } from './path';
23

34
function runScript(scriptCommand: string): Promise<string> {
4-
const commandSplit = scriptCommand.split(" ");
5+
const commandSplit = scriptCommand.split(" ");
56
const scriptLang = commandSplit[0];
6-
const scriptPath = commandSplit[1];
7+
const scriptPath = resolvePath(commandSplit[1]);
8+
console.log(`executing script ${scriptPath} with language ${scriptLang}`);
9+
const workspaceFolder = resolvePath('${workspaceFolder}');
10+
console.log('workspaceFolder', workspaceFolder);
711
// Additional args could be added here, i.e. name of output file if applicable
812
return new Promise((resolve, reject) => {
9-
execFile(scriptLang, [scriptPath], (error, stdout, stderr) => {
13+
execFile(scriptLang, [scriptPath], { cwd: workspaceFolder }, (error, stdout, stderr) => {
1014
if (error) {
1115
reject(error);
1216
return;

0 commit comments

Comments
 (0)