🗑️ remove(legacy): drop old scripts and tests

This commit is contained in:
csh
2026-01-23 14:58:31 +08:00
parent 0c4cd0e037
commit b4f712acb4
23 changed files with 158 additions and 5516 deletions
-135
View File
@@ -1,135 +0,0 @@
@echo off
setlocal enabledelayedexpansion
rem Install Codex skills from this Playbook snapshot into CODEX_HOME.
rem - Source: <snapshot>\codex\skills\<skill-name>\
rem - Dest: %CODEX_HOME%\skills\<skill-name>\ (default CODEX_HOME=%USERPROFILE%\.codex)
rem
rem Usage:
rem install_codex_skills.bat -all
rem install_codex_skills.bat -skills style-cleanup,commit-message
rem install_codex_skills.bat -local -all
rem
rem Notes:
rem - Codex loads skills at startup; restart `codex` after installation.
rem - Existing destination skill dirs are backed up with a random suffix.
set "SCRIPT_DIR=%~dp0"
for %%I in ("%SCRIPT_DIR%..") do set "SRC=%%~fI"
set "SKILLS_SRC_ROOT=%SRC%\\codex\\skills"
set "LOCAL_MODE=0"
set "INSTALL_ALL=0"
set "SKILLS="
:parse_opts
if "%~1"=="" goto opts_done
if /I "%~1"=="-help" goto show_help
if /I "%~1"=="-h" goto show_help
if /I "%~1"=="-local" (
set "LOCAL_MODE=1"
shift
goto parse_opts
)
if /I "%~1"=="-l" (
set "LOCAL_MODE=1"
shift
goto parse_opts
)
if /I "%~1"=="-all" (
set "INSTALL_ALL=1"
shift
goto parse_opts
)
if /I "%~1"=="-skills" (
if "%~2"=="" (
echo ERROR: -skills requires a value.
exit /b 1
)
set "SKILLS=%~2"
shift
shift
goto parse_opts
)
echo ERROR: Unknown option: %~1
exit /b 1
goto opts_done
:opts_done
set "CODEX_HOME=%CODEX_HOME%"
if "%LOCAL_MODE%"=="1" if "%CODEX_HOME%"=="" set "CODEX_HOME=%CD%\\.codex"
if "%CODEX_HOME%"=="" set "CODEX_HOME=%USERPROFILE%\\.codex"
set "SKILLS_DST_ROOT=%CODEX_HOME%\\skills"
if not exist "%SKILLS_SRC_ROOT%" (
echo ERROR: skills source dir not found: "%SKILLS_SRC_ROOT%"
exit /b 1
)
if not exist "%SKILLS_DST_ROOT%" mkdir "%SKILLS_DST_ROOT%"
if "%INSTALL_ALL%"=="1" if not "%SKILLS%"=="" (
echo ERROR: use either -all or -skills, not both.
exit /b 1
)
if "%INSTALL_ALL%"=="0" if "%SKILLS%"=="" (
echo ERROR: -all or -skills is required.
exit /b 1
)
if "%INSTALL_ALL%"=="1" (
for /d %%D in ("%SKILLS_SRC_ROOT%\\*") do (
set "NAME=%%~nD"
if not "!NAME!"=="" if not "!NAME:~0,1!"=="." call :InstallOne "!NAME!"
)
) else (
set "SKILLS=%SKILLS:,= %"
for %%S in (%SKILLS%) do call :InstallOne "%%~S"
)
:Done
echo Done. Skills installed to: "%SKILLS_DST_ROOT%"
endlocal
exit /b 0
:show_help
echo Usage:
echo install_codex_skills.bat -all
echo install_codex_skills.bat -skills style-cleanup,commit-message
echo.
echo Options:
echo -local, -l Install to .\\.codex ^(or CODEX_HOME if set^)
echo -skills LIST Comma/space-separated skill names
echo -all Install all skills
echo -help, -h Show this help
echo.
echo Env:
echo CODEX_HOME Target Codex home ^(default: %%USERPROFILE%%\\.codex^)
exit /b 0
:InstallOne
set "NAME=%~1"
set "SRC_DIR=%SKILLS_SRC_ROOT%\\%NAME%"
set "DST_DIR=%SKILLS_DST_ROOT%\\%NAME%"
if not exist "%SRC_DIR%" (
echo ERROR: skill not found: %NAME% "%SRC_DIR%"
exit /b 1
)
if exist "%DST_DIR%" (
set "RAND=%RANDOM%"
pushd "%SKILLS_DST_ROOT%"
ren "%NAME%" "%NAME%.bak.!RAND!"
popd
echo Backed up existing skill: %NAME% -> %NAME%.bak.!RAND!
)
xcopy "%SRC_DIR%\\*" "%DST_DIR%\\" /e /i /y >nul
if errorlevel 1 (
echo ERROR: failed to copy skill: %NAME%
exit /b 1
)
echo Installed: %NAME%
exit /b 0
-105
View File
@@ -1,105 +0,0 @@
# Install Codex skills from this Playbook snapshot into CODEX_HOME.
# - Source: <snapshot>\codex\skills\<skill-name>\
# - Dest: $env:CODEX_HOME\skills\<skill-name>\ (default CODEX_HOME=$HOME\.codex)
#
# Usage:
# powershell -File scripts/install_codex_skills.ps1
# powershell -File scripts/install_codex_skills.ps1 style-cleanup commit-message
# powershell -File scripts/install_codex_skills.ps1 -Local
#
# Notes:
# - Codex loads skills at startup; restart `codex` after installation.
# - Existing destination skill dirs are backed up with a timestamp suffix.
[CmdletBinding()]
param(
[Alias('h', '?')]
[switch]$Help,
[switch]$Local,
[switch]$All,
[Parameter(Mandatory = $false)]
[string[]]$Skills
)
$ErrorActionPreference = "Stop"
if ($Help) {
Write-Host "Usage:"
Write-Host " powershell -File scripts/install_codex_skills.ps1 -All"
Write-Host " powershell -File scripts/install_codex_skills.ps1 -Skills style-cleanup,commit-message"
Write-Host ""
Write-Host "Options:"
Write-Host " -Local Install to ./.codex (or CODEX_HOME if set)."
Write-Host " -Skills Comma/space-separated skill names."
Write-Host " -All Install all skills."
Write-Host " -Help Show this help."
Write-Host ""
Write-Host "Env:"
Write-Host " CODEX_HOME Target Codex home (default: ~/.codex)."
exit 0
}
if ($All -and $Skills -and $Skills.Count -gt 0) {
throw "Use either -All or -Skills, not both."
}
if (-not $All -and (-not $Skills -or $Skills.Count -eq 0)) {
throw "Missing -All or -Skills. Use -Help for usage."
}
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$Src = (Resolve-Path (Join-Path $ScriptDir "..")).Path
$SkillsSrcRoot = Join-Path $Src "codex/skills"
if (-not (Test-Path $SkillsSrcRoot)) {
throw "Skills source dir not found: $SkillsSrcRoot"
}
$CodexHome = $env:CODEX_HOME
if ($Local) {
$localHome = Join-Path (Get-Location) ".codex"
if (-not $CodexHome) { $CodexHome = $localHome }
}
if (-not $CodexHome) {
$homeDir = $HOME
if (-not $homeDir) { $homeDir = $env:USERPROFILE }
$CodexHome = (Join-Path $homeDir ".codex")
}
$SkillsDstRoot = Join-Path $CodexHome "skills"
New-Item -ItemType Directory -Path $SkillsDstRoot -Force | Out-Null
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
function Install-One([string]$Name) {
$srcDir = Join-Path $SkillsSrcRoot $Name
$dstDir = Join-Path $SkillsDstRoot $Name
if (-not (Test-Path $srcDir)) {
throw "Skill not found: $Name ($srcDir)"
}
if (Test-Path $dstDir) {
$bak = Join-Path $SkillsDstRoot "$Name.bak.$timestamp"
Move-Item $dstDir $bak
Write-Host "Backed up existing skill: $Name -> $(Split-Path -Leaf $bak)"
}
Copy-Item $srcDir $dstDir -Recurse -Force
Write-Host "Installed: $Name"
}
if ($All) {
foreach ($dir in (Get-ChildItem -Path $SkillsSrcRoot -Directory)) {
if ($dir.Name.StartsWith(".")) { continue }
Install-One $dir.Name
}
} else {
foreach ($item in $Skills) {
if (-not $item) { continue }
foreach ($part in $item.Split(@(',', ' '), [System.StringSplitOptions]::RemoveEmptyEntries)) {
if (-not $part) { continue }
Install-One $part
}
}
}
Write-Host "Done. Skills installed to: $SkillsDstRoot"
-142
View File
@@ -1,142 +0,0 @@
#!/usr/bin/env sh
set -eu
# Install Codex skills from this Playbook snapshot into CODEX_HOME.
# - Source: <snapshot>/codex/skills/<skill-name>/
# - Dest: $CODEX_HOME/skills/<skill-name>/ (default CODEX_HOME=~/.codex)
#
# Usage:
# sh scripts/install_codex_skills.sh -all
# sh scripts/install_codex_skills.sh -skills style-cleanup,commit-message
# sh scripts/install_codex_skills.sh -local -all # install to <cwd>/.codex
#
# Notes:
# - Codex loads skills at startup; restart `codex` after installation.
# - Existing destination skill dirs are backed up with a timestamp suffix.
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)"
SRC="$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd -P)"
SKILLS_SRC_ROOT="$SRC/codex/skills"
usage() {
cat <<'EOF' >&2
Usage:
sh scripts/install_codex_skills.sh [options]
sh scripts/install_codex_skills.sh -skills style-cleanup,commit-message
sh scripts/install_codex_skills.sh -all
Options:
-local, -l Install to ./.codex (or CODEX_HOME if set).
-skills LIST Comma/space-separated skill names.
-all Install all skills.
-h, -help Show this help.
Env:
CODEX_HOME Target Codex home (default: ~/.codex).
EOF
}
LOCAL_MODE=0
INSTALL_ALL=0
SKILLS=""
while [ $# -gt 0 ]; do
case "$1" in
-local|-l)
LOCAL_MODE=1
shift
;;
-skills)
if [ $# -lt 2 ] || [ -z "${2:-}" ]; then
echo "ERROR: -skills requires a value." >&2
usage
exit 1
fi
SKILLS="$2"
shift 2
;;
-all)
INSTALL_ALL=1
shift
;;
-h|-help)
usage
exit 0
;;
-*)
echo "ERROR: Unknown option: $1" >&2
usage
exit 1
;;
*)
echo "ERROR: positional args are not supported; use -skills/-all." >&2
usage
exit 1
;;
esac
done
if [ "$INSTALL_ALL" -eq 1 ] && [ -n "$SKILLS" ]; then
echo "ERROR: use either -all or -skills, not both." >&2
usage
exit 1
fi
if [ "$INSTALL_ALL" -eq 0 ] && [ -z "$SKILLS" ]; then
echo "ERROR: -all or -skills is required." >&2
usage
exit 1
fi
if [ "$LOCAL_MODE" -eq 1 ]; then
LOCAL_CODEX_HOME="$(pwd -P)/.codex"
CODEX_HOME="${CODEX_HOME:-$LOCAL_CODEX_HOME}"
fi
CODEX_HOME="${CODEX_HOME:-$HOME/.codex}"
SKILLS_DST_ROOT="$CODEX_HOME/skills"
if [ ! -d "$SKILLS_SRC_ROOT" ]; then
echo "ERROR: skills source dir not found: $SKILLS_SRC_ROOT" >&2
exit 1
fi
mkdir -p "$SKILLS_DST_ROOT"
timestamp="$(date +%Y%m%d%H%M%S 2>/dev/null || echo bak)"
install_one() {
name="$1"
src_dir="$SKILLS_SRC_ROOT/$name"
dst_dir="$SKILLS_DST_ROOT/$name"
if [ ! -d "$src_dir" ]; then
echo "ERROR: skill not found: $name ($src_dir)" >&2
exit 1
fi
if [ -e "$dst_dir" ]; then
mv "$dst_dir" "$SKILLS_DST_ROOT/$name.bak.$timestamp"
echo "Backed up existing skill: $name -> $name.bak.$timestamp"
fi
cp -R "$src_dir" "$dst_dir"
echo "Installed: $name"
}
if [ "$INSTALL_ALL" -eq 1 ]; then
for dir in "$SKILLS_SRC_ROOT"/*; do
[ -d "$dir" ] || continue
name="$(basename -- "$dir")"
case "$name" in
""|.*) continue ;;
esac
install_one "$name"
done
else
old_ifs="${IFS}"
IFS=', '
set -- $SKILLS
IFS="${old_ifs}"
for name in "$@"; do
[ -n "$name" ] || continue
install_one "$name"
done
fi
echo "Done. Skills installed to: $SKILLS_DST_ROOT"
-420
View File
@@ -1,420 +0,0 @@
@echo off
setlocal enabledelayedexpansion
rem Sync standards snapshot to project root.
rem - Copies <snapshot>\rulesets\<AGENTS_NS> -> <project-root>\.agents\<AGENTS_NS>
rem - Updates <project-root>\.gitattributes (append missing rules by default)
rem Existing targets are backed up before overwrite.
rem
rem Multi rulesets:
rem sync_standards.bat -langs tsl,cpp
rem Notes:
rem - When syncing multiple rulesets, .gitattributes is synced only once (first ruleset).
if /I "%~1"=="-h" goto show_help
if /I "%~1"=="-help" goto show_help
set "SCRIPT_DIR=%~dp0"
set "ROOT=%SYNC_ROOT%"
if "%ROOT%"=="" for /f "delims=" %%R in ('git -C "%SCRIPT_DIR%" rev-parse --show-toplevel 2^>nul') do set "ROOT=%%R"
if "%ROOT%"=="" set "ROOT=%cd%"
for %%I in ("%ROOT%") do set "ROOT=%%~fI"
for %%I in ("%SCRIPT_DIR%..") do set "SRC=%%~fI"
set "AGENTS_SRC_ROOT=%SRC%\rulesets"
set "GITATTR_SRC=%SRC%\.gitattributes"
if not exist "%AGENTS_SRC_ROOT%" (
echo ERROR: Standards snapshot not found at %AGENTS_SRC_ROOT% >&2
echo Run: git subtree add --prefix docs/standards/playbook ^<url^> ^<branch^> --squash >&2
exit /b 1
)
set "AGENTS_NS=%AGENTS_NS%"
set "GITATTR_DST=%ROOT%\.gitattributes"
set "SYNC_GITATTR_MODE=%SYNC_GITATTR_MODE%"
if "%SYNC_GITATTR_MODE%"=="" set "SYNC_GITATTR_MODE=append"
set "LANG_LIST="
:parse_args
if "%~1"=="" goto args_done
if /I "%~1"=="-h" goto show_help
if /I "%~1"=="-help" goto show_help
if /I "%~1"=="-langs" (
if "%~2"=="" goto missing_langs
set "LANG_LIST=%~2"
shift /1
shift /1
goto parse_args
)
echo ERROR: Unknown option: %~1
exit /b 1
:missing_langs
echo ERROR: -langs requires a value.
exit /b 1
:args_done
if not "%LANG_LIST%"=="" set "LANG_LIST=%LANG_LIST:,= %"
rem Multi rulesets: only on outer invocation.
if "%SYNC_STANDARDS_INNER%"=="" (
if "%LANG_LIST%"=="" (
if "%AGENTS_NS%"=="" (
echo ERROR: -langs is required.
exit /b 1
)
)
if not "%LANG_LIST%"=="" (
set "FIRST=1"
set "SYNC_FIRST=%SYNC_GITATTR_MODE%"
for %%L in (!LANG_LIST!) do (
if "!FIRST!"=="1" (
set "FIRST=0"
set "SYNC_STANDARDS_INNER=1"
set "AGENTS_NS=%%~L"
set "SYNC_GITATTR_MODE=!SYNC_FIRST!"
call "%~f0" -langs %%~L
) else (
set "SYNC_STANDARDS_INNER=1"
set "AGENTS_NS=%%~L"
set "SYNC_GITATTR_MODE=skip"
call "%~f0" -langs %%~L
)
)
exit /b 0
)
)
if "%AGENTS_NS%"=="" set "AGENTS_NS=tsl"
echo %AGENTS_NS%| findstr /r "[\\/]" >nul && (
echo ERROR: invalid AGENTS_NS=%AGENTS_NS%
exit /b 1
)
echo %AGENTS_NS%| findstr /c:".." >nul && (
echo ERROR: invalid AGENTS_NS=%AGENTS_NS%
exit /b 1
)
set "AGENTS_ROOT=%ROOT%\.agents"
set "AGENTS_DST=%AGENTS_ROOT%\%AGENTS_NS%"
set "AGENTS_SRC=%AGENTS_SRC_ROOT%\%AGENTS_NS%"
if not exist "%AGENTS_SRC%" (
rem Backward-compatible fallback: older snapshots used ^<snapshot^>\.agents\* directly.
if exist "%AGENTS_SRC_ROOT%\index.md" if exist "%AGENTS_SRC_ROOT%\auth.md" (
set "AGENTS_SRC=%AGENTS_SRC_ROOT%"
) else (
echo ERROR: Standards snapshot not found at "%AGENTS_SRC%".
echo Hint: set AGENTS_NS to one of the subdirs under "%AGENTS_SRC_ROOT%" ^(e.g. tsl/cpp^).
exit /b 1
)
)
if not exist "%AGENTS_SRC%" (
echo ERROR: Standards snapshot not found at "%AGENTS_SRC%".
echo Run: git subtree add --prefix docs/standards/playbook ^<standards-url^> ^<branch^> --squash
exit /b 1
)
if /I "%SRC%"=="%ROOT%" (
echo Skip: snapshot root equals project root.
goto AfterGitAttr
)
if not exist "%AGENTS_ROOT%" mkdir "%AGENTS_ROOT%"
if exist "%AGENTS_DST%" (
set "RAND=%RANDOM%"
pushd "%AGENTS_ROOT%"
ren "%AGENTS_NS%" "%AGENTS_NS%.bak.!RAND!"
popd
echo Backed up existing %AGENTS_NS% agents -> %AGENTS_NS%.bak.!RAND!
)
xcopy "%AGENTS_SRC%\\*" "%AGENTS_DST%\\" /e /i /y >nul
if errorlevel 1 (
echo ERROR: failed to copy .agents
exit /b 1
)
echo Synced .agents\%AGENTS_NS% from standards.
set "REL_SNAPSHOT=%SRC:%ROOT%\=%"
if /I not "%REL_SNAPSHOT%"=="%SRC%" (
set "DOCS_PREFIX=%REL_SNAPSHOT%\docs"
set "DOCS_PREFIX=%DOCS_PREFIX:\=/%"
for %%F in ("%AGENTS_DST%\*.md") do (
powershell -NoProfile -Command "$p='%%~fF'; $c=Get-Content -Raw $p; $c=$c.Replace('`docs/tsl/','`%DOCS_PREFIX%/tsl/'); $c=$c.Replace('`docs/cpp/','`%DOCS_PREFIX%/cpp/'); $c=$c.Replace('`docs/python/','`%DOCS_PREFIX%/python/'); $c=$c.Replace('`docs/markdown/','`%DOCS_PREFIX%/markdown/'); $c=$c.Replace('`docs/common/','`%DOCS_PREFIX%/common/'); Set-Content -Path $p -Value $c -Encoding UTF8"
)
)
if not exist "%AGENTS_ROOT%\index.md" (
> "%AGENTS_ROOT%\index.md" echo # .agents(多语言)
>> "%AGENTS_ROOT%\index.md" echo.
>> "%AGENTS_ROOT%\index.md" echo 本目录用于存放仓库级/语言级的代理规则集。
>> "%AGENTS_ROOT%\index.md" echo.
>> "%AGENTS_ROOT%\index.md" echo 建议约定:
>> "%AGENTS_ROOT%\index.md" echo.
>> "%AGENTS_ROOT%\index.md" echo - `.agents/tsl/`TSL 相关规则集(由 `sync_standards.*` 同步;适用于 `.tsl`/`.tsf`
>> "%AGENTS_ROOT%\index.md" echo - `.agents/cpp/`C++ 相关规则集(由 `sync_standards.*` 同步;适用于 C++23/Modules
>> "%AGENTS_ROOT%\index.md" echo - `.agents/python/`Python 相关规则集(由 `sync_standards.*` 同步)
>> "%AGENTS_ROOT%\index.md" echo - `.agents/markdown/`Markdown 相关规则集(仅代码格式化)
>> "%AGENTS_ROOT%\index.md" echo.
>> "%AGENTS_ROOT%\index.md" echo 规则发生冲突时,建议以“更靠近代码的目录规则更具体”为准。
>> "%AGENTS_ROOT%\index.md" echo.
>> "%AGENTS_ROOT%\index.md" echo 入口建议从:
>> "%AGENTS_ROOT%\index.md" echo.
>> "%AGENTS_ROOT%\index.md" echo - `.agents/tsl/index.md`TSL 规则集入口)
>> "%AGENTS_ROOT%\index.md" echo - `.agents/cpp/index.md`C++ 规则集入口)
>> "%AGENTS_ROOT%\index.md" echo - `.agents/markdown/index.md`Markdown 规则集入口)
>> "%AGENTS_ROOT%\index.md" echo - `docs/standards/playbook/docs/`(人类开发规范快照:`tsl/`、`cpp/`、`python/`、`common/`
echo Created .agents\index.md
)
set "AGENTS_LANGS="
for /d %%D in ("%AGENTS_ROOT%\*") do (
set "NAME=%%~nxD"
if /I not "!NAME:~0,1!"=="." (
echo "!NAME!" | findstr /I /C:".bak." >nul
if errorlevel 1 (
if exist "%%D\index.md" (
if defined AGENTS_LANGS (
set "AGENTS_LANGS=!AGENTS_LANGS!、`.agents/!NAME!/index.md`"
) else (
set "AGENTS_LANGS=`.agents/!NAME!/index.md`"
)
)
)
)
)
if not defined AGENTS_LANGS set "AGENTS_LANGS=`.agents/%AGENTS_NS%/index.md`"
set "AGENTS_BLOCK_START=<!-- playbook:agents:start -->"
set "AGENTS_BLOCK_END=<!-- playbook:agents:end -->"
set "AGENTS_BLOCK_FILE=%ROOT%\.agents_block.!RANDOM!.tmp"
> "%AGENTS_BLOCK_FILE%" echo %AGENTS_BLOCK_START%
>> "%AGENTS_BLOCK_FILE%" echo.
>> "%AGENTS_BLOCK_FILE%" echo 请以 `.agents/` 下的规则为准:
>> "%AGENTS_BLOCK_FILE%" echo.
>> "%AGENTS_BLOCK_FILE%" echo - 入口:`.agents/index.md`
>> "%AGENTS_BLOCK_FILE%" echo - 语言规则:%AGENTS_LANGS%
>> "%AGENTS_BLOCK_FILE%" echo %AGENTS_BLOCK_END%
set "AGENTS_MD=%ROOT%\AGENTS.md"
if not exist "%AGENTS_MD%" (
> "%AGENTS_MD%" echo # Agent Instructions
>> "%AGENTS_MD%" echo.
type "%AGENTS_BLOCK_FILE%" >> "%AGENTS_MD%"
echo Created AGENTS.md
) else (
findstr /C:"%AGENTS_BLOCK_START%" "%AGENTS_MD%" >nul
if not errorlevel 1 (
powershell -NoProfile -Command "$file='%AGENTS_MD%'; $block=Get-Content -Raw '%AGENTS_BLOCK_FILE%'; $start='%AGENTS_BLOCK_START%'; $end='%AGENTS_BLOCK_END%'; $pattern=[regex]::Escape($start)+'.*?'+[regex]::Escape($end); $regex=New-Object System.Text.RegularExpressions.Regex($pattern,[System.Text.RegularExpressions.RegexOptions]::Singleline); $content=Get-Content -Raw $file; $new=$regex.Replace($content,$block,1); Set-Content -Path $file -Value $new -Encoding UTF8"
echo Updated AGENTS.md (playbook block).
) else (
findstr /C:".agents/index.md" "%AGENTS_MD%" >nul
if not errorlevel 1 (
echo Skip: AGENTS.md already references .agents/index.md
) else (
>> "%AGENTS_MD%" echo.
type "%AGENTS_BLOCK_FILE%" >> "%AGENTS_MD%"
>> "%AGENTS_MD%" echo.
echo Appended playbook block to AGENTS.md
)
)
)
del "%AGENTS_BLOCK_FILE%" >nul 2>&1
:SyncGitAttr
if exist "%GITATTR_SRC%" (
if /I "%SYNC_GITATTR_MODE%"=="skip" (
echo Skip: .gitattributes sync ^(SYNC_GITATTR_MODE=skip^).
goto AfterGitAttr
)
if /I "%SYNC_GITATTR_MODE%"=="overwrite" (
for %%I in ("%GITATTR_SRC%") do set "GITATTR_SRC_F=%%~fI"
for %%I in ("%GITATTR_DST%") do set "GITATTR_DST_F=%%~fI"
if /I "!GITATTR_SRC_F!"=="!GITATTR_DST_F!" (
echo Skip: .gitattributes source equals destination.
goto AfterGitAttr
)
if exist "%GITATTR_DST%" (
set "RAND=%RANDOM%"
set "BAK_NAME=.gitattributes.bak.!RAND!"
ren "%GITATTR_DST%" "!BAK_NAME!"
echo Backed up existing .gitattributes -> !BAK_NAME!
)
copy /y "%GITATTR_SRC%" "%GITATTR_DST%" >nul
echo Synced .gitattributes from standards ^(overwrite^).
goto AfterGitAttr
)
if /I "%SYNC_GITATTR_MODE%"=="append" (
for %%I in ("%GITATTR_SRC%") do set "GITATTR_SRC_F=%%~fI"
for %%I in ("%GITATTR_DST%") do set "GITATTR_DST_F=%%~fI"
if /I "!GITATTR_SRC_F!"=="!GITATTR_DST_F!" (
echo Skip: .gitattributes source equals destination.
goto AfterGitAttr
)
set "TMP_DST=%TEMP%\\gitattributes.dst.%RANDOM%.tmp"
set "TMP_MISS=%TEMP%\\gitattributes.missing.%RANDOM%.tmp"
if exist "!TMP_DST!" del /q "!TMP_DST!" >nul 2>nul
if exist "!TMP_MISS!" del /q "!TMP_MISS!" >nul 2>nul
type nul > "!TMP_DST!"
type nul > "!TMP_MISS!"
if exist "%GITATTR_DST%" (
for /f "usebackq delims=" %%L in ("%GITATTR_DST%") do (
set "LINE=%%L"
for /f "tokens=* delims= " %%A in ("!LINE!") do set "LINE=%%A"
if not "!LINE!"=="" (
if /I not "!LINE:~0,1!"=="#" (
echo(!LINE!>>"!TMP_DST!"
)
)
)
)
for /f "usebackq delims=" %%L in ("%GITATTR_SRC%") do (
set "LINE=%%L"
for /f "tokens=* delims= " %%A in ("!LINE!") do set "LINE=%%A"
if not "!LINE!"=="" (
if /I not "!LINE:~0,1!"=="#" (
findstr /x /l /c:"!LINE!" "!TMP_DST!" >nul || (
findstr /x /l /c:"!LINE!" "!TMP_MISS!" >nul || echo(!LINE!>>"!TMP_MISS!"
)
)
)
)
set "MISS_SIZE=0"
if exist "!TMP_MISS!" for %%S in ("!TMP_MISS!") do set "MISS_SIZE=%%~zS"
if "!MISS_SIZE!"=="0" (
del /q "!TMP_DST!" "!TMP_MISS!" >nul 2>nul
echo No missing .gitattributes rules to append.
goto AfterGitAttr
)
if exist "%GITATTR_DST%" (
set "RAND=%RANDOM%"
set "BAK_NAME=.gitattributes.bak.!RAND!"
ren "%GITATTR_DST%" "!BAK_NAME!"
echo Backed up existing .gitattributes -> !BAK_NAME!
set "DST_IN=%ROOT%\\!BAK_NAME!"
) else (
set "DST_IN="
)
set "TMP_OUT=%TEMP%\\gitattributes.out.%RANDOM%.tmp"
if exist "!TMP_OUT!" del /q "!TMP_OUT!" >nul 2>nul
if not "!DST_IN!"=="" (
type "!DST_IN!" > "!TMP_OUT!"
for %%S in ("!DST_IN!") do set "DST_SIZE=%%~zS"
if not "!DST_SIZE!"=="0" echo.>>"!TMP_OUT!"
)
set "SOURCE_NOTE=%GITATTR_SRC%"
>>"!TMP_OUT!" echo # Added from playbook .gitattributes ^(source: !SOURCE_NOTE!^)
type "!TMP_MISS!" >> "!TMP_OUT!"
copy /y "!TMP_OUT!" "%GITATTR_DST%" >nul
del /q "!TMP_DST!" "!TMP_MISS!" "!TMP_OUT!" >nul 2>nul
echo Appended missing .gitattributes rules from standards.
goto AfterGitAttr
)
if /I not "%SYNC_GITATTR_MODE%"=="block" (
echo ERROR: invalid SYNC_GITATTR_MODE=%SYNC_GITATTR_MODE% ^(use block^|overwrite^|append^|skip^)
exit /b 1
)
rem block mode: maintain a managed block inside the destination file
set "BEGIN=# BEGIN playbook .gitattributes"
set "END=# END playbook .gitattributes"
set "BEGIN_OLD=# BEGIN tsl-playbook .gitattributes"
set "END_OLD=# END tsl-playbook .gitattributes"
set "TMP_FILE=%TEMP%\\gitattributes.%RANDOM%.tmp"
if exist "%GITATTR_DST%" (
set "RAND=%RANDOM%"
set "BAK_NAME=.gitattributes.bak.!RAND!"
ren "%GITATTR_DST%" "!BAK_NAME!"
echo Backed up existing .gitattributes -> !BAK_NAME!
set "DST_IN=%ROOT%\\!BAK_NAME!"
) else (
set "DST_IN="
)
set "IN_BLOCK=0"
set "DONE=0"
if not "%DST_IN%"=="" (
> "!TMP_FILE!" (
for /f "usebackq delims=" %%L in ("!DST_IN!") do (
set "LINE=%%L"
if "!LINE!"=="%BEGIN%" (
if "!DONE!"=="0" (
echo %BEGIN%
type "%GITATTR_SRC%"
echo %END%
set "DONE=1"
)
set "IN_BLOCK=1"
) else if "!LINE!"=="%BEGIN_OLD%" (
if "!DONE!"=="0" (
echo %BEGIN%
type "%GITATTR_SRC%"
echo %END%
set "DONE=1"
)
set "IN_BLOCK=1"
) else if "!LINE!"=="%END%" (
set "IN_BLOCK=0"
) else if "!LINE!"=="%END_OLD%" (
set "IN_BLOCK=0"
) else (
if "!IN_BLOCK!"=="0" echo(!LINE!
)
)
if "!DONE!"=="0" (
echo.
echo %BEGIN%
type "%GITATTR_SRC%"
echo %END%
)
)
) else (
> "!TMP_FILE!" (
echo %BEGIN%
type "%GITATTR_SRC%"
echo %END%
)
)
copy /y "!TMP_FILE!" "%GITATTR_DST%" >nul
del /q "!TMP_FILE!" >nul 2>nul
echo Updated .gitattributes from standards ^(managed block^).
)
:AfterGitAttr
echo Done.
endlocal
exit /b 0
:show_help
echo Usage:
echo sync_standards.bat
echo sync_standards.bat -langs tsl,cpp
echo.
echo Options:
echo -langs Comma/space-separated list of languages ^(required^).
echo -h, -help Show this help.
echo.
echo Env:
echo SYNC_ROOT Target project root ^(default: git root^).
echo AGENTS_NS Single ruleset name ^(default: tsl^).
echo SYNC_GITATTR_MODE append^|overwrite^|block^|skip ^(default: append^).
exit /b 0
-338
View File
@@ -1,338 +0,0 @@
# Sync standards snapshot to project root.
# - Copies <snapshot>/rulesets/<AGENTS_NS> -> <project-root>/.agents/<AGENTS_NS>
# - Updates <project-root>/.gitattributes (append missing rules by default)
# Existing targets are backed up before overwrite.
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[Alias('h', '?')]
[switch]$Help,
# Sync multiple rulesets in one run:
# -Langs tsl,cpp
# -Langs @("tsl","cpp")
[Parameter(Mandatory = $false)]
[string[]]$Langs
)
$ErrorActionPreference = "Stop"
if ($Help) {
Write-Host "Usage:"
Write-Host " powershell -File scripts/sync_standards.ps1 -Langs tsl,cpp"
Write-Host ""
Write-Host "Options:"
Write-Host " -Langs <list> Comma/space-separated list or array (required)."
Write-Host " -Help Show this help."
Write-Host ""
Write-Host "Env:"
Write-Host " SYNC_ROOT Target project root (default: git root)."
Write-Host " AGENTS_NS Single ruleset name (default: tsl)."
Write-Host " SYNC_GITATTR_MODE append|overwrite|block|skip (default: append)."
exit 0
}
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$Src = (Resolve-Path (Join-Path $ScriptDir "..")).Path
$Root = $env:SYNC_ROOT
if (-not $Root) {
$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 "rulesets"
$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"
# Require explicit -Langs on outer invocation unless AGENTS_NS is provided.
if (-not $env:SYNC_STANDARDS_INNER -and (-not $Langs -or $Langs.Count -eq 0) -and -not $env:AGENTS_NS) {
throw "Missing -Langs. Use -Help for usage."
}
# 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 = "append" }
$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."
# Rewrite docs/* references to the snapshot docs path.
$relSnapshot = $null
$rootPrefix = $Root.TrimEnd('\', '/')
$rootPrefixWithSep = $rootPrefix + [System.IO.Path]::DirectorySeparatorChar
if ($Src.ToLowerInvariant().StartsWith($rootPrefixWithSep.ToLowerInvariant())) {
$relSnapshot = $Src.Substring($rootPrefixWithSep.Length)
}
if ($relSnapshot) {
$docsPrefix = (Join-Path $relSnapshot "docs") -replace "\\", "/"
Get-ChildItem -Path $AgentsDst -Filter *.md -File | ForEach-Object {
$content = Get-Content -Raw -Path $_.FullName
$content = $content.Replace("``docs/tsl/", "``$docsPrefix/tsl/")
$content = $content.Replace("``docs/cpp/", "``$docsPrefix/cpp/")
$content = $content.Replace("``docs/python/", "``$docsPrefix/python/")
$content = $content.Replace("``docs/markdown/", "``$docsPrefix/markdown/")
$content = $content.Replace("``docs/common/", "``$docsPrefix/common/")
Set-Content -Path $_.FullName -Value $content -Encoding UTF8
}
}
$AgentsIndex = Join-Path $AgentsRoot "index.md"
if (-not (Test-Path $AgentsIndex)) {
$agentsIndexContent = @'
# .agents(多语言)
本目录用于存放仓库级/语言级的代理规则集。
建议约定:
- `.agents/tsl/`TSL 相关规则集(由 `sync_standards.*` 同步;适用于 `.tsl`/`.tsf`
- `.agents/cpp/`C++ 相关规则集(由 `sync_standards.*` 同步;适用于 C++23/Modules
- `.agents/python/`Python 相关规则集(由 `sync_standards.*` 同步)
- `.agents/markdown/`Markdown 相关规则集(仅代码格式化)
规则发生冲突时,建议以“更靠近代码的目录规则更具体”为准。
入口建议从:
- `.agents/tsl/index.md`TSL 规则集入口)
- `.agents/cpp/index.md`C++ 规则集入口)
- `.agents/markdown/index.md`Markdown 规则集入口)
- `docs/standards/playbook/docs/`(人类开发规范快照:`tsl/`、`cpp/`、`python/`、`common/`
'@
Set-Content -Path $AgentsIndex -Encoding UTF8 -Value $agentsIndexContent
Write-Host "Created .agents/index.md"
}
$AgentsMd = Join-Path $Root "AGENTS.md"
$AgentsBlockStart = "<!-- playbook:agents:start -->"
$AgentsBlockEnd = "<!-- playbook:agents:end -->"
$agentsLangs = @()
if (Test-Path $AgentsRoot) {
Get-ChildItem -Path $AgentsRoot -Directory | ForEach-Object {
$name = $_.Name
if ($name -and -not $name.StartsWith(".") -and -not ($name -match "\.bak\.") -and (Test-Path (Join-Path $_.FullName "index.md"))) {
$agentsLangs += $name
}
}
}
if ($agentsLangs.Count -eq 0) { $agentsLangs = @($AgentsNs) }
$langsLine = ($agentsLangs | ForEach-Object { "`.agents/$_/index.md`" }) -join ""
$agentsBlock = @"
<!-- playbook:agents:start -->
请以 `.agents/` 下的规则为准
- 入口`.agents/index.md`
- 语言规则$langsLine
<!-- playbook:agents:end -->
"@
if (-not (Test-Path $AgentsMd)) {
$agentsMdContent = @"
# Agent Instructions
$agentsBlock
"@
Set-Content -Path $AgentsMd -Encoding UTF8 -Value $agentsMdContent
Write-Host "Created AGENTS.md"
} else {
$content = Get-Content -Raw -Path $AgentsMd
if ($content.Contains($AgentsBlockStart)) {
$pattern = [regex]::Escape($AgentsBlockStart) + ".*?" + [regex]::Escape($AgentsBlockEnd)
$regex = New-Object System.Text.RegularExpressions.Regex($pattern, [System.Text.RegularExpressions.RegexOptions]::Singleline)
$newContent = $regex.Replace($content, $agentsBlock, 1)
Set-Content -Path $AgentsMd -Value $newContent -Encoding UTF8
Write-Host "Updated AGENTS.md (playbook block)."
} elseif ($content.Contains(".agents/index.md")) {
Write-Host "Skip: AGENTS.md already references .agents/index.md"
} else {
Add-Content -Path $AgentsMd -Value "" -Encoding UTF8
Add-Content -Path $AgentsMd -Value $agentsBlock -Encoding UTF8
Add-Content -Path $AgentsMd -Value "" -Encoding UTF8
Write-Host "Appended playbook block to AGENTS.md"
}
}
$GitAttrDst = Join-Path $Root ".gitattributes"
if (Test-Path $GitAttrSrc) {
$mode = $env:SYNC_GITATTR_MODE
if (-not $mode) { $mode = "append" }
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
}
"append" {
if ($GitAttrSrc -ieq $GitAttrDst) {
Write-Host "Skip: .gitattributes source equals destination."
break
}
$dstLines = @{}
if (Test-Path $GitAttrDst) {
Get-Content $GitAttrDst | ForEach-Object {
$line = $_.Trim()
if ($line -eq "" -or $line.StartsWith("#")) { return }
$dstLines[$line] = $true
}
}
$missing = New-Object System.Collections.Generic.List[string]
Get-Content $GitAttrSrc | ForEach-Object {
$line = $_.Trim()
if ($line -eq "" -or $line.StartsWith("#")) { return }
if (-not $dstLines.ContainsKey($line)) {
$dstLines[$line] = $true
$missing.Add($line)
}
}
if ($missing.Count -eq 0) {
Write-Host "No missing .gitattributes rules to append."
break
}
$bak = $null
if (Test-Path $GitAttrDst) {
$bak = "$GitAttrDst.bak.$timestamp"
Move-Item $GitAttrDst $bak -Force
Write-Host "Backed up existing .gitattributes -> $bak"
}
$sourceNote = $GitAttrSrc
$rootPrefix = "$Root\"
if ($sourceNote.StartsWith($rootPrefix)) {
$sourceNote = $sourceNote.Substring($rootPrefix.Length)
}
$header = "# Added from playbook .gitattributes (source: $sourceNote)"
$content = @()
if ($bak -and (Test-Path $bak)) {
$existing = Get-Content $bak
if ($existing.Count -gt 0) {
$content += $existing
$content += ""
}
}
$content += $header
$content += $missing
$content | Set-Content -Path $GitAttrDst -Encoding UTF8
Write-Host "Appended missing .gitattributes rules from standards."
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|append|skip)"
}
}
}
Write-Host "Done."
-428
View File
@@ -1,428 +0,0 @@
#!/usr/bin/env sh
set -eu
# Sync standards snapshot to project root.
# - Copies <snapshot>/rulesets/<AGENTS_NS> -> <project-root>/.agents/<AGENTS_NS>
# - Updates <project-root>/.gitattributes (append missing rules by default)
# Existing targets are backed up before overwrite.
#
# Multi rulesets:
# sh .../sync_standards.sh -langs tsl,cpp
# Notes:
# - When syncing multiple rulesets, .gitattributes is synced only once (first ruleset).
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)"
SRC="$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd -P)"
if [ -n "${SYNC_ROOT:-}" ]; then
ROOT="$SYNC_ROOT"
else
ROOT="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null || pwd)"
fi
ROOT="$(CDPATH= cd -- "$ROOT" && pwd -P)"
usage() {
cat <<'EOF' >&2
Usage:
sh scripts/sync_standards.sh -langs tsl
sh scripts/sync_standards.sh -langs tsl,cpp
Options:
-langs L1,L2 Comma/space-separated list of languages (required).
-h, -help Show this help.
Env:
SYNC_ROOT Target project root (default: git root).
AGENTS_NS Single ruleset name (default: tsl).
SYNC_GITATTR_MODE append|overwrite|block|skip (default: append).
EOF
}
if [ "${1:-}" = "-h" ] || [ "${1:-}" = "-help" ]; then
usage
exit 0
fi
langs=""
while [ $# -gt 0 ]; do
case "$1" in
-langs)
if [ $# -lt 2 ] || [ -z "${2:-}" ]; then
echo "ERROR: -langs requires a value." >&2
usage
exit 1
fi
langs="$2"
shift 2
;;
-*)
echo "ERROR: Unknown option: $1" >&2
usage
exit 1
;;
*)
echo "ERROR: positional args are not supported; use -langs." >&2
usage
exit 1
;;
esac
done
AGENTS_SRC_ROOT="$SRC/rulesets"
GITATTR_SRC="$SRC/.gitattributes"
if [ ! -d "$AGENTS_SRC_ROOT" ]; then
echo "ERROR: Standards snapshot not found at $AGENTS_SRC_ROOT" >&2
echo "Run: git subtree add --prefix docs/standards/playbook <standards-url> <branch> --squash" >&2
exit 1
fi
timestamp="$(date +%Y%m%d%H%M%S 2>/dev/null || echo bak)"
if [ "$SRC" = "$ROOT" ]; then
echo "Skip: snapshot root equals project root."
echo "Done."
exit 0
fi
# Parse multi rulesets only on the outer invocation.
if [ "${SYNC_STANDARDS_INNER:-}" != "1" ]; then
if [ -z "${langs:-}" ] && [ -z "${AGENTS_NS:-}" ]; then
echo "ERROR: -langs is required." >&2
usage
exit 1
fi
if [ -n "${langs:-}" ]; then
sync_mode_first="${SYNC_GITATTR_MODE:-append}"
first=1
old_ifs="${IFS}"
IFS=', '
set -- $langs
IFS="${old_ifs}"
for ns in "$@"; do
[ -n "$ns" ] || continue
if [ "$first" -eq 1 ]; then
first=0
SYNC_STANDARDS_INNER=1 AGENTS_NS="$ns" SYNC_GITATTR_MODE="$sync_mode_first" sh "$0" -langs "$ns"
else
SYNC_STANDARDS_INNER=1 AGENTS_NS="$ns" SYNC_GITATTR_MODE=skip sh "$0" -langs "$ns"
fi
done
exit 0
fi
fi
: "${AGENTS_NS:=tsl}"
case "$AGENTS_NS" in
""|*/*|*\\*|*..*)
echo "ERROR: invalid AGENTS_NS=$AGENTS_NS" >&2
exit 1
;;
esac
AGENTS_SRC="$AGENTS_SRC_ROOT/$AGENTS_NS"
if [ ! -d "$AGENTS_SRC" ]; then
# Backward-compatible fallback: older snapshots used <snapshot>/.agents/* directly.
if [ -f "$AGENTS_SRC_ROOT/index.md" ] && [ -f "$AGENTS_SRC_ROOT/auth.md" ]; then
AGENTS_SRC="$AGENTS_SRC_ROOT"
else
echo "ERROR: agents ruleset not found: $AGENTS_SRC" >&2
echo "Hint: set AGENTS_NS to one of the subdirs under $AGENTS_SRC_ROOT (e.g. tsl/cpp)." >&2
exit 1
fi
fi
AGENTS_ROOT="$ROOT/.agents"
AGENTS_DST="$AGENTS_ROOT/$AGENTS_NS"
mkdir -p "$AGENTS_ROOT"
if [ -e "$AGENTS_DST" ]; then
mv "$AGENTS_DST" "$AGENTS_ROOT/$AGENTS_NS.bak.$timestamp"
echo "Backed up existing $AGENTS_NS agents -> $AGENTS_NS.bak.$timestamp"
fi
cp -R "$AGENTS_SRC" "$AGENTS_DST"
echo "Synced .agents/$AGENTS_NS from standards."
# Rewrite docs/* references to the snapshot docs path.
REL_SNAPSHOT=""
case "$SRC" in
"$ROOT"/*) REL_SNAPSHOT="${SRC#$ROOT/}" ;;
esac
if [ -n "$REL_SNAPSHOT" ]; then
DOCS_PREFIX="$REL_SNAPSHOT/docs"
for md in "$AGENTS_DST"/*.md; do
[ -f "$md" ] || continue
tmp="$(mktemp 2>/dev/null || echo "$AGENTS_DST/.rewrite.$(basename "$md").$timestamp")"
sed \
-e 's#`docs/tsl/#`'"$DOCS_PREFIX"'/tsl/#g' \
-e 's#`docs/cpp/#`'"$DOCS_PREFIX"'/cpp/#g' \
-e 's#`docs/python/#`'"$DOCS_PREFIX"'/python/#g' \
-e 's#`docs/markdown/#`'"$DOCS_PREFIX"'/markdown/#g' \
-e 's#`docs/common/#`'"$DOCS_PREFIX"'/common/#g' \
"$md" >"$tmp"
mv "$tmp" "$md"
done
fi
AGENTS_INDEX="$AGENTS_ROOT/index.md"
if [ ! -f "$AGENTS_INDEX" ]; then
cat >"$AGENTS_INDEX" <<'EOF'
# .agents(多语言)
本目录用于存放仓库级/语言级的代理规则集。
建议约定:
- `.agents/tsl/`TSL 相关规则集(由 `sync_standards.*` 同步;适用于 `.tsl`/`.tsf`
- `.agents/cpp/`C++ 相关规则集(由 `sync_standards.*` 同步;适用于 C++23/Modules
- `.agents/python/`Python 相关规则集(由 `sync_standards.*` 同步)
- `.agents/markdown/`Markdown 相关规则集(仅代码格式化)
规则发生冲突时,建议以“更靠近代码的目录规则更具体”为准。
入口建议从:
- `.agents/tsl/index.md`TSL 规则集入口)
- `.agents/cpp/index.md`C++ 规则集入口)
- `.agents/markdown/index.md`Markdown 规则集入口)
- `docs/standards/playbook/docs/`(人类开发规范快照:`tsl/`、`cpp/`、`python/`、`common/`
EOF
echo "Created .agents/index.md"
fi
AGENTS_MD="$ROOT/AGENTS.md"
AGENTS_BLOCK_START="<!-- playbook:agents:start -->"
AGENTS_BLOCK_END="<!-- playbook:agents:end -->"
AGENTS_BLOCK_TMP="$(mktemp 2>/dev/null || echo "$ROOT/.agents_block.$timestamp")"
agents_langs=""
if [ -d "$AGENTS_ROOT" ]; then
for dir in "$AGENTS_ROOT"/*; do
[ -d "$dir" ] || continue
name="$(basename "$dir")"
case "$name" in
""|.*|*.bak.*) continue ;;
esac
if [ -f "$dir/index.md" ]; then
agents_langs="${agents_langs:+$agents_langs }$name"
fi
done
fi
if [ -z "$agents_langs" ]; then
agents_langs="$AGENTS_NS"
fi
langs_line=""
for name in $agents_langs; do
entry='`.agents/'"$name"'/index.md`'
if [ -z "$langs_line" ]; then
langs_line="$entry"
else
langs_line="$langs_line$entry"
fi
done
{
printf "%s\n\n" "$AGENTS_BLOCK_START"
printf "%s\n\n" '请以 `.agents/` 下的规则为准:'
printf "%s\n" '- 入口:`.agents/index.md`'
if [ -n "$langs_line" ]; then
printf "%s\n" "- 语言规则:$langs_line"
else
printf "%s\n" "- 语言规则:"
fi
printf "%s\n" "$AGENTS_BLOCK_END"
} >"$AGENTS_BLOCK_TMP"
if [ ! -f "$AGENTS_MD" ]; then
{
printf "%s\n\n" "# Agent Instructions"
cat "$AGENTS_BLOCK_TMP"
} >"$AGENTS_MD"
echo "Created AGENTS.md"
else
if grep -Fq "$AGENTS_BLOCK_START" "$AGENTS_MD"; then
tmp="$(mktemp 2>/dev/null || echo "$ROOT/.agents_md.$timestamp")"
awk -v start="$AGENTS_BLOCK_START" -v end="$AGENTS_BLOCK_END" -v block_file="$AGENTS_BLOCK_TMP" '
BEGIN {
while ((getline line < block_file) > 0) { block[++n] = line }
close(block_file)
inblock=0
replaced=0
}
{
if (!replaced && $0 == start) {
for (i=1; i<=n; i++) print block[i]
inblock=1
replaced=1
next
}
if (inblock) {
if ($0 == end) { inblock=0 }
next
}
print
}
' "$AGENTS_MD" >"$tmp"
mv "$tmp" "$AGENTS_MD"
echo "Updated AGENTS.md (playbook block)."
else
if grep -Fq ".agents/index.md" "$AGENTS_MD"; then
echo "Skip: AGENTS.md already references .agents/index.md"
else
printf "\n" >>"$AGENTS_MD"
cat "$AGENTS_BLOCK_TMP" >>"$AGENTS_MD"
printf "\n" >>"$AGENTS_MD"
echo "Appended playbook block to AGENTS.md"
fi
fi
fi
rm -f "$AGENTS_BLOCK_TMP"
echo "Synced agents ruleset to $AGENTS_DST."
GITATTR_DST="$ROOT/.gitattributes"
if [ -f "$GITATTR_SRC" ]; then
: "${SYNC_GITATTR_MODE:=append}"
case "$SYNC_GITATTR_MODE" in
skip)
echo "Skip: .gitattributes sync (SYNC_GITATTR_MODE=skip)."
;;
overwrite)
if [ "$(CDPATH= cd -- "$(dirname -- "$GITATTR_SRC")" && pwd -P)/$(basename -- "$GITATTR_SRC")" = "$GITATTR_DST" ]; then
echo "Skip: .gitattributes source equals destination."
else
if [ -e "$GITATTR_DST" ]; then
mv "$GITATTR_DST" "$ROOT/.gitattributes.bak.$timestamp"
echo "Backed up existing .gitattributes -> .gitattributes.bak.$timestamp"
fi
cp "$GITATTR_SRC" "$GITATTR_DST"
echo "Synced .gitattributes from standards (overwrite)."
fi
;;
append)
if [ "$(CDPATH= cd -- "$(dirname -- "$GITATTR_SRC")" && pwd -P)/$(basename -- "$GITATTR_SRC")" = "$GITATTR_DST" ]; then
echo "Skip: .gitattributes source equals destination."
else
missing_tmp="$(mktemp 2>/dev/null || echo "$ROOT/.gitattributes.missing.$timestamp")"
if [ -f "$GITATTR_DST" ]; then
awk '
function norm(line) {
gsub(/^[ \t]+|[ \t]+$/, "", line)
return line
}
FNR==NR {
line=norm($0)
if (line == "" || line ~ /^#/) next
seen[line]=1
next
}
{
line=norm($0)
if (line == "" || line ~ /^#/) next
if (!seen[line] && !out[line]++) print line
}
' "$GITATTR_DST" "$GITATTR_SRC" >"$missing_tmp"
else
awk '
function norm(line) {
gsub(/^[ \t]+|[ \t]+$/, "", line)
return line
}
{
line=norm($0)
if (line == "" || line ~ /^#/) next
if (!out[line]++) print line
}
' "$GITATTR_SRC" >"$missing_tmp"
fi
if [ ! -s "$missing_tmp" ]; then
rm -f "$missing_tmp"
echo "No missing .gitattributes rules to append."
echo "Done."
exit 0
fi
if [ -e "$GITATTR_DST" ]; then
mv "$GITATTR_DST" "$ROOT/.gitattributes.bak.$timestamp"
echo "Backed up existing .gitattributes -> .gitattributes.bak.$timestamp"
fi
source_note="$GITATTR_SRC"
case "$GITATTR_SRC" in
"$ROOT"/*) source_note="${GITATTR_SRC#$ROOT/}" ;;
esac
header="# Added from playbook .gitattributes (source: $source_note)"
{
if [ -f "$ROOT/.gitattributes.bak.$timestamp" ]; then
cat "$ROOT/.gitattributes.bak.$timestamp"
if [ -s "$ROOT/.gitattributes.bak.$timestamp" ]; then
printf "\n"
fi
fi
printf "%s\n" "$header"
cat "$missing_tmp"
} >"$GITATTR_DST"
rm -f "$missing_tmp"
echo "Appended missing .gitattributes rules from standards."
fi
;;
block)
begin="# BEGIN playbook .gitattributes"
end="# END playbook .gitattributes"
begin_old="# BEGIN tsl-playbook .gitattributes"
end_old="# END tsl-playbook .gitattributes"
if [ -e "$GITATTR_DST" ]; then
mv "$GITATTR_DST" "$ROOT/.gitattributes.bak.$timestamp"
echo "Backed up existing .gitattributes -> .gitattributes.bak.$timestamp"
fi
tmp="${GITATTR_DST}.tmp.${timestamp}"
if [ -f "$ROOT/.gitattributes.bak.$timestamp" ]; then
src_dst="$ROOT/.gitattributes.bak.$timestamp"
else
src_dst=""
fi
if [ -n "$src_dst" ]; then
awk -v begin="$begin" -v end="$end" -v begin_old="$begin_old" -v end_old="$end_old" -v src="$GITATTR_SRC" '
function emit_src() {
print begin
while ((getline line < src) > 0) print line
close(src)
print end
}
BEGIN { in_block=0; done=0 }
$0 == begin || $0 == begin_old { in_block=1; if (!done) { emit_src(); done=1 } ; next }
$0 == end || $0 == end_old { in_block=0; next }
!in_block { print }
END {
if (!done) {
if (NR > 0) print ""
emit_src()
}
}
' "$src_dst" >"$tmp"
else
{
printf "%s\n" "$begin"
cat "$GITATTR_SRC"
printf "\n%s\n" "$end"
} >"$tmp"
fi
mv "$tmp" "$GITATTR_DST"
echo "Updated .gitattributes from standards (managed block)."
;;
*)
echo "ERROR: invalid SYNC_GITATTR_MODE=$SYNC_GITATTR_MODE (use block|overwrite|append|skip)" >&2
exit 1
;;
esac
fi
echo "Done."
-233
View File
@@ -1,233 +0,0 @@
@echo off
setlocal enabledelayedexpansion
rem Sync project templates to target project.
rem - Copies templates/memory-bank/ -> <project-root>/memory-bank/
rem - Copies templates/prompts/ -> <project-root>/docs/prompts/
rem - Copies templates/AGENTS.template.md -> <project-root>/AGENTS.md
rem - Copies templates/AGENT_RULES.template.md -> <project-root>/AGENT_RULES.md
rem Existing targets are NOT overwritten (skip if exists).
rem
rem Usage:
rem sync_templates.bat # sync to current git root
rem sync_templates.bat -project-root <path> # sync to specified project
rem sync_templates.bat -force # overwrite existing files
rem sync_templates.bat -full # append full framework to existing AGENTS.md
set "SCRIPT_DIR=%~dp0"
for %%I in ("%SCRIPT_DIR%..") do set "SRC=%%~fI"
set "FORCE=0"
set "FULL=0"
set "PROJECT_ROOT="
:parse_args
if "%~1"=="" goto args_done
if /I "%~1"=="-force" (
set "FORCE=1"
shift
goto parse_args
)
if /I "%~1"=="-full" (
set "FULL=1"
shift
goto parse_args
)
if /I "%~1"=="-project-root" (
if "%~2"=="" (
echo ERROR: -project-root requires a path.
exit /b 1
)
set "PROJECT_ROOT=%~2"
shift
shift
goto parse_args
)
if /I "%~1"=="-h" goto show_help
if /I "%~1"=="-help" goto show_help
echo ERROR: positional args are not supported. Use -project-root.
exit /b 1
:show_help
echo Usage:
echo sync_templates.bat [options]
echo sync_templates.bat -project-root ^<path^>
echo.
echo Options:
echo -project-root PATH Target project root ^(default: git root^)
echo -force Overwrite existing files
echo -full Append full framework (规则优先级 + 新会话开始时) to existing AGENTS.md
echo -h, -help Show this help
exit /b 0
:args_done
rem Determine project root
if "%PROJECT_ROOT%"=="" (
for /f "delims=" %%R in ('git -C "%SCRIPT_DIR%" rev-parse --show-toplevel 2^>nul') do set "PROJECT_ROOT=%%R"
)
if "%PROJECT_ROOT%"=="" set "PROJECT_ROOT=%cd%"
for %%I in ("%PROJECT_ROOT%") do set "PROJECT_ROOT=%%~fI"
rem Source directories
set "TEMPLATES_DIR=%SRC%\templates"
set "MEMORY_BANK_SRC=%TEMPLATES_DIR%\memory-bank"
set "PROMPTS_SRC=%TEMPLATES_DIR%\prompts"
set "AGENTS_SRC=%TEMPLATES_DIR%\AGENTS.template.md"
set "AGENT_RULES_SRC=%TEMPLATES_DIR%\AGENT_RULES.template.md"
rem Check source exists
if not exist "%TEMPLATES_DIR%" (
echo ERROR: Templates directory not found: %TEMPLATES_DIR%
exit /b 1
)
rem Skip if source equals destination
if /I "%SRC%"=="%PROJECT_ROOT%" (
echo Skip: playbook root equals project root.
echo Done.
exit /b 0
)
for /f "usebackq delims=" %%D in (`powershell -NoProfile -Command "Get-Date -Format 'yyyy-MM-dd'"`) do set "SYNC_DATE=%%D"
if "%SYNC_DATE%"=="" set "SYNC_DATE=%date%"
echo Syncing templates to: %PROJECT_ROOT%
echo.
rem 1. Sync memory-bank/
set "MEMORY_BANK_DST=%PROJECT_ROOT%\memory-bank"
if exist "%MEMORY_BANK_SRC%" (
if exist "%MEMORY_BANK_DST%" (
if "%FORCE%"=="0" (
echo memory-bank/ already exists. Skip. Use -force to overwrite.
goto sync_prompts
)
)
if not exist "%MEMORY_BANK_DST%" mkdir "%MEMORY_BANK_DST%"
xcopy "%MEMORY_BANK_SRC%\*" "%MEMORY_BANK_DST%\" /e /i /y >nul 2>nul
rem Rename .template.md to .md
for %%F in ("%MEMORY_BANK_DST%\*.template.md") do (
set "OLDNAME=%%~nxF"
set "NEWNAME=!OLDNAME:.template.md=.md!"
ren "%%F" "!NEWNAME!"
)
rem Replace {{DATE}} placeholder
for %%F in ("%MEMORY_BANK_DST%\*.md") do (
powershell -NoProfile -Command "$f='%%~fF'; $c=Get-Content -Raw $f; $c=$c.Replace('{{DATE}}','%SYNC_DATE%'); Set-Content -Path $f -Value $c -Encoding UTF8 -NoNewline"
)
echo Synced: memory-bank/
) else (
echo Skip: memory-bank/ templates not found
)
:sync_prompts
rem 2. Sync docs/prompts/
set "PROMPTS_DST=%PROJECT_ROOT%\docs\prompts"
if exist "%PROMPTS_SRC%" (
if exist "%PROMPTS_DST%" (
if "%FORCE%"=="0" (
echo docs/prompts/ already exists. Skip. Use -force to overwrite.
goto sync_agents
)
)
if not exist "%PROJECT_ROOT%\docs" mkdir "%PROJECT_ROOT%\docs"
if not exist "%PROMPTS_DST%" mkdir "%PROMPTS_DST%"
xcopy "%PROMPTS_SRC%\*" "%PROMPTS_DST%\" /e /i /y >nul 2>nul
rem Rename .template.md to .md recursively
for /r "%PROMPTS_DST%" %%F in (*.template.md) do (
set "OLDNAME=%%~nxF"
set "NEWNAME=!OLDNAME:.template.md=.md!"
ren "%%F" "!NEWNAME!"
)
rem Replace {{DATE}} placeholder
for /r "%PROMPTS_DST%" %%F in (*.md) do (
powershell -NoProfile -Command "$f='%%~fF'; $c=Get-Content -Raw $f; $c=$c.Replace('{{DATE}}','%SYNC_DATE%'); Set-Content -Path $f -Value $c -Encoding UTF8 -NoNewline"
)
echo Synced: docs/prompts/
) else (
echo Skip: prompts/ templates not found
)
:sync_agents
rem 3. Sync AGENTS.md
set "AGENTS_DST=%PROJECT_ROOT%\AGENTS.md"
rem Choose markers based on -full flag
if "%FULL%"=="1" (
set "MARKER_START=<!-- playbook:framework:start -->"
set "MARKER_END=<!-- playbook:framework:end -->"
set "SECTION_NAME=framework"
) else (
set "MARKER_START=<!-- playbook:templates:start -->"
set "MARKER_END=<!-- playbook:templates:end -->"
set "SECTION_NAME=templates"
)
if exist "%AGENTS_SRC%" (
if not exist "%AGENTS_DST%" (
rem AGENTS.md doesn't exist: create from full template
copy /y "%AGENTS_SRC%" "%AGENTS_DST%" >nul
powershell -NoProfile -Command "$f='%AGENTS_DST%'; $c=Get-Content -Raw $f; $c=$c.Replace('{{DATE}}','%SYNC_DATE%'); Set-Content -Path $f -Value $c -Encoding UTF8 -NoNewline"
echo Created: AGENTS.md
) else (
rem AGENTS.md exists: update or append section (extract from template)
powershell -NoProfile -Command ^
"$src='%AGENTS_SRC%'; $dst='%AGENTS_DST%'; $date='%SYNC_DATE%'; " ^
"$markerStart='!MARKER_START!'; $markerEnd='!MARKER_END!'; $sectionName='!SECTION_NAME!'; " ^
"$templateContent = Get-Content -Raw $src; " ^
"$extractPattern = '(?s)(' + [regex]::Escape($markerStart) + '.*?' + [regex]::Escape($markerEnd) + ')'; " ^
"if ($templateContent -match $extractPattern) { " ^
" $snippetContent = $Matches[1]; " ^
" $content = Get-Content -Raw $dst; " ^
" if ($content -match [regex]::Escape($markerStart)) { " ^
" $replacePattern = '(?s)' + [regex]::Escape($markerStart) + '.*?' + [regex]::Escape($markerEnd); " ^
" $newContent = $content -replace $replacePattern, $snippetContent; " ^
" $newContent = $newContent.Replace('{{DATE}}', $date); " ^
" Set-Content -Path $dst -Value $newContent -Encoding UTF8 -NoNewline; " ^
" Write-Host \"Updated: AGENTS.md ($sectionName section)\"; " ^
" } else { " ^
" $newContent = $content.TrimEnd() + \"`n`n\" + $snippetContent; " ^
" $newContent = $newContent.Replace('{{DATE}}', $date); " ^
" Set-Content -Path $dst -Value $newContent -Encoding UTF8 -NoNewline; " ^
" Write-Host \"Appended: AGENTS.md ($sectionName section)\"; " ^
" } " ^
"} else { " ^
" Write-Host 'Skip: markers not found in template'; " ^
"}"
)
) else (
echo Skip: AGENTS.template.md not found
)
:sync_agent_rules
rem 4. Sync AGENT_RULES.md
set "AGENT_RULES_DST=%PROJECT_ROOT%\AGENT_RULES.md"
if exist "%AGENT_RULES_SRC%" (
if exist "%AGENT_RULES_DST%" (
if "%FORCE%"=="0" (
echo AGENT_RULES.md already exists. Skip. Use -force to overwrite.
goto sync_done
)
)
copy /y "%AGENT_RULES_SRC%" "%AGENT_RULES_DST%" >nul
powershell -NoProfile -Command "$f='%AGENT_RULES_DST%'; $c=Get-Content -Raw $f; $c=$c.Replace('{{DATE}}','%SYNC_DATE%'); Set-Content -Path $f -Value $c -Encoding UTF8 -NoNewline"
echo Synced: AGENT_RULES.md
) else (
echo Skip: AGENT_RULES.template.md not found
)
:sync_done
echo.
echo Done.
echo.
echo Next steps:
echo 1. Edit memory-bank\*.md to fill in project-specific content
echo 2. Replace remaining {{PLACEHOLDER}} values
echo 3. Run sync_standards.bat to sync .agents\ rules
endlocal
-248
View File
@@ -1,248 +0,0 @@
# Sync project templates to target project.
# - Copies templates/memory-bank/ -> <project-root>/memory-bank/
# - Copies templates/prompts/ -> <project-root>/docs/prompts/
# - Copies templates/AGENTS.template.md -> <project-root>/AGENTS.md
# - Copies templates/AGENT_RULES.template.md -> <project-root>/AGENT_RULES.md
# Existing targets are backed up before overwrite.
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[Alias('h', '?')]
[switch]$Help,
[Parameter(Mandatory = $false)]
[string]$ProjectRoot,
[Parameter(Mandatory = $false)]
[string]$ProjectName,
[Parameter(Mandatory = $false)]
[string]$Date,
[Parameter(Mandatory = $false)]
[switch]$NoBackup,
[Parameter(Mandatory = $false)]
[switch]$Force,
[Parameter(Mandatory = $false)]
[switch]$Full
)
$ErrorActionPreference = "Stop"
if ($Help) {
Write-Host "Usage:"
Write-Host " powershell -File scripts/sync_templates.ps1 [options]"
Write-Host " powershell -File scripts/sync_templates.ps1 -ProjectRoot C:\\path\\to\\project"
Write-Host ""
Write-Host "Options:"
Write-Host " -ProjectRoot PATH Target project root (default: git root)."
Write-Host " -ProjectName NAME Replace {{PROJECT_NAME}} placeholder."
Write-Host " -Date DATE Replace {{DATE}} placeholder (default: today)."
Write-Host " -NoBackup Skip backup of existing files."
Write-Host " -Force Overwrite without prompting."
Write-Host " -Full Append full framework section to AGENTS.md."
Write-Host " -Help Show this help."
exit 0
}
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$Src = (Resolve-Path (Join-Path $ScriptDir "..")).Path
# Defaults
if (-not $Date) {
$Date = Get-Date -Format "yyyy-MM-dd"
}
# Determine project root
if (-not $ProjectRoot) {
$ProjectRoot = (git -C $ScriptDir rev-parse --show-toplevel 2>$null)
if (-not $ProjectRoot) { $ProjectRoot = (Get-Location).Path }
}
$ProjectRoot = (Resolve-Path $ProjectRoot).Path
# Source directories
$TemplatesDir = Join-Path $Src "templates"
$MemoryBankSrc = Join-Path $TemplatesDir "memory-bank"
$PromptsSrc = Join-Path $TemplatesDir "prompts"
$AgentsSrc = Join-Path $TemplatesDir "AGENTS.template.md"
$AgentRulesSrc = Join-Path $TemplatesDir "AGENT_RULES.template.md"
# Check source exists
if (-not (Test-Path $TemplatesDir)) {
throw "Templates directory not found: $TemplatesDir"
}
# Skip if source equals destination
if ($Src -ieq $ProjectRoot) {
Write-Host "Skip: playbook root equals project root."
Write-Host "Done."
exit 0
}
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
# Function: backup file/directory
function Backup-IfExists {
param([string]$Target)
if ((Test-Path $Target) -and -not $NoBackup) {
$backup = "$Target.bak.$timestamp"
Move-Item $Target $backup
Write-Host "Backed up: $(Split-Path -Leaf $Target) -> $(Split-Path -Leaf $backup)"
}
}
# Function: replace placeholders in file
function Replace-Placeholders {
param([string]$File)
if (-not (Test-Path $File)) { return }
$content = Get-Content -Raw -Path $File
if ($ProjectName) {
$content = $content.Replace("{{PROJECT_NAME}}", $ProjectName)
}
$content = $content.Replace("{{DATE}}", $Date)
Set-Content -Path $File -Value $content -Encoding UTF8 -NoNewline
}
# Function: replace placeholders in directory
function Replace-PlaceholdersDir {
param([string]$Dir)
if (-not (Test-Path $Dir)) { return }
Get-ChildItem -Path $Dir -Filter "*.md" -Recurse -File | ForEach-Object {
Replace-Placeholders -File $_.FullName
}
}
Write-Host "Syncing templates to: $ProjectRoot"
Write-Host ""
# 1. Sync memory-bank/
if (Test-Path $MemoryBankSrc) {
$MemoryBankDst = Join-Path $ProjectRoot "memory-bank"
if ((Test-Path $MemoryBankDst) -and -not $Force) {
Write-Host "memory-bank/ already exists. Use -Force to overwrite."
} else {
Backup-IfExists -Target $MemoryBankDst
New-Item -ItemType Directory -Path $MemoryBankDst -Force | Out-Null
Copy-Item -Path (Join-Path $MemoryBankSrc "*") -Destination $MemoryBankDst -Recurse -Force
# Rename .template.md to .md
Get-ChildItem -Path $MemoryBankDst -Filter "*.template.md" -File | ForEach-Object {
$newName = $_.Name -replace "\.template\.md$", ".md"
Rename-Item -Path $_.FullName -NewName $newName
}
Replace-PlaceholdersDir -Dir $MemoryBankDst
Write-Host "Synced: memory-bank/"
}
} else {
Write-Host "Skip: memory-bank/ templates not found"
}
# 2. Sync docs/prompts/
if (Test-Path $PromptsSrc) {
$PromptsDst = Join-Path $ProjectRoot "docs\prompts"
if ((Test-Path $PromptsDst) -and -not $Force) {
Write-Host "docs/prompts/ already exists. Use -Force to overwrite."
} else {
Backup-IfExists -Target $PromptsDst
$DocsDir = Join-Path $ProjectRoot "docs"
New-Item -ItemType Directory -Path $DocsDir -Force | Out-Null
New-Item -ItemType Directory -Path $PromptsDst -Force | Out-Null
Copy-Item -Path (Join-Path $PromptsSrc "*") -Destination $PromptsDst -Recurse -Force
# Rename .template.md to .md recursively
Get-ChildItem -Path $PromptsDst -Filter "*.template.md" -Recurse -File | ForEach-Object {
$newName = $_.Name -replace "\.template\.md$", ".md"
Rename-Item -Path $_.FullName -NewName $newName
}
Replace-PlaceholdersDir -Dir $PromptsDst
Write-Host "Synced: docs/prompts/"
}
} else {
Write-Host "Skip: prompts/ templates not found"
}
# 3. Sync AGENTS.md
# Choose markers based on -Full flag
if ($Full) {
$MarkerStart = "<!-- playbook:framework:start -->"
$MarkerEnd = "<!-- playbook:framework:end -->"
$SectionName = "framework"
} else {
$MarkerStart = "<!-- playbook:templates:start -->"
$MarkerEnd = "<!-- playbook:templates:end -->"
$SectionName = "templates"
}
if (Test-Path $AgentsSrc) {
$AgentsDst = Join-Path $ProjectRoot "AGENTS.md"
if (-not (Test-Path $AgentsDst)) {
# AGENTS.md doesn't exist: create from full template
Copy-Item -Path $AgentsSrc -Destination $AgentsDst -Force
Replace-Placeholders -File $AgentsDst
Write-Host "Created: AGENTS.md"
} else {
# AGENTS.md exists: update or append section
# Extract snippet from template
$templateContent = Get-Content -Raw -Path $AgentsSrc
$extractPattern = "(?s)(" + [regex]::Escape($MarkerStart) + ".*?" + [regex]::Escape($MarkerEnd) + ")"
if ($templateContent -match $extractPattern) {
$snippetContent = $Matches[1]
$content = Get-Content -Raw -Path $AgentsDst
if ($content -match [regex]::Escape($MarkerStart)) {
# Has markers: replace content between markers
$replacePattern = "(?s)" + [regex]::Escape($MarkerStart) + ".*?" + [regex]::Escape($MarkerEnd)
$newContent = $content -replace $replacePattern, $snippetContent
Set-Content -Path $AgentsDst -Value $newContent -Encoding UTF8 -NoNewline
Replace-Placeholders -File $AgentsDst
Write-Host "Updated: AGENTS.md ($SectionName section)"
} else {
# No markers: append snippet at the end
$newContent = $content.TrimEnd() + "`n`n" + $snippetContent
Set-Content -Path $AgentsDst -Value $newContent -Encoding UTF8 -NoNewline
Replace-Placeholders -File $AgentsDst
Write-Host "Appended: AGENTS.md ($SectionName section)"
}
} else {
Write-Host "Skip: markers not found in template"
}
}
} else {
Write-Host "Skip: AGENTS.template.md not found"
}
# 4. Sync AGENT_RULES.md
if (Test-Path $AgentRulesSrc) {
$AgentRulesDst = Join-Path $ProjectRoot "AGENT_RULES.md"
if ((Test-Path $AgentRulesDst) -and -not $Force) {
Write-Host "AGENT_RULES.md already exists. Use -Force to overwrite."
} else {
Backup-IfExists -Target $AgentRulesDst
Copy-Item -Path $AgentRulesSrc -Destination $AgentRulesDst -Force
Replace-Placeholders -File $AgentRulesDst
Write-Host "Synced: AGENT_RULES.md"
}
} else {
Write-Host "Skip: AGENT_RULES.template.md not found"
}
Write-Host ""
Write-Host "Done."
Write-Host ""
Write-Host "Next steps:"
Write-Host " 1. Edit memory-bank/*.md to fill in project-specific content"
Write-Host " 2. Replace remaining {{PLACEHOLDER}} values"
Write-Host " 3. Run sync_standards.ps1 to sync .agents/ rules"
-323
View File
@@ -1,323 +0,0 @@
#!/usr/bin/env sh
set -eu
# Sync project templates to target project.
# - Copies templates/memory-bank/ -> <project-root>/memory-bank/
# - Copies templates/prompts/ -> <project-root>/docs/prompts/
# - Copies templates/AGENTS.template.md -> <project-root>/AGENTS.md
# - Copies templates/AGENT_RULES.template.md -> <project-root>/AGENT_RULES.md
# Existing targets are backed up before overwrite.
#
# Usage:
# sh scripts/sync_templates.sh # sync to current git root
# sh scripts/sync_templates.sh -project-root /path/to/project
# sh scripts/sync_templates.sh -project-root /path/to/project -project-name "MyProject" -date "2026-01-20"
#
# Options:
# -project-root PATH Target project root (default: git root)
# -project-name NAME Replace {{PROJECT_NAME}} placeholder
# -date DATE Replace {{DATE}} placeholder (default: today)
# -no-backup Skip backup of existing files
# -force Overwrite without prompting
# -full Append full framework (规则优先级 + 新会话开始时) to existing AGENTS.md
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)"
SRC="$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd -P)"
# Defaults
PROJECT_NAME=""
SYNC_DATE="$(date +%Y-%m-%d 2>/dev/null || echo "{{DATE}}")"
NO_BACKUP=0
FORCE=0
FULL=0
PROJECT_ROOT=""
# Parse arguments
while [ $# -gt 0 ]; do
case "$1" in
-project-root)
if [ $# -lt 2 ] || [ -z "${2:-}" ]; then
echo "ERROR: -project-root requires a path." >&2
exit 1
fi
PROJECT_ROOT="$2"
shift 2
;;
-project-name)
if [ $# -lt 2 ] || [ -z "${2:-}" ]; then
echo "ERROR: -project-name requires a value." >&2
exit 1
fi
PROJECT_NAME="$2"
shift 2
;;
-date)
if [ $# -lt 2 ] || [ -z "${2:-}" ]; then
echo "ERROR: -date requires a value." >&2
exit 1
fi
SYNC_DATE="$2"
shift 2
;;
-no-backup)
NO_BACKUP=1
shift
;;
-force)
FORCE=1
shift
;;
-full)
FULL=1
shift
;;
-h|-help)
cat <<'EOF'
Usage:
sh scripts/sync_templates.sh [options]
sh scripts/sync_templates.sh -project-root /path/to/project
Options:
-project-root PATH Target project root (default: git root)
-project-name NAME Replace {{PROJECT_NAME}} placeholder
-date DATE Replace {{DATE}} placeholder (default: today)
-no-backup Skip backup of existing files
-force Overwrite without prompting
-full Append full framework (规则优先级 + 新会话开始时) to existing AGENTS.md
-h, -help Show this help
Examples:
sh scripts/sync_templates.sh
sh scripts/sync_templates.sh -project-root /path/to/project
sh scripts/sync_templates.sh -project-root /path/to/project -full
EOF
exit 0
;;
-*)
echo "ERROR: Unknown option: $1" >&2
exit 1
;;
*)
echo "ERROR: positional args are not supported; use -project-root." >&2
exit 1
;;
esac
done
# Determine project root
if [ -z "$PROJECT_ROOT" ]; then
PROJECT_ROOT="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null || pwd)"
fi
PROJECT_ROOT="$(CDPATH= cd -- "$PROJECT_ROOT" && pwd -P)"
# Source directories
TEMPLATES_DIR="$SRC/templates"
MEMORY_BANK_SRC="$TEMPLATES_DIR/memory-bank"
PROMPTS_SRC="$TEMPLATES_DIR/prompts"
AGENTS_SRC="$TEMPLATES_DIR/AGENTS.template.md"
AGENT_RULES_SRC="$TEMPLATES_DIR/AGENT_RULES.template.md"
# Check source exists
if [ ! -d "$TEMPLATES_DIR" ]; then
echo "ERROR: Templates directory not found: $TEMPLATES_DIR" >&2
exit 1
fi
# Skip if source equals destination (running from playbook repo itself)
if [ "$SRC" = "$PROJECT_ROOT" ]; then
echo "Skip: playbook root equals project root."
echo "Done."
exit 0
fi
timestamp="$(date +%Y%m%d%H%M%S 2>/dev/null || echo bak)"
# Function: backup file/directory
backup_if_exists() {
target="$1"
if [ -e "$target" ] && [ "$NO_BACKUP" -eq 0 ]; then
backup="${target}.bak.$timestamp"
mv "$target" "$backup"
echo "Backed up: $(basename "$target") -> $(basename "$backup")"
fi
}
escape_sed_replacement() {
printf '%s' "$1" | sed 's/[&/|\\]/\\&/g'
}
# Function: replace placeholders in file
replace_placeholders() {
file="$1"
[ -f "$file" ] || return 0
tmp="$(mktemp 2>/dev/null || echo "$file.tmp.$timestamp")"
date_repl="$(escape_sed_replacement "$SYNC_DATE")"
if [ -n "$PROJECT_NAME" ]; then
project_repl="$(escape_sed_replacement "$PROJECT_NAME")"
sed -e "s/{{PROJECT_NAME}}/$project_repl/g" -e "s/{{DATE}}/$date_repl/g" "$file" > "$tmp"
else
sed -e "s/{{DATE}}/$date_repl/g" "$file" > "$tmp"
fi
mv "$tmp" "$file"
}
# Function: replace placeholders in directory
replace_placeholders_dir() {
dir="$1"
[ -d "$dir" ] || return 0
find "$dir" -type f -name '*.md' -print | while IFS= read -r file; do
replace_placeholders "$file"
done
}
echo "Syncing templates to: $PROJECT_ROOT"
echo ""
# 1. Sync memory-bank/
if [ -d "$MEMORY_BANK_SRC" ]; then
MEMORY_BANK_DST="$PROJECT_ROOT/memory-bank"
if [ -e "$MEMORY_BANK_DST" ] && [ "$FORCE" -eq 0 ]; then
echo "memory-bank/ already exists. Use -force to overwrite."
else
backup_if_exists "$MEMORY_BANK_DST"
mkdir -p "$MEMORY_BANK_DST"
cp -R "$MEMORY_BANK_SRC"/* "$MEMORY_BANK_DST/" 2>/dev/null || true
# Rename .template.md to .md
for f in "$MEMORY_BANK_DST"/*.template.md; do
[ -f "$f" ] || continue
newname="$(echo "$f" | sed 's/\.template\.md$/.md/')"
mv "$f" "$newname"
done
replace_placeholders_dir "$MEMORY_BANK_DST"
echo "Synced: memory-bank/"
fi
else
echo "Skip: memory-bank/ templates not found"
fi
# 2. Sync docs/prompts/
if [ -d "$PROMPTS_SRC" ]; then
PROMPTS_DST="$PROJECT_ROOT/docs/prompts"
if [ -e "$PROMPTS_DST" ] && [ "$FORCE" -eq 0 ]; then
echo "docs/prompts/ already exists. Use -force to overwrite."
else
backup_if_exists "$PROMPTS_DST"
mkdir -p "$PROJECT_ROOT/docs"
mkdir -p "$PROMPTS_DST"
cp -R "$PROMPTS_SRC"/* "$PROMPTS_DST/" 2>/dev/null || true
# Rename .template.md to .md (recursive)
find "$PROMPTS_DST" -type f -name '*.template.md' -print | while IFS= read -r f; do
newname="$(echo "$f" | sed 's/\.template\.md$/.md/')"
mv "$f" "$newname"
done
replace_placeholders_dir "$PROMPTS_DST"
echo "Synced: docs/prompts/"
fi
else
echo "Skip: prompts/ templates not found"
fi
# 3. Sync AGENTS.md
# Choose markers based on -full flag
if [ "$FULL" -eq 1 ]; then
MARKER_START="<!-- playbook:framework:start -->"
MARKER_END="<!-- playbook:framework:end -->"
SECTION_NAME="framework"
else
MARKER_START="<!-- playbook:templates:start -->"
MARKER_END="<!-- playbook:templates:end -->"
SECTION_NAME="templates"
fi
if [ -f "$AGENTS_SRC" ]; then
AGENTS_DST="$PROJECT_ROOT/AGENTS.md"
if [ ! -e "$AGENTS_DST" ]; then
# AGENTS.md doesn't exist: create from full template
cp "$AGENTS_SRC" "$AGENTS_DST"
replace_placeholders "$AGENTS_DST"
echo "Created: AGENTS.md"
else
# AGENTS.md exists: update or append section
# Extract snippet from template
snippet_content="$(awk -v start="$MARKER_START" -v end="$MARKER_END" '
$0 ~ start { found=1 }
found { print }
$0 ~ end { found=0 }
' "$AGENTS_SRC")"
if [ -z "$snippet_content" ]; then
echo "Skip: markers not found in template"
elif grep -q "$MARKER_START" "$AGENTS_DST"; then
# Has markers: replace content between markers in place
snippet_tmp="$(mktemp 2>/dev/null || echo "$AGENTS_DST.snippet.$timestamp")"
printf "%s\n" "$snippet_content" > "$snippet_tmp"
tmp="$(mktemp 2>/dev/null || echo "$AGENTS_DST.tmp.$timestamp")"
awk -v start="$MARKER_START" -v end="$MARKER_END" -v snippet="$snippet_tmp" '
BEGIN {
while ((getline line < snippet) > 0) { block[++n] = line }
close(snippet)
inblock = 0
replaced = 0
}
{
if (!replaced && $0 ~ start) {
for (i=1; i<=n; i++) print block[i]
inblock = 1
replaced = 1
next
}
if (inblock) {
if ($0 ~ end) { inblock = 0 }
next
}
print
}
' "$AGENTS_DST" > "$tmp"
mv "$tmp" "$AGENTS_DST"
rm -f "$snippet_tmp"
replace_placeholders "$AGENTS_DST"
echo "Updated: AGENTS.md ($SECTION_NAME section)"
else
# No markers: append snippet at the end
echo "" >> "$AGENTS_DST"
echo "$snippet_content" >> "$AGENTS_DST"
replace_placeholders "$AGENTS_DST"
echo "Appended: AGENTS.md ($SECTION_NAME section)"
fi
fi
else
echo "Skip: AGENTS.template.md not found"
fi
# 4. Sync AGENT_RULES.md
if [ -f "$AGENT_RULES_SRC" ]; then
AGENT_RULES_DST="$PROJECT_ROOT/AGENT_RULES.md"
if [ -e "$AGENT_RULES_DST" ] && [ "$FORCE" -eq 0 ]; then
echo "AGENT_RULES.md already exists. Use -force to overwrite."
else
backup_if_exists "$AGENT_RULES_DST"
cp "$AGENT_RULES_SRC" "$AGENT_RULES_DST"
replace_placeholders "$AGENT_RULES_DST"
echo "Synced: AGENT_RULES.md"
fi
else
echo "Skip: AGENT_RULES.template.md not found"
fi
echo ""
echo "Done."
echo ""
echo "Next steps:"
echo " 1. Edit memory-bank/*.md to fill in project-specific content"
echo " 2. Replace remaining {{PLACEHOLDER}} values"
echo " 3. Run sync_standards.sh -langs <lang> to sync .agents/ rules"
-379
View File
@@ -1,379 +0,0 @@
@echo off
setlocal enabledelayedexpansion
rem Vendor a trimmed Playbook snapshot into a target project (offline copy),
rem then run sync_standards to materialize .agents\<lang>\ and .gitattributes in
rem the target project root.
rem
rem Usage:
rem scripts\vendor_playbook.bat -project-root <path> (default: tsl)
rem scripts\vendor_playbook.bat -project-root <path> -langs tsl,cpp
rem scripts\vendor_playbook.bat -project-root <path> -langs tsl,cpp -apply-templates
rem
rem Options:
rem -project-root Target project root (required)
rem -apply-templates Apply CI/lang templates to project root (skip if exists)
rem
rem Notes:
rem - Snapshot is written to: <project-root>\docs\standards\playbook\
rem - Existing snapshot is backed up before overwrite.
rem - With -apply-templates, CI and lang templates are copied to project root.
set "SCRIPT_DIR=%~dp0"
for %%I in ("%SCRIPT_DIR%..") do set "SRC=%%~fI"
if "%~1"=="" goto Usage
if "%~1"=="-h" goto Usage
if "%~1"=="-help" goto Usage
set "DEST_ROOT="
set "LANGS="
set "APPLY_TEMPLATES=0"
rem Parse arguments
:parse_args
if "%~1"=="" goto args_done
if "%~1"=="-project-root" (
if "%~2"=="" (
echo ERROR: -project-root requires a path.
exit /b 1
)
set "DEST_ROOT=%~2"
shift /1
shift /1
goto parse_args
)
if "%~1"=="-langs" (
if "%~2"=="" (
echo ERROR: -langs requires a value.
exit /b 1
)
set "LANGS=%~2"
shift /1
shift /1
goto parse_args
)
if "%~1"=="-apply-templates" (
set "APPLY_TEMPLATES=1"
shift /1
goto parse_args
)
echo ERROR: positional args are not supported. Use -project-root/-langs.
exit /b 1
:args_done
if "%DEST_ROOT%"=="" (
echo ERROR: -project-root is required.
exit /b 1
)
if "%LANGS%"=="" set "LANGS=tsl"
set "LANGS=%LANGS:,= %"
if not exist "%DEST_ROOT%" mkdir "%DEST_ROOT%"
for %%I in ("%DEST_ROOT%") do set "DEST_ROOT_ABS=%%~fI"
set "STANDARDS_DIR=%DEST_ROOT_ABS%\\docs\\standards"
set "DEST_PREFIX=%STANDARDS_DIR%\\playbook"
if not exist "%STANDARDS_DIR%" mkdir "%STANDARDS_DIR%"
if exist "%DEST_PREFIX%" (
set "RAND=%RANDOM%"
pushd "%STANDARDS_DIR%"
ren "playbook" "playbook.bak.!RAND!"
popd
echo Backed up existing snapshot -^> docs\\standards\\playbook.bak.!RAND!
)
if not exist "%DEST_PREFIX%" mkdir "%DEST_PREFIX%"
copy /y "%SRC%\\.gitattributes" "%DEST_PREFIX%\\.gitattributes" >nul
copy /y "%SRC%\\SKILLS.md" "%DEST_PREFIX%\\SKILLS.md" >nul
xcopy "%SRC%\\scripts\\*" "%DEST_PREFIX%\\scripts\\" /e /i /y >nul
if errorlevel 1 (
echo ERROR: failed to copy scripts
exit /b 1
)
xcopy "%SRC%\\codex\\*" "%DEST_PREFIX%\\codex\\" /e /i /y >nul
if errorlevel 1 (
echo ERROR: failed to copy codex
exit /b 1
)
xcopy "%SRC%\\docs\\common\\*" "%DEST_PREFIX%\\docs\\common\\" /e /i /y >nul
if errorlevel 1 (
echo ERROR: failed to copy docs\\common
exit /b 1
)
if not exist "%DEST_PREFIX%\\rulesets" mkdir "%DEST_PREFIX%\\rulesets"
copy /y "%SRC%\\rulesets\\index.md" "%DEST_PREFIX%\\rulesets\\index.md" >nul
if not exist "%DEST_PREFIX%\\templates" mkdir "%DEST_PREFIX%\\templates"
if exist "%SRC%\\templates\\ci" (
xcopy "%SRC%\\templates\\ci\\*" "%DEST_PREFIX%\\templates\\ci\\" /e /i /y >nul
if errorlevel 1 (
echo ERROR: failed to copy templates\\ci
exit /b 1
)
)
set "LANGS_CSV="
for %%L in (%LANGS%) do (
echo %%~L| findstr /r "[\\/]" >nul && (
echo ERROR: invalid lang=%%~L
exit /b 1
)
echo %%~L| findstr /c:".." >nul && (
echo ERROR: invalid lang=%%~L
exit /b 1
)
if not exist "%SRC%\\docs\\%%~L" (
echo ERROR: docs not found for lang=%%~L "%SRC%\\docs\\%%~L"
exit /b 1
)
if not exist "%SRC%\\rulesets\\%%~L" (
echo ERROR: agents ruleset not found for lang=%%~L "%SRC%\\rulesets\\%%~L"
exit /b 1
)
xcopy "%SRC%\\docs\\%%~L\\*" "%DEST_PREFIX%\\docs\\%%~L\\" /e /i /y >nul
if errorlevel 1 (
echo ERROR: failed to copy docs for lang=%%~L
exit /b 1
)
xcopy "%SRC%\\rulesets\\%%~L\\*" "%DEST_PREFIX%\\rulesets\\%%~L\\" /e /i /y >nul
if errorlevel 1 (
echo ERROR: failed to copy agents for lang=%%~L
exit /b 1
)
if exist "%SRC%\\templates\\%%~L" (
xcopy "%SRC%\\templates\\%%~L\\*" "%DEST_PREFIX%\\templates\\%%~L\\" /e /i /y >nul
if errorlevel 1 (
echo ERROR: failed to copy templates for lang=%%~L
exit /b 1
)
)
if "!LANGS_CSV!"=="" (
set "LANGS_CSV=%%~L"
) else (
set "LANGS_CSV=!LANGS_CSV!,%%~L"
)
)
set "DOC_INDEX=%DEST_PREFIX%\\docs\\index.md"
> "%DOC_INDEX%" echo # 文档导航(Docs Index
>> "%DOC_INDEX%" echo.
>> "%DOC_INDEX%" echo 本快照为裁剪版 Playbooklangs: %LANGS_CSV%)。
>> "%DOC_INDEX%" echo.
>> "%DOC_INDEX%" echo ## 跨语言(common
>> "%DOC_INDEX%" echo.
>> "%DOC_INDEX%" echo - 提交信息与版本号:`common/commit_message.md`
for %%L in (%LANGS%) do call :AppendDocsSection "%%~L"
set "COMMIT="
for /f "delims=" %%H in ('git -C "%SRC%" rev-parse HEAD 2^>nul') do set "COMMIT=%%H"
if "%COMMIT%"=="" set "COMMIT=N/A"
set "README=%DEST_PREFIX%\\README.md"
> "%README%" echo # Playbook(裁剪快照)
>> "%README%" echo.
>> "%README%" echo 本目录为从 Playbook vendoring 的裁剪快照(langs: %LANGS_CSV%)。
>> "%README%" echo.
>> "%README%" echo ## 使用
>> "%README%" echo.
>> "%README%" echo 在目标项目根目录执行(多语言一次同步):
>> "%README%" echo.
>> "%README%" echo ```sh
>> "%README%" echo sh docs/standards/playbook/scripts/sync_standards.sh -langs %LANGS_CSV%
>> "%README%" echo ```
>> "%README%" echo.
>> "%README%" echo 查看规范入口:
>> "%README%" echo.
>> "%README%" echo - `docs/standards/playbook/docs/index.md`
>> "%README%" echo - `.agents/index.md`
>> "%README%" echo.
>> "%README%" echo ## Codex skills(可选)
>> "%README%" echo.
>> "%README%" echo 安装到本机(需要先在 `~/.codex/config.toml` 启用 skills;见 `docs/standards/playbook/SKILLS.md`):
>> "%README%" echo.
>> "%README%" echo ```sh
>> "%README%" echo sh docs/standards/playbook/scripts/install_codex_skills.sh -all
>> "%README%" echo ```
>> "%README%" echo.
>> "%README%" echo ## CI templates(可选)
>> "%README%" echo.
>> "%README%" echo 目标项目可复制启用的 CI 示例模板(如 Gitea Actions):`templates/ci/`。
set "SOURCE=%DEST_PREFIX%\\SOURCE.md"
> "%SOURCE%" echo # SOURCE
>> "%SOURCE%" echo.
>> "%SOURCE%" echo - Source: %SRC%
>> "%SOURCE%" echo - Commit: %COMMIT%
>> "%SOURCE%" echo - Date: %DATE% %TIME%
>> "%SOURCE%" echo - Langs: %LANGS_CSV%
>> "%SOURCE%" echo - Generated-by: scripts/vendor_playbook.bat
echo Vendored snapshot -^> %DEST_PREFIX%
set "PROJECT_AGENTS_ROOT=%DEST_ROOT_ABS%\\.agents"
set "PROJECT_AGENTS_INDEX=%PROJECT_AGENTS_ROOT%\\index.md"
if not exist "%PROJECT_AGENTS_ROOT%" mkdir "%PROJECT_AGENTS_ROOT%"
if not exist "%PROJECT_AGENTS_INDEX%" (
> "%PROJECT_AGENTS_INDEX%" echo # .agents(多语言)
>> "%PROJECT_AGENTS_INDEX%" echo.
>> "%PROJECT_AGENTS_INDEX%" echo 本目录用于存放仓库级/语言级的代理规则集。
>> "%PROJECT_AGENTS_INDEX%" echo.
>> "%PROJECT_AGENTS_INDEX%" echo 本项目已启用的规则集:
for %%L in (%LANGS%) do (
if /I "%%~L"=="tsl" >> "%PROJECT_AGENTS_INDEX%" echo - .agents/tsl/TSL 相关规则集(适用于 .tsl/.tsf)
if /I "%%~L"=="cpp" >> "%PROJECT_AGENTS_INDEX%" echo - .agents/cpp/C++ 相关规则集(C++23,含 Modules
if /I "%%~L"=="python" >> "%PROJECT_AGENTS_INDEX%" echo - .agents/python/Python 相关规则集
if /I "%%~L"=="markdown" >> "%PROJECT_AGENTS_INDEX%" echo - .agents/markdown/Markdown 相关规则集(仅代码格式化)
)
>> "%PROJECT_AGENTS_INDEX%" echo.
>> "%PROJECT_AGENTS_INDEX%" echo 入口建议从:
for %%L in (%LANGS%) do >> "%PROJECT_AGENTS_INDEX%" echo - .agents/%%~L/index.md
>> "%PROJECT_AGENTS_INDEX%" echo.
>> "%PROJECT_AGENTS_INDEX%" echo 标准快照文档入口:
>> "%PROJECT_AGENTS_INDEX%" echo.
>> "%PROJECT_AGENTS_INDEX%" echo - docs/standards/playbook/docs/index.md
)
set "OLD_SYNC_ROOT=%SYNC_ROOT%"
set "SYNC_ROOT=%DEST_ROOT_ABS%"
pushd "%DEST_ROOT_ABS%"
call "%DEST_PREFIX%\\scripts\\sync_standards.bat" -langs %LANGS_CSV%
popd
set "SYNC_ROOT=%OLD_SYNC_ROOT%"
rem Apply templates to project root if requested
if "%APPLY_TEMPLATES%"=="1" (
echo.
echo Applying templates to project root...
rem Apply CI templates ^(Gitea workflows^)
set "CI_SRC=%DEST_PREFIX%\templates\ci\gitea\.gitea"
if exist "!CI_SRC!" (
if exist "%DEST_ROOT_ABS%\.gitea" (
echo Skip ^(exists^): .gitea\
) else (
xcopy "!CI_SRC!\*" "%DEST_ROOT_ABS%\.gitea\" /e /i /y >nul 2>nul
echo Applied: .gitea\
)
)
rem Apply lang-specific templates
for %%L in (%LANGS%) do (
set "LANG_SRC=%DEST_PREFIX%\templates\%%~L"
if exist "!LANG_SRC!" (
if /I "%%~L"=="cpp" (
call :CopyIfNotExists "!LANG_SRC!\.clang-format" "%DEST_ROOT_ABS%\.clang-format"
call :CopyIfNotExists "!LANG_SRC!\.clangd" "%DEST_ROOT_ABS%\.clangd"
call :CopyIfNotExists "!LANG_SRC!\CMakeLists.txt" "%DEST_ROOT_ABS%\CMakeLists.txt"
call :CopyIfNotExists "!LANG_SRC!\CMakeUserPresets.json" "%DEST_ROOT_ABS%\CMakeUserPresets.json"
call :CopyIfNotExists "!LANG_SRC!\conanfile.txt" "%DEST_ROOT_ABS%\conanfile.txt"
if exist "!LANG_SRC!\conan" (
if exist "%DEST_ROOT_ABS%\conan" (
echo Skip ^(exists^): conan\
) else (
xcopy "!LANG_SRC!\conan\*" "%DEST_ROOT_ABS%\conan\" /e /i /y >nul 2>nul
echo Applied: conan\
)
)
)
if /I "%%~L"=="python" (
call :CopyIfNotExists "!LANG_SRC!\.editorconfig" "%DEST_ROOT_ABS%\.editorconfig"
call :CopyIfNotExists "!LANG_SRC!\.flake8" "%DEST_ROOT_ABS%\.flake8"
call :CopyIfNotExists "!LANG_SRC!\.pre-commit-config.yaml" "%DEST_ROOT_ABS%\.pre-commit-config.yaml"
call :CopyIfNotExists "!LANG_SRC!\.pylintrc" "%DEST_ROOT_ABS%\.pylintrc"
call :CopyIfNotExists "!LANG_SRC!\pyproject.toml" "%DEST_ROOT_ABS%\pyproject.toml"
if exist "!LANG_SRC!\.vscode" (
if exist "%DEST_ROOT_ABS%\.vscode" (
echo Skip ^(exists^): .vscode\
) else (
xcopy "!LANG_SRC!\.vscode\*" "%DEST_ROOT_ABS%\.vscode\" /e /i /y >nul 2>nul
echo Applied: .vscode\
)
)
)
)
)
echo Templates applied.
)
echo Done.
endlocal
exit /b 0
:AppendDocsSection
set "LANG=%~1"
if /I "%LANG%"=="tsl" (
>> "%DOC_INDEX%" echo.
>> "%DOC_INDEX%" echo ## TSLtsl
>> "%DOC_INDEX%" echo.
>> "%DOC_INDEX%" echo - 代码风格:`tsl/code_style.md`
>> "%DOC_INDEX%" echo - 命名规范:`tsl/naming.md`
>> "%DOC_INDEX%" echo - 语法手册:`tsl/syntax_book/index.md`
>> "%DOC_INDEX%" echo - 工具链与验证命令(模板):`tsl/toolchain.md`
)
if /I "%LANG%"=="cpp" (
>> "%DOC_INDEX%" echo.
>> "%DOC_INDEX%" echo ## C++cpp
>> "%DOC_INDEX%" echo.
>> "%DOC_INDEX%" echo - 代码风格:`cpp/code_style.md`
>> "%DOC_INDEX%" echo - 命名规范:`cpp/naming.md`
>> "%DOC_INDEX%" echo - 工具链与验证命令(模板):`cpp/toolchain.md`
>> "%DOC_INDEX%" echo - 第三方依赖(Conan):`cpp/dependencies_conan.md`
>> "%DOC_INDEX%" echo - clangd 配置:`cpp/clangd.md`
)
if /I "%LANG%"=="python" (
>> "%DOC_INDEX%" echo.
>> "%DOC_INDEX%" echo ## Pythonpython
>> "%DOC_INDEX%" echo.
>> "%DOC_INDEX%" echo - 代码风格:`python/style_guide.md`
>> "%DOC_INDEX%" echo - 工具链:`python/tooling.md`
>> "%DOC_INDEX%" echo - 配置清单:`python/configuration.md`
)
if /I "%LANG%"=="markdown" (
>> "%DOC_INDEX%" echo.
>> "%DOC_INDEX%" echo ## Markdownmarkdown
>> "%DOC_INDEX%" echo.
>> "%DOC_INDEX%" echo - 代码块与行内代码格式:`markdown/index.md`
)
exit /b 0
:CopyIfNotExists
set "SRC_FILE=%~1"
set "DST_FILE=%~2"
if exist "%SRC_FILE%" (
if exist "%DST_FILE%" (
for %%F in ("%DST_FILE%") do echo Skip ^(exists^): %%~nxF
) else (
copy /y "%SRC_FILE%" "%DST_FILE%" >nul
for %%F in ("%DST_FILE%") do echo Applied: %%~nxF
)
)
exit /b 0
:Usage
echo Usage:
echo scripts\vendor_playbook.bat -project-root ^<path^> ^(default: tsl^)
echo scripts\vendor_playbook.bat -project-root ^<path^> -langs tsl,cpp
echo scripts\vendor_playbook.bat -project-root ^<path^> -langs tsl,cpp -apply-templates
echo.
echo Options:
echo -project-root Target project root ^(required^)
echo -langs Comma/space-separated list of languages ^(default: tsl^)
echo -apply-templates Apply CI/lang templates to project root ^(skip if exists^)
exit /b 1
-353
View File
@@ -1,353 +0,0 @@
# Vendor a trimmed Playbook snapshot into a target project (offline copy),
# then run sync_standards to materialize rulesets\<lang>\ and .gitattributes in
# the target project root.
#
# Usage:
# powershell -File scripts/vendor_playbook.ps1 -ProjectRoot <path>
# powershell -File scripts/vendor_playbook.ps1 -ProjectRoot <path> -Langs tsl,cpp
# powershell -File scripts/vendor_playbook.ps1 -ProjectRoot <path> -Langs @("tsl","cpp") -ApplyTemplates
#
# Options:
# -ApplyTemplates Apply CI/lang templates to project root (skip if exists)
#
# Notes:
# - Snapshot is written to: <project-root>\docs\standards\playbook\
# - Existing snapshot is backed up before overwrite.
# - With -ApplyTemplates, CI and lang templates are copied to project root.
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[Alias('h', '?')]
[switch]$Help,
[Parameter(Mandatory = $false)]
[string]$ProjectRoot,
[Parameter(Mandatory = $false)]
[string[]]$Langs,
[Parameter(Mandatory = $false)]
[switch]$ApplyTemplates
)
$ErrorActionPreference = "Stop"
if ($Help) {
Write-Host "Usage:"
Write-Host " powershell -File scripts/vendor_playbook.ps1 -ProjectRoot <path>"
Write-Host " powershell -File scripts/vendor_playbook.ps1 -ProjectRoot <path> -Langs tsl,cpp"
Write-Host " powershell -File scripts/vendor_playbook.ps1 -ProjectRoot <path> -Langs @('tsl','cpp') -ApplyTemplates"
Write-Host ""
Write-Host "Options:"
Write-Host " -ProjectRoot Target project root (required)."
Write-Host " -Langs Comma/space-separated list or array (default: tsl)."
Write-Host " -ApplyTemplates Apply CI/lang templates to project root."
Write-Host " -Help Show this help."
exit 0
}
if (-not $ProjectRoot) {
throw "ProjectRoot is required. Use -Help for usage."
}
function Normalize-Langs([string[]]$InputLangs) {
if (-not $InputLangs -or $InputLangs.Count -eq 0) { return @("tsl") }
$result = New-Object System.Collections.Generic.List[string]
foreach ($item in $InputLangs) {
if (-not $item) { continue }
foreach ($part in $item.Split(@(',', ' '), [System.StringSplitOptions]::RemoveEmptyEntries)) {
if (-not $part) { continue }
$result.Add($part)
}
}
if ($result.Count -eq 0) { return @("tsl") }
return $result.ToArray()
}
$Langs = Normalize-Langs $Langs
foreach ($lang in $Langs) {
if ($lang -match '[\\/]' -or $lang -match '\.\.') {
throw "Invalid lang=$lang"
}
}
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$Src = (Resolve-Path (Join-Path $ScriptDir "..")).Path
New-Item -ItemType Directory -Path $ProjectRoot -Force | Out-Null
$DestRootAbs = (Resolve-Path $ProjectRoot).Path
$StandardsDir = Join-Path $DestRootAbs "docs/standards"
$DestPrefix = Join-Path $StandardsDir "playbook"
New-Item -ItemType Directory -Path $StandardsDir -Force | Out-Null
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
if (Test-Path $DestPrefix) {
$bak = Join-Path $StandardsDir "playbook.bak.$timestamp"
Move-Item $DestPrefix $bak
Write-Host "Backed up existing snapshot -> docs\\standards\\$(Split-Path -Leaf $bak)"
}
New-Item -ItemType Directory -Path $DestPrefix -Force | Out-Null
Copy-Item (Join-Path $Src ".gitattributes") (Join-Path $DestPrefix ".gitattributes") -Force
Copy-Item (Join-Path $Src "scripts") $DestPrefix -Recurse -Force
Copy-Item (Join-Path $Src "codex") $DestPrefix -Recurse -Force
Copy-Item (Join-Path $Src "SKILLS.md") (Join-Path $DestPrefix "SKILLS.md") -Force
$DocsDir = Join-Path $DestPrefix "docs"
New-Item -ItemType Directory -Path $DocsDir -Force | Out-Null
Copy-Item (Join-Path $Src "docs/common") $DocsDir -Recurse -Force
$AgentsDir = Join-Path $DestPrefix "rulesets"
New-Item -ItemType Directory -Path $AgentsDir -Force | Out-Null
Copy-Item (Join-Path $Src "rulesets/index.md") (Join-Path $AgentsDir "index.md") -Force
$TemplatesDir = Join-Path $DestPrefix "templates"
New-Item -ItemType Directory -Path $TemplatesDir -Force | Out-Null
$ciTplSrc = Join-Path (Join-Path $Src "templates") "ci"
if (Test-Path $ciTplSrc) {
Copy-Item $ciTplSrc $TemplatesDir -Recurse -Force
}
foreach ($lang in $Langs) {
$docsSrc = Join-Path (Join-Path $Src "docs") $lang
if (-not (Test-Path $docsSrc)) { throw "Docs not found for lang=$lang ($docsSrc)" }
Copy-Item $docsSrc $DocsDir -Recurse -Force
$agentsSrc = Join-Path (Join-Path $Src "rulesets") $lang
if (-not (Test-Path $agentsSrc)) { throw "Agents ruleset not found for lang=$lang ($agentsSrc)" }
Copy-Item $agentsSrc $AgentsDir -Recurse -Force
$tplSrc = Join-Path (Join-Path $Src "templates") $lang
if (Test-Path $tplSrc) {
Copy-Item $tplSrc $TemplatesDir -Recurse -Force
}
}
$langsCsv = ($Langs -join ",")
$docLines = New-Object System.Collections.Generic.List[string]
$docLines.Add("# 文档导航(Docs Index")
$docLines.Add("")
$docLines.Add("本快照为裁剪版 Playbooklangs: $langsCsv)。")
$docLines.Add("")
$docLines.Add("## 跨语言(common")
$docLines.Add("")
$docLines.Add('- 提交信息与版本号:`common/commit_message.md`')
function Append-DocsSection([string]$Lang) {
switch ($Lang) {
"tsl" {
$docLines.Add("")
$docLines.Add("## TSLtsl")
$docLines.Add("")
$docLines.Add('- 代码风格:`tsl/code_style.md`')
$docLines.Add('- 命名规范:`tsl/naming.md`')
$docLines.Add('- 语法手册:`tsl/syntax_book/index.md`')
$docLines.Add('- 工具链与验证命令(模板):`tsl/toolchain.md`')
break
}
"cpp" {
$docLines.Add("")
$docLines.Add("## C++cpp")
$docLines.Add("")
$docLines.Add('- 代码风格:`cpp/code_style.md`')
$docLines.Add('- 命名规范:`cpp/naming.md`')
$docLines.Add('- 工具链与验证命令(模板):`cpp/toolchain.md`')
$docLines.Add('- 第三方依赖(Conan):`cpp/dependencies_conan.md`')
$docLines.Add('- clangd 配置:`cpp/clangd.md`')
break
}
"python" {
$docLines.Add("")
$docLines.Add("## Pythonpython")
$docLines.Add("")
$docLines.Add('- 代码风格:`python/style_guide.md`')
$docLines.Add('- 工具链:`python/tooling.md`')
$docLines.Add('- 配置清单:`python/configuration.md`')
break
}
"markdown" {
$docLines.Add("")
$docLines.Add("## Markdownmarkdown")
$docLines.Add("")
$docLines.Add('- 代码块与行内代码格式:`markdown/index.md`')
break
}
}
}
foreach ($lang in $Langs) {
Append-DocsSection $lang
}
($docLines -join "`n") | Set-Content -Path (Join-Path $DocsDir "index.md") -Encoding UTF8
$commit = ""
try {
$commit = (git -C $Src rev-parse HEAD 2>$null)
} catch {
$commit = ""
}
if (-not $commit) { $commit = "N/A" }
@"
# Playbook
Playbook vendoring langs: $langsCsv
## 使
```sh
sh docs/standards/playbook/scripts/sync_standards.sh -langs $langsCsv
```
- `docs/standards/playbook/docs/index.md`
- `.agents/index.md`
## Codex skills
`~/.codex/config.toml` skills `docs/standards/playbook/SKILLS.md`
```sh
sh docs/standards/playbook/scripts/install_codex_skills.sh -all
```
## CI templates
CI Gitea Actions`templates/ci/`
"@ | Set-Content -Path (Join-Path $DestPrefix "README.md") -Encoding UTF8
@"
# SOURCE
- Source: $Src
- Commit: $commit
- Date: $(Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
- Langs: $langsCsv
- Generated-by: scripts/vendor_playbook.ps1
"@ | Set-Content -Path (Join-Path $DestPrefix "SOURCE.md") -Encoding UTF8
Write-Host "Vendored snapshot -> $DestPrefix"
$ProjectAgentsRoot = Join-Path $DestRootAbs ".agents"
$ProjectAgentsIndex = Join-Path $ProjectAgentsRoot "index.md"
New-Item -ItemType Directory -Path $ProjectAgentsRoot -Force | Out-Null
if (-not (Test-Path $ProjectAgentsIndex)) {
$agentLines = New-Object System.Collections.Generic.List[string]
$agentLines.Add("# .agents(多语言)")
$agentLines.Add("")
$agentLines.Add("本目录用于存放仓库级/语言级的代理规则集。")
$agentLines.Add("")
$agentLines.Add("本项目已启用的规则集:")
foreach ($lang in $Langs) {
switch ($lang) {
"tsl" { $agentLines.Add("- .agents/tsl/TSL 相关规则集(适用于 .tsl/.tsf)"); break }
"cpp" { $agentLines.Add("- .agents/cpp/C++ 相关规则集(C++23,含 Modules"); break }
"python" { $agentLines.Add("- .agents/python/Python 相关规则集"); break }
"markdown" { $agentLines.Add("- .agents/markdown/Markdown 相关规则集(仅代码格式化)"); break }
}
}
$agentLines.Add("")
$agentLines.Add("入口建议从:")
foreach ($lang in $Langs) { $agentLines.Add("- .agents/$lang/index.md") }
$agentLines.Add("")
$agentLines.Add("标准快照文档入口:")
$agentLines.Add("")
$agentLines.Add("- docs/standards/playbook/docs/index.md")
($agentLines -join "`n") | Set-Content -Path $ProjectAgentsIndex -Encoding UTF8
}
$oldSyncRoot = $env:SYNC_ROOT
$env:SYNC_ROOT = $DestRootAbs
try {
& (Join-Path $DestPrefix "scripts/sync_standards.ps1") -Langs $Langs
} finally {
$env:SYNC_ROOT = $oldSyncRoot
}
# Apply templates to project root if requested
if ($ApplyTemplates) {
Write-Host ""
Write-Host "Applying templates to project root..."
# Helper function: copy file if not exists
function Copy-IfNotExists {
param([string]$SrcFile, [string]$DstFile)
if (Test-Path $SrcFile) {
if (Test-Path $DstFile) {
Write-Host " Skip (exists): $(Split-Path -Leaf $DstFile)"
} else {
Copy-Item $SrcFile $DstFile -Force
Write-Host " Applied: $(Split-Path -Leaf $DstFile)"
}
}
}
# Apply CI templates (Gitea workflows)
$ciSrc = Join-Path $DestPrefix "templates/ci/gitea/.gitea"
if (Test-Path $ciSrc) {
$ciDst = Join-Path $DestRootAbs ".gitea"
if (Test-Path $ciDst) {
Write-Host " Skip (exists): .gitea/"
} else {
Copy-Item $ciSrc $ciDst -Recurse -Force
Write-Host " Applied: .gitea/"
}
}
# Apply lang-specific templates
foreach ($lang in $Langs) {
$langSrc = Join-Path $DestPrefix "templates/$lang"
if (-not (Test-Path $langSrc)) { continue }
switch ($lang) {
"cpp" {
Copy-IfNotExists (Join-Path $langSrc ".clang-format") (Join-Path $DestRootAbs ".clang-format")
Copy-IfNotExists (Join-Path $langSrc ".clangd") (Join-Path $DestRootAbs ".clangd")
Copy-IfNotExists (Join-Path $langSrc "CMakeLists.txt") (Join-Path $DestRootAbs "CMakeLists.txt")
Copy-IfNotExists (Join-Path $langSrc "CMakeUserPresets.json") (Join-Path $DestRootAbs "CMakeUserPresets.json")
Copy-IfNotExists (Join-Path $langSrc "conanfile.txt") (Join-Path $DestRootAbs "conanfile.txt")
$conanSrc = Join-Path $langSrc "conan"
$conanDst = Join-Path $DestRootAbs "conan"
if ((Test-Path $conanSrc) -and -not (Test-Path $conanDst)) {
Copy-Item $conanSrc $conanDst -Recurse -Force
Write-Host " Applied: conan/"
} elseif (Test-Path $conanDst) {
Write-Host " Skip (exists): conan/"
}
break
}
"python" {
Copy-IfNotExists (Join-Path $langSrc ".editorconfig") (Join-Path $DestRootAbs ".editorconfig")
Copy-IfNotExists (Join-Path $langSrc ".flake8") (Join-Path $DestRootAbs ".flake8")
Copy-IfNotExists (Join-Path $langSrc ".pre-commit-config.yaml") (Join-Path $DestRootAbs ".pre-commit-config.yaml")
Copy-IfNotExists (Join-Path $langSrc ".pylintrc") (Join-Path $DestRootAbs ".pylintrc")
Copy-IfNotExists (Join-Path $langSrc "pyproject.toml") (Join-Path $DestRootAbs "pyproject.toml")
$vscodeSrc = Join-Path $langSrc ".vscode"
$vscodeDst = Join-Path $DestRootAbs ".vscode"
if ((Test-Path $vscodeSrc) -and -not (Test-Path $vscodeDst)) {
Copy-Item $vscodeSrc $vscodeDst -Recurse -Force
Write-Host " Applied: .vscode/"
} elseif (Test-Path $vscodeDst) {
Write-Host " Skip (exists): .vscode/"
}
break
}
}
}
Write-Host "Templates applied."
}
Write-Host "Done."
-392
View File
@@ -1,392 +0,0 @@
#!/usr/bin/env sh
set -eu
# Vendor a trimmed Playbook snapshot into a target project (offline copy),
# then run sync_standards to materialize .agents/<lang>/ and .gitattributes in
# the target project root.
#
# Usage:
# sh scripts/vendor_playbook.sh -project-root <path> # default: tsl
# sh scripts/vendor_playbook.sh -project-root <path> -langs tsl,cpp
# sh scripts/vendor_playbook.sh -project-root <path> -langs tsl,cpp -apply-templates
#
# Options:
# -project-root PATH Target project root (required)
# -langs L1,L2 Comma/space-separated list of languages (default: tsl)
# -apply-templates Apply CI/lang templates to project root (skip if exists)
#
# Notes:
# - Snapshot is written to: <project-root>/docs/standards/playbook/
# - Existing snapshot is backed up before overwrite.
# - Ruleset templates from rulesets/ will be copied to snapshot for sync_standards use.
# - With -apply-templates, CI and lang templates are copied to project root.
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)"
SRC="$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd -P)"
usage() {
cat <<'EOF' >&2
Usage:
sh scripts/vendor_playbook.sh -project-root <path> # default: tsl
sh scripts/vendor_playbook.sh -project-root <path> -langs tsl,cpp
sh scripts/vendor_playbook.sh -project-root <path> -langs tsl,cpp -apply-templates
Options:
-project-root PATH Target project root (required)
-langs L1,L2 Comma/space-separated list of languages (default: tsl)
-apply-templates Apply CI/lang templates to project root (skip if exists)
-h, -help Show this help
EOF
}
if [ "${1:-}" = "-h" ] || [ "${1:-}" = "-help" ]; then
usage
exit 0
fi
PROJECT_ROOT=""
langs=""
APPLY_TEMPLATES=0
# Parse arguments
while [ $# -gt 0 ]; do
case "$1" in
-project-root)
if [ $# -lt 2 ] || [ -z "${2:-}" ]; then
echo "ERROR: -project-root requires a path." >&2
usage
exit 1
fi
PROJECT_ROOT="$2"
shift 2
;;
-langs)
if [ $# -lt 2 ] || [ -z "${2:-}" ]; then
echo "ERROR: -langs requires a value." >&2
usage
exit 1
fi
langs="$2"
shift 2
;;
-apply-templates)
APPLY_TEMPLATES=1
shift
;;
-*)
echo "ERROR: Unknown option: $1" >&2
exit 1
;;
*)
echo "ERROR: positional args are not supported; use -project-root/-langs." >&2
exit 1
;;
esac
done
if [ -z "$PROJECT_ROOT" ]; then
echo "ERROR: -project-root is required." >&2
usage
exit 1
fi
if [ -z "${langs:-}" ]; then
langs="tsl"
fi
timestamp="$(date +%Y%m%d%H%M%S 2>/dev/null || echo bak)"
if [ ! -d "$PROJECT_ROOT" ]; then
echo "ERROR: project root does not exist: $PROJECT_ROOT" >&2
exit 1
fi
PROJECT_ROOT_ABS="$(CDPATH= cd -- "$PROJECT_ROOT" && pwd -P)"
DEST_PREFIX="$PROJECT_ROOT_ABS/docs/standards/playbook"
DEST_STANDARDS="$PROJECT_ROOT_ABS/docs/standards"
mkdir -p "$DEST_STANDARDS"
if [ -e "$DEST_PREFIX" ]; then
mv "$DEST_PREFIX" "$DEST_STANDARDS/playbook.bak.$timestamp"
echo "Backed up existing snapshot -> docs/standards/playbook.bak.$timestamp"
fi
mkdir -p "$DEST_PREFIX"
# Always include: scripts + gitattributes + docs/common + codex/skills
cp "$SRC/.gitattributes" "$DEST_PREFIX/.gitattributes"
cp -R "$SRC/scripts" "$DEST_PREFIX/"
cp -R "$SRC/codex" "$DEST_PREFIX/"
cp "$SRC/SKILLS.md" "$DEST_PREFIX/SKILLS.md"
mkdir -p "$DEST_PREFIX/docs"
cp -R "$SRC/docs/common" "$DEST_PREFIX/docs/"
# Copy rulesets
mkdir -p "$DEST_PREFIX/rulesets"
cp "$SRC/rulesets/index.md" "$DEST_PREFIX/rulesets/index.md"
mkdir -p "$DEST_PREFIX/templates"
if [ -d "$SRC/templates/ci" ]; then
cp -R "$SRC/templates/ci" "$DEST_PREFIX/templates/"
fi
old_ifs="${IFS}"
IFS=', '
set -- $langs
IFS="${old_ifs}"
langs_csv=""
for lang in "$@"; do
[ -n "$lang" ] || continue
case "$lang" in
""|*/*|*\\*|*..*)
echo "ERROR: invalid lang=$lang" >&2
exit 1
;;
esac
if [ ! -d "$SRC/docs/$lang" ]; then
echo "ERROR: docs not found for lang=$lang ($SRC/docs/$lang)" >&2
exit 1
fi
if [ ! -d "$SRC/rulesets/$lang" ]; then
echo "ERROR: rulesets not found for lang=$lang ($SRC/rulesets/$lang)" >&2
exit 1
fi
cp -R "$SRC/docs/$lang" "$DEST_PREFIX/docs/"
cp -R "$SRC/rulesets/$lang" "$DEST_PREFIX/rulesets/"
if [ -d "$SRC/templates/$lang" ]; then
cp -R "$SRC/templates/$lang" "$DEST_PREFIX/templates/"
fi
if [ -n "$langs_csv" ]; then
langs_csv="$langs_csv,$lang"
else
langs_csv="$lang"
fi
done
cat >"$DEST_PREFIX/docs/index.md" <<EOF
# 文档导航(Docs Index
本快照为裁剪版 Playbooklangs: ${langs_csv})。
## 跨语言(common
- 提交信息与版本号:\`common/commit_message.md\`
EOF
append_docs_section() {
lang="$1"
case "$lang" in
tsl)
cat >>"$DEST_PREFIX/docs/index.md" <<'EOF'
## TSLtsl
- 代码风格:`tsl/code_style.md`
- 命名规范:`tsl/naming.md`
- 语法手册:`tsl/syntax_book/index.md`
- 工具链与验证命令(模板):`tsl/toolchain.md`
EOF
;;
cpp)
cat >>"$DEST_PREFIX/docs/index.md" <<'EOF'
## C++cpp
- 代码风格:`cpp/code_style.md`
- 命名规范:`cpp/naming.md`
- 工具链与验证命令(模板):`cpp/toolchain.md`
- 第三方依赖(Conan):`cpp/dependencies_conan.md`
- clangd 配置:`cpp/clangd.md`
EOF
;;
python)
cat >>"$DEST_PREFIX/docs/index.md" <<'EOF'
## Pythonpython
- 代码风格:`python/style_guide.md`
- 工具链:`python/tooling.md`
- 配置清单:`python/configuration.md`
EOF
;;
markdown)
cat >>"$DEST_PREFIX/docs/index.md" <<'EOF'
## Markdownmarkdown
- 代码块与行内代码格式:`markdown/index.md`
EOF
;;
esac
}
for lang in "$@"; do
[ -n "$lang" ] || continue
append_docs_section "$lang"
done
commit=""
if command -v git >/dev/null 2>&1; then
commit="$(git -C "$SRC" rev-parse HEAD 2>/dev/null || true)"
fi
cat >"$DEST_PREFIX/README.md" <<EOF
# Playbook(裁剪快照)
本目录为从 Playbook vendoring 的裁剪快照(langs: ${langs_csv})。
## 使用
在目标项目根目录执行(多语言一次同步):
\`\`\`sh
sh docs/standards/playbook/scripts/sync_standards.sh -langs ${langs_csv}
\`\`\`
查看规范入口:
- \`docs/standards/playbook/docs/index.md\`
- \`.agents/index.md\`
## Codex skills(可选)
安装到本机(需要先在 \`~/.codex/config.toml\` 启用 skills;见 \`docs/standards/playbook/SKILLS.md\`):
\`\`\`sh
sh docs/standards/playbook/scripts/install_codex_skills.sh -all
\`\`\`
## CI templates(可选)
目标项目可复制启用的 CI 示例模板(如 Gitea Actions):\`templates/ci/\`。
EOF
cat >"$DEST_PREFIX/SOURCE.md" <<EOF
# SOURCE
- Source: $SRC
- Commit: ${commit:-N/A}
- Date: $(date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date)
- Langs: ${langs_csv}
- Generated-by: scripts/vendor_playbook.sh
EOF
echo "Vendored snapshot -> $DEST_PREFIX"
PROJECT_AGENTS_ROOT="$PROJECT_ROOT_ABS/.agents"
PROJECT_AGENTS_INDEX="$PROJECT_AGENTS_ROOT/index.md"
mkdir -p "$PROJECT_AGENTS_ROOT"
if [ ! -f "$PROJECT_AGENTS_INDEX" ]; then
cat >"$PROJECT_AGENTS_INDEX" <<EOF
# .agents(多语言)
本目录用于存放仓库级/语言级的代理规则集。
本项目已启用的规则集:
EOF
for lang in "$@"; do
case "$lang" in
tsl) printf '%s\n' "- .agents/tsl/TSL 相关规则集(适用于 .tsl/.tsf" >>"$PROJECT_AGENTS_INDEX" ;;
cpp) printf '%s\n' "- .agents/cpp/C++ 相关规则集(C++23,含 Modules" >>"$PROJECT_AGENTS_INDEX" ;;
python) printf '%s\n' "- .agents/python/Python 相关规则集" >>"$PROJECT_AGENTS_INDEX" ;;
markdown) printf '%s\n' "- .agents/markdown/Markdown 相关规则集(仅代码格式化)" >>"$PROJECT_AGENTS_INDEX" ;;
esac
done
cat >>"$PROJECT_AGENTS_INDEX" <<'EOF'
入口建议从:
EOF
for lang in "$@"; do
printf '%s\n' "- .agents/$lang/index.md" >>"$PROJECT_AGENTS_INDEX"
done
cat >>"$PROJECT_AGENTS_INDEX" <<'EOF'
标准快照文档入口:
- docs/standards/playbook/docs/index.md
EOF
fi
SYNC_ROOT="$PROJECT_ROOT_ABS" sh "$DEST_PREFIX/scripts/sync_standards.sh" -langs "$langs_csv"
# Apply templates to project root if requested
if [ "$APPLY_TEMPLATES" -eq 1 ]; then
echo ""
echo "Applying templates to project root..."
# Helper function: copy file if not exists
copy_if_not_exists() {
src_file="$1"
dst_file="$2"
if [ -f "$src_file" ]; then
if [ -f "$dst_file" ]; then
echo " Skip (exists): $(basename "$dst_file")"
else
cp "$src_file" "$dst_file"
echo " Applied: $(basename "$dst_file")"
fi
fi
}
# Apply CI templates (Gitea workflows)
CI_SRC="$DEST_PREFIX/templates/ci/gitea"
if [ -d "$CI_SRC/.gitea" ]; then
if [ -d "$PROJECT_ROOT_ABS/.gitea" ]; then
echo " Skip (exists): .gitea/"
else
cp -R "$CI_SRC/.gitea" "$PROJECT_ROOT_ABS/"
echo " Applied: .gitea/"
fi
fi
# Apply lang-specific templates
for lang in "$@"; do
[ -n "$lang" ] || continue
LANG_SRC="$DEST_PREFIX/templates/$lang"
[ -d "$LANG_SRC" ] || continue
case "$lang" in
cpp)
copy_if_not_exists "$LANG_SRC/.clang-format" "$PROJECT_ROOT_ABS/.clang-format"
copy_if_not_exists "$LANG_SRC/.clangd" "$PROJECT_ROOT_ABS/.clangd"
copy_if_not_exists "$LANG_SRC/CMakeLists.txt" "$PROJECT_ROOT_ABS/CMakeLists.txt"
copy_if_not_exists "$LANG_SRC/CMakeUserPresets.json" "$PROJECT_ROOT_ABS/CMakeUserPresets.json"
copy_if_not_exists "$LANG_SRC/conanfile.txt" "$PROJECT_ROOT_ABS/conanfile.txt"
if [ -d "$LANG_SRC/conan" ] && [ ! -d "$PROJECT_ROOT_ABS/conan" ]; then
cp -R "$LANG_SRC/conan" "$PROJECT_ROOT_ABS/"
echo " Applied: conan/"
elif [ -d "$PROJECT_ROOT_ABS/conan" ]; then
echo " Skip (exists): conan/"
fi
;;
python)
copy_if_not_exists "$LANG_SRC/.editorconfig" "$PROJECT_ROOT_ABS/.editorconfig"
copy_if_not_exists "$LANG_SRC/.flake8" "$PROJECT_ROOT_ABS/.flake8"
copy_if_not_exists "$LANG_SRC/.pre-commit-config.yaml" "$PROJECT_ROOT_ABS/.pre-commit-config.yaml"
copy_if_not_exists "$LANG_SRC/.pylintrc" "$PROJECT_ROOT_ABS/.pylintrc"
copy_if_not_exists "$LANG_SRC/pyproject.toml" "$PROJECT_ROOT_ABS/pyproject.toml"
if [ -d "$LANG_SRC/.vscode" ] && [ ! -d "$PROJECT_ROOT_ABS/.vscode" ]; then
cp -R "$LANG_SRC/.vscode" "$PROJECT_ROOT_ABS/"
echo " Applied: .vscode/"
elif [ -d "$PROJECT_ROOT_ABS/.vscode" ]; then
echo " Skip (exists): .vscode/"
fi
;;
esac
done
echo "Templates applied."
fi
echo "Done."