180 lines
5.8 KiB
PowerShell
180 lines
5.8 KiB
PowerShell
# Sync standards snapshot to project root.
|
||
# - Copies <snapshot>/.agents/<AGENTS_NS> -> <project-root>/.agents/<AGENTS_NS>
|
||
# - Updates <project-root>/.gitattributes (managed block by default)
|
||
# Existing targets are backed up before overwrite.
|
||
[CmdletBinding()]
|
||
param(
|
||
# Sync multiple rulesets in one run:
|
||
# -Langs tsl,cpp
|
||
# -Langs @("tsl","cpp")
|
||
[Parameter(Mandatory = $false)]
|
||
[string[]]$Langs
|
||
)
|
||
|
||
$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
|
||
|
||
$AgentsSrcRoot = Join-Path $Src ".agents"
|
||
$GitAttrSrc = Join-Path $Src ".gitattributes"
|
||
|
||
if (-not (Test-Path $AgentsSrcRoot)) {
|
||
throw "Standards snapshot not found at $AgentsSrcRoot. Run: git subtree add --prefix docs/standards/playbook <url> <branch> --squash"
|
||
}
|
||
|
||
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
|
||
|
||
# Multi rulesets: only on the outer invocation.
|
||
if (-not $env:SYNC_STANDARDS_INNER -and $Langs -and $Langs.Count -gt 0) {
|
||
$oldInner = $env:SYNC_STANDARDS_INNER
|
||
$oldAgentsNs = $env:AGENTS_NS
|
||
$oldMode = $env:SYNC_GITATTR_MODE
|
||
|
||
$syncModeFirst = $env:SYNC_GITATTR_MODE
|
||
if (-not $syncModeFirst) { $syncModeFirst = "block" }
|
||
|
||
$first = $true
|
||
foreach ($ns in $Langs) {
|
||
if (-not $ns) { continue }
|
||
|
||
$env:SYNC_STANDARDS_INNER = "1"
|
||
$env:AGENTS_NS = $ns
|
||
if ($first) {
|
||
$first = $false
|
||
$env:SYNC_GITATTR_MODE = $syncModeFirst
|
||
} else {
|
||
$env:SYNC_GITATTR_MODE = "skip"
|
||
}
|
||
|
||
& $MyInvocation.MyCommand.Path
|
||
}
|
||
|
||
$env:SYNC_STANDARDS_INNER = $oldInner
|
||
$env:AGENTS_NS = $oldAgentsNs
|
||
$env:SYNC_GITATTR_MODE = $oldMode
|
||
exit 0
|
||
}
|
||
|
||
$AgentsNs = $env:AGENTS_NS
|
||
if (-not $AgentsNs) { $AgentsNs = "tsl" }
|
||
if ($AgentsNs -match '[\\/]' -or $AgentsNs -match '\.\.') {
|
||
throw "Invalid AGENTS_NS=$AgentsNs"
|
||
}
|
||
$AgentsSrc = Join-Path $AgentsSrcRoot $AgentsNs
|
||
if (-not (Test-Path $AgentsSrc)) {
|
||
# Backward-compatible fallback: older snapshots used <snapshot>/.agents/* directly.
|
||
if ((Test-Path (Join-Path $AgentsSrcRoot "index.md")) -and (Test-Path (Join-Path $AgentsSrcRoot "auth.md"))) {
|
||
$AgentsSrc = $AgentsSrcRoot
|
||
} else {
|
||
throw "Agents ruleset not found: $AgentsSrc (set AGENTS_NS to one of the subdirs under $AgentsSrcRoot, e.g. tsl/cpp)."
|
||
}
|
||
}
|
||
$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.*` 同步;适用于 `.tsl`/`.tsf`)
|
||
- `.agents/cpp/`:C++ 相关规则集(由 `sync_standards.*` 同步;适用于 C++23/Modules)
|
||
- `.agents/python/` 等:其他语言的规则集(按需增加)
|
||
|
||
规则发生冲突时,建议以“更靠近代码的目录规则更具体”为准。
|
||
|
||
入口建议从:
|
||
|
||
- `.agents/tsl/index.md`(TSL 规则集入口)
|
||
- `.agents/cpp/index.md`(C++ 规则集入口)
|
||
- `docs/standards/playbook/docs/`(人类开发规范快照:`tsl/`、`cpp/`、`python/`、`common/`)
|
||
'@ | Set-Content -Path $AgentsIndex -Encoding UTF8
|
||
Write-Host "Created .agents/index.md"
|
||
}
|
||
|
||
$GitAttrDst = Join-Path $Root ".gitattributes"
|
||
if (Test-Path $GitAttrSrc) {
|
||
$mode = $env:SYNC_GITATTR_MODE
|
||
if (-not $mode) { $mode = "block" }
|
||
switch ($mode.ToLowerInvariant()) {
|
||
"skip" {
|
||
Write-Host "Skip: .gitattributes sync (SYNC_GITATTR_MODE=skip)."
|
||
break
|
||
}
|
||
"overwrite" {
|
||
if ($GitAttrSrc -ieq $GitAttrDst) {
|
||
Write-Host "Skip: .gitattributes source equals destination."
|
||
break
|
||
}
|
||
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 (overwrite)."
|
||
break
|
||
}
|
||
"block" {
|
||
$begin = "# BEGIN playbook .gitattributes"
|
||
$end = "# END playbook .gitattributes"
|
||
$beginOld = "# BEGIN tsl-playbook .gitattributes"
|
||
$endOld = "# END tsl-playbook .gitattributes"
|
||
$src = Get-Content -Path $GitAttrSrc -Raw
|
||
$block = $begin + "`r`n" + $src.TrimEnd() + "`r`n" + $end + "`r`n"
|
||
|
||
$dst = ""
|
||
if (Test-Path $GitAttrDst) {
|
||
$bak = "$GitAttrDst.bak.$timestamp"
|
||
Move-Item $GitAttrDst $bak
|
||
Write-Host "Backed up existing .gitattributes -> $bak"
|
||
$dst = Get-Content -Path $bak -Raw
|
||
}
|
||
|
||
$pattern = "(?ms)^(" + [regex]::Escape($begin) + "|" + [regex]::Escape($beginOld) + ")\\R.*?^(" + [regex]::Escape($end) + "|" + [regex]::Escape($endOld) + ")\\R?"
|
||
if ($dst -and ($dst -match $pattern)) {
|
||
$new = [regex]::Replace($dst, $pattern, $block)
|
||
} elseif ($dst) {
|
||
$new = $dst.TrimEnd() + "`r`n`r`n" + $block
|
||
} else {
|
||
$new = $block
|
||
}
|
||
|
||
$new | Set-Content -Path $GitAttrDst -Encoding UTF8
|
||
Write-Host "Updated .gitattributes from standards (managed block)."
|
||
break
|
||
}
|
||
default {
|
||
throw "Invalid SYNC_GITATTR_MODE=$mode (use block|overwrite|skip)"
|
||
}
|
||
}
|
||
}
|
||
|
||
Write-Host "Done."
|