setup_windows.ps1 12 KB

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