Skip to content

Commit d98f49c

Browse files
authored
Merge branch 'main' into test-for-issue-1504
2 parents d015124 + 37ddc1c commit d98f49c

40 files changed

+820
-285
lines changed

.devcontainer/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
FROM mcr.microsoft.com/dotnet/sdk:8.0
5+
6+
RUN pwsh --command Install-Module platyPS,Pester -Force

.devcontainer/devcontainer.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// For format details, see https://aka.ms/vscode-remote/devcontainer.json
2+
{
3+
"name": "C# (.NET 8.0)",
4+
"dockerFile": "Dockerfile",
5+
"customizations": {
6+
"vscode": {
7+
"settings": {
8+
"terminal.integrated.defaultProfile.linux": "pwsh"
9+
},
10+
"extensions": [
11+
"ms-dotnettools.csharp",
12+
"ms-vscode.powershell"
13+
]
14+
}
15+
}
16+
}

.github/workflows/ci-test.yml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,43 @@ jobs:
3131
shell: pwsh
3232

3333
- name: Build
34-
run: ./build.ps1 -Configuration Release -All
34+
run: ./build.ps1 -Configuration Release -All -Verbose
3535
shell: pwsh
3636

3737
- name: Package
38-
run: ./build.ps1 -BuildNupkg
38+
run: ./build.ps1 -BuildNupkg -Verbose
3939
shell: pwsh
4040

4141
- name: Test
42-
run: ./build.ps1 -Test
42+
run: ./build.ps1 -Test -Verbose
4343
shell: pwsh
4444

4545
- name: Test Windows PowerShell
46+
if: matrix.os == 'windows-latest'
4647
run: |
4748
Install-Module Pester -Scope CurrentUser -Force -SkipPublisherCheck
48-
./build.ps1 -Test
49-
if: matrix.os == 'windows-latest'
49+
./build.ps1 -Test -Verbose
5050
shell: powershell
5151

52+
- name: Download PowerShell install script
53+
uses: actions/checkout@v4
54+
with:
55+
repository: PowerShell/PowerShell
56+
path: pwsh
57+
sparse-checkout: tools/install-powershell.ps1
58+
sparse-checkout-cone-mode: false
59+
60+
- name: Install preview
61+
continue-on-error: true
62+
run: ./pwsh/tools/install-powershell.ps1 -Preview -Destination ./preview
63+
shell: pwsh
64+
65+
- name: Test preview
66+
run: |
67+
$PwshPreview = if ($isWindows) { "./preview/pwsh.exe" } else { "./preview/pwsh" }
68+
./build.ps1 -Test -WithPowerShell:$PwshPreview -Verbose
69+
shell: pwsh
70+
5271
- name: Upload build artifacts
5372
uses: actions/upload-artifact@v4
5473
if: always()

.pipelines/PSScriptAnalyzer-Official.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ extends:
8080
inputs:
8181
packageType: sdk
8282
useGlobalJson: true
83-
- pwsh: |
84-
Register-PSRepository -Name CFS -SourceLocation "https://pkgs.dev.azure.com/powershell/PowerShell/_packaging/powershell/nuget/v2" -InstallationPolicy Trusted
85-
Install-Module -Repository CFS -Name Microsoft.PowerShell.PSResourceGet
86-
./tools/installPSResources.ps1 -PSRepository CFS
83+
- pwsh: ./tools/installPSResources.ps1 -PSRepository CFS
8784
displayName: Install PSResources
8885
- pwsh: ./build.ps1 -Configuration Release -All
8986
displayName: Build

CHANGELOG.MD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# CHANGELOG
22

