playbook/scripts/check_no_legacy_refs.ps1

72 lines
2.0 KiB
PowerShell

param(
[string]$RepoRoot = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
)
$ErrorActionPreference = "Stop"
$targets = @(
(Join-Path $RepoRoot "docs/tsl/index.md"),
(Join-Path $RepoRoot "docs/tsl/syntax"),
(Join-Path $RepoRoot "docs/tsl/finance"),
(Join-Path $RepoRoot "docs/tsl/reference")
)
$excludeFragments = @(
"\docs\plans\",
"\archive\",
"\docs\tsl\legacy\",
"\docs\tsl\syntax_book\"
)
$pattern = '(?i)\b(?:syntax_book|legacy)\b'
function Get-RelativePath([string]$basePath, [string]$childPath) {
$baseUri = [System.Uri]::new(($basePath.TrimEnd('\', '/') + [System.IO.Path]::DirectorySeparatorChar))
$childUri = [System.Uri]::new($childPath)
return [System.Uri]::UnescapeDataString($baseUri.MakeRelativeUri($childUri).ToString()).Replace('\', '/')
}
$files = foreach ($target in $targets) {
if (!(Test-Path -LiteralPath $target)) {
throw "Target not found: $target"
}
if ((Get-Item -LiteralPath $target).PSIsContainer) {
Get-ChildItem -LiteralPath $target -Recurse -File -Filter *.md
}
else {
Get-Item -LiteralPath $target
}
}
$activeFiles = $files |
Where-Object {
$fullName = $_.FullName
foreach ($fragment in $excludeFragments) {
if ($fullName -like "*$fragment*") {
return $false
}
}
return $true
} |
Sort-Object FullName -Unique
$hits = foreach ($file in $activeFiles) {
Select-String -Path $file.FullName -Pattern $pattern -AllMatches
}
if ($hits.Count -eq 0) {
Write-Output "No legacy references found in active TSL docs."
exit 0
}
Write-Output "Legacy references found in active TSL docs:"
foreach ($hit in $hits) {
$relativePath = Get-RelativePath -basePath $RepoRoot -childPath $hit.Path
Write-Output ("{0}:{1}: {2}" -f $relativePath, $hit.LineNumber, $hit.Line.Trim())
}
Write-Output ("Total files scanned: {0}" -f $activeFiles.Count)
Write-Output ("Total legacy hits: {0}" -f $hits.Count)
exit 1