setup_windows.ps1 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #Requires -Version 5.1
  2. <#
  3. .SYNOPSIS
  4. DOCX Skill — Environment Setup for Windows (Win10/Win11)
  5. .DESCRIPTION
  6. Detects platform, checks and installs all dependencies for the DOCX skill.
  7. Supports China mirror fallback for pip/npm.
  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 DOCX_SKILL_DIR ──
  39. $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  40. $DOCX_SKILL_DIR = Split-Path -Parent $ScriptDir
  41. $env:DOCX_SKILL_DIR = $DOCX_SKILL_DIR
  42. Write-Host "============================================"
  43. Write-Host " DOCX 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 "DOCX_SKILL_DIR=$DOCX_SKILL_DIR"
  52. Write-Host ""
  53. # ── China mirror detection ──
  54. $PipMirrorArgs = @()
  55. $NpmMirrorArgs = @()
  56. if ($UseChinaMirror) {
  57. $global:UseCN = $true
  58. } else {
  59. try {
  60. $null = Invoke-WebRequest -Uri "https://pypi.org" -TimeoutSec 3 -UseBasicParsing -ErrorAction Stop
  61. $global:UseCN = $false
  62. } catch {
  63. Write-Warn "pypi.org unreachable - enabling China mirrors"
  64. $global:UseCN = $true
  65. }
  66. }
  67. if ($global:UseCN) {
  68. $PipMirrorArgs = @("-i", "https://pypi.tuna.tsinghua.edu.cn/simple", "--trusted-host", "pypi.tuna.tsinghua.edu.cn")
  69. $NpmMirrorArgs = @("--registry", "https://registry.npmmirror.com")
  70. Write-Info "China mirrors enabled (pip: tuna, npm: npmmirror)"
  71. Write-Host ""
  72. }
  73. $Errors = 0
  74. # ── Step 2a: Node.js + npm ──
  75. Write-Host "--- [1/6] Node.js + npm ---"
  76. try {
  77. $nodeVer = & node --version 2>&1
  78. if ($LASTEXITCODE -eq 0) {
  79. Write-Ok "node ($nodeVer)"
  80. } else { throw "no node" }
  81. } catch {
  82. Write-Fail "node not found (required - docx generation uses docx-js on Node)"
  83. Write-Info "Install option 1: winget install OpenJS.NodeJS.LTS"
  84. Write-Info "Install option 2: https://nodejs.org/"
  85. Write-Info "Install option 3: choco install nodejs-lts"
  86. if ($global:UseCN) {
  87. Write-Info "China alt: https://npmmirror.com/mirrors/node/"
  88. }
  89. $Errors++
  90. }
  91. try {
  92. $npmVer = & npm --version 2>&1
  93. if ($LASTEXITCODE -eq 0) {
  94. Write-Ok "npm ($npmVer)"
  95. } else { throw "no npm" }
  96. } catch {
  97. Write-Fail "npm not found (installed with Node.js)"
  98. $Errors++
  99. }
  100. Write-Host ""
  101. # ── Step 2b: npm package: docx ──
  102. Write-Host "--- [2/6] npm package: docx ---"
  103. try {
  104. $null = & node -e "require('docx')" 2>&1
  105. if ($LASTEXITCODE -eq 0) {
  106. $docxVer = & node -e "try{console.log(require('docx/package.json').version)}catch(e){console.log('installed')}" 2>&1
  107. Write-Ok "docx ($docxVer)"
  108. } else { throw "no docx" }
  109. } catch {
  110. Write-Fail "docx not installed"
  111. Write-Info "Installing docx..."
  112. try {
  113. $installArgs = @("install", "-g", "docx") + $NpmMirrorArgs
  114. & npm @installArgs 2>&1 | Out-Null
  115. Write-Ok "Installed: docx"
  116. } catch {
  117. Write-Fail "npm install failed. Try: npm install -g docx $($NpmMirrorArgs -join ' ')"
  118. $Errors++
  119. }
  120. }
  121. Write-Host ""
  122. # ── Step 2c: Python 3 + pip ──
  123. Write-Host "--- [3/6] Python 3 + pip (post-processing) ---"
  124. if (-not $PyCmd) {
  125. Write-Fail "Python 3 not found"
  126. Write-Info "Install option 1: winget install Python.Python.3.11"
  127. Write-Info "Install option 2: https://www.python.org/downloads/"
  128. Write-Info "Install option 3: choco install python3"
  129. if ($global:UseCN) {
  130. Write-Info "China alt: https://npmmirror.com/mirrors/python/"
  131. }
  132. $Errors++
  133. }
  134. if ($PyCmd) {
  135. try {
  136. $pipVer = & $PyCmd -m pip --version 2>&1
  137. if ($pipVer -match "pip") {
  138. Write-Ok "pip ($pipVer)"
  139. } else { throw "no pip" }
  140. } catch {
  141. Write-Fail "pip not found"
  142. Write-Info "Install: $PyCmd -m ensurepip --upgrade"
  143. $Errors++
  144. }
  145. }
  146. Write-Host ""
  147. # ── Step 2d: Python packages ──
  148. Write-Host "--- [4/6] Python packages (defusedxml) ---"
  149. $PyPkgs = @(
  150. @{ Module = "defusedxml"; Package = "defusedxml" }
  151. )
  152. $MissingPy = @()
  153. if ($PyCmd) {
  154. foreach ($pkg in $PyPkgs) {
  155. try {
  156. $result = & $PyCmd -c "import $($pkg.Module); print(getattr($($pkg.Module), '__version__', 'ok'))" 2>&1
  157. if ($LASTEXITCODE -eq 0) {
  158. Write-Ok "$($pkg.Package) ($result)"
  159. } else { throw "not installed" }
  160. } catch {
  161. Write-Fail "$($pkg.Package) not installed"
  162. $MissingPy += $pkg.Package
  163. }
  164. }
  165. if ($MissingPy.Count -gt 0) {
  166. Write-Info "Installing: $($MissingPy -join ', ')"
  167. $installArgs = @("-m", "pip", "install") + $PipMirrorArgs + $MissingPy
  168. try {
  169. & $PyCmd @installArgs 2>&1 | Out-Null
  170. Write-Ok "Installed: $($MissingPy -join ', ')"
  171. } catch {
  172. Write-Fail "Python package installation failed. Try: $PyCmd -m pip install $($PipMirrorArgs -join ' ') $($MissingPy -join ' ')"
  173. $Errors++
  174. }
  175. }
  176. }
  177. Write-Host ""
  178. # ── Step 2e: Font Installation (from CDN) ──
  179. Write-Host "--- [5/6] Font Installation ---"
  180. $FontCdnBase = "https://z-cdn.chatglm.cn/office-skill/fonts"
  181. $FontList = Join-Path $ScriptDir "font_list.txt"
  182. $UserFontDir = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\Fonts"
  183. if (-not (Test-Path $UserFontDir)) { New-Item -ItemType Directory -Path $UserFontDir -Force | Out-Null }
  184. $Marker = Join-Path $UserFontDir ".office-skill-fonts-installed"
  185. if (Test-Path $Marker) {
  186. Write-Ok "Fonts already installed (marker found). To re-install, delete $Marker"
  187. } else {
  188. if (-not (Test-Path $FontList)) {
  189. Write-Fail "Font list not found: $FontList"
  190. $Errors++
  191. } else {
  192. $lines = Get-Content $FontList | Where-Object { $_.Trim() -ne "" }
  193. $Total = $lines.Count
  194. $Installed = 0; $Skipped = 0; $Failed = 0
  195. Write-Info "Downloading $Total fonts from CDN..."
  196. foreach ($relPath in $lines) {
  197. $fname = Split-Path $relPath -Leaf
  198. $dest = Join-Path $UserFontDir $fname
  199. if (Test-Path $dest) {
  200. $Skipped++
  201. continue
  202. }
  203. $encoded = $relPath -replace '\[','%5B' -replace '\]','%5D' -replace ' ','%20'
  204. $url = "$FontCdnBase/$encoded"
  205. try {
  206. Invoke-WebRequest -Uri $url -OutFile $dest -TimeoutSec 30 -ErrorAction Stop
  207. $regPath = "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
  208. $null = New-ItemProperty -Path $regPath -Name $fname -Value $dest -PropertyType String -Force -ErrorAction SilentlyContinue
  209. $Installed++
  210. } catch {
  211. Write-Warn "Failed to download: $relPath"
  212. $Failed++
  213. Remove-Item $dest -Force -ErrorAction SilentlyContinue
  214. }
  215. }
  216. if ($Failed -eq 0) {
  217. New-Item -ItemType File -Path $Marker -Force | Out-Null
  218. Write-Ok "Fonts: $Installed newly installed, $Skipped already present (target: $UserFontDir)"
  219. } else {
  220. Write-Warn "Fonts: $Installed installed, $Skipped skipped, $Failed failed (marker not written, will retry next run)"
  221. $Errors++
  222. }
  223. }
  224. }
  225. # Set DOCX_FONTS_DIR to user font directory (fonts are now installed there)
  226. $DOCX_FONTS_DIR = $UserFontDir
  227. $env:DOCX_FONTS_DIR = $DOCX_FONTS_DIR
  228. Write-Host ""
  229. # ── Step 2f: CJK Font Verification ──
  230. Write-Host "--- [6/6] CJK Font Verification ---"
  231. $CjkFound = $false
  232. $FontsDir = Join-Path $env:WINDIR "Fonts"
  233. $UserFontDir2 = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\Fonts"
  234. $CjkFonts = @("NotoSansSC[wght].ttf", "NotoSerifSC-Regular.ttf")
  235. foreach ($f in $CjkFonts) {
  236. if ((Test-Path (Join-Path $FontsDir $f)) -or (Test-Path (Join-Path $UserFontDir2 $f))) {
  237. Write-Ok "CJK font found: $f"
  238. $CjkFound = $true
  239. break
  240. }
  241. }
  242. if (-not $CjkFound) {
  243. Write-Warn "No CJK font verified yet - fonts were just installed, restart may be needed"
  244. }
  245. Write-Host ""
  246. # ── Summary ──
  247. Write-Host "============================================"
  248. if ($Errors -eq 0) {
  249. Write-Host " All dependencies OK."
  250. } else {
  251. Write-Host " $Errors issue(s) found. Fix them above."
  252. }
  253. Write-Host " DOCX_SKILL_DIR=$DOCX_SKILL_DIR"
  254. Write-Host " DOCX_FONTS_DIR=$DOCX_FONTS_DIR (user font directory)"
  255. Write-Host "============================================"
  256. # ── LibreOffice:统一检查,受管模式不建议系统安装 ──
  257. $LoHelper = Join-Path $PSScriptRoot "../../../scripts/zcode_libreoffice.py"
  258. Write-Host "--- LibreOffice ---"
  259. # 前面已解析 py/python3/python;固定调用 python 会漏掉支持的解释器。
  260. if ($PyCmd) {
  261. & $PyCmd $LoHelper check
  262. if ($LASTEXITCODE -ne 0) {
  263. Write-Info "Installation guidance (desktop: ask whether to wait or install): $PSScriptRoot/libreoffice-unmanaged.md"
  264. }
  265. } else {
  266. Write-Fail "LibreOffice check - skipped (Python not found)"
  267. }