Skip to content

Commit ed43569

Browse files
committed
Invoke-ScriptAnalyzer: Print summary only once per invocation
1 parent dc4ae4b commit ed43569

File tree

1 file changed

+42
-62
lines changed

1 file changed

+42
-62
lines changed

Diff for: Engine/Commands/InvokeScriptAnalyzerCommand.cs

+42-62
Original file line numberDiff line numberDiff line change
@@ -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<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 List<DiagnosticRecord> RunAnalysis()
@@ -459,64 +497,6 @@ private List<DiagnosticRecord> RunAnalysis()
459497
return diagnostics;
460498
}
461499

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-
520500
private void ProcessPath()
521501
{
522502
Collection<PathInfo> paths = this.SessionState.Path.GetResolvedPSPathFromPSPath(path);

0 commit comments

Comments
 (0)