param( [switch]$Force = $false, [switch]$Rebuild = $false ) $ErrorActionPreference = "Stop" Write-Host "====================================" -ForegroundColor Cyan Write-Host " Hua.Todo.Host Start Script" -ForegroundColor Cyan Write-Host "====================================" -ForegroundColor Cyan Write-Host "" $hostProjectPath = Join-Path $PSScriptRoot "src\Hua.Todo.Host" $hostProjectFile = Join-Path $hostProjectPath "Hua.Todo.Host.csproj" $launchSettingsFile = Join-Path $hostProjectPath "Properties\launchSettings.json" $outputDir = Join-Path $hostProjectPath "bin\Debug\net10.0" $hostExe = Join-Path $outputDir "Hua.Todo.Host.exe" if (!(Test-Path $hostProjectFile)) { Write-Host "ERROR: Host project file not found: $hostProjectFile" -ForegroundColor Red exit 1 } if (!(Get-Command "dotnet" -ErrorAction SilentlyContinue)) { Write-Host "ERROR: dotnet not found, please install .NET SDK first" -ForegroundColor Red exit 1 } $envSettings = @{} $appUrl = "http://localhost:5173" if (Test-Path $launchSettingsFile) { try { $launchSettings = Get-Content $launchSettingsFile -Raw | ConvertFrom-Json $profile = $launchSettings.profiles.http if ($profile) { $appUrl = $profile.applicationUrl if ($profile.environmentVariables) { foreach ($key in $profile.environmentVariables.PSObject.Properties.Name) { $envSettings[$key] = $profile.environmentVariables.$key } } } Write-Host "OK: Loaded launch settings from $launchSettingsFile" -ForegroundColor Green } catch { Write-Host "WARNING: Failed to read launch settings, using defaults: $_" -ForegroundColor Yellow } } else { Write-Host "WARNING: launchSettings.json not found, using defaults" -ForegroundColor Yellow } Write-Host "[1/2] Checking if Hua.Todo.Host is running..." -ForegroundColor Yellow $runningProcesses = Get-CimInstance Win32_Process -Filter "Name='dotnet.exe'" | Where-Object { $_.CommandLine -and ( $_.CommandLine -like "*Hua.Todo.Host*" -or $_.CommandLine -like "*Hua.Todo.Host.dll*" ) } if ($runningProcesses) { $pids = ($runningProcesses.ProcessId | Sort-Object) -join ", " Write-Host "WARNING: Hua.Todo.Host may be running" -ForegroundColor Yellow Write-Host " Process IDs: ($pids)" -ForegroundColor Gray if ($Force) { Write-Host "" Write-Host "Restarting..." -ForegroundColor Yellow try { $runningProcesses | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction Stop } Write-Host "Old process stopped" -ForegroundColor Green Start-Sleep -Seconds 1 } catch { Write-Host "ERROR: Failed to stop process: $_" -ForegroundColor Red exit 1 } } else { Write-Host " Use -Force to restart" -ForegroundColor Gray Write-Host "" Write-Host "API Address:" -ForegroundColor Yellow Write-Host " $appUrl" -ForegroundColor Cyan Write-Host "" exit 0 } } else { Write-Host "OK: Hua.Todo.Host is not running" -ForegroundColor Green Write-Host "" } Write-Host "[2/2] Starting Hua.Todo.Host..." -ForegroundColor Yellow Write-Host "Working directory: $hostProjectPath" -ForegroundColor Gray $needBuild = $Rebuild -or !(Test-Path $hostExe) if ($needBuild) { Write-Host "Building..." -ForegroundColor Yellow try { $buildArgs = @("build", $hostProjectFile, "--configuration", "Debug") if ($Rebuild) { $buildArgs += "--no-incremental" } $buildProcess = Start-Process -FilePath "dotnet" -ArgumentList $buildArgs -WorkingDirectory $hostProjectPath -Wait -PassThru -NoNewWindow if ($buildProcess.ExitCode -ne 0) { Write-Host "ERROR: Build failed, exit code: $($buildProcess.ExitCode)" -ForegroundColor Red exit $buildProcess.ExitCode } Write-Host "Build succeeded" -ForegroundColor Green } catch { Write-Host "ERROR: Build failed: $_" -ForegroundColor Red exit 1 } } else { Write-Host "OK: Found compiled executable, skipping build" -ForegroundColor Green } Write-Host "Starting service..." -ForegroundColor Green Write-Host "" try { foreach ($key in $envSettings.Keys) { [Environment]::SetEnvironmentVariable($key, $envSettings[$key]) } [Environment]::SetEnvironmentVariable("ASPNETCORE_URLS", $appUrl) $hostProcess = Start-Process -FilePath $hostExe -WorkingDirectory $outputDir -PassThru Write-Host "OK: Hua.Todo.Host started" -ForegroundColor Green Write-Host " Process ID: ($($hostProcess.Id))" -ForegroundColor Gray Write-Host "" Write-Host "API Address:" -ForegroundColor Yellow Write-Host " $appUrl" -ForegroundColor Cyan Write-Host "" Write-Host "Tips:" -ForegroundColor Yellow Write-Host " - Use -Force to restart" -ForegroundColor Gray Write-Host " - Use -Rebuild to force full rebuild" -ForegroundColor Gray Write-Host "" } catch { Write-Host "ERROR: Failed to start: $_" -ForegroundColor Red exit 1 }