3+
## [1.23.0](https://github.com/PowerShell/PSScriptAnalyzer/tree/1.23.0) - 2024-10-09
4+
5+
## What's Changed
6+
* Adding OneBranch pipeline YAML config file for OSS_Microsoft_PSSA-Official by @adityapatwardhan in https://github.com/PowerShell/PSScriptAnalyzer/pull/1981
7+
* Update format and grammar of AvoidUsingAllowUnencryptedAuthentication by @sdwheeler in https://github.com/PowerShell/PSScriptAnalyzer/pull/1974
8+
* Move to OneBranch Signing and SBOM generation by @TravisEz13 in https://github.com/PowerShell/PSScriptAnalyzer/pull/1982
9+
* Sync rule docs changes by @sdwheeler in https://github.com/PowerShell/PSScriptAnalyzer/pull/1985
10+
* Sync docs changes from MicrosoftDocs/PowerShell-Docs-Modules#213 by @sdwheeler in https://github.com/PowerShell/PSScriptAnalyzer/pull/1987
11+
* Update CHANGELOG for 1.22.0 release by @sdwheeler in https://github.com/PowerShell/PSScriptAnalyzer/pull/1990
12+
* Update Code of Conduct by @andyleejordan in https://github.com/PowerShell/PSScriptAnalyzer/pull/2002
13+
* Update default type definition of `RuleInfo` by @liamjpeters in https://github.com/PowerShell/PSScriptAnalyzer/pull/2011
14+
* PSUseConsistentWhitespace: Handle redirect operators which are not in stream order by @liamjpeters in https://github.com/PowerShell/PSScriptAnalyzer/pull/2001
15+
* Setup GitHub Actions CI by @andyleejordan in https://github.com/PowerShell/PSScriptAnalyzer/pull/2018
16+
* Setup new OneBranch pipeline by @andyleejordan in https://github.com/PowerShell/PSScriptAnalyzer/pull/2027
17+
* Bump SMA version by @andyleejordan in https://github.com/PowerShell/PSScriptAnalyzer/pull/2028
18+
* Package updates by @andyleejordan in https://github.com/PowerShell/PSScriptAnalyzer/pull/2030
19+
* v1.23.0: Update version for new release by @andyleejordan in https://github.com/PowerShell/PSScriptAnalyzer/pull/2032
20+
* Migrate release pipeline to DeployBox by @andyleejordan in https://github.com/PowerShell/PSScriptAnalyzer/pull/2033
21+
22+
## New Contributors
23+
* @adityapatwardhan made their first contribution in https://github.com/PowerShell/PSScriptAnalyzer/pull/1981
24+
325
## [1.22.0](https://github.com/PowerShell/PSScriptAnalyzer/tree/1.22.0) - 2024-03-05
426

527
Minimum required version when using PowerShell 7 is now `7.2.11`.

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
1212
<PackageVersion Include="Pluralize.NET" Version="1.0.2" />
1313
<PackageVersion Include="PowerShellStandard.Library" Version="3.0.0-preview-02" />
14-
<PackageVersion Include="System.Management.Automation" Version="7.2.23" />
14+
<PackageVersion Include="System.Management.Automation" Version="7.4.7" />
1515
<PackageVersion Include="System.Reflection.TypeExtensions" Version="4.7.0" />
1616
</ItemGroup>
1717
</Project>

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 56 additions & 66 deletions
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(typeof(DiagnosticSeverity)).Cast<DiagnosticSeverity>().ToDictionary(s => s, _ => 0);
3840
#endregion // Private variables
3941

4042
#region Parameters
@@ -414,8 +416,35 @@ protected override void EndProcessing()
414416
ScriptAnalyzer.Instance.CleanUp();
415417
base.EndProcessing();
416418

417-
if (EnableExit) {
418-
this.Host.SetShouldExit(totalDiagnosticCount);
419+
var diagnosticCount = diagnosticCounts.Values.Sum();
420+
421+
if (ReportSummary.IsPresent)
422+
{
423+
if (diagnosticCount == 0)
424+
{
425+
Host.UI.WriteLine("0 rule violations found.");
426+
}
427+
else
428+
{
429+
var infoCount = diagnosticCounts[DiagnosticSeverity.Information];
430+
var warningCount = diagnosticCounts[DiagnosticSeverity.Warning];
431+
var errorCount = diagnosticCounts[DiagnosticSeverity.Error] + diagnosticCounts[DiagnosticSeverity.ParseError];
432+
var severeDiagnosticCount = diagnosticCount - infoCount;
433+
434+
var colorPropertyPrefix = severeDiagnosticCount == 0 ? "Warning" : "Error";
435+
var pluralS = diagnosticCount > 1 ? "s" : string.Empty;
436+
ConsoleHostHelper.DisplayMessageUsingSystemProperties(
437+
Host, colorPropertyPrefix + "ForegroundColor", colorPropertyPrefix + "BackgroundColor",
438+
$"{diagnosticCount} rule violation{pluralS} found. Severity distribution: " +
439+
$"{DiagnosticSeverity.Error} = {errorCount}, " +
440+
$"{DiagnosticSeverity.Warning} = {warningCount}, " +
441+
$"{DiagnosticSeverity.Information} = {infoCount}");
442+
}
443+
}
444+
445+
if (EnableExit)
446+
{
447+
this.Host.SetShouldExit(diagnosticCount);
419448
}
420449
}
421450

