|
1 | 1 | [CmdletBinding()] |
2 | 2 | param( |
3 | | - [switch]$Json |
| 3 | + [switch]$Json, |
| 4 | + [switch]$ValidateAssetStructure |
4 | 5 | ) |
5 | 6 |
|
6 | 7 | Set-StrictMode -Version Latest |
7 | 8 | $ErrorActionPreference = "Stop" |
8 | 9 |
|
9 | | -$validatorPath = Join-Path $PSScriptRoot "Validate-ScriptStructure.ps1" |
10 | | -if (-not (Test-Path -LiteralPath $validatorPath)) { |
11 | | - throw "Missing validation entrypoint: $validatorPath" |
| 10 | +function Get-RepoRoot { |
| 11 | + return [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "..\..\..")) |
12 | 12 | } |
13 | 13 |
|
14 | | -try { |
| 14 | +function New-ValidationResult { |
| 15 | + param( |
| 16 | + [Parameter(Mandatory = $true)] |
| 17 | + [string]$Check, |
| 18 | + [Parameter(Mandatory = $true)] |
| 19 | + [bool]$Passed, |
| 20 | + [Parameter(Mandatory = $true)] |
| 21 | + [string]$Details, |
| 22 | + [object]$Data |
| 23 | + ) |
| 24 | + |
| 25 | + return [pscustomobject]@{ |
| 26 | + check = $Check |
| 27 | + passed = $Passed |
| 28 | + details = $Details |
| 29 | + data = $Data |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function Test-RequiredFolders { |
| 34 | + param( |
| 35 | + [Parameter(Mandatory = $true)] |
| 36 | + [string]$RepoRoot |
| 37 | + ) |
| 38 | + |
| 39 | + $required = @( |
| 40 | + "scripts\PS", |
| 41 | + "scripts\PS\codex", |
| 42 | + "scripts\PS\deploy", |
| 43 | + "scripts\PS\validate" |
| 44 | + ) |
| 45 | + |
| 46 | + $missing = @() |
| 47 | + foreach ($relativePath in $required) { |
| 48 | + $fullPath = Join-Path $RepoRoot $relativePath |
| 49 | + if (-not (Test-Path -LiteralPath $fullPath)) { |
| 50 | + $missing += $relativePath |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if ($missing.Count -eq 0) { |
| 55 | + return New-ValidationResult -Check "requiredFolders" -Passed $true -Details "All required folders are present." -Data ([pscustomobject]@{ |
| 56 | + required = $required |
| 57 | + missing = @() |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + return New-ValidationResult -Check "requiredFolders" -Passed $false -Details "Missing required folders: $($missing -join ', ')" -Data ([pscustomobject]@{ |
| 62 | + required = $required |
| 63 | + missing = $missing |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +function Test-ScriptStructureContract { |
| 68 | + param( |
| 69 | + [Parameter(Mandatory = $true)] |
| 70 | + [string]$ValidatorPath |
| 71 | + ) |
| 72 | + |
| 73 | + if (-not (Test-Path -LiteralPath $ValidatorPath)) { |
| 74 | + return New-ValidationResult -Check "scriptStructure" -Passed $false -Details "Missing validation entrypoint: $ValidatorPath" -Data $null |
| 75 | + } |
| 76 | + |
15 | 77 | $global:LASTEXITCODE = 0 |
| 78 | + $raw = & $ValidatorPath -Json |
| 79 | + $exitCode = if ($null -eq $LASTEXITCODE) { 0 } else { [int]$LASTEXITCODE } |
| 80 | + |
| 81 | + $jsonText = ($raw | Out-String).Trim() |
| 82 | + if ([string]::IsNullOrWhiteSpace($jsonText)) { |
| 83 | + return New-ValidationResult -Check "scriptStructure" -Passed $false -Details "Validate-ScriptStructure produced no JSON output." -Data $null |
| 84 | + } |
| 85 | + |
| 86 | + $parsed = $null |
| 87 | + try { |
| 88 | + $parsed = $jsonText | ConvertFrom-Json |
| 89 | + } |
| 90 | + catch { |
| 91 | + return New-ValidationResult -Check "scriptStructure" -Passed $false -Details "Validate-ScriptStructure JSON parsing failed." -Data ([pscustomobject]@{ |
| 92 | + exitCode = $exitCode |
| 93 | + raw = $jsonText |
| 94 | + }) |
| 95 | + } |
| 96 | + |
| 97 | + $passed = $exitCode -eq 0 -and $parsed.valid -eq $true |
| 98 | + $issueCount = if ($null -ne $parsed.issues) { @($parsed.issues).Count } else { 0 } |
| 99 | + $details = if ($passed) { |
| 100 | + "Script structure validation passed." |
| 101 | + } |
| 102 | + else { |
| 103 | + "Script structure validation failed with $issueCount issue(s)." |
| 104 | + } |
| 105 | + |
| 106 | + return New-ValidationResult -Check "scriptStructure" -Passed $passed -Details $details -Data ([pscustomobject]@{ |
| 107 | + exitCode = $exitCode |
| 108 | + issueCount = $issueCount |
| 109 | + issues = $parsed.issues |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +function Test-OptionalAssetStructure { |
| 114 | + param( |
| 115 | + [Parameter(Mandatory = $true)] |
| 116 | + [string]$RepoRoot |
| 117 | + ) |
| 118 | + |
| 119 | + $gamesRoot = Join-Path $RepoRoot "games" |
| 120 | + if (-not (Test-Path -LiteralPath $gamesRoot)) { |
| 121 | + return New-ValidationResult -Check "assetStructure" -Passed $false -Details "Missing games root: games/" -Data $null |
| 122 | + } |
| 123 | + |
| 124 | + $domains = @("sprites", "tilemaps", "parallax", "vectors") |
| 125 | + $issues = New-Object System.Collections.Generic.List[string] |
| 126 | + $checkedGames = New-Object System.Collections.Generic.List[string] |
| 127 | + $checkedDomains = 0 |
| 128 | + |
| 129 | + foreach ($gameDir in (Get-ChildItem -LiteralPath $gamesRoot -Directory -ErrorAction SilentlyContinue)) { |
| 130 | + $assetsRoot = Join-Path $gameDir.FullName "assets" |
| 131 | + if (-not (Test-Path -LiteralPath $assetsRoot)) { |
| 132 | + continue |
| 133 | + } |
| 134 | + |
| 135 | + $checkedGames.Add($gameDir.Name) |
| 136 | + foreach ($domain in $domains) { |
| 137 | + $domainRoot = Join-Path $assetsRoot $domain |
| 138 | + if (-not (Test-Path -LiteralPath $domainRoot -PathType Container)) { |
| 139 | + continue |
| 140 | + } |
| 141 | + |
| 142 | + $checkedDomains += 1 |
| 143 | + $dataRoot = Join-Path $domainRoot "data" |
| 144 | + if (-not (Test-Path -LiteralPath $dataRoot -PathType Container)) { |
| 145 | + $issues.Add("Missing data folder for $($gameDir.Name)/assets/$domain (expected: games/$($gameDir.Name)/assets/$domain/data)") |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + $passed = $issues.Count -eq 0 |
| 151 | + $details = if ($passed) { |
| 152 | + if ($checkedDomains -eq 0) { |
| 153 | + "Asset structure check passed. No active domain folders were found to validate." |
| 154 | + } |
| 155 | + else { |
| 156 | + "Asset structure check passed for $checkedDomains domain folder(s)." |
| 157 | + } |
| 158 | + } |
| 159 | + else { |
| 160 | + "Asset structure check failed with $($issues.Count) issue(s)." |
| 161 | + } |
| 162 | + |
| 163 | + return New-ValidationResult -Check "assetStructure" -Passed $passed -Details $details -Data ([pscustomobject]@{ |
| 164 | + checkedGames = @($checkedGames) |
| 165 | + checkedDomainCount = $checkedDomains |
| 166 | + issues = @($issues) |
| 167 | + }) |
| 168 | +} |
| 169 | + |
| 170 | +$validatorPath = Join-Path $PSScriptRoot "Validate-ScriptStructure.ps1" |
| 171 | +$repoRoot = Get-RepoRoot |
| 172 | + |
| 173 | +try { |
| 174 | + $results = New-Object System.Collections.ArrayList |
| 175 | + |
| 176 | + [void]$results.Add((Test-RequiredFolders -RepoRoot $repoRoot)) |
| 177 | + [void]$results.Add((Test-ScriptStructureContract -ValidatorPath $validatorPath)) |
| 178 | + |
| 179 | + if ($ValidateAssetStructure.IsPresent) { |
| 180 | + [void]$results.Add((Test-OptionalAssetStructure -RepoRoot $repoRoot)) |
| 181 | + } |
| 182 | + |
| 183 | + $failed = @($results | Where-Object { -not $_.passed }) |
| 184 | + $summary = if ($failed.Count -eq 0) { "PASS" } else { "FAIL" } |
| 185 | + $output = [pscustomobject]@{ |
| 186 | + schema = "html-js-gaming.validate-all" |
| 187 | + version = 1 |
| 188 | + summary = $summary |
| 189 | + checkCount = @($results).Count |
| 190 | + failedCount = $failed.Count |
| 191 | + validateAssetStructure = $ValidateAssetStructure.IsPresent |
| 192 | + results = @($results) |
| 193 | + } |
| 194 | + |
16 | 195 | if ($Json.IsPresent) { |
17 | | - & $validatorPath -Json |
| 196 | + $output | ConvertTo-Json -Depth 30 |
18 | 197 | } |
19 | 198 | else { |
20 | | - & $validatorPath |
| 199 | + foreach ($result in $results) { |
| 200 | + $status = if ($result.passed) { "PASS" } else { "FAIL" } |
| 201 | + Write-Host "[$status] $($result.check): $($result.details)" |
| 202 | + $issues = @() |
| 203 | + if ($null -ne $result.data -and ($result.data.PSObject.Properties.Name -contains "issues")) { |
| 204 | + $issues = @($result.data.issues) |
| 205 | + } |
| 206 | + if (-not $result.passed -and $issues.Count -gt 0) { |
| 207 | + foreach ($issue in $issues) { |
| 208 | + Write-Host " - $issue" |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + Write-Host "Validate-All summary: $summary" |
21 | 213 | } |
| 214 | + |
| 215 | + if ($failed.Count -gt 0) { |
| 216 | + exit 1 |
| 217 | + } |
| 218 | + |
| 219 | + exit 0 |
22 | 220 | } |
23 | 221 | catch { |
24 | 222 | Write-Host "Validate-All summary: FAIL" |
25 | 223 | Write-Host "Reason: $($_.Exception.Message)" |
26 | 224 | exit 1 |
27 | 225 | } |
28 | | - |
29 | | -$validatorExitCode = if ($null -eq $LASTEXITCODE) { 0 } else { [int]$LASTEXITCODE } |
30 | | -$summary = if ($validatorExitCode -eq 0) { "PASS" } else { "FAIL" } |
31 | | -if (-not $Json.IsPresent) { |
32 | | - Write-Host "Validate-All summary: $summary" |
33 | | -} |
34 | | - |
35 | | -if ($validatorExitCode -ne 0) { |
36 | | - exit $validatorExitCode |
37 | | -} |
38 | | - |
39 | | -exit 0 |
|
0 commit comments