setup_windows.ps1 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. # ── Resolve DOCX_SKILL_DIR ──
  18. $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  19. $DOCX_SKILL_DIR = Split-Path -Parent $ScriptDir
  20. $env:DOCX_SKILL_DIR = $DOCX_SKILL_DIR
  21. Write-Host "============================================"
  22. Write-Host " DOCX Skill - Environment Setup"
  23. Write-Host " (Windows)"
  24. Write-Host "============================================"
  25. Write-Host ""
  26. # ── Step 1: Platform Detection ──
  27. $WinVer = [System.Environment]::OSVersion.Version
  28. $WinName = if ($WinVer.Build -ge 22000) { "Windows 11" } elseif ($WinVer.Build -ge 10240) { "Windows 10" } else { "Windows (older)" }
  29. Write-Host "Platform: $WinName (Build $($WinVer.Build)), $([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)"
  30. Write-Host "DOCX_SKILL_DIR=$DOCX_SKILL_DIR"
  31. Write-Host ""
  32. # ── China mirror detection ──
  33. $PipMirrorArgs = @()
  34. $NpmMirrorArgs = @()
  35. if ($UseChinaMirror) {
  36. $global:UseCN = $true
  37. } else {
  38. try {
  39. $null = Invoke-WebRequest -Uri "https://pypi.org" -TimeoutSec 3 -UseBasicParsing -ErrorAction Stop
  40. $global:UseCN = $false
  41. } catch {
  42. Write-Warn "pypi.org unreachable - enabling China mirrors"
  43. $global:UseCN = $true
  44. }
  45. }
  46. if ($global:UseCN) {
  47. $PipMirrorArgs = @("-i", "https://pypi.tuna.tsinghua.edu.cn/simple", "--trusted-host", "pypi.tuna.tsinghua.edu.cn")
  48. $NpmMirrorArgs = @("--registry", "https://registry.npmmirror.com")
  49. Write-Info "China mirrors enabled (pip: tuna, npm: npmmirror)"
  50. Write-Host ""
  51. }
  52. $Errors = 0
  53. # ── Step 2a: Node.js + npm ──
  54. Write-Host "--- [1/6] Node.js + npm ---"
  55. try {
  56. $nodeVer = & node --version 2>&1
  57. if ($LASTEXITCODE -eq 0) {
  58. Write-Ok "node ($nodeVer)"
  59. } else { throw "no node" }
  60. } catch {
  61. Write-Fail "node not found (required - docx generation uses docx-js on Node)"
  62. Write-Info "Install option 1: winget install OpenJS.NodeJS.LTS"
  63. Write-Info "Install option 2: https://nodejs.org/"
  64. Write-Info "Install option 3: choco install nodejs-lts"
  65. if ($global:UseCN) {
  66. Write-Info "China alt: https://npmmirror.com/mirrors/node/"
  67. }
  68. $Errors++
  69. }
  70. try {
  71. $npmVer = & npm --version 2>&1
  72. if ($LASTEXITCODE -eq 0) {
  73. Write-Ok "npm ($npmVer)"
  74. } else { throw "no npm" }
  75. } catch {
  76. Write-Fail "npm not found (installed with Node.js)"
  77. $Errors++
  78. }
  79. Write-Host ""
  80. # ── Step 2b: npm package: docx ──
  81. Write-Host "--- [2/6] npm package: docx ---"
  82. try {
  83. $null = & node -e "require('docx')" 2>&1
  84. if ($LASTEXITCODE -eq 0) {
  85. $docxVer = & node -e "try{console.log(require('docx/package.json').version)}catch(e){console.log('installed')}" 2>&1
  86. Write-Ok "docx ($docxVer)"
  87. } else { throw "no docx" }
  88. } catch {
  89. Write-Fail "docx not installed"
  90. Write-Info "Installing docx..."
  91. try {
  92. $installArgs = @("install", "-g", "docx") + $NpmMirrorArgs
  93. & npm @installArgs 2>&1 | Out-Null
  94. Write-Ok "Installed: docx"
  95. } catch {
  96. Write-Fail "npm install failed. Try: npm install -g docx $($NpmMirrorArgs -join ' ')"
  97. $Errors++
  98. }
  99. }
  100. Write-Host ""
  101. # ── Step 2c: Python 3 + pip ──
  102. Write-Host "--- [3/6] Python 3 + pip (post-processing) ---"
  103. $PyCmd = $null
  104. foreach ($cmd in @("python3", "python", "py")) {
  105. try {
  106. $ver = & $cmd --version 2>&1
  107. if ($ver -match "Python 3") {
  108. $PyCmd = $cmd
  109. Write-Ok "$cmd ($ver)"
  110. break
  111. }
  112. } catch {}
  113. }
  114. if (-not $PyCmd) {
  115. Write-Fail "Python 3 not found"
  116. Write-Info "Install option 1: winget install Python.Python.3.11"
  117. Write-Info "Install option 2: https://www.python.org/downloads/"
  118. Write-Info "Install option 3: choco install python3"
  119. if ($global:UseCN) {
  120. Write-Info "China alt: https://npmmirror.com/mirrors/python/"
  121. }
  122. $Errors++
  123. }
  124. if ($PyCmd) {
  125. try {
  126. $pipVer = & $PyCmd -m pip --version 2>&1
  127. if ($pipVer -match "pip") {
  128. Write-Ok "pip ($pipVer)"
  129. } else { throw "no pip" }
  130. } catch {
  131. Write-Fail "pip not found"
  132. Write-Info "Install: $PyCmd -m ensurepip --upgrade"
  133. $Errors++
  134. }
  135. }
  136. Write-Host ""
  137. # ── Step 2d: Python packages ──
  138. Write-Host "--- [4/6] Python packages (defusedxml) ---"
  139. $PyPkgs = @(
  140. @{ Module = "defusedxml"; Package = "defusedxml" }
  141. )
  142. $MissingPy = @()
  143. if ($PyCmd) {
  144. foreach ($pkg in $PyPkgs) {
  145. try {
  146. $result = & $PyCmd -c "import $($pkg.Module); print(getattr($($pkg.Module), '__version__', 'ok'))" 2>&1
  147. if ($LASTEXITCODE -eq 0) {
  148. Write-Ok "$($pkg.Package) ($result)"
  149. } else { throw "not installed" }
  150. } catch {
  151. Write-Fail "$($pkg.Package) not installed"
  152. $MissingPy += $pkg.Package
  153. }
  154. }
  155. if ($MissingPy.Count -gt 0) {
  156. Write-Info "Installing: $($MissingPy -join ', ')"
  157. $installArgs = @("-m", "pip", "install") + $PipMirrorArgs + $MissingPy
  158. try {
  159. & $PyCmd @installArgs 2>&1 | Out-Null
  160. Write-Ok "Installed: $($MissingPy -join ', ')"
  161. } catch {
  162. Write-Fail "pip install failed. Try: $PyCmd -m pip install $($PipMirrorArgs -join ' ') $($MissingPy -join ' ')"
  163. $Errors++
  164. }
  165. }
  166. }
  167. Write-Host ""
  168. # ── Step 2e: Font Installation (from CDN) ──
  169. Write-Host "--- [5/6] Font Installation ---"
  170. $FontCdnBase = "https://z-cdn.chatglm.cn/office-skill/fonts"
  171. $FontList = Join-Path $ScriptDir "font_list.txt"
  172. $UserFontDir = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\Fonts"
  173. if (-not (Test-Path $UserFontDir)) { New-Item -ItemType Directory -Path $UserFontDir -Force | Out-Null }
  174. $Marker = Join-Path $UserFontDir ".office-skill-fonts-installed"
  175. if (Test-Path $Marker) {
  176. Write-Ok "Fonts already installed (marker found). To re-install, delete $Marker"
  177. } else {
  178. if (-not (Test-Path $FontList)) {
  179. Write-Fail "Font list not found: $FontList"
  180. $Errors++
  181. } else {
  182. $lines = Get-Content $FontList | Where-Object { $_.Trim() -ne "" }
  183. $Total = $lines.Count
  184. $Installed = 0; $Skipped = 0; $Failed = 0
  185. Write-Info "Downloading $Total fonts from CDN..."
  186. foreach ($relPath in $lines) {
  187. $fname = Split-Path $relPath -Leaf
  188. $dest = Join-Path $UserFontDir $fname
  189. if (Test-Path $dest) {
  190. $Skipped++
  191. continue
  192. }
  193. $encoded = $relPath -replace '\[','%5B' -replace '\]','%5D' -replace ' ','%20'
  194. $url = "$FontCdnBase/$encoded"
  195. try {
  196. Invoke-WebRequest -Uri $url -OutFile $dest -TimeoutSec 30 -ErrorAction Stop
  197. $regPath = "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
  198. $null = New-ItemProperty -Path $regPath -Name $fname -Value $dest -PropertyType String -Force -ErrorAction SilentlyContinue
  199. $Installed++
  200. } catch {
  201. Write-Warn "Failed to download: $relPath"
  202. $Failed++
  203. Remove-Item $dest -Force -ErrorAction SilentlyContinue
  204. }
  205. }
  206. if ($Failed -eq 0) {
  207. New-Item -ItemType File -Path $Marker -Force | Out-Null
  208. Write-Ok "Fonts: $Installed newly installed, $Skipped already present (target: $UserFontDir)"
  209. } else {
  210. Write-Warn "Fonts: $Installed installed, $Skipped skipped, $Failed failed (marker not written, will retry next run)"
  211. $Errors++
  212. }
  213. }
  214. }
  215. # Set DOCX_FONTS_DIR to user font directory (fonts are now installed there)
  216. $DOCX_FONTS_DIR = $UserFontDir
  217. $env:DOCX_FONTS_DIR = $DOCX_FONTS_DIR
  218. Write-Host ""
  219. # ── Step 2f: CJK Font Verification ──
  220. Write-Host "--- [6/6] CJK Font Verification ---"
  221. $CjkFound = $false
  222. $FontsDir = Join-Path $env:WINDIR "Fonts"
  223. $UserFontDir2 = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\Fonts"
  224. $CjkFonts = @("NotoSansSC[wght].ttf", "NotoSerifSC-Regular.ttf")
  225. foreach ($f in $CjkFonts) {
  226. if ((Test-Path (Join-Path $FontsDir $f)) -or (Test-Path (Join-Path $UserFontDir2 $f))) {
  227. Write-Ok "CJK font found: $f"
  228. $CjkFound = $true
  229. break
  230. }
  231. }
  232. if (-not $CjkFound) {
  233. Write-Warn "No CJK font verified yet - fonts were just installed, restart may be needed"
  234. }
  235. Write-Host ""
  236. # ── Summary ──
  237. Write-Host "============================================"
  238. if ($Errors -eq 0) {
  239. Write-Host " All dependencies OK."
  240. } else {
  241. Write-Host " $Errors issue(s) found. Fix them above."
  242. }
  243. Write-Host " DOCX_SKILL_DIR=$DOCX_SKILL_DIR"
  244. Write-Host " DOCX_FONTS_DIR=$DOCX_FONTS_DIR (user font directory)"
  245. Write-Host "============================================"