diff --git a/.gitignore b/.gitignore index 5001a5e..b2ea3ee 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ docs/.vuepress/dist .trae/ .DS_Store *.log +_publish/ diff --git a/build-and-publish.bat b/build-and-publish.bat new file mode 100644 index 0000000..6d51315 --- /dev/null +++ b/build-and-publish.bat @@ -0,0 +1,133 @@ +@echo off +setlocal EnableExtensions + +chcp 65001 >nul + +REM Paths and settings +set "ROOT_DIR=%~dp0" +set "PROJECT_DIR=%ROOT_DIR%" +set "DIST_DIR=%PROJECT_DIR%docs\.vuepress\dist" +set "PUBLISH_DIR=%PROJECT_DIR%_publish" +set "REMOTE_URL=https://gitea.simengweb.com/si-meng-spec/build_notes_simengweb.git" +set "BRANCH=main" + +REM Args +set "LIGHT_FLAG=%~1" +set "COMMIT_MSG=%~2" + +echo [Info] Working dir: %PROJECT_DIR% + +REM Pre-checks +where npm >nul 2>&1 +if errorlevel 1 goto :npm_missing +where git >nul 2>&1 +if errorlevel 1 goto :git_missing + +REM Install deps on first run +if not exist "%PROJECT_DIR%node_modules" ( + echo [Info] Installing deps ^(npm ci^)... + call npm ci + if errorlevel 1 goto :fail +) else ( + echo [Info] node_modules exists, skip install. +) + +REM Light build mode +if /I "%LIGHT_FLAG%"=="light" ( + set "LIGHT_BUILD=1" + echo [Info] LIGHT_BUILD=1 enabled +) + +set "NODE_OPTIONS=--max-old-space-size=4096" + +echo [Info] Building docs... +call npm run docs:build +if errorlevel 1 goto :fail + +if not exist "%DIST_DIR%" goto :no_dist + +REM Prepare publish repo +if not exist "%PUBLISH_DIR%\.git" ( + echo [Info] Cloning publish repo... + git clone "%REMOTE_URL%" "%PUBLISH_DIR%" + if errorlevel 1 goto :fail +) else ( + echo [Info] Updating publish repo... + pushd "%PUBLISH_DIR%" + git fetch --all + if errorlevel 1 goto :gitfail + git checkout "%BRANCH%" + if errorlevel 1 goto :gitfail + git reset --hard "origin/%BRANCH%" + if errorlevel 1 goto :gitfail + popd +) + +echo [Info] Cleaning publish directory ^(keep .git^)... +pushd "%PUBLISH_DIR%" +for /f "delims=" %%F in ('dir /a /b') do ( + if /I not "%%F"==".git" ( + if exist "%%F" ( + attrib -R "%%F" >nul 2>&1 + rd /s /q "%%F" 2>nul + del /f /q "%%F" 2>nul + ) + ) +) +popd + +echo [Info] Copying dist to publish... +robocopy "%DIST_DIR%" "%PUBLISH_DIR%" *.* /E /NFL /NDL /NP /NJH /NJS >nul +if errorlevel 8 goto :fail + +pushd "%PUBLISH_DIR%" +if defined GIT_USERNAME git config user.name "%GIT_USERNAME%" +if defined GIT_EMAIL git config user.email "%GIT_EMAIL%" + +git add -A + +if "%COMMIT_MSG%"=="" set "COMMIT_MSG=Build: %DATE% %TIME%" + +set "HAS_CHANGES=1" +git diff --cached --quiet +if not errorlevel 1 set "HAS_CHANGES=0" + +if "%HAS_CHANGES%"=="0" ( + echo [Info] No changes to commit; skipping push. +) else ( + git commit -m "%COMMIT_MSG%" + if errorlevel 1 ( + popd + goto :fail + ) + git push origin "%BRANCH%" + if errorlevel 1 ( + popd + goto :fail + ) + echo [Info] Pushed to %REMOTE_URL% ^(branch %BRANCH%^). +) +popd + +echo [Success] Build and publish done. +exit /b 0 + +:npm_missing +echo [ERROR] npm not found. Please install Node.js. +goto :fail + +:git_missing +echo [ERROR] git not found. Please install Git. +goto :fail + +:no_dist +echo [ERROR] Dist directory not found: %DIST_DIR% +goto :fail + +:gitfail +echo [ERROR] Git operation failed. +popd +goto :fail + +:fail +exit /b 1 \ No newline at end of file