enviroment-check.ps1 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Environment check - use local nodejs/node and python/x64 only
  2. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  3. $OutputEncoding = [System.Text.Encoding]::UTF8
  4. chcp 65001 | Out-Null
  5. $scriptRoot = $PSScriptRoot
  6. if (-not $scriptRoot) { $scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path }
  7. if (-not $scriptRoot) { $scriptRoot = (Get-Location).Path }
  8. $nodeDir = Join-Path $scriptRoot 'nodejs\node'
  9. $nodeExe = Join-Path $nodeDir 'node.exe'
  10. $npmCmd = Join-Path $nodeDir 'npm.cmd'
  11. if (-not (Test-Path $nodeExe)) { $nodeExe = $null }
  12. if (-not (Test-Path $npmCmd)) { $npmCmd = $null }
  13. $arch = if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') { 'arm64' } else { 'x64' }
  14. $pythonRoot = Join-Path $scriptRoot ('python\' + $arch)
  15. # Python 优先在 python/x64/py 下查找
  16. $pythonExe = $null
  17. foreach ($p in @(
  18. (Join-Path $pythonRoot 'py\python.exe'),
  19. (Join-Path $pythonRoot 'env\Scripts\python.exe'),
  20. (Join-Path $pythonRoot 'python.exe')
  21. )) {
  22. if (Test-Path $p) { $pythonExe = $p; break }
  23. }
  24. Write-Host ''
  25. Write-Host ('Checking development environment (local nodejs/node and python/' + $arch + ')...') -ForegroundColor Cyan
  26. Write-Host '================================' -ForegroundColor Cyan
  27. Write-Host ''
  28. Write-Host 'Checking Node.js (local nodejs/node)...' -ForegroundColor Yellow
  29. if (-not $nodeExe) {
  30. Write-Host ('[X] Local Node not found. Put Node in: ' + $nodeDir) -ForegroundColor Red
  31. exit 1
  32. }
  33. $nodeVersion = & $nodeExe --version 2>$null
  34. if ($nodeVersion) {
  35. Write-Host ('[OK] Node.js: ' + $nodeVersion) -ForegroundColor Green
  36. } else {
  37. Write-Host '[X] Local node.exe failed' -ForegroundColor Red
  38. exit 1
  39. }
  40. Write-Host ''
  41. Write-Host 'Checking npm (local)...' -ForegroundColor Yellow
  42. if (-not $npmCmd) {
  43. Write-Host ('[X] Local npm not found: ' + $nodeDir + '\npm.cmd') -ForegroundColor Red
  44. exit 1
  45. }
  46. $npmVersion = & $npmCmd --version 2>$null
  47. if ($npmVersion) {
  48. Write-Host ('[OK] npm: ' + $npmVersion) -ForegroundColor Green
  49. } else {
  50. Write-Host '[WARN] npm version check skipped' -ForegroundColor Yellow
  51. }
  52. Write-Host ''
  53. Write-Host 'Checking Node.js dependencies (nodejs/node vs dependencies.txt)...' -ForegroundColor Yellow
  54. $nodeModulesPath = Join-Path $nodeDir 'node_modules'
  55. $depsTxt = Join-Path $scriptRoot ('nodejs\dependences\' + $arch + '\dependencies.txt')
  56. $nodeDependenciesScript = Join-Path $scriptRoot ('nodejs\dependences\' + $arch + '\nodejs-dependencies-install.js')
  57. if (-not (Test-Path $nodeModulesPath)) {
  58. Write-Host '[X] nodejs/node/node_modules not found' -ForegroundColor Red
  59. exit 1
  60. }
  61. if (-not (Test-Path $depsTxt)) {
  62. Write-Host '[X] dependencies.txt not found: ' $depsTxt -ForegroundColor Red
  63. exit 1
  64. }
  65. $requiredLines = Get-Content $depsTxt -Encoding UTF8 | Where-Object { $_.Trim() -and -not $_.Trim().StartsWith('#') }
  66. $missing = @()
  67. foreach ($line in $requiredLines) {
  68. $pkgSpec = $line.Trim().Split('==', 2)[0].Trim()
  69. if (-not $pkgSpec) { continue }
  70. if ($pkgSpec -match '^@([^/]+)/(.+)$') {
  71. $subPath = Join-Path $nodeModulesPath ('@' + $Matches[1])
  72. $pkgPath = Join-Path $subPath $Matches[2]
  73. } else {
  74. $pkgPath = Join-Path $nodeModulesPath $pkgSpec
  75. }
  76. if (-not (Test-Path $pkgPath)) { $missing += $pkgSpec }
  77. }
  78. if ($missing.Count -gt 0) {
  79. Write-Host ('[X] Missing ' + $missing.Count + ' package(s) in nodejs/node/node_modules (per dependencies.txt):') -ForegroundColor Red
  80. $missing | Select-Object -First 15 | ForEach-Object { Write-Host ' - ' $_ -ForegroundColor Red }
  81. if ($missing.Count -gt 15) { Write-Host (' ... and ' + ($missing.Count - 15) + ' more') -ForegroundColor Red }
  82. if (Test-Path $nodeDependenciesScript) {
  83. Write-Host 'Running nodejs-dependencies-install.js to install missing...' -ForegroundColor Yellow
  84. & $nodeExe $nodeDependenciesScript
  85. if ($LASTEXITCODE -ne 0) {
  86. Write-Host '[X] Node dependencies installation failed' -ForegroundColor Red
  87. exit 1
  88. }
  89. } else {
  90. exit 1
  91. }
  92. } else {
  93. Write-Host ('[OK] Node dependencies match dependencies.txt (' + $requiredLines.Count + ' packages)') -ForegroundColor Green
  94. }
  95. $generateScript = Join-Path $scriptRoot 'nodejs\dependences\generate-nodejs-dependencies.js'
  96. if (Test-Path $generateScript) {
  97. & $nodeExe $generateScript 2>$null
  98. }
  99. Write-Host ''
  100. Write-Host ('Checking Python (local python/' + $arch + ')...') -ForegroundColor Yellow
  101. if (-not $pythonExe) {
  102. Write-Host ('[X] Local Python not found. Put Python in: ' + $pythonRoot) -ForegroundColor Red
  103. exit 1
  104. }
  105. # Python --version 常输出到 stderr,需合并 stdout+stderr 再判断
  106. $pythonVersion = (& $pythonExe --version 2>&1) | Out-String
  107. $pythonVersion = $pythonVersion.Trim()
  108. if (-not $pythonVersion) { $pythonVersion = $null }
  109. if ($pythonVersion -or (($LASTEXITCODE -eq 0) -and (Test-Path $pythonExe))) {
  110. if (-not $pythonVersion) { $pythonVersion = '(version ok)' }
  111. Write-Host ('[OK] python: ' + $pythonVersion) -ForegroundColor Green
  112. } else {
  113. Write-Host '[X] Local python failed' -ForegroundColor Red
  114. exit 1
  115. }
  116. Write-Host ''
  117. Write-Host 'Checking pip...' -ForegroundColor Yellow
  118. $pipVersion = (& $pythonExe -m pip --version 2>&1) | Out-String
  119. $pipVersion = $pipVersion.Trim()
  120. if ($pipVersion) {
  121. Write-Host ('[OK] pip: ' + $pipVersion) -ForegroundColor Green
  122. } else {
  123. Write-Host '[X] pip not available' -ForegroundColor Red
  124. Write-Host 'Installing pip (ensurepip)...' -ForegroundColor Yellow
  125. & $pythonExe -m ensurepip --upgrade 2>$null
  126. if ($LASTEXITCODE -eq 0) {
  127. Write-Host '[OK] pip ready' -ForegroundColor Green
  128. } else {
  129. Write-Host '[WARN] ensurepip failed (embedded Python may not include it). Continuing; venv step will create env with pip.' -ForegroundColor Yellow
  130. }
  131. }
  132. Write-Host ''
  133. Write-Host 'Checking python virtual environment...' -ForegroundColor Yellow
  134. $venvPath = Join-Path $scriptRoot ('python\' + $arch + '\env')
  135. $venvPython = Join-Path $venvPath 'Scripts\python.exe'
  136. if (Test-Path $venvPython) {
  137. Write-Host ('[OK] python venv at: ' + $venvPath) -ForegroundColor Green
  138. } else {
  139. Write-Host '[X] python virtual environment not found' -ForegroundColor Yellow
  140. Write-Host 'Creating python virtual environment...' -ForegroundColor Yellow
  141. & $pythonExe -m venv $venvPath
  142. if ($LASTEXITCODE -ne 0) {
  143. Write-Host '[X] venv creation failed' -ForegroundColor Red
  144. Write-Host '[WARN] Continuing without venv...' -ForegroundColor Yellow
  145. } else {
  146. Write-Host '[OK] venv created' -ForegroundColor Green
  147. }
  148. }
  149. Write-Host ''
  150. Write-Host 'Checking python dependencies...' -ForegroundColor Yellow
  151. $pythonDependenciesScript = Join-Path $scriptRoot ('python\' + $arch + '\python-enviroment-install.py')
  152. if (Test-Path $pythonDependenciesScript) {
  153. $env:PYTHON_VENV_PATH = $venvPath
  154. # 始终用本地 Python (py/python.exe) 运行,避免 venv 内脚本指向系统 Python (如 C:\programs\python)
  155. & $pythonExe $pythonDependenciesScript
  156. if ($LASTEXITCODE -ne 0) {
  157. Write-Host '[X] Python dependencies check/installation failed' -ForegroundColor Red
  158. exit 1
  159. }
  160. } else {
  161. Write-Host ('[X] Not found: ' + $pythonDependenciesScript) -ForegroundColor Red
  162. Write-Host '[WARN] Continuing without Python dependency check...' -ForegroundColor Yellow
  163. }
  164. Write-Host ''
  165. Write-Host '================================' -ForegroundColor Cyan
  166. Write-Host 'Environment check completed!' -ForegroundColor Green
  167. Write-Host 'All dependencies are ready. You can now start the project.' -ForegroundColor Green