@@ -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
}
@@ -414,21 +413,34 @@ Please make sure the folder is not occupied by other procedures .`);
414
413
* @returns True if successful, false on error.
415
414
*/
416
415
protected async runPrePostBuildCommand ( dc : DeviceContext ,
416
+ environment : any ,
417
417
what : "pre" | "post" ) : Promise < boolean > {
418
418
const cmdline = what === "pre"
419
419
? dc . prebuild
420
420
: dc . postbuild ;
421
421
422
422
if ( cmdline ) {
423
423
arduinoChannel . info ( `Running ${ what } -build command: "${ cmdline } "` ) ;
424
- // TODO 2020-02-27, EW: We could call bash -c "cmd" here at least for
425
- // UNIX systems. Windows users must live with their poor system :)
426
- const args = cmdline . split ( / \s + / ) ;
427
- const cmd = args . shift ( ) ;
424
+ let cmd : string ;
425
+ let args : string [ ] ;
426
+ // pre-/post-build commands feature full bash support on UNIX systems.
427
+ // Windows users must live with their poor system unless someone is
428
+ // willing to fight with the annoying Windows cmd escaping -- good luck!
429
+ if ( os . platform ( ) === "win32" ) {
430
+ args = cmdline . split ( / \s + / ) ;
431
+ cmd = args . shift ( ) ;
432
+ } else {
433
+ args = [ "-c" , cmdline ] ;
434
+ cmd = "bash" ;
435
+ }
428
436
try {
429
437
await util . spawn ( cmd ,
430
438
args ,
431
- { shell : true , cwd : ArduinoWorkspace . rootPath } ,
439
+ {
440
+ shell : os . platform ( ) === "win32" ,
441
+ cwd : ArduinoWorkspace . rootPath ,
442
+ env : { ...environment } ,
443
+ } ,
432
444
{ channel : arduinoChannel . channel } ) ;
433
445
} catch ( ex ) {
434
446
const msg = ex . error
@@ -533,23 +545,38 @@ Please make sure the folder is not occupied by other procedures .`);
533
545
arduinoChannel . start ( `${ mode } sketch '${ dc . sketch } '` ) ;
534
546
535
547
if ( buildDir || dc . output ) {
536
- const outputPath = path . resolve ( ArduinoWorkspace . rootPath , buildDir || dc . output ) ;
537
- const dirPath = path . dirname ( outputPath ) ;
548
+ buildDir = path . resolve ( ArduinoWorkspace . rootPath , buildDir || dc . output ) ;
549
+ const dirPath = path . dirname ( buildDir ) ;
538
550
if ( ! util . directoryExistsSync ( dirPath ) ) {
539
- logger . notifyUserError ( "InvalidOutPutPath" , new Error ( constants . messages . INVALID_OUTPUT_PATH + outputPath ) ) ;
551
+ logger . notifyUserError ( "InvalidOutPutPath" , new Error ( constants . messages . INVALID_OUTPUT_PATH + buildDir ) ) ;
540
552
return false ;
541
553
}
542
- args . push ( "--pref" , `build.path=${ outputPath } ` ) ;
543
- arduinoChannel . info ( `Please see the build logs in output path: ${ outputPath } ` ) ;
554
+ args . push ( "--pref" , `build.path=${ buildDir } ` ) ;
555
+ arduinoChannel . info ( `Please see the build logs in output path: ${ buildDir } ` ) ;
544
556
} else {
545
557
const msg = "Output path is not specified. Unable to reuse previously compiled files. Build will be slower. See README." ;
546
558
arduinoChannel . warning ( msg ) ;
547
559
}
548
560
561
+ // Environment variables passed to pre- and post-build commands
562
+ const env = {
563
+ VSCA_BUILD_MODE : mode ,
564
+ VSCA_SKETCH : dc . sketch ,
565
+ VSCA_BOARD : boardDescriptor ,
566
+ VSCA_WORKSPACE_DIR : ArduinoWorkspace . rootPath ,
567
+ VSCA_LOG_LEVEL : verbose ? constants . LogLevel . Verbose : constants . LogLevel . Info ,
568
+ } ;
569
+ if ( dc . port ) {
570
+ env [ "VSCA_SERIAL" ] = dc . port ;
571
+ }
572
+ if ( buildDir ) {
573
+ env [ "VSCA_BUILD_DIR" ] = buildDir ;
574
+ }
575
+
549
576
// TODO EW: What should we do with pre-/post build commands when running
550
577
// analysis? Some could use it to generate/manipulate code which could
551
578
// be a prerequisite for a successful build
552
- if ( ! await this . runPrePostBuildCommand ( dc , "pre" ) ) {
579
+ if ( ! await this . runPrePostBuildCommand ( dc , env , "pre" ) ) {
553
580
return false ;
554
581
}
555
582
@@ -566,7 +593,7 @@ Please make sure the folder is not occupied by other procedures .`);
566
593
const cleanup = async ( result : "ok" | "error" ) => {
567
594
let ret = true ;
568
595
if ( result === "ok" ) {
569
- ret = await this . runPrePostBuildCommand ( dc , "post" ) ;
596
+ ret = await this . runPrePostBuildCommand ( dc , env , "post" ) ;
570
597
}
571
598
await cocopa . conclude ( ) ;
572
599
if ( mode === BuildMode . Upload || mode === BuildMode . UploadProgrammer ) {
@@ -618,7 +645,7 @@ Please make sure the folder is not occupied by other procedures .`);
618
645
undefined ,
619
646
{ stdout : stdoutcb , stderr : stderrcb } ,
620
647
) . then ( async ( ) => {
621
- const ret = await cleanup ( "ok" ) ;
648
+ const ret = await cleanup ( "ok" ) ;
622
649
if ( ret ) {
623
650
arduinoChannel . end ( `${ mode } sketch '${ dc . sketch } '${ os . EOL } ` ) ;
624
651
}
0 commit comments