setup_windows.ps1 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #Requires -Version 5.1
  2. <#
  3. .SYNOPSIS
  4. XLSX Skill — Environment Setup for Windows (Win10/Win11)
  5. .DESCRIPTION
  6. Detects platform, checks and installs all dependencies for the XLSX skill.
  7. Supports China mirror fallback for pip.
  8. #>
  9. param(
  10. [switch]$UseChinaMirror
  11. )
  12. $ErrorActionPreference = "Continue"
  13. function Write-Ok { param($msg) Write-Host " [OK] $msg" -ForegroundColor Green }
  14. function Write-Fail { param($msg) Write-Host " [FAIL] $msg" -ForegroundColor Red }
  15. function Write-Warn { param($msg) Write-Host " [WARN] $msg" -ForegroundColor Yellow }
  16. function Write-Info { param($msg) Write-Host " [->] $msg" -ForegroundColor Cyan }
  17. # 入口存在即选用;运行失败不自动改用系统解释器。
  18. $PreinstalledPython = Get-Command zcode-python -ErrorAction SilentlyContinue
  19. $PyCmd = if ($PreinstalledPython) { $PreinstalledPython.Source } else { $null }
  20. if (-not $PyCmd) {
  21. foreach ($cmd in @("python3", "python", "py")) {
  22. try {
  23. $ver = & $cmd --version 2>&1
  24. if ($ver -match "Python 3") {
  25. $PyCmd = $cmd
  26. Write-Ok "$cmd ($ver)"
  27. break
  28. }
  29. } catch {}
  30. }
  31. }
  32. # 没有解释器时无法运行 Python 引导脚本;必须在网络探测和安装前给出桌面选择。
  33. if (-not $PyCmd -and $env:ZCODE_PREINSTALLED_TOOLS_AVAILABLE) {
  34. Write-Host "预装版本未就绪或已关闭,请在 帮助 → 预装工具 中查看"
  35. Write-Host "Choose to wait, enable Python, or install system Python using the manual steps in env_setup/setup.md."
  36. exit 1
  37. }
  38. # ── Resolve XLSX_SKILL_DIR ──
  39. $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  40. $XLSX_SKILL_DIR = Split-Path -Parent $ScriptDir
  41. $env:XLSX_SKILL_DIR = $XLSX_SKILL_DIR
  42. Write-Host "============================================"
  43. Write-Host " XLSX Skill - Environment Setup"
  44. Write-Host " (Windows)"
  45. Write-Host "============================================"
  46. Write-Host ""
  47. # ── Step 1: Platform Detection ──
  48. $WinVer = [System.Environment]::OSVersion.Version
  49. $WinName = if ($WinVer.Build -ge 22000) { "Windows 11" } elseif ($WinVer.Build -ge 10240) { "Windows 10" } else { "Windows (older)" }
  50. Write-Host "Platform: $WinName (Build $($WinVer.Build)), $([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)"
  51. Write-Host "XLSX_SKILL_DIR=$XLSX_SKILL_DIR"
  52. Write-Host ""
  53. # ── China mirror detection ──
  54. $PipMirrorArgs = @()
  55. if ($UseChinaMirror) {
  56. $global:UseCN = $true
  57. } else {
  58. try {
  59. $null = Invoke-WebRequest -Uri "https://pypi.org" -TimeoutSec 3 -UseBasicParsing -ErrorAction Stop
  60. $global:UseCN = $false
  61. } catch {
  62. Write-Warn "pypi.org unreachable - enabling China mirrors"
  63. $global:UseCN = $true
  64. }
  65. }
  66. if ($global:UseCN) {
  67. $PipMirrorArgs = @("-i", "https://pypi.tuna.tsinghua.edu.cn/simple", "--trusted-host", "pypi.tuna.tsinghua.edu.cn")
  68. Write-Info "China mirrors enabled (pip: tuna)"
  69. Write-Host ""
  70. }
  71. $Errors = 0
  72. # ── Step 2a: Python 3 ──
  73. Write-Host "--- [1/5] Python 3 + pip ---"
  74. if (-not $PyCmd) {
  75. Write-Fail "Python 3 not found"
  76. Write-Info "Install option 1: winget install Python.Python.3.11"
  77. Write-Info "Install option 2: https://www.python.org/downloads/"
  78. Write-Info "Install option 3: choco install python3"
  79. if ($global:UseCN) {
  80. Write-Info "China alt: https://npmmirror.com/mirrors/python/"
  81. }
  82. $Errors++
  83. }
  84. if ($PyCmd) {
  85. try {
  86. $pipVer = & $PyCmd -m pip --version 2>&1
  87. if ($pipVer -match "pip") {
  88. Write-Ok "pip ($pipVer)"
  89. } else { throw "no pip" }
  90. } catch {
  91. Write-Fail "pip not found"
  92. Write-Info "Install: $PyCmd -m ensurepip --upgrade"
  93. $Errors++
  94. }
  95. }
  96. Write-Host ""
  97. # ── Step 2b: Python packages ──
  98. Write-Host "--- [2/5] Python Packages (openpyxl, XlsxWriter) ---"
  99. $PyPkgs = @(
  100. @{ Module = "openpyxl"; Package = "openpyxl" },
  101. @{ Module = "xlsxwriter"; Package = "XlsxWriter" }
  102. )
  103. $MissingPy = @()
  104. if ($PyCmd) {
  105. foreach ($pkg in $PyPkgs) {
  106. try {
  107. $result = & $PyCmd -c "import $($pkg.Module); print(getattr($($pkg.Module), '__version__', 'ok'))" 2>&1
  108. if ($LASTEXITCODE -eq 0) {
  109. Write-Ok "$($pkg.Package) ($result)"
  110. } else { throw "not installed" }
  111. } catch {
  112. Write-Fail "$($pkg.Package) not installed"
  113. $MissingPy += $pkg.Package
  114. }
  115. }
  116. if ($MissingPy.Count -gt 0) {
  117. Write-Info "Installing: $($MissingPy -join ', ')"
  118. $installArgs = @("-m", "pip", "install") + $PipMirrorArgs + $MissingPy
  119. try {
  120. & $PyCmd @installArgs 2>&1 | Out-Null
  121. Write-Ok "Installed: $($MissingPy -join ', ')"
  122. } catch {
  123. Write-Fail "Python package installation failed. Try: $PyCmd -m pip install $($PipMirrorArgs -join ' ') $($MissingPy -join ' ')"
  124. $Errors++
  125. }
  126. }
  127. } else {
  128. Write-Fail "Python packages - skipped (Python not found)"
  129. }
  130. Write-Host ""
  131. # ── LibreOffice:统一检查,受管模式不建议系统安装 ──
  132. $LoHelper = Join-Path $PSScriptRoot "../../../scripts/zcode_libreoffice.py"
  133. Write-Host "--- LibreOffice ---"
  134. # 前面已解析 py/python3/python;固定调用 python 会漏掉支持的解释器。
  135. if ($PyCmd) {
  136. & $PyCmd $LoHelper check
  137. if ($LASTEXITCODE -ne 0) {
  138. Write-Info "Installation guidance (desktop: ask whether to wait or install): $PSScriptRoot/libreoffice-unmanaged.md"
  139. }
  140. } else {
  141. Write-Fail "LibreOffice check - skipped (Python not found)"
  142. }
  143. # ── Step 2d: Font Installation (from CDN) ──
  144. Write-Host "--- [4/5] Font Installation ---"
  145. $FontCdnBase = "https://z-cdn.chatglm.cn/office-skill/fonts"
  146. $FontList = Join-Path $ScriptDir "font_list.txt"
  147. $UserFontDir = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\Fonts"
  148. if (-not (Test-Path $UserFontDir)) { New-Item -ItemType Directory -Path $UserFontDir -Force | Out-Null }
  149. $Marker = Join-Path $UserFontDir ".office-skill-fonts-installed"
  150. if (Test-Path $Marker) {
  151. Write-Ok "Fonts already installed (marker found). To re-install, delete $Marker"
  152. } else {
  153. if (-not (Test-Path $FontList)) {
  154. Write-Fail "Font list not found: $FontList"
  155. $Errors++
  156. } else {
  157. $lines = Get-Content $FontList | Where-Object { $_.Trim() -ne "" }
  158. $Total = $lines.Count
  159. $Installed = 0; $Skipped = 0; $Failed = 0
  160. Write-Info "Downloading $Total fonts from CDN..."
  161. foreach ($relPath in $lines) {
  162. $fname = Split-Path $relPath -Leaf
  163. $dest = Join-Path $UserFontDir $fname
  164. if (Test-Path $dest) {
  165. $Skipped++
  166. continue
  167. }
  168. $encoded = $relPath -replace '\[','%5B' -replace '\]','%5D' -replace ' ','%20'
  169. $url = "$FontCdnBase/$encoded"
  170. try {
  171. Invoke-WebRequest -Uri $url -OutFile $dest -TimeoutSec 30 -ErrorAction Stop
  172. $regPath = "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
  173. $null = New-ItemProperty -Path $regPath -Name $fname -Value $dest -PropertyType String -Force -ErrorAction SilentlyContinue
  174. $Installed++
  175. } catch {
  176. Write-Warn "Failed to download: $relPath"
  177. $Failed++
  178. Remove-Item $dest -Force -ErrorAction SilentlyContinue
  179. }
  180. }
  181. if ($Failed -eq 0) {
  182. New-Item -ItemType File -Path $Marker -Force | Out-Null
  183. Write-Ok "Fonts: $Installed newly installed, $Skipped already present (target: $UserFontDir)"
  184. } else {
  185. Write-Warn "Fonts: $Installed installed, $Skipped skipped, $Failed failed (marker not written, will retry next run)"
  186. $Errors++
  187. }
  188. }
  189. }
  190. # Set XLSX_FONTS_DIR to user font directory (fonts are now installed there)
  191. $XLSX_FONTS_DIR = $UserFontDir
  192. $env:XLSX_FONTS_DIR = $XLSX_FONTS_DIR
  193. Write-Host ""
  194. # ── Step 2e: CJK Font Verification ──
  195. Write-Host "--- [5/5] CJK Font Verification ---"
  196. $CjkFound = $false
  197. $FontsDir = Join-Path $env:WINDIR "Fonts"
  198. $UserFontDir2 = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\Fonts"
  199. $CjkFonts = @("NotoSansSC[wght].ttf", "NotoSerifSC-Regular.ttf")
  200. foreach ($f in $CjkFonts) {
  201. if ((Test-Path (Join-Path $FontsDir $f)) -or (Test-Path (Join-Path $UserFontDir2 $f))) {
  202. Write-Ok "CJK font found: $f"
  203. $CjkFound = $true
  204. break
  205. }
  206. }
  207. if (-not $CjkFound) {
  208. Write-Warn "No CJK font verified yet - fonts were just installed, restart may be needed"
  209. }
  210. Write-Host ""
  211. # ── Summary ──
  212. Write-Host "============================================"
  213. if ($Errors -eq 0) {
  214. Write-Host " All dependencies OK."
  215. } else {
  216. Write-Host " $Errors issue(s) found. Fix them above."
  217. }
  218. Write-Host " XLSX_SKILL_DIR=$XLSX_SKILL_DIR"
  219. Write-Host " XLSX_FONTS_DIR=$XLSX_FONTS_DIR (user font directory)"
  220. Write-Host "============================================"