88 lines
2.8 KiB
Batchfile
88 lines
2.8 KiB
Batchfile
@echo off
|
||
setlocal enabledelayedexpansion
|
||
|
||
rem Sync standards snapshot to project root.
|
||
rem - Copies <snapshot>\.agents -> <project-root>\.agents\tsl
|
||
rem - Copies <snapshot>\.gitattributes -> <project-root>\.gitattributes
|
||
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"
|
||
set "AGENTS_ROOT=%ROOT%\.agents"
|
||
set "AGENTS_DST=%AGENTS_ROOT%\%AGENTS_NS%"
|
||
set "GITATTR_DST=%ROOT%\.gitattributes"
|
||
|
||
if not exist "%AGENTS_SRC%" (
|
||
echo ERROR: Standards snapshot not found at "%AGENTS_SRC%".
|
||
echo Run: git subtree add --prefix ^<your-prefix^> ^<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.
|
||
|
||
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.*` 同步)
|
||
>> "%AGENTS_ROOT%\index.md" echo - `.agents/cpp/`、`.agents/python/` 等:其他语言的规则集(按需增加)
|
||
>> "%AGENTS_ROOT%\index.md" echo.
|
||
>> "%AGENTS_ROOT%\index.md" echo 规则发生冲突时,建议以“更靠近代码的目录规则更具体”为准。
|
||
echo Created .agents\index.md
|
||
)
|
||
|
||
:SyncGitAttr
|
||
if exist "%GITATTR_SRC%" (
|
||
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.
|
||
)
|
||
|
||
:AfterGitAttr
|
||
echo Done.
|
||
endlocal
|