@echo off setlocal enabledelayedexpansion rem Sync standards snapshot to project root. rem - Copies \.agents -> \.agents\tsl rem - Updates \.gitattributes (managed block by default) rem Existing targets are backed up before overwrite. set "SCRIPT_DIR=%~dp0" 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=%SRC%\.agents" set "GITATTR_SRC=%SRC%\.gitattributes" set "AGENTS_NS=%AGENTS_NS%" 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 "GITATTR_DST=%ROOT%\.gitattributes" set "SYNC_GITATTR_MODE=%SYNC_GITATTR_MODE%" if "%SYNC_GITATTR_MODE%"=="" set "SYNC_GITATTR_MODE=block" if not exist "%AGENTS_SRC%" ( echo ERROR: Standards snapshot not found at "%AGENTS_SRC%". echo Run: git subtree add --prefix ^ ^ ^ --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. 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/`、`.agents/python/` 等:其他语言的规则集(按需增加) >> "%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 - `docs/standards/tsl/docs/tsl/`(TSL 人类开发规范快照,推荐 subtree/vendoring 到该路径) echo Created .agents\index.md ) :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 not "%SYNC_GITATTR_MODE%"=="block" ( echo ERROR: invalid SYNC_GITATTR_MODE=%SYNC_GITATTR_MODE% ^(use block^|overwrite^|skip^) exit /b 1 ) rem block mode: maintain a managed block inside the destination file set "BEGIN=# BEGIN tsl-playbook .gitattributes" set "END=# 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!"=="%END%" ( 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