Skip to content

Invoke-ScriptAnalyzer: Print summary only once per invocation #2063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(typeof(DiagnosticSeverity)).Cast<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 IEnumerable<DiagnosticRecord> RunAnalysis()
Expand Down Expand Up @@ -469,64 +507,6 @@ private IEnumerable<DiagnosticRecord> RunAnalysis()
}
}

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