run-hook.cmd 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. : << 'CMDBLOCK'
  2. @echo off
  3. REM Cross-platform polyglot wrapper for hook scripts.
  4. REM On Windows: cmd.exe runs the batch portion, which finds and calls bash.
  5. REM On Unix: the shell interprets this as a script (: is a no-op in bash).
  6. REM
  7. REM Hook scripts use extensionless filenames (e.g. "session-start" not
  8. REM "session-start.sh") so Claude Code's Windows auto-detection -- which
  9. REM prepends "bash" to any command containing .sh -- doesn't interfere.
  10. REM
  11. REM Usage: run-hook.cmd <script-name> [args...]
  12. if "%~1"=="" (
  13. echo run-hook.cmd: missing script name >&2
  14. exit /b 1
  15. )
  16. set "HOOK_DIR=%~dp0"
  17. REM Try Git for Windows bash in standard locations
  18. if exist "C:\Program Files\Git\bin\bash.exe" (
  19. "C:\Program Files\Git\bin\bash.exe" "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
  20. exit /b %ERRORLEVEL%
  21. )
  22. if exist "C:\Program Files (x86)\Git\bin\bash.exe" (
  23. "C:\Program Files (x86)\Git\bin\bash.exe" "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
  24. exit /b %ERRORLEVEL%
  25. )
  26. REM Try bash on PATH (e.g. user-installed Git Bash, MSYS2, Cygwin)
  27. where bash >nul 2>nul
  28. if %ERRORLEVEL% equ 0 (
  29. bash "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
  30. exit /b %ERRORLEVEL%
  31. )
  32. REM No bash found - exit silently rather than error
  33. REM (plugin still works, just without SessionStart context injection)
  34. exit /b 0
  35. CMDBLOCK
  36. # Unix: run the named script directly
  37. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  38. SCRIPT_NAME="$1"
  39. shift
  40. exec bash "${SCRIPT_DIR}/${SCRIPT_NAME}" "$@"