diff --git a/Engine/Commands/InvokeScriptAnalyzerCommand.cs b/Engine/Commands/InvokeScriptAnalyzerCommand.cs index 18a632874..bcc6be9de 100644 --- a/Engine/Commands/InvokeScriptAnalyzerCommand.cs +++ b/Engine/Commands/InvokeScriptAnalyzerCommand.cs @@ -34,7 +34,9 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter #region Private variables List processedPaths; - private int totalDiagnosticCount = 0; + // initialize to zero for all severity enum values + private Dictionary diagnosticCounts = + Enum.GetValues(typeof(DiagnosticSeverity)).Cast().ToDictionary(s => s, _ => 0); #endregion // Private variables #region Parameters @@ -414,8 +416,36 @@ protected override void EndProcessing() ScriptAnalyzer.Instance.CleanUp(); base.EndProcessing(); - if (EnableExit) { - this.Host.SetShouldExit(totalDiagnosticCount); + var infoCount = diagnosticCounts[DiagnosticSeverity.Information]; + var warningCount = diagnosticCounts[DiagnosticSeverity.Warning]; + var errorCount = diagnosticCounts[DiagnosticSeverity.Error]; + var parseErrorCount = diagnosticCounts[DiagnosticSeverity.ParseError]; + + if (ReportSummary.IsPresent) + { + var numberOfRuleViolations = infoCount + warningCount + errorCount; + if (numberOfRuleViolations == 0) + { + Host.UI.WriteLine("0 rule violations found."); + } + else + { + var pluralS = numberOfRuleViolations > 1 ? "s" : string.Empty; + var message = $"{numberOfRuleViolations} rule violation{pluralS} found. Severity distribution: {DiagnosticSeverity.Error} = {errorCount}, {DiagnosticSeverity.Warning} = {warningCount}, {DiagnosticSeverity.Information} = {infoCount}"; + if (warningCount + errorCount == 0) + { + ConsoleHostHelper.DisplayMessageUsingSystemProperties(Host, "WarningForegroundColor", "WarningBackgroundColor", message); + } + else + { + ConsoleHostHelper.DisplayMessageUsingSystemProperties(Host, "ErrorForegroundColor", "ErrorBackgroundColor", message); + } + } + } + + if (EnableExit) + { + this.Host.SetShouldExit(diagnosticCounts.Values.Sum()); } } @@ -431,7 +461,15 @@ protected override void StopProcessing() private void ProcessInput() { - WriteToOutput(RunAnalysis()); + foreach (var diagnostic in RunAnalysis()) + { + diagnosticCounts[diagnostic.Severity]++; + + foreach (var logger in ScriptAnalyzer.Instance.Loggers) + { + logger.LogObject(diagnostic, this); + } + } } private IEnumerable RunAnalysis() @@ -469,64 +507,6 @@ private IEnumerable RunAnalysis() } } - private void WriteToOutput(IEnumerable diagnosticRecords) - { - var errorCount = 0; - var warningCount = 0; - var infoCount = 0; - var parseErrorCount = 0; - - foreach (DiagnosticRecord diagnostic in diagnosticRecords) - { - foreach (ILogger logger in ScriptAnalyzer.Instance.Loggers) - { - logger.LogObject(diagnostic, this); - } - - totalDiagnosticCount++; - - switch (diagnostic.Severity) - { - case DiagnosticSeverity.Information: - infoCount++; - break; - case DiagnosticSeverity.Warning: - warningCount++; - break; - case DiagnosticSeverity.Error: - errorCount++; - break; - case DiagnosticSeverity.ParseError: - parseErrorCount++; - break; - default: - throw new ArgumentOutOfRangeException(nameof(diagnostic.Severity), $"Severity '{diagnostic.Severity}' is unknown"); - } - } - - if (ReportSummary.IsPresent) - { - var numberOfRuleViolations = infoCount + warningCount + errorCount; - if (numberOfRuleViolations == 0) - { - Host.UI.WriteLine("0 rule violations found."); - } - else - { - var pluralS = numberOfRuleViolations > 1 ? "s" : string.Empty; - var message = $"{numberOfRuleViolations} rule violation{pluralS} found. Severity distribution: {DiagnosticSeverity.Error} = {errorCount}, {DiagnosticSeverity.Warning} = {warningCount}, {DiagnosticSeverity.Information} = {infoCount}"; - if (warningCount + errorCount == 0) - { - ConsoleHostHelper.DisplayMessageUsingSystemProperties(Host, "WarningForegroundColor", "WarningBackgroundColor", message); - } - else - { - ConsoleHostHelper.DisplayMessageUsingSystemProperties(Host, "ErrorForegroundColor", "ErrorBackgroundColor", message); - } - } - } - } - private void ProcessPath() { Collection paths = this.SessionState.Path.GetResolvedPSPathFromPSPath(path);