test: 加一个 PowerShell 语法检查脚本
Some checks failed
Tests / test (push) Has been cancelled

本项目大部分 PowerShell 是在 Mac 上写的,那里没有解析器。「看起来没问题」
不算检查 —— 一个多余字符就能让 Windows 安装器完全跑不起来,而这件事只有
在 Windows 上才能发现。

用法(在仓库根目录):
    powershell -ExecutionPolicy Bypass -File portable\advanced\Check-PowerShell.ps1

退出码等于出错文件数,以后也能接进 CI。
This commit is contained in:
2026-08-17 23:20:04 +08:00
parent d2cd9d4528
commit 4a42071172

View File

@@ -0,0 +1,52 @@
# Parses every .ps1 in the repository and reports syntax errors.
#
# Why this exists: most of this project's PowerShell was written on a Mac, where
# no parser is available. "It looks right" is not a check — a single stray
# character stops the Windows installer from running at all, and there is no way
# to find that out except on Windows.
#
# Run from the repository root:
# powershell -ExecutionPolicy Bypass -File portable\advanced\Check-PowerShell.ps1
#
# Exit code is the number of files with errors, so CI can use it too.
$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
$failed = 0
$checked = 0
Write-Host ""
Write-Host " Parsing every .ps1 under $root" -ForegroundColor Cyan
Write-Host ""
Get-ChildItem -Path $root -Recurse -Filter *.ps1 -File |
Where-Object { $_.FullName -notmatch '\\node_modules\\|\\app\\|\\\.git\\' } |
Sort-Object FullName |
ForEach-Object {
$checked++
$errors = $null
$relative = $_.FullName.Substring($root.Length).TrimStart('\')
[void][System.Management.Automation.Language.Parser]::ParseFile($_.FullName, [ref]$null, [ref]$errors)
if ($errors -and $errors.Count -gt 0) {
$failed++
Write-Host (" FAIL " + $relative) -ForegroundColor Red
foreach ($e in $errors) {
$line = $e.Extent.StartLineNumber
Write-Host (" line " + $line + ": " + $e.Message) -ForegroundColor Red
}
} else {
Write-Host (" ok " + $relative) -ForegroundColor Green
}
}
Write-Host ""
if ($failed -eq 0) {
Write-Host " $checked file(s) parsed, no syntax errors." -ForegroundColor Green
} else {
Write-Host " $failed of $checked file(s) have syntax errors." -ForegroundColor Red
Write-Host " Send the red lines above to whoever wrote them." -ForegroundColor Yellow
}
Write-Host ""
exit $failed