@@ -34,7 +34,9 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
3434
3535 #region Private variables
3636 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 ) ;
3840 #endregion // Private variables
3941
4042 #region Parameters
@@ -414,8 +416,36 @@ protected override void EndProcessing()
414416 ScriptAnalyzer . Instance . CleanUp ( ) ;
415417 base . EndProcessing ( ) ;
416418
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 ( ) ) ;
419449 }
420450 }
421451
@@ -431,7 +461,15 @@ protected override void StopProcessing()
431461
432462 private void ProcessInput ( )
433463 {
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+ }
435473 }
436474
437475 private IEnumerable < DiagnosticRecord > RunAnalysis ( )
@@ -469,64 +507,6 @@ private IEnumerable<DiagnosticRecord> RunAnalysis()
469507 }
470508 }
471509
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-
530510 private void ProcessPath ( )
531511 {
532512 Collection < PathInfo > paths = this . SessionState . Path . GetResolvedPSPathFromPSPath ( path ) ;
0 commit comments