#!/usr/bin/env pwsh # fix_format.ps1 — Auto-format all game-owned GDScript files with gdformat. # Usage: & ./tools/fix_format.ps1 $ROOT = Split-Path -Parent $PSScriptRoot $previousLocation = Get-Location $exitCode = 0 function Resolve-GdFormat { $localTool = Join-Path $ROOT ".venv/Scripts/gdformat.exe" if (Test-Path $localTool) { return [pscustomobject]@{ Command = $localTool; Prefix = @() } } $directTool = Get-Command gdformat -ErrorAction SilentlyContinue if ($directTool) { return [pscustomobject]@{ Command = $directTool.Source; Prefix = @() } } $localPython = Join-Path $ROOT ".venv/Scripts/python.exe" $pythonCandidates = @( [pscustomobject]@{ Command = $localPython; Prefix = @() }, [pscustomobject]@{ Command = "python"; Prefix = @() }, [pscustomobject]@{ Command = "python3"; Prefix = @() }, [pscustomobject]@{ Command = "py"; Prefix = @("-3") } ) foreach ($candidate in $pythonCandidates) { $isLocalPython = $candidate.Command -eq $localPython if ($isLocalPython) { if (-not (Test-Path $candidate.Command)) { continue } $command = $candidate.Command } else { $resolved = Get-Command $candidate.Command -ErrorAction SilentlyContinue if (-not $resolved) { continue } $command = $resolved.Source } $prefix = @($candidate.Prefix) $null = @(& $command @prefix -m gdtoolkit.formatter --help 2>&1) if ($LASTEXITCODE -eq 0) { return [pscustomobject]@{ Command = $command; Prefix = $prefix + @("-m", "gdtoolkit.formatter") } } } return $null } try { Set-Location $ROOT $formatter = Resolve-GdFormat if (-not $formatter) { throw "gdformat not found. Install requirements-dev.txt; see docs/local_quality_gate.md." } Write-Host "Formatting all game-owned .gd files in $ROOT ..." $command = $formatter.Command $prefix = @($formatter.Prefix) & $command @prefix player simulation tests tools world if ($LASTEXITCODE -ne 0) { throw "gdformat exited with code $LASTEXITCODE." } Write-Host "Done." } catch { Write-Host "ERROR: $($_.Exception.Message)" $exitCode = 1 } finally { Set-Location $previousLocation } exit $exitCode