Files
AX-Copilot-Codex/scripts/check-encoding.ps1

91 lines
2.5 KiB
PowerShell

[CmdletBinding()]
param(
[string]$Root = ""
)
$ErrorActionPreference = "Stop"
if ([string]::IsNullOrWhiteSpace($Root)) {
$basePath = if ($PSScriptRoot) {
$PSScriptRoot
}
elseif ($PSCommandPath) {
Split-Path -Parent $PSCommandPath
}
else {
(Get-Location).Path
}
$Root = (Resolve-Path (Join-Path $basePath "..")).Path
}
$extensions = @(".cs", ".xaml", ".csproj", ".props", ".targets", ".sln", ".json", ".md", ".txt", ".xml", ".yml", ".yaml", ".ps1")
$utf8Bom = New-Object byte[] 3
$utf8Bom[0] = 0xEF
$utf8Bom[1] = 0xBB
$utf8Bom[2] = 0xBF
$scanRoots = @(
(Join-Path $Root "src"),
(Join-Path $Root "scripts")
) | Where-Object { Test-Path $_ }
$files = New-Object System.Collections.Generic.List[System.IO.FileInfo]
foreach ($scanRoot in $scanRoots) {
Get-ChildItem -Path $scanRoot -Recurse -File |
Where-Object { $extensions -contains $_.Extension.ToLowerInvariant() } |
Where-Object { $_.FullName -notmatch '\\(bin|obj|\.git|\.decompiled|\.decompiledproj|src2|dist|\.tools)\\' } |
Where-Object { $_.Name -notlike '*.bak-*' } |
Where-Object { $_.Name -notlike '*.broken' } |
ForEach-Object { $files.Add($_) }
}
$rootFiles = @(".editorconfig", ".gitattributes", "README.md", "AxCopilot.sln", "CLAUDE.md")
foreach ($relativePath in $rootFiles) {
$fullPath = Join-Path $Root $relativePath
if (Test-Path $fullPath) {
$files.Add((Get-Item $fullPath))
}
}
$problems = New-Object System.Collections.Generic.List[object]
foreach ($file in $files) {
$bytes = [System.IO.File]::ReadAllBytes($file.FullName)
if ($bytes.Length -lt 3) {
$problems.Add([pscustomobject]@{
File = $file.FullName
Issue = "too-short-to-confirm-bom"
})
continue
}
$hasBom = $bytes[0] -eq $utf8Bom[0] -and $bytes[1] -eq $utf8Bom[1] -and $bytes[2] -eq $utf8Bom[2]
if (-not $hasBom) {
$problems.Add([pscustomobject]@{
File = $file.FullName
Issue = "missing-utf8-bom"
})
continue
}
try {
$null = [System.Text.Encoding]::UTF8.GetString($bytes, 3, $bytes.Length - 3)
}
catch {
$problems.Add([pscustomobject]@{
File = $file.FullName
Issue = "invalid-utf8-content"
})
}
}
if ($problems.Count -eq 0) {
Write-Host "Encoding check passed. All scanned files use UTF-8 BOM."
exit 0
}
Write-Host "Encoding check found issues:" -ForegroundColor Yellow
$problems | Sort-Object File | Format-Table -AutoSize
exit 1