enviroment-check.ps1 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. # Check if dependencies are installed
  47. Write-Host "`nChecking project dependencies..." -ForegroundColor Yellow
  48. # 调用 nodejs-dependencies-install.js 脚本进行依赖检查和安装
  49. $nodeDependenciesScript = Join-Path $scriptRoot "nodejs\dependences\nodejs-dependencies-install.js"
  50. if (Test-Path $nodeDependenciesScript) {
  51. node $nodeDependenciesScript
  52. if ($LASTEXITCODE -ne 0) {
  53. Write-Host "[X] Node dependencies check/installation failed" -ForegroundColor Red
  54. exit 1
  55. }
  56. } else {
  57. Write-Host "[X] nodejs-dependencies-install.js not found at: $nodeDependenciesScript" -ForegroundColor Red
  58. Write-Host "[WARN] Continuing without dependency check..." -ForegroundColor Yellow
  59. }
  60. # Check if python is installed
  61. Write-Host "`nChecking if python is installed..." -ForegroundColor Yellow
  62. $pythonVersion = python --version 2>$null
  63. if ($pythonVersion) {
  64. Write-Host "[OK] python: $pythonVersion" -ForegroundColor Green
  65. } else {
  66. Write-Host "[X] python is not installed" -ForegroundColor Red
  67. # Download python
  68. Write-Host "Downloading python..." -ForegroundColor Yellow
  69. npm install -g python
  70. if ($LASTEXITCODE -ne 0) {
  71. Write-Host "[X] python download failed" -ForegroundColor Red
  72. exit 1
  73. }
  74. Write-Host "[OK] python downloaded successfully" -ForegroundColor Green
  75. python --version
  76. }
  77. # check pip is installed
  78. Write-Host "`nChecking pip is installed..." -ForegroundColor Yellow
  79. $pipVersion = pip --version 2>$null
  80. if ($pipVersion) {
  81. Write-Host "[OK] pip: $pipVersion" -ForegroundColor Green
  82. } else {
  83. Write-Host "[X] pip is not installed" -ForegroundColor Red
  84. Write-Host "Installing pip..." -ForegroundColor Yellow
  85. pip install --upgrade pip
  86. if ($LASTEXITCODE -ne 0) {
  87. Write-Host "[X] pip installation failed" -ForegroundColor Red
  88. exit 1
  89. }
  90. Write-Host "[OK] pip installed successfully" -ForegroundColor Green
  91. pip --version
  92. }
  93. #check python virtual environment
  94. Write-Host "`nChecking python virtual environment..." -ForegroundColor Yellow
  95. $arch = if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") { "arm64" } else { "x64" }
  96. $_p = node (Join-Path $scriptRoot "configs\get-python-env-path.js") 2>$null
  97. $venvPath = if ($_p) { $_p.Trim() } else { Join-Path $scriptRoot "python\env-$arch" }
  98. if (Test-Path $venvPath) {
  99. Write-Host "[OK] python virtual environment exists at: $venvPath" -ForegroundColor Green
  100. } else {
  101. Write-Host "[X] python virtual environment is not installed" -ForegroundColor Yellow
  102. Write-Host "Creating python virtual environment..." -ForegroundColor Yellow
  103. python -m venv $venvPath
  104. if ($LASTEXITCODE -ne 0) {
  105. Write-Host "[X] python virtual environment creation failed" -ForegroundColor Red
  106. Write-Host "[WARN] Continuing without virtual environment..." -ForegroundColor Yellow
  107. } else {
  108. Write-Host "[OK] python virtual environment created successfully" -ForegroundColor Green
  109. }
  110. }
  111. # check python dependencies
  112. Write-Host "`nChecking python dependencies..." -ForegroundColor Yellow
  113. # 调用 python-enviroment-install.py 脚本进行依赖检查和安装(安装到虚拟环境,脚本在 python\x64 或 python\arm64 下)
  114. $pythonDependenciesScript = Join-Path $scriptRoot "python\$arch\python-enviroment-install.py"
  115. if ($pythonDependenciesScript -and (Test-Path $pythonDependenciesScript)) {
  116. $env:PYTHON_VENV_PATH = $venvPath; python $pythonDependenciesScript
  117. if ($LASTEXITCODE -ne 0) {
  118. Write-Host "[X] Python dependencies check/installation failed" -ForegroundColor Red
  119. exit 1
  120. }
  121. } else {
  122. Write-Host "[X] python-enviroment-install.py not found at: $pythonDependenciesScript" -ForegroundColor Red
  123. Write-Host "[WARN] Continuing without Python dependency check..." -ForegroundColor Yellow
  124. }
  125. Write-Host "`n================================" -ForegroundColor Cyan
  126. Write-Host "Environment check completed!" -ForegroundColor Green
  127. Write-Host "All dependencies are ready. You can now start the project." -ForegroundColor Green