11import * as vscode from 'vscode' ;
22import * as cp from 'child_process' ;
33import * as path from "path" ;
4- import * as os from "os" ;
54import * as xml2js from 'xml2js' ;
65
76import { runScript } from './util/scripts' ;
7+ import { resolvePath } from './util/path' ;
88
99enum 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 } ) ;
0 commit comments