playbook/scripts/sync_standards.ps1

83 lines
2.6 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Sync standards snapshot to project root.
# - Copies <snapshot>/.agents -> <project-root>/.agents/tsl
# - Copies <snapshot>/.gitattributes -> <project-root>/.gitattributes
# Existing targets are backed up before overwrite.
$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$Src = (Resolve-Path (Join-Path $ScriptDir "..")).Path
$Root = (git -C $ScriptDir rev-parse --show-toplevel 2>$null)
if (-not $Root) { $Root = (Get-Location).Path }
$Root = (Resolve-Path $Root).Path
$AgentsSrc = Join-Path $Src ".agents"
$GitAttrSrc = Join-Path $Src ".gitattributes"
if (-not (Test-Path $AgentsSrc)) {
throw "Standards snapshot not found at $AgentsSrc. Run git subtree add first (choose any prefix)."
}
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
$AgentsNs = $env:AGENTS_NS
if (-not $AgentsNs) { $AgentsNs = "tsl" }
if ($AgentsNs -match '[\\/]' -or $AgentsNs -match '\.\.') {
throw "Invalid AGENTS_NS=$AgentsNs"
}
$AgentsRoot = Join-Path $Root ".agents"
$AgentsDst = Join-Path $AgentsRoot $AgentsNs
if ($Src -ieq $Root) {
Write-Host "Skip: snapshot root equals project root."
Write-Host "Done."
exit 0
}
New-Item -ItemType Directory -Path $AgentsRoot -Force | Out-Null
if (Test-Path $AgentsDst) {
$bak = (Join-Path $AgentsRoot "$AgentsNs.bak.$timestamp")
Move-Item $AgentsDst $bak
Write-Host "Backed up existing $AgentsNs agents -> $(Split-Path -Leaf $bak)"
}
New-Item -ItemType Directory -Path $AgentsDst -Force | Out-Null
Copy-Item (Join-Path $AgentsSrc "*") $AgentsDst -Recurse -Force
Write-Host "Synced .agents/$AgentsNs from standards."
$AgentsIndex = Join-Path $AgentsRoot "index.md"
if (-not (Test-Path $AgentsIndex)) {
@"
# .agents
/
- `.agents/tsl/`TSL `sync_standards.*`
- `.agents/cpp/``.agents/python/`
"@ | Set-Content -Path $AgentsIndex -Encoding UTF8
Write-Host "Created .agents/index.md"
}
$GitAttrDst = Join-Path $Root ".gitattributes"
if (Test-Path $GitAttrSrc) {
if ($GitAttrSrc -ieq $GitAttrDst) {
Write-Host "Skip: .gitattributes source equals destination."
Write-Host "Done."
exit 0
}
if (Test-Path $GitAttrDst) {
$bak = "$GitAttrDst.bak.$timestamp"
Move-Item $GitAttrDst $bak
Write-Host "Backed up existing .gitattributes -> $bak"
}
Copy-Item $GitAttrSrc $GitAttrDst -Force
Write-Host "Synced .gitattributes from standards."
}
Write-Host "Done."