@@ -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 < 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 List < DiagnosticRecord > RunAnalysis ( )
@@ -459,64 +497,6 @@ private List<DiagnosticRecord> RunAnalysis()
459
497
return diagnostics ;
460
498
}
461
499
462
- private void WriteToOutput ( IEnumerable < DiagnosticRecord > diagnosticRecords )
463
- {
464
- var errorCount = 0 ;
465
- var warningCount = 0 ;
466
- var infoCount = 0 ;
467
- var parseErrorCount = 0 ;
468
-
469
- foreach ( DiagnosticRecord diagnostic in diagnosticRecords )
470
- {
471
- foreach ( ILogger logger in ScriptAnalyzer . Instance . Loggers )
472
- {
473
- logger . LogObject ( diagnostic , this ) ;
474
- }
475
-
476
- totalDiagnosticCount ++ ;
477
-
478
- switch ( diagnostic . Severity )
479
- {
480
- case DiagnosticSeverity . Information :
481
- infoCount ++ ;
482
- break ;
483
- case DiagnosticSeverity . Warning :
484
- warningCount ++ ;
485
- break ;
486
- case DiagnosticSeverity . Error :
487
- errorCount ++ ;
488
- break ;
489
- case DiagnosticSeverity . ParseError :
490
- parseErrorCount ++ ;
491
- break ;
492
- default :
493
- throw new ArgumentOutOfRangeException ( nameof ( diagnostic . Severity ) , $ "Severity '{ diagnostic . Severity } ' is unknown") ;
494
- }
495
- }
496
-
497
- if ( ReportSummary . IsPresent )
498
- {
499
- var numberOfRuleViolations = infoCount + warningCount + errorCount ;
500
- if ( numberOfRuleViolations == 0 )
501
- {
502
- Host . UI . WriteLine ( "0 rule violations found." ) ;
503
- }
504
- else
505
- {
506
- var pluralS = numberOfRuleViolations > 1 ? "s" : string . Empty ;
507
- var message = $ "{ numberOfRuleViolations } rule violation{ pluralS } found. Severity distribution: { DiagnosticSeverity . Error } = { errorCount } , { DiagnosticSeverity . Warning } = { warningCount } , { DiagnosticSeverity . Information } = { infoCount } ";
508
- if ( warningCount + errorCount == 0 )
509
- {
510
- ConsoleHostHelper . DisplayMessageUsingSystemProperties ( Host , "WarningForegroundColor" , "WarningBackgroundColor" , message ) ;
511
- }
512
- else
513
- {
514
- ConsoleHostHelper . DisplayMessageUsingSystemProperties ( Host , "ErrorForegroundColor" , "ErrorBackgroundColor" , message ) ;
515
- }
516
- }
517
- }
518
- }
519
-
520
500
private void ProcessPath ( )
521
501
{
522
502
Collection < PathInfo > paths = this . SessionState . Path . GetResolvedPSPathFromPSPath ( path ) ;
0 commit comments