<# .SYNOPSIS 把测试与静态分析用的模块装进仓库内的 .tools\modules(不动机器上的全局模块)。 .DESCRIPTION 装两样: * Pester —— tests\BakNRet.Tests.ps1 需要 5.0+。本机常见的坑是 Windows 自带的是 3.4.0,没有 `Should -Be`,套件会语法错误; * PSScriptAnalyzer —— tools\Invoke-Analyzer.ps1 用它做"按微软规范"的门禁。 两者都用 Save-Module 下载到仓库内,而不是 Install-Module:后者会装到全局, 可能影响机器上别的、按旧语法写的脚本。.tools\ 已进 .gitignore,不污染版本库。 .EXAMPLE pwsh -File .\tools\Install-TestDependencies.ps1 .EXAMPLE pwsh -File .\tools\Install-TestDependencies.ps1 -Force #> [CmdletBinding()] param( [version]$PesterVersion = [version]'5.9.1', [string]$PSScriptAnalyzerVersion = 'latest', [switch]$Force ) $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' $projectRoot = Split-Path -Parent $PSScriptRoot $target = Join-Path $projectRoot '.tools\modules' $installed = Join-Path $target ("Pester\{0}" -f $PesterVersion) Write-Host '' Write-Host ("目标目录 : {0}" -f $target) Write-Host ("Pester : {0}" -f $PesterVersion) Write-Host ("PSScriptAnalyzer: {0}" -f $PSScriptAnalyzerVersion) Write-Host '' if (-not (Get-Command Save-Module -ErrorAction SilentlyContinue)) { Write-Error '当前环境没有 Save-Module(需要 PowerShellGet 2.x)。请改用:Install-Module Pester -Scope CurrentUser -MinimumVersion 5.0.0' exit 1 } New-Item -ItemType Directory -Path $target -Force | Out-Null $needPester = $Force -or -not (Test-Path -LiteralPath $installed) if ($needPester) { if ($Force -and (Test-Path -LiteralPath (Join-Path $target 'Pester'))) { Remove-Item -LiteralPath (Join-Path $target 'Pester') -Recurse -Force } try { Save-Module -Name Pester -RequiredVersion $PesterVersion -Path $target -Force -ErrorAction Stop } catch { Write-Error "Pester 下载失败(多半是访问不到 PSGallery):$_" exit 1 } Write-Host ("已安装:Pester {0}" -f $PesterVersion) -ForegroundColor Green } else { Write-Host "Pester {0} 已经装好了,跳过(要重装加 -Force)。" -f $PesterVersion -ForegroundColor DarkGray } # PSScriptAnalyzer:版本目录带具体版本号,所以"装没装过"按目录是否存在判断 $analyzerInstalled = @(Test-Path -LiteralPath (Join-Path $target 'PSScriptAnalyzer')) if ($Force -or -not $analyzerInstalled) { if ($Force -and $analyzerInstalled) { Remove-Item -LiteralPath (Join-Path $target 'PSScriptAnalyzer') -Recurse -Force } try { Save-Module -Name PSScriptAnalyzer -Path $target -Force -ErrorAction Stop } catch { Write-Error "PSScriptAnalyzer 下载失败(多半是访问不到 PSGallery):$_" exit 1 } $analyzerVersion = (Get-ChildItem (Join-Path $target 'PSScriptAnalyzer') -Directory | Select-Object -First 1).Name Write-Host ("已安装:PSScriptAnalyzer {0}" -f $analyzerVersion) -ForegroundColor Green } else { Write-Host 'PSScriptAnalyzer 已经装好了,跳过(要重装加 -Force)。' -ForegroundColor DarkGray } Write-Host '' Write-Host '现在可以跑:' -ForegroundColor Cyan Write-Host ' .\tests\Run-Pester.ps1 (单元套件)' -ForegroundColor Gray Write-Host ' .\tools\Invoke-Analyzer.ps1 (静态分析门禁)' -ForegroundColor Gray Write-Host '' exit 0