@echo off REM ============================================================ REM ObsiGate Desktop — Build Windows (NSIS installer) REM Prérequis : Rust, Tauri CLI, NSIS, Python 3.11 embed REM ============================================================ setlocal enabledelayedexpansion REM Always run from the script directory, regardless of where the REM caller invoked us from. Without this, a call like REM `desktop\build-windows.bat` from the repo root would download into REM the repo root and, worse, delete the repo's backend/ and frontend/ REM directories in the staging/cleanup steps. cd /d "%~dp0" echo ======================================== echo ObsiGate Desktop — Build Windows echo ======================================== echo. REM ── 0. Close a running build so the exe can be replaced ─────── REM Windows verrouille l'exécutable : sans cette étape, cargo échoue REM avec « Access is denied » si l'application tourne encore. echo [0/6] Closing running ObsiGate Desktop / embedded backend... taskkill /F /IM obsigate-desktop.exe >nul 2>&1 REM Only target the python.exe shipped in python-embed/, never the REM user's own Python installs. powershell -NoProfile -Command "Get-Process python -ErrorAction SilentlyContinue | Where-Object { $_.Path -like '*\python-embed\python.exe' } | Stop-Process -Force -ErrorAction SilentlyContinue" echo. REM ── 1. Vérifier les prérequis ───────────────────────────────── echo [1/6] Verifying prerequisites... where cargo >nul 2>&1 || (echo ❌ Rust/Cargo not found. Install from https://rustup.rs & exit /b 1) where tauri >nul 2>&1 || cargo install tauri-cli echo ✅ Rust + Tauri OK REM ── 2. Préparer Python 3.11 embed ───────────────────────────── echo [2/6] Preparing Python embed... if exist "python-embed" rmdir /s /q "python-embed" if errorlevel 1 ( echo ❌ Cannot remove python-embed - a process is still using it goto :fail ) echo 📥 Downloading Python 3.11 embed... curl -L --fail -o python-embed.zip "https://www.python.org/ftp/python/3.11.9/python-3.11.9-embed-amd64.zip" if errorlevel 1 ( echo ❌ Python embed download failed goto :fail ) REM Use the native bsdtar when available: Expand-Archive can fail when a REM PS7 PSModulePath is inherited by Windows PowerShell (overload error), REM and its failure is non-fatal (exit code 0), so we verify python.exe REM afterwards in all cases. if not exist "python-embed" mkdir "python-embed" where tar >nul 2>&1 if not errorlevel 1 ( tar -xf python-embed.zip -C python-embed ) else ( powershell -NoProfile -Command "Expand-Archive -Path 'python-embed.zip' -DestinationPath 'python-embed'" ) if errorlevel 1 ( echo ❌ Python embed extraction failed goto :fail ) if not exist "python-embed\python.exe" ( echo ❌ Python embed extraction failed - python.exe missing goto :fail ) del /q python-embed.zip REM Write the ._pth explicitly instead of appending to the archive's REM file. An embedded Python finds its standard library ONLY through REM this file: without python311.zip + '.' it cannot import `encodings` REM and the backend dies before serving a single request (the UI then REM stays on the unstyled Tauri splash — "CSS is broken"). REM python311.zip → stdlib REM . → python-embed\ (DLLs, .pyd) REM import site → enables Lib\site-packages REM .. → exe dir (so `import backend` works next to the .exe) ( echo python311.zip echo . echo import site echo .. ) > python-embed\python311._pth REM Download get-pip.py (embed Python doesn't include ensurepip) echo 📥 Downloading get-pip.py... curl -L --fail -o get-pip.py "https://bootstrap.pypa.io/get-pip.py" if errorlevel 1 ( echo ❌ get-pip.py download failed goto :fail ) python-embed\python.exe get-pip.py --no-cache-dir if errorlevel 1 ( echo ❌ pip bootstrap failed goto :fail ) del /q get-pip.py REM Install dependencies python-embed\python.exe -m pip install --no-cache-dir -r ..\backend\requirements.txt if errorlevel 1 ( echo ❌ Python dependencies installation failed goto :fail ) REM Fail fast if the runtime is unusable (missing stdlib / deps). python-embed\python.exe -c "import encodings, uvicorn, fastapi, pyotp" if errorlevel 1 ( echo ❌ python-embed sanity check failed ^(stdlib or deps missing^) goto :fail ) echo ✅ Python embed ready REM ── 2b. Copier backend + frontend localement pour le bundle ──── echo [2b/6] Staging backend + frontend for bundle... if exist "backend" rmdir /s /q "backend" if exist "frontend" rmdir /s /q "frontend" xcopy /E /I /Q /Y "..\backend" "backend" xcopy /E /I /Q /Y "..\frontend" "frontend" echo ✅ Staged REM ── 2c. Clé de signature des mises à jour (updater Tauri) ────── REM La clé privée peut être protégée par mot de passe. Tauri CLI REM demande alors ce mot de passe de façon interactive (une fois par REM invocation, donc deux fois à cause du repli NSIS) — impossible à REM fournir dans un script non surveillé. On ne signe donc que si le REM mot de passe est explicitement fourni dans l'environnement : REM set TAURI_SIGNING_PRIVATE_KEY_PASSWORD=*** & build-windows.bat REM Sinon les artefacts .sig sont désactivés, sans aucune invite. set "SIGN_CONFIG=" if exist "obsigate-updater.key" ( if defined TAURI_SIGNING_PRIVATE_KEY_PASSWORD ( set "TAURI_SIGNING_PRIVATE_KEY_PATH=obsigate-updater.key" echo [2c/6] Cle updater + mot de passe fournis - artefacts .sig actives ) else ( > updater-off.json echo {"bundle":{"createUpdaterArtifacts":false}} set "SIGN_CONFIG=--config updater-off.json" echo [2c/6] Cle updater presente mais TAURI_SIGNING_PRIVATE_KEY_PASSWORD non defini echo [2c/6] Build sans artefacts .sig ^(aucune invite de mot de passe^) ) ) else ( > updater-off.json echo {"bundle":{"createUpdaterArtifacts":false}} set "SIGN_CONFIG=--config updater-off.json" echo [2c/6] Cle updater absente - build sans .sig ) REM ── 3. Build Tauri ──────────────────────────────────────────── echo [3/6] Building Tauri application (NSIS + MSI installers)... REM `--ci` interdit toute invite interactive (dont le mot de passe de REM la clé updater) : en cas de valeur manquante, le build échoue REM proprement au lieu de bloquer le terminal. cargo tauri build --ci --target x86_64-pc-windows-msvc --bundles nsis,msi %SIGN_CONFIG% if errorlevel 1 ( echo ⚠️ Standard bundle build failed, trying fallback to NSIS only... cargo tauri build --ci --target x86_64-pc-windows-msvc --bundles nsis %SIGN_CONFIG% if errorlevel 1 ( echo ❌ Build failed! goto :fail ) ) echo ✅ Build successful REM ── 4. Copier le runtime vers l'output dir (exe lancé sur place) ─ REM L'exe résout `python-embed/python.exe`, `backend/` (PYTHONPATH) et REM `frontend/` (fichiers statiques servis par FastAPI) *à côté de lui*. REM Sans ces trois dossiers, l'exe ne démarre aucun backend : la fenêtre REM reste sur le HTML embarqué par Tauri, dont les liens /static/* ne REM résolvent pas → interface sans CSS. echo [4/6] Copying runtime (python-embed, backend, frontend)... set "OUTDIR=target\x86_64-pc-windows-msvc\release" for %%D in (python-embed backend frontend) do ( if exist "%OUTDIR%\%%D" rmdir /s /q "%OUTDIR%\%%D" xcopy /E /I /Q /Y "%%D" "%OUTDIR%\%%D" >nul if errorlevel 1 ( echo ❌ Copy of %%D to %OUTDIR% failed goto :fail ) ) echo ✅ Dev runtime ready call :cleanup REM ── 6. Résultats ─────────────────────────────────────────────── echo [6/6] Binaries generated: dir /b "%OUTDIR%\bundle\nsis\*.exe" 2>nul dir /b "%OUTDIR%\bundle\msi\*.msi" 2>nul echo. echo ✅ Done! Installers generated in: %OUTDIR%\bundle\ endlocal exit /b 0 :cleanup REM ── 5. Nettoyer les dossiers stagés ─────────────────────────── echo [5/6] Cleaning up staged dirs... if exist "backend" rmdir /s /q "backend" if exist "frontend" rmdir /s /q "frontend" if exist "updater-off.json" del /q "updater-off.json" echo ✅ Cleaned goto :eof :fail echo. echo ❌ Build failed - see messages above. call :cleanup endlocal exit /b 1