@@ -431,86 +460,47 @@ protected override void StopProcessing()
431460

432461
private void ProcessInput()
433462
{
434-
var diagnosticRecords = RunAnalysis();
435-
WriteToOutput(diagnosticRecords);
436-
totalDiagnosticCount += diagnosticRecords.Count;
437-
}
438-
439-
private List<DiagnosticRecord> RunAnalysis()
440-
{
441-
if (!IsFileParameterSet())
463+
foreach (var diagnostic in RunAnalysis())
442464
{
443-
return ScriptAnalyzer.Instance.AnalyzeScriptDefinition(scriptDefinition, out _, out _);
444-
}
465+
diagnosticCounts[diagnostic.Severity]++;
445466

446-
var diagnostics = new List<DiagnosticRecord>();
447-
foreach (string path in this.processedPaths)
448-
{
449-
if (fix)
467+
foreach (var logger in ScriptAnalyzer.Instance.Loggers)
450468
{
451-
ShouldProcess(path, $"Analyzing and fixing path with Recurse={this.recurse}");
452-
diagnostics.AddRange(ScriptAnalyzer.Instance.AnalyzeAndFixPath(path, this.ShouldProcess, this.recurse));
453-
}
454-
else
455-
{
456-
ShouldProcess(path, $"Analyzing path with Recurse={this.recurse}");
457-
diagnostics.AddRange(ScriptAnalyzer.Instance.AnalyzePath(path, this.ShouldProcess, this.recurse));
469+
logger.LogObject(diagnostic, this);
458470
}
459471
}
460-
461-
return diagnostics;
462472
}
463473

