@@ -34,7 +34,9 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
34
34
35
35
#region Private variables
36
36
List < string > processedPaths ;
37
- private int totalDiagnosticCount = 0 ;
37
+ // initialize to zero for all severity enum values
38
+ private Dictionary < DiagnosticSeverity , int > diagnosticCounts =
39
+ Enum . GetValues ( typeof ( DiagnosticSeverity ) ) . Cast < DiagnosticSeverity > ( ) . ToDictionary ( s => s , _ => 0 ) ;
38
40
#endregion // Private variables
39
41
40
42
#region Parameters
@@ -414,8 +416,36 @@ protected override void EndProcessing()
414
416
ScriptAnalyzer . Instance . CleanUp ( ) ;
415
417
base . EndProcessing ( ) ;
416
418
417
- if ( EnableExit ) {
418
- this . Host . SetShouldExit ( totalDiagnosticCount ) ;
419
+ var infoCount = diagnosticCounts [ DiagnosticSeverity . Information ] ;
420
+ var warningCount = diagnosticCounts [ DiagnosticSeverity . Warning ] ;
421
+ var errorCount = diagnosticCounts [ DiagnosticSeverity . Error ] ;
422
+ var parseErrorCount = diagnosticCounts [ DiagnosticSeverity . ParseError ] ;
423
+
424
+ if ( ReportSummary . IsPresent )
425
+ {
426
+ var numberOfRuleViolations = infoCount + warningCount + errorCount ;
427
+ if ( numberOfRuleViolations == 0 )
428
+ {
429
+ Host . UI . WriteLine ( "0 rule violations found." ) ;
430
+ }
431
+ else
432
+ {
433
+ var pluralS = numberOfRuleViolations > 1 ? "s" : string . Empty ;
434
+ var message = $ "{ numberOfRuleViolations } rule violation{ pluralS } found. Severity distribution: { DiagnosticSeverity . Error } = { errorCount } , { DiagnosticSeverity . Warning } = { warningCount } , { DiagnosticSeverity . Information } = { infoCount } ";
435
+ if ( warningCount + errorCount == 0 )
436
+ {
437
+ ConsoleHostHelper . DisplayMessageUsingSystemProperties ( Host , "WarningForegroundColor" , "WarningBackgroundColor" , message ) ;
438
+ }
439
+ else
440
+ {
441
+ ConsoleHostHelper . DisplayMessageUsingSystemProperties ( Host , "ErrorForegroundColor" , "ErrorBackgroundColor" , message ) ;
442
+ }
443
+ }
444
+ }
445
+
446
+ if ( EnableExit )
447
+ {
448
+ this . Host . SetShouldExit ( diagnosticCounts . Values . Sum ( ) ) ;
419
449
}
420
450
}
421
451
@@ -431,7 +461,15 @@ protected override void StopProcessing()
431
461
432
462
private void ProcessInput ( )
433
463
{
434
- WriteToOutput ( RunAnalysis ( ) ) ;
464
+ foreach ( var diagnostic in RunAnalysis ( ) )
465
+ {
466
+ diagnosticCounts [ diagnostic . Severity ] ++ ;
467
+
468
+ foreach ( var logger in ScriptAnalyzer . Instance . Loggers )
469
+ {
470
+ logger . LogObject ( diagnostic , this ) ;
471
+ }
472
+ }
435
473
}
436
474
437
475
private IEnumerable < DiagnosticRecord > RunAnalysis ( )
@@ -469,64 +507,6 @@ private IEnumerable<DiagnosticRecord> RunAnalysis()
469
507
}
470
508
}
471
509
472
- private void WriteToOutput ( IEnumerable < DiagnosticRecord > diagnosticRecords )
473
- {
474
- var errorCount = 0 ;
475
- var warningCount = 0 ;
476
- var infoCount = 0 ;
477
- var parseErrorCount = 0 ;
478
-
479
- foreach ( DiagnosticRecord diagnostic in diagnosticRecords )
480
- {
481
- foreach ( ILogger logger in ScriptAnalyzer . Instance . Loggers )
482
- {
483
- logger . LogObject ( diagnostic , this ) ;
484
- }
485
-
486
- totalDiagnosticCount ++ ;
487
-
488
- switch ( diagnostic . Severity )
489
- {
490
- case DiagnosticSeverity . Information :
491
- infoCount ++ ;
492
- break ;
493
- case DiagnosticSeverity . Warning :
494
- warningCount ++ ;
495
- break ;
496
- case DiagnosticSeverity . Error :
497
- errorCount ++ ;
498
- break ;
499
- case DiagnosticSeverity . ParseError :
500
- parseErrorCount ++ ;
501
- break ;
502
- default :
503
- throw new ArgumentOutOfRangeException ( nameof ( diagnostic . Severity ) , $ "Severity '{ diagnostic . Severity } ' is unknown") ;
504
- }
505
- }
506
-
507
- if ( ReportSummary . IsPresent )
508
- {
509
- var numberOfRuleViolations = infoCount + warningCount + errorCount ;
510
- if ( numberOfRuleViolations == 0 )
511
- {
512
- Host . UI . WriteLine ( "0 rule violations found." ) ;
513
- }
514
- else
515
- {
516
- var pluralS = numberOfRuleViolations > 1 ? "s" : string . Empty ;
517
- var message = $ "{ numberOfRuleViolations } rule violation{ pluralS } found. Severity distribution: { DiagnosticSeverity . Error } = { errorCount } , { DiagnosticSeverity . Warning } = { warningCount } , { DiagnosticSeverity . Information } = { infoCount } ";
518
- if ( warningCount + errorCount == 0 )
519
- {
520
- ConsoleHostHelper . DisplayMessageUsingSystemProperties ( Host , "WarningForegroundColor" , "WarningBackgroundColor" , message ) ;
521
- }
522
- else
523
- {
524
- ConsoleHostHelper . DisplayMessageUsingSystemProperties ( Host , "ErrorForegroundColor" , "ErrorBackgroundColor" , message ) ;
525
- }
526
- }
527
- }
528
- }
529
-
530
510
private void ProcessPath ( )
531
511
{
532
512
Collection < PathInfo > paths = this . SessionState . Path . GetResolvedPSPathFromPSPath ( path ) ;
0 commit comments