aidop_init.ps1 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. param(
  2. [switch]$StartServices
  3. )
  4. $ErrorActionPreference = "Stop"
  5. function Write-Step($msg) {
  6. Write-Host "[AIDOP INIT] $msg" -ForegroundColor Cyan
  7. }
  8. function Write-Ok($msg) {
  9. Write-Host "[ OK ] $msg" -ForegroundColor Green
  10. }
  11. function Write-Warn($msg) {
  12. Write-Host "[ WARN ] $msg" -ForegroundColor Yellow
  13. }
  14. function Write-Fail($msg) {
  15. Write-Host "[ FAIL ] $msg" -ForegroundColor Red
  16. }
  17. function Test-Command($name) {
  18. return [bool](Get-Command $name -ErrorAction SilentlyContinue)
  19. }
  20. function Invoke-Checked($command, [string[]]$arguments) {
  21. & $command @arguments
  22. if ($LASTEXITCODE -ne 0) {
  23. throw "$command $($arguments -join ' ') failed with exit code $LASTEXITCODE"
  24. }
  25. }
  26. # ---------- paths ----------
  27. $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  28. $platformRoot = Split-Path -Parent $scriptDir
  29. $repoRoot = Split-Path -Parent $platformRoot
  30. $webDir = Join-Path $repoRoot "Web"
  31. $serverDir = Join-Path $repoRoot "server"
  32. $entryDir = Join-Path $serverDir "Admin.NET.Web.Entry"
  33. $slnFile = Join-Path $serverDir "Admin.NET.sln"
  34. Write-Step "Repo root: $repoRoot"
  35. Write-Host ""
  36. # ---------- 1. directory sanity ----------
  37. Write-Step "1/6 Checking directory structure..."
  38. $dirChecks = @(
  39. @{ Path = $webDir; Label = "Web (frontend)" },
  40. @{ Path = $serverDir; Label = "server (backend)" },
  41. @{ Path = $entryDir; Label = "Admin.NET.Web.Entry (startup project)" },
  42. @{ Path = $slnFile; Label = "Admin.NET.sln" }
  43. )
  44. foreach ($chk in $dirChecks) {
  45. if (-not (Test-Path $chk.Path)) {
  46. throw "$($chk.Label) not found: $($chk.Path)"
  47. }
  48. Write-Ok $chk.Label
  49. }
  50. # ---------- 2. dotnet ----------
  51. Write-Step "2/6 Checking .NET SDK..."
  52. if (-not (Test-Command "dotnet")) {
  53. Write-Fail "dotnet CLI not found."
  54. throw "Please install .NET 10 SDK from https://dotnet.microsoft.com/download"
  55. }
  56. $dotnetVersion = (dotnet --version).Trim()
  57. Write-Ok "dotnet $dotnetVersion"
  58. if (-not $dotnetVersion.StartsWith("10.")) {
  59. Write-Warn "global.json requires SDK 10.0.x but found $dotnetVersion — restore/build may fail."
  60. }
  61. # ---------- 3. node / pnpm ----------
  62. Write-Step "3/6 Checking Node.js & pnpm..."
  63. if (-not (Test-Command "node")) {
  64. throw "node not found. Please install Node.js >= 18 from https://nodejs.org"
  65. }
  66. $nodeVersion = (node --version).Trim()
  67. Write-Ok "node $nodeVersion"
  68. $hasPnpm = Test-Command "pnpm"
  69. if ($hasPnpm) {
  70. $pnpmVersion = (pnpm --version).Trim()
  71. Write-Ok "pnpm $pnpmVersion"
  72. } else {
  73. Write-Warn "pnpm not found; will fall back to npm. Recommend: npm install -g pnpm"
  74. }
  75. # ---------- 4. frontend deps ----------
  76. Write-Step "4/6 Installing frontend dependencies..."
  77. Push-Location $webDir
  78. try {
  79. if ($hasPnpm) {
  80. Invoke-Checked "pnpm" @("install")
  81. } else {
  82. Invoke-Checked "npm" @("install", "--legacy-peer-deps")
  83. }
  84. if ($hasPnpm) {
  85. Invoke-Checked "pnpm" @("run", "prepare")
  86. } else {
  87. Invoke-Checked "npm" @("run", "prepare")
  88. }
  89. Write-Ok "Frontend dependencies installed & git hooks prepared"
  90. } finally {
  91. Pop-Location
  92. }
  93. # ---------- 5. backend restore & build ----------
  94. Write-Step "5/6 Restoring & building backend solution..."
  95. Invoke-Checked "dotnet" @("restore", $slnFile)
  96. Write-Ok "dotnet restore succeeded"
  97. Invoke-Checked "dotnet" @("build", $slnFile, "-c", "Debug", "--no-restore")
  98. Write-Ok "dotnet build succeeded"
  99. # ---------- 6. summary ----------
  100. Write-Host ""
  101. Write-Step "6/6 Environment summary"
  102. Write-Host " -----------------------------------------------"
  103. Write-Host " .NET SDK : $dotnetVersion"
  104. Write-Host " Node.js : $nodeVersion"
  105. if ($hasPnpm) {
  106. Write-Host " pnpm : $pnpmVersion"
  107. }
  108. Write-Host " Backend entry : $entryDir"
  109. Write-Host " Backend URL : http://localhost:5005"
  110. Write-Host " Frontend dir : $webDir"
  111. Write-Host " Frontend URL : http://localhost:8888"
  112. Write-Host " -----------------------------------------------"
  113. Write-Host ""
  114. # ---------- optional: start services ----------
  115. if ($StartServices) {
  116. Write-Step "Starting backend (dotnet run)..."
  117. Start-Process powershell -ArgumentList "-NoExit", "-Command", "Set-Location '$entryDir'; dotnet run"
  118. Write-Step "Starting frontend (pnpm dev)..."
  119. if ($hasPnpm) {
  120. Start-Process powershell -ArgumentList "-NoExit", "-Command", "Set-Location '$webDir'; pnpm dev"
  121. } else {
  122. Start-Process powershell -ArgumentList "-NoExit", "-Command", "Set-Location '$webDir'; npm run dev"
  123. }
  124. Write-Host ""
  125. Write-Ok "Services launched in separate windows."
  126. Write-Host " Backend -> http://localhost:5005"
  127. Write-Host " Frontend -> http://localhost:8888"
  128. Write-Host ""
  129. Write-Host " Tip: start backend first; frontend dev-server proxies API to :5005."
  130. }
  131. Write-Ok "Initialization complete."