Merge upstream and add generic Codex notifications
This commit is contained in:
+6
-3
@@ -9,7 +9,7 @@ if exist "%VSWHERE%" (
|
||||
for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do set "VSPATH=%%i"
|
||||
)
|
||||
if defined VSPATH if exist "%VSPATH%\VC\Auxiliary\Build\vcvars64.bat" (
|
||||
echo === 配置 MSVC 环境: %VSPATH% ===
|
||||
echo === Configure MSVC env: %VSPATH% ===
|
||||
call "%VSPATH%\VC\Auxiliary\Build\vcvars64.bat" >nul
|
||||
)
|
||||
|
||||
@@ -17,10 +17,13 @@ echo === NativeAOT publish (win-x64) -^> bin\notify.exe ===
|
||||
dotnet publish Notify -c Release -r win-x64 -p:PublishAot=true -o bin
|
||||
if errorlevel 1 (
|
||||
echo.
|
||||
echo *** 发布失败。若提示找不到 link.exe,请从 "Developer Command Prompt for VS" 运行本脚本 ***
|
||||
echo *** publish failed. If link.exe not found, run from "Developer Command Prompt for VS" ***
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
rem 清理发布目录残留:只留 notify.exe(AOT 下 -o 拷贝时序使 csproj 内清理不可靠,这里统一处理)
|
||||
del /q bin\*.pdb bin\*.dll bin\*.lib >nul 2>&1
|
||||
|
||||
echo.
|
||||
echo === 完成: %CD%\bin\notify.exe ===
|
||||
echo === Done: %CD%\bin\notify.exe ===
|
||||
endlocal
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$Url,
|
||||
[Parameter(Mandatory = $true)][string]$Out,
|
||||
[Parameter(Mandatory = $true)][string]$Lock
|
||||
)
|
||||
|
||||
# Download notify.exe with curl to a temp file, then atomic-rename on success.
|
||||
# Always remove the lock dir at the end (success or failure).
|
||||
$tmp = $Out + ".downloading"
|
||||
try {
|
||||
& curl.exe -fsSL $Url -o $tmp
|
||||
if ((Test-Path -LiteralPath $tmp) -and ((Get-Item -LiteralPath $tmp).Length -gt 0)) {
|
||||
Move-Item -LiteralPath $tmp -Destination $Out -Force
|
||||
}
|
||||
}
|
||||
finally {
|
||||
Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
|
||||
Remove-Item -LiteralPath $Lock -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
+52
-13
@@ -6,21 +6,60 @@ rem ============================================================
|
||||
setlocal
|
||||
set "BIN=%~dp0..\bin"
|
||||
set "EXE=%BIN%\notify.exe"
|
||||
set "PART=%BIN%\notify.exe.partial"
|
||||
set "LOCK=%BIN%\notify.download.lock"
|
||||
|
||||
rem first run: kick off a background download, do NOT block the hook
|
||||
if not exist "%EXE%" call :bootstrap
|
||||
rem hidden self-reinvocation: detached background downloader (see :downloader)
|
||||
if "%~1"=="__download" goto downloader
|
||||
|
||||
if exist "%EXE%" "%EXE%" %*
|
||||
endlocal
|
||||
exit /b
|
||||
rem common path: exe present -> run directly (keeps piped stdin intact)
|
||||
if exist "%EXE%" (
|
||||
"%EXE%" %*
|
||||
endlocal & exit /b
|
||||
)
|
||||
|
||||
:bootstrap
|
||||
rem ---- exe missing: never block the hook ----
|
||||
rem kick off the download once (atomic mkdir lock); a detached worker survives the
|
||||
rem hook timeout. then report progress and return immediately.
|
||||
if not exist "%BIN%" mkdir "%BIN%" 2>nul
|
||||
rem mkdir is atomic; success = we start the download, failure = already downloading
|
||||
mkdir "%LOCK%" 2>nul
|
||||
if errorlevel 1 exit /b
|
||||
if exist "%EXE%" ( rmdir "%LOCK%" 2>nul & exit /b )
|
||||
rem launch a detached hidden PowerShell downloader (ShellExecute breaks away from the hook)
|
||||
powershell -NoProfile -WindowStyle Hidden -Command "Start-Process -WindowStyle Hidden -FilePath powershell -ArgumentList @('-NoProfile','-ExecutionPolicy','Bypass','-File','%~dp0download.ps1','-Url','%DOWNLOAD_URL%','-Out','%EXE%','-Lock','%LOCK%')"
|
||||
exit /b
|
||||
|
||||
rem self-heal: if a previous worker was hard-killed (shutdown/crash) it may leave a
|
||||
rem stale lock that blocks all retries. reclaim it so -C - can resume the .partial.
|
||||
if exist "%LOCK%" call :reclaim
|
||||
|
||||
rem atomic lock: only the first hook spawns the worker; others fall through and just
|
||||
rem report progress -> no duplicate downloads even when hooks fire concurrently.
|
||||
rem
|
||||
rem must use Start-Process (not `start /b`): a child started by cmd inherits the
|
||||
rem hook's stdout pipe handle, so the caller won't see EOF until the download ends ->
|
||||
rem the hook would block. Start-Process spawns WITHOUT inheriting handles, so the
|
||||
rem hook returns immediately while curl keeps running detached.
|
||||
mkdir "%LOCK%" 2>nul && powershell -nop -w hidden -c "Start-Process -WindowStyle Hidden -FilePath '%~f0' -ArgumentList '__download'" >nul 2>&1
|
||||
|
||||
rem downloaded size so far (from the .partial file), shown as X.X MB
|
||||
set "DLBYTES=0"
|
||||
if exist "%PART%" for %%A in ("%PART%") do set "DLBYTES=%%~zA"
|
||||
set /a DLKB=DLBYTES/1024
|
||||
set /a DLMB=DLKB/1024
|
||||
set /a DLF=(DLKB*10/1024)%%10
|
||||
|
||||
rem Some hook clients display this progress JSON; others ignore it.
|
||||
echo {"suppressOutput":true,"systemMessage":"[Notify] notifier not ready, downloading in background: %DLMB%.%DLF% MB / ~14 MB done. Works automatically once finished; this notification is skipped."}
|
||||
endlocal & exit /b 0
|
||||
|
||||
:downloader
|
||||
rem detached worker: resume-capable download (-C -), atomic install, always free lock.
|
||||
rem if curl fails (slow/flaky net), the .partial is kept and the next hook resumes it.
|
||||
curl -fsSL -C - "%DOWNLOAD_URL%" -o "%PART%"
|
||||
if not errorlevel 1 move /y "%PART%" "%EXE%" >nul 2>&1
|
||||
rmdir "%LOCK%" 2>nul
|
||||
endlocal & exit /b
|
||||
|
||||
:reclaim
|
||||
rem no .partial yet => worker died before downloading anything; reclaim immediately
|
||||
if not exist "%PART%" ( rmdir "%LOCK%" 2>nul & goto :eof )
|
||||
rem .partial idle for >120s => worker is dead (a live curl writes continuously); reclaim
|
||||
for /f "delims=" %%T in ('powershell -nop -c "[int]((Get-Date)-(Get-Item '%PART%').LastWriteTime).TotalSeconds" 2^>nul') do set "IDLE=%%T"
|
||||
if not defined IDLE goto :eof
|
||||
if %IDLE% geq 120 rmdir "%LOCK%" 2>nul
|
||||
goto :eof
|
||||
|
||||
+31
-10
@@ -7,18 +7,39 @@ DOWNLOAD_URL="https://git.pchuan.top/cc-tools/notify/releases/download/v1.0.0/no
|
||||
DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
BIN="$DIR/../bin"
|
||||
EXE="$BIN/notify.exe"
|
||||
PART="$BIN/notify.exe.partial"
|
||||
LOCK="$BIN/notify.download.lock"
|
||||
|
||||
# 首次运行:后台下载,不阻塞钩子
|
||||
if [ ! -f "$EXE" ]; then
|
||||
mkdir -p "$BIN" 2>/dev/null
|
||||
# mkdir 原子:成功=本进程负责下载,失败=已有进程在下
|
||||
if mkdir "$LOCK" 2>/dev/null; then
|
||||
TMP="$EXE.downloading"
|
||||
# nohup + & 让下载脱离钩子在后台跑;完成后原子改名并清锁
|
||||
nohup sh -c "curl -fsSL '$DOWNLOAD_URL' -o '$TMP' && mv -f '$TMP' '$EXE'; rmdir '$LOCK' 2>/dev/null" >/dev/null 2>&1 &
|
||||
# 常规路径:exe 已就绪 -> 直接转发参数与 stdin(保持管道完整)
|
||||
if [ -f "$EXE" ]; then
|
||||
exec "$EXE" "$@"
|
||||
fi
|
||||
|
||||
# ---- exe 缺失:绝不阻塞 hook ----
|
||||
mkdir -p "$BIN" 2>/dev/null
|
||||
|
||||
# 自愈:上次下载进程被硬杀(关机/崩溃)可能留下陈旧锁,挡住所有重试。
|
||||
# .partial 不存在(没真正开始)或 >2 分钟没增长(进程已死)则回收锁,让 -C - 续传。
|
||||
if [ -d "$LOCK" ]; then
|
||||
if [ ! -f "$PART" ]; then
|
||||
rmdir "$LOCK" 2>/dev/null
|
||||
elif [ -n "$(find "$PART" -mmin +2 2>/dev/null)" ]; then
|
||||
rmdir "$LOCK" 2>/dev/null
|
||||
fi
|
||||
fi
|
||||
|
||||
# 本次不等下载;exe 就绪后才转发参数与 stdin
|
||||
[ -f "$EXE" ] && exec "$EXE" "$@"
|
||||
# 原子锁:只有第一个 hook 派生唯一的后台下载进程;并发/后续 hook 抢锁失败
|
||||
# -> 不重复下载,只汇报进度。下载脱离 hook 后台进行,不受 30s 超时影响。
|
||||
if mkdir "$LOCK" 2>/dev/null; then
|
||||
# 断点续传 -C -;失败保留 .partial 供下次续传;无论成败都释放锁
|
||||
nohup sh -c "curl -fsSL -C - '$DOWNLOAD_URL' -o '$PART' && mv -f '$PART' '$EXE' && chmod +x '$EXE'; rmdir '$LOCK' 2>/dev/null" >/dev/null 2>&1 &
|
||||
fi
|
||||
|
||||
# 已下载大小(取 .partial 字节数)
|
||||
DLBYTES=0
|
||||
[ -f "$PART" ] && DLBYTES=$(wc -c < "$PART" 2>/dev/null | tr -d ' ')
|
||||
DLMB=$(awk "BEGIN{printf \"%.1f\", $DLBYTES/1048576}")
|
||||
|
||||
# 部分 hook 客户端会显示进度 JSON,其他客户端会忽略它。
|
||||
printf '{"suppressOutput":true,"systemMessage":"[Notify] notifier not ready, downloading in background: %s MB / ~14 MB done. Works automatically once finished; this notification is skipped."}\n' "$DLMB"
|
||||
exit 0
|
||||
|
||||
Reference in New Issue
Block a user