Skip to content

Commit 6239329

Browse files
Provision the selected PSScriptAnalyzer version via Version/Prerelease
Version/Prerelease now control the PSScriptAnalyzer module (the action's namesake). A bundled prescript installs the chosen version, removes any other PSScriptAnalyzer version from the session, and imports the chosen version -Global so the tests use it instead of the runner's preinstalled copy.
1 parent 342f68b commit 6239329

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ customize rule selection, severity filtering, and custom rule inclusion.
2121
| `SettingsFilePath` | The path to the settings file. | false | `.github/linters/.powershell-psscriptanalyzer.psd1` |
2222
| `Debug` | Enable debug output. | false | `'false'` |
2323
| `Verbose` | Enable verbose output. | false | `'false'` |
24+
| `Version` | Specifies the version of the PSScriptAnalyzer module to install (NuGet range). | false | |
25+
| `Prerelease` | Allow prerelease versions of the PSScriptAnalyzer module if available. | false | `'false'` |
2426
| `PesterVersion` | Specifies the version of the Pester module to install (NuGet range). | false | |
2527
| `PesterPrerelease` | Allow prerelease versions of the Pester module if available. | false | `'false'` |
2628
| `GitHubVersion` | Specifies the version of the GitHub module to install (NuGet range). | false | |

action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ inputs:
2121
description: Enable verbose output.
2222
required: false
2323
default: 'false'
24+
Version:
25+
description: |
26+
Specifies the version of the PSScriptAnalyzer module to install, using NuGet version-range syntax (for example '[1.0.0, 2.0.0)').
27+
When empty, the latest available version is installed.
28+
required: false
29+
Prerelease:
30+
description: Allow prerelease versions of the PSScriptAnalyzer module if available.
31+
required: false
32+
default: 'false'
2433
PesterVersion:
2534
description: |
2635
Specifies the version of the Pester module to install, using NuGet version-range syntax (for example '[6.0.0,7.0.0)').
@@ -277,6 +286,8 @@ runs:
277286
id: test
278287
env:
279288
SettingsFilePath: ${{ fromJson(steps.paths.outputs.result).SettingsFilePath }}
289+
PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Version: ${{ inputs.Version }}
290+
PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Prerelease: ${{ inputs.Prerelease }}
280291
with:
281292
Debug: ${{ inputs.Debug }}
282293
GitHubPrerelease: ${{ inputs.GitHubPrerelease }}
@@ -286,6 +297,7 @@ runs:
286297
Version: ${{ inputs.PesterVersion }}
287298
WorkingDirectory: ${{ inputs.WorkingDirectory }}
288299
TestResult_TestSuiteName: ${{ inputs.TestResult_TestSuiteName }}
300+
Prescript: ${{ github.action_path }}/src/Install-PSScriptAnalyzer.ps1
289301
Path: ${{ github.action_path }}/src/tests/PSScriptAnalyzer
290302
Run_Path: ${{ fromJson(steps.paths.outputs.result).CodePath }}
291303
ReportAsJson: ${{ inputs.ReportAsJson }}

src/Install-PSScriptAnalyzer.ps1

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<#
2+
.SYNOPSIS
3+
Ensures the requested PSScriptAnalyzer version is the only one installed and loaded.
4+
5+
.DESCRIPTION
6+
Runs as the Invoke-Pester prescript, in the same process as the analyzer test run. It installs the
7+
PSScriptAnalyzer version selected through the action's Version/Prerelease inputs (retrying transient
8+
PSGallery failures), removes any other PSScriptAnalyzer version from the session, and imports the chosen
9+
version into the global session state. This guarantees the tests use the selected version instead of
10+
PowerShell auto-loading whatever copy happens to be preinstalled on the runner.
11+
#>
12+
13+
[CmdletBinding()]
14+
param()
15+
16+
$moduleName = 'PSScriptAnalyzer'
17+
$version = $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Version
18+
$prerelease = $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Prerelease -eq 'true'
19+
20+
$installParams = @{
21+
Name = $moduleName
22+
Repository = 'PSGallery'
23+
TrustRepository = $true
24+
PassThru = $true
25+
WarningAction = 'SilentlyContinue'
26+
}
27+
if (-not [string]::IsNullOrWhiteSpace($version)) {
28+
$installParams['Version'] = $version
29+
}
30+
if ($prerelease) {
31+
$installParams['Prerelease'] = $true
32+
}
33+
34+
$label = [string]::IsNullOrWhiteSpace($version) ? $moduleName : "$moduleName $version"
35+
Write-Host "Installing module: $label"
36+
37+
$installed = $null
38+
$retryCount = 5
39+
$retryDelay = 10
40+
for ($i = 0; $i -lt $retryCount; $i++) {
41+
try {
42+
$installed = Install-PSResource @installParams
43+
break
44+
} catch {
45+
Write-Warning "Installation of $moduleName failed with error: $_"
46+
if ($i -eq $retryCount - 1) {
47+
throw
48+
}
49+
Write-Warning "Retrying in $retryDelay seconds..."
50+
Start-Sleep -Seconds $retryDelay
51+
}
52+
}
53+
54+
# Resolve the exact version to load. Prefer what was just installed; if the resource was already
55+
# present, Install-PSResource returns nothing, so fall back to the newest installed version that
56+
# satisfies the requested constraint.
57+
$resolved = $installed | Where-Object { $_.Name -eq $moduleName } | Sort-Object Version -Descending | Select-Object -First 1
58+
if (-not $resolved) {
59+
$getParams = @{ Name = $moduleName; Verbose = $false; ErrorAction = 'SilentlyContinue' }
60+
if (-not [string]::IsNullOrWhiteSpace($version)) {
61+
$getParams['Version'] = $version
62+
}
63+
$resolved = Get-InstalledPSResource @getParams | Sort-Object Version -Descending | Select-Object -First 1
64+
}
65+
if (-not $resolved) {
66+
throw "No installed '$moduleName' version satisfies constraint '$version'."
67+
}
68+
69+
# Remove any already-loaded versions so only the chosen one remains, then import that exact version
70+
# into the global session state used by the Pester run.
71+
Write-Host "Removing any loaded '$moduleName' module from the session"
72+
Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue
73+
74+
Write-Host "Importing module: $moduleName $($resolved.Version)"
75+
Import-Module -Name $moduleName -RequiredVersion $resolved.Version -Force -Global

0 commit comments

Comments
 (0)