From da32c344a3d527c16dd216f2275035b4392a53f3 Mon Sep 17 00:00:00 2001 From: Josh King Date: Wed, 3 Apr 2024 16:00:37 +1300 Subject: [PATCH 1/2] (#142) Skip PSSA analysis when no files to analyze --- .../Content/run-psscriptanalyzer.ps1 | 68 +++++++++++-------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/Chocolatey.Cake.Recipe/Content/run-psscriptanalyzer.ps1 b/Chocolatey.Cake.Recipe/Content/run-psscriptanalyzer.ps1 index fb32857..296b2bd 100644 --- a/Chocolatey.Cake.Recipe/Content/run-psscriptanalyzer.ps1 +++ b/Chocolatey.Cake.Recipe/Content/run-psscriptanalyzer.ps1 @@ -73,48 +73,58 @@ $modules = Get-ChildItem -Path $AnalyzePath -Filter "*.psm1" -Recurse | ForEach- } } -Write-Output "Analyzing module files..." +if ($null -ne $modules) { + Write-Output "Analyzing module files..." -$records = Start-Job -ArgumentList $modules, $SettingsPath { - Param( - $modules, - $SettingsPath - ) - $modules | Invoke-ScriptAnalyzer -Settings $SettingsPath | Select-Object RuleName, ScriptPath, Line, Message -} | Wait-Job | Receive-Job + $records = Start-Job -ArgumentList $modules, $SettingsPath { + Param( + $modules, + $SettingsPath + ) + $modules | Invoke-ScriptAnalyzer -Settings $SettingsPath | Select-Object RuleName, ScriptPath, Line, Message + } | Wait-Job | Receive-Job -if (-not ($null -EQ $records)) { - Write-Output "Violations found in Module Files..." - $records | Format-List | Out-String + if (-not ($null -EQ $records)) { + Write-Output "Violations found in Module Files..." + $records | Format-List | Out-String - Write-Output $OutputPath + Write-Output $OutputPath - Write-Output "Writing violations to output file..." - $records | ConvertTo-SARIF -FilePath "$OutputPath\modules.sarif" + Write-Output "Writing violations to output file..." + $records | ConvertTo-SARIF -FilePath "$OutputPath\modules.sarif" + } + else { + Write-Output "No rule violations found in Module Files." + } } else { - Write-Output "No rule violations found in Module Files." + Write-Output "No Module Files to analyze" } -Write-Output "Analyzing script files..." +if ($null -ne $scripts) { + Write-Output "Analyzing script files..." -$records = Start-Job -ArgumentList $Scripts, $SettingsPath { - Param( - $Scripts, - $SettingsPath - ) - $Scripts | Invoke-ScriptAnalyzer -Settings $SettingsPath | Select-Object RuleName, ScriptPath, Line, Message -} | Wait-Job | Receive-Job + $records = Start-Job -ArgumentList $Scripts, $SettingsPath { + Param( + $Scripts, + $SettingsPath + ) + $Scripts | Invoke-ScriptAnalyzer -Settings $SettingsPath | Select-Object RuleName, ScriptPath, Line, Message + } | Wait-Job | Receive-Job -if (-not ($null -EQ $records)) { - Write-Output "Violations found in Script Files..." - $records | Format-List | Out-String + if (-not ($null -EQ $records)) { + Write-Output "Violations found in Script Files..." + $records | Format-List | Out-String - Write-Output "Writing violations to output file..." - $records | ConvertTo-SARIF -FilePath "$OutputPath\scripts.sarif" + Write-Output "Writing violations to output file..." + $records | ConvertTo-SARIF -FilePath "$OutputPath\scripts.sarif" + } + else { + Write-Output "No rule violations found in Script Files." + } } else { - Write-Output "No rule violations found in Script Files." + Write-Output "No Script Files to analyze" } Write-Output "Analyzing complete." \ No newline at end of file From a4151e27096ca5a74a0cbf6e78adaf4e8bda8937 Mon Sep 17 00:00:00 2001 From: Cory Knox Date: Thu, 21 Mar 2024 19:57:17 -0700 Subject: [PATCH 2/2] (#130) Install PowerShell Module to Current User Update the code that installs the PSScriptAnalyzer module to ensure the NuGet Package Provider is installed and correctly loaded for the CurrentUser. This ensures the user running the build does not need to be running as an administrator, nor do they need to already have the NuGet Package Provider installed and up to date. --- .../Content/install-module.ps1 | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Chocolatey.Cake.Recipe/Content/install-module.ps1 b/Chocolatey.Cake.Recipe/Content/install-module.ps1 index 2a79778..f66b9ad 100644 --- a/Chocolatey.Cake.Recipe/Content/install-module.ps1 +++ b/Chocolatey.Cake.Recipe/Content/install-module.ps1 @@ -30,5 +30,18 @@ if (Get-Module -ListAvailable -FullyQualifiedName $FullyQualifiedName) { } else { Write-Host "Install Module $ModuleName with version $RequiredVersion..." - Install-Module -Name $ModuleName -RequiredVersion $RequiredVersion -Force -} \ No newline at end of file + # Bootstrap PowerShell Get + if (-not (Get-PackageProvider NuGet -ErrorAction Ignore)) { + Write-Host "Installing NuGet package provider" + Install-PackageProvider NuGet -MinimumVersion 2.8.5.201 -ForceBootstrap -Force -Scope CurrentUser + } + + if (-not (Get-InstalledModule PowerShellGet -MinimumVersion 2.0 -MaximumVersion 2.99 -ErrorAction Ignore)) { + Install-Module PowerShellGet -MaximumVersion 2.99 -Force -AllowClobber -Scope CurrentUser + Remove-Module PowerShellGet -Force + Import-Module PowerShellGet -MinimumVersion 2.0 -Force + Import-PackageProvider -Name PowerShellGet -MinimumVersion 2.0 -Force + } + + Install-Module -Name $ModuleName -RequiredVersion $RequiredVersion -Force -Scope CurrentUser +}