|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +[CmdletBinding()] |
| 3 | +param( |
| 4 | + [string]$Root = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path |
| 5 | +) |
| 6 | + |
| 7 | +$ErrorActionPreference = 'Stop' |
| 8 | + |
| 9 | +$errors = New-Object System.Collections.Generic.List[string] |
| 10 | + |
| 11 | +function Get-RepoRelativePath { |
| 12 | + param([string]$Path) |
| 13 | + |
| 14 | + $rootFull = [System.IO.Path]::GetFullPath($Root).TrimEnd([char[]]@('\', '/')) |
| 15 | + $pathFull = [System.IO.Path]::GetFullPath($Path) |
| 16 | + $prefix = $rootFull + [System.IO.Path]::DirectorySeparatorChar |
| 17 | + if ($pathFull.StartsWith($prefix, [System.StringComparison]::OrdinalIgnoreCase)) { |
| 18 | + return $pathFull.Substring($prefix.Length) |
| 19 | + } |
| 20 | + return $pathFull |
| 21 | +} |
| 22 | + |
| 23 | +function Add-ParseErrors { |
| 24 | + param([string]$Source, $ParseErrors) |
| 25 | + if (-not $ParseErrors) { return } |
| 26 | + foreach ($err in $ParseErrors) { |
| 27 | + $errors.Add("$Source (line $($err.Extent.StartLineNumber):$($err.Extent.StartColumnNumber)): $($err.Message)") | Out-Null |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +$trackedScripts = & git -C $Root ls-files '*.ps1' '*.psm1' '*.psd1' |
| 32 | +foreach ($relative in $trackedScripts) { |
| 33 | + $path = Join-Path $Root $relative |
| 34 | + $parseErrors = $null |
| 35 | + [void][System.Management.Automation.Language.Parser]::ParseFile($path, [ref]$null, [ref]$parseErrors) |
| 36 | + Add-ParseErrors -Source $relative -ParseErrors $parseErrors |
| 37 | +} |
| 38 | + |
| 39 | +$workflowDir = Join-Path $Root '.github\workflows' |
| 40 | +if (Test-Path -LiteralPath $workflowDir) { |
| 41 | + $ghaPattern = [regex]'\$\{\{[^}]*\}\}' |
| 42 | + Get-ChildItem -LiteralPath $workflowDir -Filter '*.yml' | ForEach-Object { |
| 43 | + $relativeWorkflow = Get-RepoRelativePath -Path $_.FullName |
| 44 | + $lines = Get-Content -LiteralPath $_.FullName |
| 45 | + $stepName = '<unnamed>' |
| 46 | + $isPwsh = $false |
| 47 | + $inRun = $false |
| 48 | + $runIndent = -1 |
| 49 | + $runStart = 0 |
| 50 | + $blockLines = New-Object System.Collections.Generic.List[string] |
| 51 | + |
| 52 | + $flush = { |
| 53 | + if ($blockLines.Count -eq 0) { return } |
| 54 | + $baseline = -1 |
| 55 | + foreach ($line in $blockLines) { |
| 56 | + if ($line.Trim().Length -eq 0) { continue } |
| 57 | + $baseline = $line.Length - $line.TrimStart(' ').Length |
| 58 | + break |
| 59 | + } |
| 60 | + if ($baseline -lt 0) { return } |
| 61 | + $body = ($blockLines | ForEach-Object { |
| 62 | + if ($_.Length -gt $baseline) { $_.Substring($baseline) } else { '' } |
| 63 | + }) -join "`n" |
| 64 | + $stubbed = $ghaPattern.Replace($body, '__GHA_EXPR__') |
| 65 | + $parseErrors = $null |
| 66 | + [void][System.Management.Automation.Language.Parser]::ParseInput($stubbed, [ref]$null, [ref]$parseErrors) |
| 67 | + Add-ParseErrors -Source "$relativeWorkflow step '$stepName' run block starting line $runStart" -ParseErrors $parseErrors |
| 68 | + } |
| 69 | + |
| 70 | + for ($i = 0; $i -lt $lines.Count; $i++) { |
| 71 | + $line = $lines[$i] |
| 72 | + $indent = $line.Length - $line.TrimStart(' ').Length |
| 73 | + $trimmed = $line.Trim() |
| 74 | + |
| 75 | + if ($inRun) { |
| 76 | + if ($trimmed.Length -gt 0 -and $indent -le $runIndent) { |
| 77 | + & $flush |
| 78 | + $blockLines.Clear() |
| 79 | + $inRun = $false |
| 80 | + } else { |
| 81 | + $blockLines.Add($line) | Out-Null |
| 82 | + continue |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if ($trimmed -match '^- name:\s*(.+?)\s*$') { |
| 87 | + $stepName = $Matches[1] |
| 88 | + $isPwsh = $false |
| 89 | + continue |
| 90 | + } |
| 91 | + if ($trimmed -match '^shell:\s*(.+?)\s*$') { |
| 92 | + $isPwsh = ($Matches[1] -eq 'pwsh') |
| 93 | + continue |
| 94 | + } |
| 95 | + if ($isPwsh -and $trimmed -match '^run:\s*\|\s*$') { |
| 96 | + $inRun = $true |
| 97 | + $runIndent = $indent |
| 98 | + $runStart = $i + 1 |
| 99 | + $blockLines.Clear() |
| 100 | + } |
| 101 | + } |
| 102 | + if ($inRun) { & $flush } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +if ($errors.Count -gt 0) { |
| 107 | + Write-Host 'PowerShell syntax errors:' |
| 108 | + foreach ($err in $errors) { Write-Host " $err" } |
| 109 | + throw "Found $($errors.Count) PowerShell syntax error(s)." |
| 110 | +} |
| 111 | + |
| 112 | +Write-Host 'PowerShell scripts and inline workflow blocks parsed cleanly.' |
0 commit comments