From 4a42071172230971f602945cf60e5b5e5822dc0e Mon Sep 17 00:00:00 2001 From: zheng <18760530726@163.com> Date: Mon, 17 Aug 2026 23:20:04 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=8A=A0=E4=B8=80=E4=B8=AA=20PowerShel?= =?UTF-8?q?l=20=E8=AF=AD=E6=B3=95=E6=A3=80=E6=9F=A5=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 本项目大部分 PowerShell 是在 Mac 上写的,那里没有解析器。「看起来没问题」 不算检查 —— 一个多余字符就能让 Windows 安装器完全跑不起来,而这件事只有 在 Windows 上才能发现。 用法(在仓库根目录): powershell -ExecutionPolicy Bypass -File portable\advanced\Check-PowerShell.ps1 退出码等于出错文件数,以后也能接进 CI。 --- portable/advanced/Check-PowerShell.ps1 | 52 ++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 portable/advanced/Check-PowerShell.ps1 diff --git a/portable/advanced/Check-PowerShell.ps1 b/portable/advanced/Check-PowerShell.ps1 new file mode 100644 index 0000000..e4ed7e9 --- /dev/null +++ b/portable/advanced/Check-PowerShell.ps1 @@ -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