|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +[CmdletBinding()] |
| 3 | +param( |
| 4 | + [string]$Root = (Get-Location).Path |
| 5 | +) |
| 6 | + |
| 7 | +$ErrorActionPreference = "Stop" |
| 8 | + |
| 9 | +$errors = [System.Collections.Generic.List[string]]::new() |
| 10 | + |
| 11 | +function Add-ParserErrors { |
| 12 | + param( |
| 13 | + [Parameter(Mandatory = $true)][string]$Source, |
| 14 | + [Parameter(Mandatory = $true)]$ParseErrors |
| 15 | + ) |
| 16 | + |
| 17 | + foreach ($parseError in $ParseErrors) { |
| 18 | + $line = $parseError.Extent.StartLineNumber |
| 19 | + $col = $parseError.Extent.StartColumnNumber |
| 20 | + $errors.Add("$Source (line ${line}:${col}): $($parseError.Message)") | Out-Null |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +Get-ChildItem -LiteralPath $Root -Filter "*.ps1" -Recurse | |
| 25 | + Where-Object { |
| 26 | + $_.FullName -notmatch "\\bridge\\target\\" -and |
| 27 | + $_.FullName -notmatch "\\dist\\" -and |
| 28 | + $_.FullName -notmatch "\\pico-bridge\\build" |
| 29 | + } | |
| 30 | + ForEach-Object { |
| 31 | + $parseErrors = $null |
| 32 | + [void][System.Management.Automation.Language.Parser]::ParseFile( |
| 33 | + $_.FullName, |
| 34 | + [ref]$null, |
| 35 | + [ref]$parseErrors |
| 36 | + ) |
| 37 | + if ($parseErrors) { |
| 38 | + Add-ParserErrors -Source $_.FullName -ParseErrors $parseErrors |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | +$workflowDir = Join-Path $Root ".github\workflows" |
| 43 | +if (Test-Path -LiteralPath $workflowDir) { |
| 44 | + $ghaPattern = [regex]"\$\{\{[^}]*\}\}" |
| 45 | + Get-ChildItem -LiteralPath $workflowDir -Filter "*.yml" | ForEach-Object { |
| 46 | + $path = $_.FullName |
| 47 | + $lines = Get-Content -LiteralPath $path |
| 48 | + $stepName = "<unnamed>" |
| 49 | + $isPwsh = $false |
| 50 | + $inRun = $false |
| 51 | + $runIndent = -1 |
| 52 | + $runStart = 0 |
| 53 | + $block = [System.Collections.Generic.List[string]]::new() |
| 54 | + |
| 55 | + $flush = { |
| 56 | + if ($block.Count -eq 0) { return } |
| 57 | + $baseline = -1 |
| 58 | + foreach ($line in $block) { |
| 59 | + if ($line.Trim().Length -eq 0) { continue } |
| 60 | + $baseline = $line.Length - $line.TrimStart(" ").Length |
| 61 | + break |
| 62 | + } |
| 63 | + if ($baseline -lt 0) { return } |
| 64 | + |
| 65 | + $body = ($block | ForEach-Object { |
| 66 | + if ($_.Length -gt $baseline) { $_.Substring($baseline) } else { "" } |
| 67 | + }) -join "`n" |
| 68 | + $body = $ghaPattern.Replace($body, "__GHA_EXPR__") |
| 69 | + |
| 70 | + $parseErrors = $null |
| 71 | + [void][System.Management.Automation.Language.Parser]::ParseInput( |
| 72 | + $body, |
| 73 | + [ref]$null, |
| 74 | + [ref]$parseErrors |
| 75 | + ) |
| 76 | + if ($parseErrors) { |
| 77 | + Add-ParserErrors -Source "$path step '$stepName' (run: line $runStart)" -ParseErrors $parseErrors |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + for ($i = 0; $i -lt $lines.Count; $i++) { |
| 82 | + $line = $lines[$i] |
| 83 | + $indent = $line.Length - $line.TrimStart(" ").Length |
| 84 | + $trimmed = $line.Trim() |
| 85 | + |
| 86 | + if ($inRun) { |
| 87 | + if ($trimmed.Length -gt 0 -and $indent -le $runIndent) { |
| 88 | + & $flush |
| 89 | + $block.Clear() |
| 90 | + $inRun = $false |
| 91 | + } else { |
| 92 | + $block.Add($line) | Out-Null |
| 93 | + continue |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if ($trimmed -match "^- name:\s*(.+?)\s*$") { |
| 98 | + if ($block.Count -gt 0) { |
| 99 | + & $flush |
| 100 | + $block.Clear() |
| 101 | + $inRun = $false |
| 102 | + } |
| 103 | + $stepName = $matches[1] |
| 104 | + $isPwsh = $false |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + if ($trimmed -match "^shell:\s*(.+?)\s*$") { |
| 109 | + $isPwsh = ($matches[1] -eq "pwsh") |
| 110 | + continue |
| 111 | + } |
| 112 | + |
| 113 | + if ($isPwsh -and $trimmed -match "^run:\s*\|\s*$") { |
| 114 | + $inRun = $true |
| 115 | + $runIndent = $indent |
| 116 | + $runStart = $i + 1 |
| 117 | + $block.Clear() |
| 118 | + continue |
| 119 | + } |
| 120 | + } |
| 121 | + if ($inRun) { & $flush } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +if ($errors.Count -gt 0) { |
| 126 | + Write-Host "PowerShell syntax errors:" |
| 127 | + foreach ($message in $errors) { |
| 128 | + Write-Host " $message" |
| 129 | + } |
| 130 | + throw "Found $($errors.Count) PowerShell syntax error(s)." |
| 131 | +} |
| 132 | + |
| 133 | +Write-Host "PowerShell scripts and inline workflow blocks parsed cleanly." |
0 commit comments