OneClickScripts/OneClickDeploy.cmd
2025-06-09 03:51:41 -07:00

347 lines
11 KiB
Batchfile

@echo off
SETLOCAL EnableDelayedExpansion
goto :main
:parseArgs
@REM Parse command line arguments by `call :parseArgs %*`
@REM use !ARG[%n%]! to access arguments
set "_ARG_MAX=0"
for %%A in (%*) do (
set "_ARG[!_ARG_MAX!]=%%A"
set /a _ARG_MAX+=1
)
set "ARG_MAX=0"
set "_ARG_PTR=0"
:while
if !_ARG_PTR! geq !_ARG_MAX! goto :endwhile
set "_ARG=!_ARG[%_ARG_PTR%]!"
if "!_ARG:~0,1!"=="-" (
set "_ARG_KEY=!_ARG:~1!"
set /a _ARG_PTR+=1
call set "_ARG_VALUE=%%_ARG[!_ARG_PTR!]%%"
set "ARG[!_ARG_KEY!]=!_ARG_VALUE!"
) else (
set "ARG[!ARG_MAX!]=!_ARG!"
set /a ARG_MAX+=1
)
set /a _ARG_PTR+=1
goto :while
:endwhile
exit /b 0
:raise
set gErrorLevel=%~2
echo %~1
goto :end
:assert
if %errorlevel% neq 0 (
call :raise "Assertion failed: %~1" %errorlevel%
)
exit /b 0
:download
SETLOCAL EnableDelayedExpansion
set "url=%~1"
set "dest=%~2"
set "destDir=%~dp2"
if not exist "%destDir%" (
mkdir "%destDir%"
call :assert "Failed to create directory %destDir%"
)
if not exist "%dest%" (
echo Downloading %url% to %dest%...
@REM powershell -Command "Invoke-WebRequest -Uri '%url%' -OutFile '%dest%'"
powershell -Command "$ProgressPreference='SilentlyContinue'; Invoke-WebRequest -Uri '%url%' -OutFile '%dest%'"
call :assert "Failed to download %url%"
) else (
call :raise "File %dest% already exists."
)
ENDLOCAL
exit /b 0
:unzip
SETLOCAL EnableDelayedExpansion
set "zipFile=%~1"
set "destDir=%~2"
if not exist "%zipFile%" (
call :raise "Zip file %zipFile% does not exist."
)
if not exist "%destDir%" (
mkdir "%destDir%"
)
powershell -Command "Expand-Archive -Path '%zipFile%' -DestinationPath '%destDir%' -Force"
call :assert "Failed to unzip %zipFile% to %destDir%"
ENDLOCAL
exit /b 0
:ensure-mongodb
SETLOCAL EnableDelayedExpansion
set "mongodbArchive=%ExternalDir%\.tmp\mongodb.zip"
set "mongodbDir=%ExternalDir%\mongodb\bin"
set "expectedDir=%ExternalDir%\mongodb-win32-x86_64-windows-8.0.10"
set "dlLink=https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-8.0.10.zip"
if not exist "%mongodbDir%" (
echo MongoDB not found...
if not exist "%mongodbArchive%" (
echo Downloading MongoDB...
call :download "%dlLink%" "%mongodbArchive%"
)
echo Unzipping MongoDB...
call :unzip "%mongodbArchive%" "%ExternalDir%"
if exist "%expectedDir%" (
move /Y "%expectedDir%" "%ExternalDir%\mongodb"
) else (
call :raise "Expected directory %expectedDir% does not exist after unzipping."
)
)
SET "PATH=%mongodbDir%;%PATH%"
where mongod >nul 2>&1
call :assert "MongoDB executable not found in PATH after setup."
echo MongoDB setup complete.
ENDLOCAL
exit /b 0
:ensure-nodejs
SETLOCAL EnableDelayedExpansion
set "nodejsArchive=%ExternalDir%\.tmp\nodejs.zip"
set "nodejsDir=%ExternalDir%\nodejs"
set "expectedDir=%ExternalDir%\node-v22.16.0-win-x64"
set "dlLink=https://nodejs.org/dist/v22.16.0/node-v22.16.0-win-x64.zip"
if not exist "%nodejsDir%" (
echo Node.js not found...
if not exist "%nodejsArchive%" (
echo Downloading Node.js...
call :download "%dlLink%" "%nodejsArchive%"
)
echo Unzipping Node.js...
call :unzip "%nodejsArchive%" "%ExternalDir%"
if exist "%expectedDir%" (
move /Y "%expectedDir%" "%ExternalDir%\nodejs"
) else (
call :raise "Expected directory %expectedDir% does not exist after unzipping."
)
)
SET "PATH=%nodejsDir%;%PATH%"
where npm >nul 2>&1
call :assert "Node.js executable not found in PATH after setup."
echo Node.js setup complete.
ENDLOCAL
exit /b 0
:ensure-git
SETLOCAL EnableDelayedExpansion
set "gitSelfInstall=%ExternalDir%\.tmp\PortableGit-2.49.0-64-bit.7z.exe"
set "gitDir=%ExternalDir%\git"
set "dlLink=https://github.com/git-for-windows/git/releases/download/v2.49.0.windows.1/PortableGit-2.49.0-64-bit.7z.exe"
if "%~1" neq "" (
where git >nul 2>&1
if %errorlevel% == 0 (
echo Using global Git installation...
set "foundGit=1"
)
)
if not defined foundGit (
if not exist "%gitDir%" (
echo Git not found...
if not exist "%gitSelfInstall%" (
echo Downloading Git...
call :download "%dlLink%" "%gitSelfInstall%"
)
echo Unzipping Git...
start /b /wait "" "%gitSelfInstall%" -o "%gitDir%" -y
if not exist "%gitDir%" (
call :raise "Expected directory %gitDir% does not exist after unzipping."
)
)
SET "PATH=%gitDir%;%PATH%"
where git >nul 2>&1
call :assert "Git executable not found in PATH."
echo Git setup complete.
)
ENDLOCAL
exit /b 0
:ensure-server
SETLOCAL EnableDelayedExpansion
if exist "%ServerDir%" (
echo Server repository already exists, updating...
pushd "%ServerDir%"
git pull
@REM call :assert "Failed to update server repository %ServerRepo% in %ServerDir%"
if %errorlevel% neq 0 (
echo Failed to update server repository in %ServerDir%.
)
popd
echo Server updated in %ServerDir%.
) else (
git clone "%ServerRepo%" "%ServerDir%"
call :assert "Failed to clone server repository %ServerRepo% to %ServerDir%"
echo Server repository cloned to %ServerDir%.
)
if not exist "%ServerDir%\config.json" (
copy "%ServerDir%\config.json.example" "%ServerDir%\config.json"
call :assert "Failed to copy config.json.example to config.json"
)
ENDLOCAL
exit /b 0
:ensure-extras
SETLOCAL EnableDelayedExpansion
set "extrasDir=%ServerDir%\static\data"
set "repoDir=%extrasDir%\0"
set "flagDontDownload=%extrasDir%\.dontDownload"
set "repoUrl=https://openwf.io/0.git"
if exist "%flagDontDownload%" (
echo Skipping extra data download as per flag.
) else (
if exist "%repoDir%" (
echo Extra data repository already exists, updating...
pushd "%repoDir%"
git pull
@REM call :assert "Failed to update repository %repoUrl% in %repoDir%"
if %errorlevel% neq 0 (
echo Failed to update repository in %repoDir%.
)
popd
echo Extra data updated in %repoDir%.
) else (
set /p "downloadExtras=Do you want to download extra data? (y/n): "
if /i "!downloadExtras!"=="y" (
echo Downloading extra data...
git clone "%repoUrl%" "%repoDir%"
call :assert "Failed to clone repository %repoUrl% to %repoDir%"
echo Extra data downloaded to %repoDir%.
) else (
echo Skipping extra data download.
echo . > "%flagDontDownload%"
echo If you want to download it later, delete the file "%flagDontDownload%".
)
)
)
ENDLOCAL
exit /b 0
:ensure-irc
SETLOCAL EnableDelayedExpansion
set "ircPath=%ExternalDir%\IRC\warframe-irc-server.exe"
set "noIrcFlag=%ExternalDir%\.dontDownloadIRC"
set "ircDlLink=https://github.com/Sainan/warframe-irc-server/releases/download/1.3.1/warframe-irc-server.exe"
if exist "%noIrcFlag%" (
echo Skipping IRC server download as per flag.
) else (
if exist "%ircPath%" (
echo IRC server already exists, skipping download.
) else (
set /p "downloadIrc=Do you want to download the IRC server? (y/n): "
if /i "!downloadIrc!"=="y" (
call :download "%ircDlLink%" "%ircPath%"
) else (
echo Skipping IRC server download.
echo . > "%noIrcFlag%"
echo If you want to download it later, delete the file "%noIrcFlag%".
)
)
)
ENDLOCAL
exit /b 0
:main
set gErrorLevel=0
call :parseArgs %*
echo Usage: %0 [options]
echo Options:
echo -externalDir [path] Set the external directory (default: current script directory\external)
echo -serverDir [path] Set the server directory (default: current script directory\SpaceNinjaServer)
echo -serverRepo [url] Set the server repository URL (default: https://openwf.io/SpaceNinjaServer.git)
echo -databasePath [path] Set the MongoDB data directory (default: externalDir\mongodb-data)
echo -ignorePull Ignore pulling updates for the server repository
echo -irc-options [args] Options to pass to the IRC server executable
echo -db-options [args] Options to pass to the MongoDB server executable
echo -server-options [args] Options to pass to the main server
echo ============================================================
if defined ARG[externalDir] (
set "ExternalDir=%ARG[externalDir]%"
) else (
set "ExternalDir=%~dp0\external"
)
if defined ARG[serverDir] (
set "ServerDir=%ARG[serverDir]%"
) else (
set "ServerDir=%~dp0\SpaceNinjaServer"
)
if defined ARG[serverRepo] (
set "ServerRepo=%ARG[serverRepo]%"
) else (
set "ServerRepo=https://openwf.io/SpaceNinjaServer.git"
)
if defined ARG[databasePath] (
set "DatabasePath=%ARG[databasePath]%"
) else (
set "DatabasePath=%ExternalDir%\mongodb-data"
)
echo Using External Directory: %ExternalDir%
echo Using Server Directory: %ServerDir%
echo Using Server Repository: %ServerRepo%
echo Using MongoDB Data Directory: %DatabasePath%
if not exist "%ExternalDir%" (
mkdir "%ExternalDir%"
)
call :ensure-mongodb
call :ensure-nodejs
call :ensure-git
call :ensure-server
call :ensure-extras
call :ensure-irc
pushd "%ServerDir%"
call npm install --omit=dev
call :assert "Failed to install Node.js dependencies."
call npm run build
call :assert "Failed to build the server."
popd
if exist "%ExternalDir%\IRC\warframe-irc-server.exe" (
set "hasIrc=1"
) else (
set "hasIrc=0"
)
if not exist "%DatabasePath%" (
mkdir "%DatabasePath%"
call :assert "Failed to create MongoDB data directory %DatabasePath%"
)
@REM Maybe use docker-compose instead?
if %hasIrc%==1 (
echo Extra Irc options: %ARG[irc-options]%
if defined ARG[irc-options] set "IRC_OPTIONS=%ARG[irc-options]:"=%"
start /D "%ExternalDir%\IRC" "IRC Server" "%ExternalDir%\IRC\warframe-irc-server.exe" %IRC_OPTIONS%
)
echo Starting MongoDB with options: %ARG[db-options]%
if defined ARG[db-options] set "DB_OPTIONS=%ARG[db-options]:"=%"
start /D "%ExternalDir%" "Database" "%ExternalDir%\mongodb\bin\mongod" --dbpath "%DatabasePath%" %DB_OPTIONS%
echo Starting Main Server with options: %ARG[server-options]%
if defined ARG[server-options] set "SERVER_OPTIONS=%ARG[server-options]:"=%"
@REM start /D "%ServerDir%" "Main Server" "%ExternalDir%\nodejs\npm" run start %SERVER_OPTIONS%
@REM exit /b 0
pushd "%ServerDir%"
call npm run start %SERVER_OPTIONS%
popd
exit /b %errorlevel%
:end
ENDLOCAL
pause
exit %gErrorLevel%