@@ -133,8 +133,7 @@ export class ArduinoApp {
133
133
public async setPref ( key , value ) {
134
134
try {
135
135
await util . spawn ( this . _settings . commandPath ,
136
- null ,
137
- [ "--pref" , `${ key } =${ value } ` , "--save-prefs" ] ) ;
136
+ [ "--pref" , `${ key } =${ value } ` , "--save-prefs" ] ) ;
138
137
} catch ( ex ) {
139
138
}
140
139
}
@@ -331,30 +330,45 @@ export class ArduinoApp {
331
330
arduinoChannel . start ( `${ buildMode } sketch '${ dc . sketch } '` ) ;
332
331
333
332
if ( ( buildDir || dc . output ) && compile ) {
334
- const outputPath = path . resolve ( ArduinoWorkspace . rootPath , buildDir || dc . output ) ;
335
- const dirPath = path . dirname ( outputPath ) ;
333
+ buildDir = path . resolve ( ArduinoWorkspace . rootPath , buildDir || dc . output ) ;
334
+ const dirPath = path . dirname ( buildDir ) ;
336
335
if ( ! util . directoryExistsSync ( dirPath ) ) {
337
- logger . notifyUserError ( "InvalidOutPutPath" , new Error ( constants . messages . INVALID_OUTPUT_PATH + outputPath ) ) ;
336
+ logger . notifyUserError ( "InvalidOutPutPath" , new Error ( constants . messages . INVALID_OUTPUT_PATH + buildDir ) ) ;
338
337
return false ;
339
338
}
340
339
341
340
if ( this . useArduinoCli ( ) ) {
342
- args . push ( "--build-path" , outputPath ) ;
341
+ args . push ( "--build-path" , buildDir ) ;
343
342
344
343
} else {
345
- args . push ( "--pref" , `build.path=${ outputPath } ` ) ;
344
+ args . push ( "--pref" , `build.path=${ buildDir } ` ) ;
346
345
}
347
346
348
- arduinoChannel . info ( `Please see the build logs in output path: ${ outputPath } ` ) ;
347
+ arduinoChannel . info ( `Please see the build logs in output path: ${ buildDir } ` ) ;
349
348
} else {
350
349
const msg = "Output path is not specified. Unable to reuse previously compiled files. Build will be slower. See README." ;
351
350
arduinoChannel . warning ( msg ) ;
352
351
}
353
352
353
+ // Environment variables passed to pre- and post-build commands
354
+ const env = {
355
+ VSCA_BUILD_MODE : buildMode ,
356
+ VSCA_SKETCH : dc . sketch ,
357
+ VSCA_BOARD : boardDescriptor ,
358
+ VSCA_WORKSPACE_DIR : ArduinoWorkspace . rootPath ,
359
+ VSCA_LOG_LEVEL : verbose ? constants . LogLevel . Verbose : constants . LogLevel . Info ,
360
+ } ;
361
+ if ( dc . port ) {
362
+ env [ "VSCA_SERIAL" ] = dc . port ;
363
+ }
364
+ if ( buildDir ) {
365
+ env [ "VSCA_BUILD_DIR" ] = buildDir ;
366
+ }
367
+
354
368
// TODO EW: What should we do with pre-/post build commands when running
355
369
// analysis? Some could use it to generate/manipulate code which could
356
370
// be a prerequisite for a successful build
357
- if ( ! await this . runPrePostBuildCommand ( dc , "pre" ) ) {
371
+ if ( ! await this . runPrePostBuildCommand ( dc , env , "pre" ) ) {
358
372
return false ;
359
373
}
360
374
@@ -370,7 +384,7 @@ export class ArduinoApp {
370
384
const cleanup = async ( result : "ok" | "error" ) => {
371
385
let ret = true ;
372
386
if ( result === "ok" ) {
373
- ret = await this . runPrePostBuildCommand ( dc , "post" ) ;
387
+ ret = await this . runPrePostBuildCommand ( dc , env , "post" ) ;
374
388
}
375
389
await cocopa . conclude ( ) ;
376
390
if ( buildMode === BuildMode . Upload || buildMode === BuildMode . UploadProgrammer ) {
@@ -689,21 +703,34 @@ export class ArduinoApp {
689
703
* @returns True if successful, false on error.
690
704
*/
691
705
protected async runPrePostBuildCommand ( dc : DeviceContext ,
706
+ environment : any ,
692
707
what : "pre" | "post" ) : Promise < boolean > {
693
708
const cmdline = what === "pre"
694
709
? dc . prebuild
695
710
: dc . postbuild ;
696
711
697
712
if ( cmdline ) {
698
713
arduinoChannel . info ( `Running ${ what } -build command: "${ cmdline } "` ) ;
699
- // TODO 2020-02-27, EW: We could call bash -c "cmd" here at least for
700
- // UNIX systems. Windows users must live with their poor system :)
701
- const args = cmdline . split ( / \s + / ) ;
702
- const cmd = args . shift ( ) ;
714
+ let cmd : string ;
715
+ let args : string [ ] ;
716
+ // pre-/post-build commands feature full bash support on UNIX systems.
717
+ // Windows users must live with their poor system unless someone is
718
+ // willing to fight with the annoying Windows cmd escaping -- good luck!
719
+ if ( os . platform ( ) === "win32" ) {
720
+ args = cmdline . split ( / \s + / ) ;
721
+ cmd = args . shift ( ) ;
722
+ } else {
723
+ args = [ "-c" , cmdline ] ;
724
+ cmd = "bash" ;
725
+ }
703
726
try {
704
727
await util . spawn ( cmd ,
705
728
args ,
706
- { shell : true , cwd : ArduinoWorkspace . rootPath } ,
729
+ {
730
+ shell : os . platform ( ) === "win32" ,
731
+ cwd : ArduinoWorkspace . rootPath ,
732
+ env : { ...environment } ,
733
+ } ,
707
734
{ channel : arduinoChannel . channel } ) ;
708
735
} catch ( ex ) {
709
736
const msg = ex . error
0 commit comments