param([switch]$Build, [string]$SecretsFile = (Join-Path $PSScriptRoot 'secrets.psd1')) . (Join-Path $PSScriptRoot 'common.ps1') $settings = Read-LocalSettings $SecretsFile New-Item -ItemType Directory -Path $runtimePath -Force | Out-Null $existing = Get-LocalServerProcess if ($existing) { throw "The local server is already running with PID $($existing.ProcessId)." } $jarPath = Join-Path $repoPath 'yudao-server/target/yudao-server.jar' if ($Build -or -not (Test-Path -LiteralPath $jarPath)) { Push-Location $repoPath try { & mvn -B -ntp -pl yudao-server -am package -DskipTests if ($LASTEXITCODE -ne 0) { throw 'Maven build failed.' } } finally { Pop-Location } } $listener = Get-NetTCPConnection -State Listen -LocalPort $settings.ServerPort -ErrorAction SilentlyContinue if ($listener) { throw "Port $($settings.ServerPort) is already in use. Set a different ServerPort in secrets.psd1." } Invoke-LocalMySql $settings 'SELECT 1 FROM system_users LIMIT 1;' $settings.DatabaseName | Out-Null $javaExe = Resolve-LocalProgram $settings.JavaExe 'java.exe' $environment = @{ ELDER_DB_HOST = [string]$settings.DatabaseHost ELDER_DB_PORT = [string]$settings.DatabasePort ELDER_DB_NAME = [string]$settings.DatabaseName ELDER_DB_USER = [string]$settings.DatabaseUser ELDER_DB_PASSWORD = [string]$settings.DatabasePassword ELDER_REDIS_HOST = [string]$settings.RedisHost ELDER_REDIS_PORT = [string]$settings.RedisPort ELDER_REDIS_DATABASE = [string]$settings.RedisDatabase SPRING_REDIS_PASSWORD = $(if ($settings.RedisPassword) { [string]$settings.RedisPassword } else { $null }) ELDER_SERVER_PORT = [string]$settings.ServerPort ELDER_LOG_FILE = Join-Path $runtimePath 'application.log' } $previousEnvironment = @{} try { foreach ($key in $environment.Keys) { $previousEnvironment[$key] = [Environment]::GetEnvironmentVariable($key, 'Process') if ($null -eq $environment[$key]) { Remove-Item -LiteralPath "Env:\$key" -ErrorAction SilentlyContinue } else { [Environment]::SetEnvironmentVariable($key, $environment[$key], 'Process') } } $server = Start-Process -FilePath $javaExe -ArgumentList @('-Xms256m', '-Xmx1024m', '-Dfile.encoding=UTF-8', '-jar', ('"' + $jarPath + '"'), '--spring.profiles.active=local,desktop') ` -WorkingDirectory $repoPath -WindowStyle Hidden -PassThru ` -RedirectStandardOutput (Join-Path $runtimePath 'stdout.log') -RedirectStandardError (Join-Path $runtimePath 'stderr.log') } finally { foreach ($key in $previousEnvironment.Keys) { if ($null -eq $previousEnvironment[$key]) { Remove-Item -LiteralPath "Env:\$key" -ErrorAction SilentlyContinue } else { [Environment]::SetEnvironmentVariable($key, $previousEnvironment[$key], 'Process') } } } [System.IO.File]::WriteAllText((Join-Path $runtimePath 'server.pid'), [string]$server.Id) Write-Output "Started PID $($server.Id). API docs: http://127.0.0.1:$($settings.ServerPort)/doc.html" Write-Output "Check health at http://127.0.0.1:$($settings.ServerPort)/actuator/health and logs in $runtimePath."