@@ -384,6 +384,19 @@ public override bool Execute()
384
384
UseShellExecute = false ,
385
385
} ;
386
386
387
+ var output = new DataReceivedEventHandler ( ( o , e ) =>
388
+ {
389
+ if ( e . Data != null )
390
+ {
391
+ if ( this . Log . LogMessageFromText ( e . Data , MessageImportance . High ) == false )
392
+ {
393
+ // plain text
394
+ this . Log . LogMessage ( MessageImportance . High , e . Data ) ;
395
+ Console . WriteLine ( e . Data ) ;
396
+ }
397
+ }
398
+ } ) ;
399
+
387
400
// non-windows?
388
401
if ( Environment . OSVersion . Platform != PlatformID . Win32NT )
389
402
{
@@ -392,29 +405,35 @@ public override bool Execute()
392
405
}
393
406
394
407
//
395
- var process = Process . Start ( pi ) ;
396
- var dataReceived = new DataReceivedEventHandler ( ( o , e ) =>
408
+ this . Log . LogCommandLine ( MessageImportance . High , $ "{ pi . FileName } { pi . Arguments } ") ;
409
+
410
+ //
411
+ using ( var process = new Process ( ) { StartInfo = pi , } )
397
412
{
398
- this . Log . LogMessageFromText ( e . Data , MessageImportance . High ) ;
399
- } ) ;
413
+ process . OutputDataReceived += output ;
414
+ process . ErrorDataReceived += output ;
400
415
401
- process . OutputDataReceived += dataReceived ;
402
- process . ErrorDataReceived += dataReceived ;
416
+ process . Start ( ) ;
417
+
418
+ process . BeginOutputReadLine ( ) ;
419
+ process . BeginErrorReadLine ( ) ;
403
420
404
- using ( var cancellationHandler = cancellation . Register ( ( ) =>
405
- {
406
- if ( process . HasExited == false )
421
+ //
422
+ using ( var cancellationHandler = cancellation . Register ( ( ) =>
423
+ {
424
+ if ( process . HasExited == false )
425
+ {
426
+ this . Log . LogMessageFromText ( "Cancelled by user" , MessageImportance . High ) ;
427
+ // TODO: send signal first
428
+ process . Kill ( ) ;
429
+ }
430
+ } ) )
407
431
{
408
- this . Log . LogMessageFromText ( "Cancelled by user" , MessageImportance . High ) ;
409
- // TODO: send signal first
410
- process . Kill ( ) ;
432
+ process . WaitForExit ( ) ;
433
+
434
+ //
435
+ return process . ExitCode == 0 ;
411
436
}
412
- } ) )
413
- {
414
- process . WaitForExit ( ) ;
415
- process . StandardOutput . ReadToEnd ( ) ;
416
- //
417
- return process . ExitCode == 0 ;
418
437
}
419
438
}
420
439
@@ -464,8 +483,8 @@ static string FlatternArgs(string[] args)
464
483
// sanitize {arg}
465
484
sb . Append ( '\" ' ) ;
466
485
sb . Append ( arg . Trim ( )
467
- //.Replace("\\", "\\\\")
468
- //.Replace("\"", "\\\"")
486
+ //.Replace("\\", "\\\\")
487
+ //.Replace("\"", "\\\"")
469
488
) ;
470
489
sb . Append ( '\" ' ) ;
471
490
}
@@ -530,77 +549,5 @@ bool HasDebugPlus
530
549
return false ;
531
550
}
532
551
}
533
-
534
- // honestly I don't know why msbuild in VS does not handle Console.Output,
535
- // so we have our custom TextWriter that we pass to Log
536
- sealed class LogWriter : TextWriter
537
- {
538
- TaskLoggingHelper Log { get ; }
539
-
540
- StringBuilder Buffer { get ; } = new StringBuilder ( ) ;
541
-
542
- public override Encoding Encoding => Encoding . UTF8 ;
543
-
544
- public LogWriter ( TaskLoggingHelper log )
545
- {
546
- Debug . Assert ( log != null ) ;
547
-
548
- this . Log = log ;
549
- this . NewLine = "\n " ;
550
- }
551
-
552
- bool TryLogCompleteMessage ( )
553
- {
554
- string line = null ;
555
-
556
- lock ( Buffer ) // accessed in parallel
557
- {
558
- // get line from the buffer:
559
- for ( int i = 0 ; i < Buffer . Length ; i ++ )
560
- {
561
- if ( Buffer [ i ] == '\n ' )
562
- {
563
- line = Buffer . ToString ( 0 , i ) ;
564
-
565
- Buffer . Remove ( 0 , i + 1 ) ;
566
- }
567
- }
568
- }
569
-
570
- //
571
- return line != null && LogCompleteMessage ( line ) ;
572
- }
573
-
574
- bool LogCompleteMessage ( string line )
575
- {
576
- // TODO: following logs only Warnings and Errors,
577
- // to log Info diagnostics properly, parse it by ourselves
578
-
579
- return this . Log . LogMessageFromText ( line . Trim ( ) , MessageImportance . High ) ;
580
- }
581
-
582
- public override void Write ( char value )
583
- {
584
- lock ( Buffer ) // accessed in parallel
585
- {
586
- Buffer . Append ( value ) ;
587
- }
588
-
589
- if ( value == '\n ' )
590
- {
591
- TryLogCompleteMessage ( ) ;
592
- }
593
- }
594
-
595
- public override void Write ( string value )
596
- {
597
- lock ( Buffer )
598
- {
599
- Buffer . Append ( value ) ;
600
- }
601
-
602
- TryLogCompleteMessage ( ) ;
603
- }
604
- }
605
552
}
606
553
}
0 commit comments