464-
private void WriteToOutput(List<DiagnosticRecord> diagnosticRecords)
474+
private IEnumerable<DiagnosticRecord> RunAnalysis()
465475
{
466-
foreach (ILogger logger in ScriptAnalyzer.Instance.Loggers)
476+
if (!IsFileParameterSet())
467477
{
468-
var errorCount = 0;
469-
var warningCount = 0;
470-
var infoCount = 0;
471-
var parseErrorCount = 0;
478+
foreach (var record in ScriptAnalyzer.Instance.AnalyzeScriptDefinition(scriptDefinition, out _, out _))
479+
{
480+
yield return record;
481+
}
482+
yield break;
483+
}
472484

473-
foreach (DiagnosticRecord diagnostic in diagnosticRecords)
485+
foreach (var path in this.processedPaths)
486+
{
487+
if (!ShouldProcess(path, $"Analyzing path with Fix={this.fix} and Recurse={this.recurse}"))
474488
{
475-
logger.LogObject(diagnostic, this);
476-
switch (diagnostic.Severity)
477-
{
478-
case DiagnosticSeverity.Information:
479-
infoCount++;
480-
break;
481-
case DiagnosticSeverity.Warning:
482-
warningCount++;
483-
break;
484-
case DiagnosticSeverity.Error:
485-
errorCount++;
486-
break;
487-
case DiagnosticSeverity.ParseError:
488-
parseErrorCount++;
489-
break;
490-
default:
491-
throw new ArgumentOutOfRangeException(nameof(diagnostic.Severity), $"Severity '{diagnostic.Severity}' is unknown");
492-
}
489+
continue;
493490
}
494491

495-
if (ReportSummary.IsPresent)
492+
if (fix)
496493
{
497-
var numberOfRuleViolations = infoCount + warningCount + errorCount;
498-
if (numberOfRuleViolations == 0)
494+
foreach (var record in ScriptAnalyzer.Instance.AnalyzeAndFixPath(path, this.ShouldProcess, this.recurse))
499495
{
500-
Host.UI.WriteLine("0 rule violations found.");
496+
yield return record;
501497
}
502-
else
498+
}
499+
else
500+
{
501+
foreach (var record in ScriptAnalyzer.Instance.AnalyzePath(path, this.ShouldProcess, this.recurse))
503502
{
504-
var pluralS = numberOfRuleViolations > 1 ? "s" : string.Empty;
505-
var message = $"{numberOfRuleViolations} rule violation{pluralS} found. Severity distribution: {DiagnosticSeverity.Error} = {errorCount}, {DiagnosticSeverity.Warning} = {warningCount}, {DiagnosticSeverity.Information} = {infoCount}";
506-
if (warningCount + errorCount == 0)
507-
{
508-
ConsoleHostHelper.DisplayMessageUsingSystemProperties(Host, "WarningForegroundColor", "WarningBackgroundColor", message);
509-
}
510-
else
511-
{
512-
ConsoleHostHelper.DisplayMessageUsingSystemProperties(Host, "ErrorForegroundColor", "ErrorBackgroundColor", message);
513-
}
503+
yield return record;
514504
}
515505
}
516506
}

Engine/Engine.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<VersionPrefix>$(ModuleVersion)</VersionPrefix>
5-
<TargetFrameworks>net6;net462</TargetFrameworks>
5+
<TargetFrameworks>net8;net462</TargetFrameworks>
66
<AssemblyName>Microsoft.Windows.PowerShell.ScriptAnalyzer</AssemblyName>
77
<AssemblyVersion>$(ModuleVersion)</AssemblyVersion>
88
<PackageId>Engine</PackageId>
@@ -18,11 +18,11 @@
1818
<DebugType>portable</DebugType>
1919
</PropertyGroup>
2020

21-
<PropertyGroup Condition=" '$(TargetFramework)' == 'net6' ">
21+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net8' ">
2222
<DefineConstants>$(DefineConstants);CORECLR</DefineConstants>
2323
</PropertyGroup>
2424

25-
<ItemGroup Condition=" '$(TargetFramework)' == 'net6' ">
25+
<ItemGroup Condition=" '$(TargetFramework)' == 'net8' ">
2626
<Compile Remove="SafeDirectoryCatalog.cs" />
2727
</ItemGroup>
2828

@@ -69,10 +69,10 @@
6969
</PropertyGroup>
7070

7171

72-
<ItemGroup Condition="'$(TargetFramework)' == 'net6'">
72+
<ItemGroup Condition="'$(TargetFramework)' == 'net8'">
7373
<PackageReference Include="System.Management.Automation" />
7474
</ItemGroup>
75-
<PropertyGroup Condition="'$(TargetFramework)' == 'net6'">
75+
<PropertyGroup Condition="'$(TargetFramework)' == 'net8'">
7676
<DefineConstants>$(DefineConstants);PSV7;CORECLR</DefineConstants>
7777
</PropertyGroup>
7878

Engine/Formatter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static string Format<TCmdlet>(
4747
"PSAvoidUsingDoubleQuotesForConstantString",
4848
"PSAvoidSemicolonsAsLineTerminators",
4949
"PSAvoidExclaimOperator",
50+
"PSAvoidTrailingWhitespace",
5051
};
5152

5253
var text = new EditableText(scriptDefinition);

Engine/Generic/DiagnosticRecord.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public string ScriptPath
7474
}
7575

7676
/// <summary>
77-
/// Returns the rule id for this record
77+
/// Returns the rule suppression id for this record
7878
/// </summary>
7979
public string RuleSuppressionID
8080
{
@@ -88,7 +88,7 @@ public string RuleSuppressionID
8888
/// </summary>
8989
public IEnumerable<CorrectionExtent> SuggestedCorrections
9090
{
91-
get { return suggestedCorrections; }
91+
get { return suggestedCorrections; }
9292
set { suggestedCorrections = value; }
9393
}
9494

@@ -100,14 +100,15 @@ public IEnumerable<CorrectionExtent> SuggestedCorrections
100100
public DiagnosticRecord()
101101
{
102102
}
103-
103+
104104
/// <summary>
105105
/// DiagnosticRecord: The constructor for DiagnosticRecord class that takes in suggestedCorrection
106106
/// </summary>
107107
/// <param name="message">A string about why this diagnostic was created</param>
108108
/// <param name="extent">The place in the script this diagnostic refers to</param>
109109
/// <param name="ruleName">The name of the rule that created this diagnostic</param>
110110
/// <param name="severity">The severity of this diagnostic</param>
111+
/// <param name="ruleId">The rule suppression ID of this diagnostic</param>
111112
/// <param name="scriptPath">The full path of the script file being analyzed</param>
112113
/// <param name="suggestedCorrections">The correction suggested by the rule to replace the extent text</param>
113114
public DiagnosticRecord(

0 commit comments

Comments
 (0)