enviroment-check.ps1 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Environment check script
  2. # 设置控制台编码为 UTF-8,避免乱码
  3. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  4. $OutputEncoding = [System.Text.Encoding]::UTF8
  5. chcp 65001 | Out-Null
  6. # 脚本所在目录(部分调用方式下 MyCommand.Path 可能为 null,使用 $PSScriptRoot 或当前目录兜底)
  7. $scriptRoot = $PSScriptRoot
  8. if (-not $scriptRoot) { $scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path }
  9. if (-not $scriptRoot) { $scriptRoot = (Get-Location).Path }
  10. Write-Host "Checking development environment..." -ForegroundColor Cyan
  11. Write-Host "================================" -ForegroundColor Cyan
  12. # Check Node.js
  13. Write-Host "`nChecking Node.js..." -ForegroundColor Yellow
  14. $nodeVersion = node --version 2>$null
  15. if ($nodeVersion) {
  16. Write-Host "[OK] Node.js: $nodeVersion" -ForegroundColor Green
  17. } else {
  18. Write-Host "[X] Node.js is not installed" -ForegroundColor Red
  19. # Download nodejs
  20. Write-Host "Downloading Node.js..." -ForegroundColor Yellow
  21. npm install -g node
  22. if ($LASTEXITCODE -ne 0) {
  23. Write-Host "[X] Node.js download failed" -ForegroundColor Red
  24. exit 1
  25. }
  26. Write-Host "[OK] Node.js downloaded successfully" -ForegroundColor Green
  27. node --version
  28. }
  29. # Check npm
  30. Write-Host "`nChecking npm..." -ForegroundColor Yellow
  31. $npmVersion = npm --version 2>$null
  32. if ($npmVersion) {
  33. Write-Host "[OK] npm: $npmVersion" -ForegroundColor Green
  34. } else {
  35. Write-Host "[X] npm is not installed" -ForegroundColor Red
  36. # Download npm
  37. Write-Host "Downloading npm..." -ForegroundColor Yellow
  38. npm install -g npm
  39. if ($LASTEXITCODE -ne 0) {
  40. Write-Host "[X] npm download failed" -ForegroundColor Red
  41. exit 1
  42. }
  43. Write-Host "[OK] npm downloaded successfully" -ForegroundColor Green
  44. npm --version
  45. }
  46. # 根据当前架构确定 x64 或 arm64
  47. $arch = if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") { "arm64" } else { "x64" }
  48. # Check if dependencies are installed
  49. Write-Host "`nChecking project dependencies..." -ForegroundColor Yellow
  50. # 调用对应架构的 nodejs-dependencies-install.js 进行依赖检查和安装
  51. $nodeDependenciesScript = Join-Path $scriptRoot "nodejs\dependences\$arch\nodejs-dependencies-install.js"
  52. if (Test-Path $nodeDependenciesScript) {
  53. node $nodeDependenciesScript
  54. if ($LASTEXITCODE -ne 0) {
  55. Write-Host "[X] Node dependencies check/installation failed" -ForegroundColor Red
  56. exit 1
  57. }
  58. # 生成 x64 和 arm64 两份 dependencies.txt
  59. $generateScript = Join-Path $scriptRoot "nodejs\dependences\generate-nodejs-dependencies.js"
  60. if (Test-Path $generateScript) {
  61. node $generateScript
  62. }
  63. } else {
  64. Write-Host "[X] nodejs-dependencies-install.js not found at: $nodeDependenciesScript" -ForegroundColor Red
  65. Write-Host "[WARN] Continuing without dependency check..." -ForegroundColor Yellow
  66. }
  67. # Check if python is installed
  68. Write-Host "`nChecking if python is installed..." -ForegroundColor Yellow
  69. $pythonVersion = python --version 2>$null
  70. if ($pythonVersion) {
  71. Write-Host "[OK] python: $pythonVersion" -ForegroundColor Green
  72. } else {
  73. Write-Host "[X] python is not installed" -ForegroundColor Red
  74. # Download python
  75. Write-Host "Downloading python..." -ForegroundColor Yellow
  76. npm install -g python
  77. if ($LASTEXITCODE -ne 0) {
  78. Write-Host "[X] python download failed" -ForegroundColor Red
  79. exit 1
  80. }
  81. Write-Host "[OK] python downloaded successfully" -ForegroundColor Green
  82. python --version
  83. }
  84. # check pip is installed
  85. Write-Host "`nChecking pip is installed..." -ForegroundColor Yellow
  86. $pipVersion = pip --version 2>$null
  87. if ($pipVersion) {
  88. Write-Host "[OK] pip: $pipVersion" -ForegroundColor Green
  89. } else {
  90. Write-Host "[X] pip is not installed" -ForegroundColor Red
  91. Write-Host "Installing pip..." -ForegroundColor Yellow
  92. pip install --upgrade pip
  93. if ($LASTEXITCODE -ne 0) {
  94. Write-Host "[X] pip installation failed" -ForegroundColor Red
  95. exit 1
  96. }
  97. Write-Host "[OK] pip installed successfully" -ForegroundColor Green
  98. pip --version
  99. }
  100. #check python virtual environment(venv 在 python/x64/env 或 python/arm64/env)
  101. Write-Host "`nChecking python virtual environment..." -ForegroundColor Yellow
  102. $_p = node (Join-Path $scriptRoot "configs\get-python-env-path.js") 2>$null
  103. $venvPath = if ($_p -and $_p -ne "undefined") { $_p.Trim() } else { Join-Path $scriptRoot "python\$arch\env" }
  104. if (Test-Path $venvPath) {
  105. Write-Host "[OK] python virtual environment exists at: $venvPath" -ForegroundColor Green
  106. } else {
  107. Write-Host "[X] python virtual environment is not installed" -ForegroundColor Yellow
  108. Write-Host "Creating python virtual environment..." -ForegroundColor Yellow
  109. python -m venv $venvPath
  110. if ($LASTEXITCODE -ne 0) {
  111. Write-Host "[X] python virtual environment creation failed" -ForegroundColor Red
  112. Write-Host "[WARN] Continuing without virtual environment..." -ForegroundColor Yellow
  113. } else {
  114. Write-Host "[OK] python virtual environment created successfully" -ForegroundColor Green
  115. }
  116. }
  117. # check python dependencies
  118. Write-Host "`nChecking python dependencies..." -ForegroundColor Yellow
  119. # 调用 python-enviroment-install.py 脚本进行依赖检查和安装(安装到虚拟环境,脚本在 python\x64 或 python\arm64 下)
  120. $pythonDependenciesScript = Join-Path $scriptRoot "python\$arch\python-enviroment-install.py"
  121. if ($pythonDependenciesScript -and (Test-Path $pythonDependenciesScript)) {
  122. $env:PYTHON_VENV_PATH = $venvPath; python $pythonDependenciesScript
  123. if ($LASTEXITCODE -ne 0) {
  124. Write-Host "[X] Python dependencies check/installation failed" -ForegroundColor Red
  125. exit 1
  126. }
  127. } else {
  128. Write-Host "[X] python-enviroment-install.py not found at: $pythonDependenciesScript" -ForegroundColor Red
  129. Write-Host "[WARN] Continuing without Python dependency check..." -ForegroundColor Yellow
  130. }
  131. Write-Host "`n================================" -ForegroundColor Cyan
  132. Write-Host "Environment check completed!" -ForegroundColor Green
  133. Write-Host "All dependencies are ready. You can now start the project." -ForegroundColor Green