feat(sync_standards): auto-detect existing languages

This commit is contained in:
csh
2026-01-08 13:33:47 +08:00
parent e97fb00649
commit 4ae2733ad2
6 changed files with 104 additions and 39 deletions
+28 -13
View File
@@ -21,27 +21,30 @@ for %%I in ("%SCRIPT_DIR%..") do set "SRC=%%~fI"
set "AGENTS_SRC_ROOT=%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=append"
rem Multi rulesets: only on outer invocation.
if "%SYNC_STANDARDS_INNER%"=="" (
if not "%~1"=="" (
set "LANG_LIST="
if not "%~1"=="" set "LANG_LIST=%*"
if "%LANG_LIST%"=="" (
if "%AGENTS_NS%"=="" (
if exist "%ROOT%\.agents" (
for /d %%D in ("%ROOT%\.agents\*") do (
set "CAND=%%~nxD"
if exist "%AGENTS_SRC_ROOT%\!CAND!\" (
if defined LANG_LIST (set "LANG_LIST=!LANG_LIST! !CAND!") else set "LANG_LIST=!CAND!"
)
)
)
)
)
if not "%LANG_LIST%"=="" (
set "FIRST=1"
set "SYNC_FIRST=%SYNC_GITATTR_MODE%"
for %%L in (%*) do (
for %%L in (!LANG_LIST!) do (
if "!FIRST!"=="1" (
set "FIRST=0"
set "SYNC_STANDARDS_INNER=1"
@@ -59,6 +62,18 @@ if "%SYNC_STANDARDS_INNER%"=="" (
)
)
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.
+11
View File
@@ -31,6 +31,17 @@ if (-not (Test-Path $AgentsSrcRoot)) {
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
# Auto-detect languages from existing .agents when no args are provided.
if (-not $env:SYNC_STANDARDS_INNER -and (-not $Langs -or $Langs.Count -eq 0) -and -not $env:AGENTS_NS) {
$agentsRoot = Join-Path $Root ".agents"
if (Test-Path $agentsRoot) {
$autoLangs = @(Get-ChildItem -Path $agentsRoot -Directory | ForEach-Object { $_.Name } | Where-Object { Test-Path (Join-Path $AgentsSrcRoot $_) })
if ($autoLangs.Count -gt 0) {
$Langs = $autoLangs
}
}
}
# 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
+46 -22
View File
@@ -47,6 +47,21 @@ if [ "${SYNC_STANDARDS_INNER:-}" != "1" ]; then
if [ -z "${langs:-}" ] && [ "$#" -gt 0 ]; then
langs="$*"
fi
if [ -z "${langs:-}" ] && [ "$#" -eq 0 ] && [ -z "${AGENTS_NS:-}" ]; then
auto_langs=""
if [ -d "$ROOT/.agents" ]; then
for dir in "$ROOT/.agents"/*; do
[ -d "$dir" ] || continue
ns="$(basename "$dir")"
if [ -d "$AGENTS_SRC_ROOT/$ns" ]; then
auto_langs="${auto_langs:+$auto_langs }$ns"
fi
done
fi
if [ -n "$auto_langs" ]; then
langs="$auto_langs"
fi
fi
if [ -n "${langs:-}" ]; then
sync_mode_first="${SYNC_GITATTR_MODE:-append}"
@@ -162,29 +177,38 @@ if [ -f "$GITATTR_SRC" ]; then
if [ "$(CDPATH= cd -- "$(dirname -- "$GITATTR_SRC")" && pwd -P)/$(basename -- "$GITATTR_SRC")" = "$GITATTR_DST" ]; then
echo "Skip: .gitattributes source equals destination."
else
if [ -f "$GITATTR_DST" ]; then
dst_file="$GITATTR_DST"
else
dst_file="/dev/null"
fi
missing_tmp="$(mktemp 2>/dev/null || echo "$ROOT/.gitattributes.missing.$timestamp")"
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
}
' "$dst_file" "$GITATTR_SRC" >"$missing_tmp"
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"