-
Notifications
You must be signed in to change notification settings - Fork 6
124 lines (107 loc) · 4.88 KB
/
Copy pathlint-powershell.yml
File metadata and controls
124 lines (107 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
name: PowerShell Install Scripts
on:
push:
paths:
- 'install/powershell/*.ps1'
- '.github/workflows/lint-powershell.yml'
pull_request:
paths:
- 'install/powershell/*.ps1'
- '.github/workflows/lint-powershell.yml'
workflow_dispatch:
jobs:
parse-windows-powershell-51:
name: Parse under Windows PowerShell 5.1
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
# The install scripts are fetched over HTTP and executed on stock Windows,
# where powershell.exe (5.1) is the default shell. 5.1 reads BOM-less files
# as ANSI (CP1252), so a non-ASCII glyph such as U+2713 decodes into a smart
# quote that silently terminates the enclosing string. The resulting parse
# errors point at unrelated lines far below the real cause, so parse every
# script with 5.1 explicitly rather than relying on PowerShell 7.
- name: Parse each script with 5.1
shell: powershell
run: |
$ErrorActionPreference = 'Stop'
Write-Host "PowerShell $($PSVersionTable.PSVersion) / ANSI codepage $([System.Text.Encoding]::Default.WebName)"
$failed = $false
Get-ChildItem 'install/powershell' -Filter *.ps1 | Sort-Object Name | ForEach-Object {
$errors = $null
[System.Management.Automation.Language.Parser]::ParseFile(
$_.FullName, [ref]$null, [ref]$errors) | Out-Null
if ($errors -and $errors.Count -gt 0) {
$failed = $true
Write-Host "FAIL $($_.Name): $($errors.Count) parse error(s)"
$errors | Sort-Object { $_.Extent.StartLineNumber } | ForEach-Object {
Write-Host " L$($_.Extent.StartLineNumber): $($_.Message)"
}
} else {
Write-Host "OK $($_.Name)"
}
}
if ($failed) { exit 1 }
# A BOM would fix file-based parsing but break `irm ... | iex`: PowerShell 5.1
# decodes an HTTP text/* body with no charset as ISO-8859-1, turning the BOM
# into literal "i>>?" text and failing with
# "The term 'i>>?$ErrorActionPreference' is not recognized".
# Keeping the sources pure ASCII is what makes both paths safe, so enforce it.
- name: Require pure ASCII and no BOM
shell: powershell
run: |
$ErrorActionPreference = 'Stop'
$failed = $false
Get-ChildItem 'install/powershell' -Filter *.ps1 | Sort-Object Name | ForEach-Object {
$fileBad = $false
$bytes = [System.IO.File]::ReadAllBytes($_.FullName)
if ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF) {
$failed = $true
$fileBad = $true
Write-Host "FAIL $($_.Name): has a UTF-8 BOM (breaks 'irm | iex'); remove it"
}
# Report line/column of any byte outside ASCII so the fix is obvious.
$text = [System.Text.Encoding]::UTF8.GetString($bytes)
$lineNo = 0
foreach ($line in ($text -split "`r?`n")) {
$lineNo++
for ($i = 0; $i -lt $line.Length; $i++) {
if ([int]$line[$i] -gt 127) {
$failed = $true
$fileBad = $true
$cp = "U+{0:X4}" -f [int]$line[$i]
Write-Host "FAIL $($_.Name) L${lineNo} C$($i + 1): non-ASCII $cp"
Write-Host " build it from its code point instead, e.g. `$CHECK = [char]0x2713"
break
}
}
}
if (-not $fileBad) { Write-Host "OK $($_.Name)" }
}
if ($failed) { exit 1 }
parse-powershell-7:
name: Parse under PowerShell 7
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Parse each script with 7
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
Write-Host "PowerShell $($PSVersionTable.PSVersion)"
$failed = $false
Get-ChildItem 'install/powershell' -Filter *.ps1 | Sort-Object Name | ForEach-Object {
$errors = $null
[System.Management.Automation.Language.Parser]::ParseFile(
$_.FullName, [ref]$null, [ref]$errors) | Out-Null
if ($errors -and $errors.Count -gt 0) {
$failed = $true
Write-Host "FAIL $($_.Name): $($errors.Count) parse error(s)"
$errors | Sort-Object { $_.Extent.StartLineNumber } | ForEach-Object {
Write-Host " L$($_.Extent.StartLineNumber): $($_.Message)"
}
} else {
Write-Host "OK $($_.Name)"
}
}
if ($failed) { exit 1 }