Skip to content

Commit

Permalink
Invoke-ScriptAnalyzer: Print summary only once per invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
MatejKafka committed Feb 21, 2025
1 parent dc4ae4b commit ed43569
Showing 1 changed file with 42 additions and 62 deletions.
104 changes: 42 additions & 62 deletions Engine/Commands/InvokeScriptAnalyzerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter

#region Private variables
List<string> processedPaths;
private int totalDiagnosticCount = 0;
// initialize to zero for all severity enum values
private Dictionary<DiagnosticSeverity, int> diagnosticCounts =
Enum.GetValues<DiagnosticSeverity>().ToDictionary(s => s, _ => 0);
#endregion // Private variables

#region Parameters
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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 List<DiagnosticRecord> RunAnalysis()
Expand Down Expand Up @@ -459,64 +497,6 @@ private List<DiagnosticRecord> RunAnalysis()
return diagnostics;
}

private void WriteToOutput(IEnumerable<DiagnosticRecord> 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<PathInfo> paths = this.SessionState.Path.GetResolvedPSPathFromPSPath(path);
Expand Down

0 comments on commit ed43569

Please sign in to comment.