setup.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/usr/bin/env bash
  2. # ---
  3. # name: pdf-setup
  4. # author: Z.AI
  5. # version: "1.0"
  6. # description: Environment setup for the PDF skill. Checks/installs CORE deps (Python + ReportLab/pypdf/PyMuPDF/pikepdf/pdfplumber + CJK font). Playwright/Chromium and Tectonic are OPTIONAL — install on demand.
  7. # ---
  8. #
  9. # Installs only dependencies required by the PDF skill.
  10. set -euo pipefail
  11. # 优先选择预装入口;存在但运行失败时仍保留它,由用户决定是否切换。
  12. PY="$(command -v zcode-python || command -v python3)" || PY=""
  13. # 没有解释器时无法运行 Python 引导脚本;先给桌面选择,避免进入自动装包或下载。
  14. if [ -z "$PY" ] && [ -n "${ZCODE_PREINSTALLED_TOOLS_AVAILABLE:-}" ]; then
  15. echo "预装版本未就绪或已关闭,请在 帮助 → 预装工具 中查看"
  16. echo "Choose to wait, enable Python, or install system Python using the manual steps in env_setup/setup.md."
  17. exit 1
  18. fi
  19. RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
  20. ok() { echo -e " ${GREEN}✓${NC} $1"; }
  21. fail() { echo -e " ${RED}✗${NC} $1"; }
  22. warn() { echo -e " ${YELLOW}○${NC} $1"; }
  23. info() { echo -e " ${BLUE}→${NC} $1"; }
  24. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  25. echo "============================================"
  26. echo " PDF Skill — Environment Setup"
  27. echo "============================================"
  28. echo ""
  29. # ── Detect platform ──
  30. OS="$(uname -s)"
  31. ARCH="$(uname -m)"
  32. echo "Platform: $OS $ARCH"
  33. echo ""
  34. # ── 0. macOS: Homebrew ──
  35. if [ "$OS" = "Darwin" ]; then
  36. echo "--- Homebrew (macOS package manager) ---"
  37. if command -v brew &>/dev/null; then
  38. BREW_VER=$(brew --version 2>/dev/null | head -1)
  39. ok "brew ($BREW_VER)"
  40. else
  41. fail "brew not found — most dependencies below need Homebrew on macOS"
  42. info "Install: /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
  43. fi
  44. echo ""
  45. fi
  46. # ── 1. Python 3 ──
  47. echo "--- Python ---"
  48. if command -v "$PY" &>/dev/null; then
  49. PY_VER=$("$PY" --version 2>&1)
  50. ok "python3 ($PY_VER)"
  51. # macOS: warn if using system Python
  52. if [ "$OS" = "Darwin" ]; then
  53. PY_PATH="$PY"
  54. if [[ "$PY_PATH" == "/usr/bin/python3" ]]; then
  55. warn "Using macOS system Python (limited). Recommend: brew install python3"
  56. fi
  57. fi
  58. else
  59. fail "python3 not found"
  60. case "$OS" in
  61. Darwin) info "Install: brew install python3" ;;
  62. Linux) info "Install: sudo apt install python3 python3-pip (Debian/Ubuntu)"
  63. info " sudo dnf install python3 python3-pip (Fedora/RHEL)" ;;
  64. *) info "Install: https://www.python.org/downloads/" ;;
  65. esac
  66. fi
  67. # ── 2. pip ──
  68. echo ""
  69. echo "--- pip ---"
  70. if "$PY" -m pip --version &>/dev/null 2>&1; then
  71. PIP_VER=$("$PY" -m pip --version 2>/dev/null | head -1)
  72. ok "pip ($PIP_VER)"
  73. else
  74. fail "pip not found"
  75. case "$OS" in
  76. Darwin) info "Install: \"$PY\" -m ensurepip --upgrade"
  77. info " or: brew install python3 (includes pip)" ;;
  78. Linux) info "Install: sudo apt install python3-pip (Debian/Ubuntu)" ;;
  79. *) info "Install: \"$PY\" -m ensurepip --upgrade" ;;
  80. esac
  81. fi
  82. # ── 3. Python packages (pip) ──
  83. echo ""
  84. echo "--- Python Packages ---"
  85. PY_PKGS=(
  86. "pikepdf:pikepdf"
  87. "pdfplumber:pdfplumber"
  88. "pypdf:pypdf"
  89. "reportlab:reportlab"
  90. "pymupdf:PyMuPDF"
  91. )
  92. MISSING_PY=()
  93. for entry in "${PY_PKGS[@]}"; do
  94. mod="${entry%%:*}"
  95. pkg="${entry##*:}"
  96. if "$PY" -c "import $mod" 2>/dev/null; then
  97. ver=$("$PY" -c "import $mod; print(getattr($mod, '__version__', 'installed'))" 2>/dev/null)
  98. ok "$pkg ($ver)"
  99. else
  100. fail "$pkg not installed"
  101. MISSING_PY+=("$pkg")
  102. fi
  103. done
  104. if [ ${#MISSING_PY[@]} -gt 0 ]; then
  105. echo ""
  106. if [ -t 0 ]; then
  107. read -p " Install missing Python packages? [Y/n] " -n 1 -r REPLY
  108. echo ""
  109. REPLY=${REPLY:-Y}
  110. else
  111. warn "Non-interactive mode — skipping auto-install. Run interactively or install manually."
  112. REPLY=N
  113. fi
  114. if [[ ! $REPLY =~ ^[Nn]$ ]]; then
  115. "$PY" -m pip install -q "${MISSING_PY[@]}" 2>/dev/null \
  116. || "$PY" -m pip install -q --user "${MISSING_PY[@]}" 2>/dev/null \
  117. || "$PY" -m pip install -q --break-system-packages "${MISSING_PY[@]}" 2>/dev/null \
  118. || { fail "Python package installation failed. Try manually: \"$PY\" -m pip install ${MISSING_PY[*]}"; }
  119. ok "Installed: ${MISSING_PY[*]}"
  120. fi
  121. fi
  122. # ── 4. Node.js (OPTIONAL — Creative/HTML pipeline only) ──
  123. echo ""
  124. echo "--- Node.js (optional: Creative/HTML pipeline) ---"
  125. if command -v node &>/dev/null; then
  126. NODE_VER=$(node --version)
  127. ok "node ($NODE_VER)"
  128. else
  129. warn "node not found (optional — only needed for the Creative/poster/HTML→PDF route)"
  130. case "$OS" in
  131. Darwin) info "Install on demand: brew install node" ;;
  132. Linux) info "Install on demand: curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -"
  133. info " sudo apt install -y nodejs" ;;
  134. *) info "Install on demand: https://nodejs.org/" ;;
  135. esac
  136. fi
  137. # ── 5. npm (OPTIONAL) ──
  138. echo ""
  139. echo "--- npm (optional) ---"
  140. if command -v npm &>/dev/null; then
  141. NPM_VER=$(npm --version 2>/dev/null)
  142. ok "npm ($NPM_VER)"
  143. else
  144. warn "npm not found (optional — bundled with Node.js)"
  145. case "$OS" in
  146. Darwin) info "Install on demand: brew install node (includes npm)" ;;
  147. Linux) info "Install on demand: comes with nodejs" ;;
  148. *) info "Install on demand: https://nodejs.org/" ;;
  149. esac
  150. fi
  151. # ── 6. Playwright + Chromium (OPTIONAL — Creative/HTML pipeline only) ──
  152. echo ""
  153. echo "--- Playwright + Chromium (optional: HTML→PDF engine) ---"
  154. info "Optional. Covers now render with ReportLab; Playwright is only for Creative/poster/HTML→PDF."
  155. info "Install on demand, with the user's confirmation (large download / long first-run wait)."
  156. if node -e "require('playwright')" 2>/dev/null; then
  157. PW_VER=$(node -e "console.log(require('playwright/package.json').version)" 2>/dev/null)
  158. ok "playwright ($PW_VER)"
  159. else
  160. warn "playwright not installed (optional)"
  161. info "Install on demand: npm install -g playwright"
  162. fi
  163. # Check Chromium
  164. if [ "$OS" = "Darwin" ]; then
  165. PW_CACHE="$HOME/Library/Caches/ms-playwright"
  166. else
  167. PW_CACHE="$HOME/.cache/ms-playwright"
  168. fi
  169. if ls "$PW_CACHE"/chromium-* &>/dev/null 2>&1; then
  170. CR_DIR=$(ls -d "$PW_CACHE"/chromium-* 2>/dev/null | tail -1)
  171. ok "chromium ($(basename "$CR_DIR"))"
  172. else
  173. warn "chromium not installed (optional)"
  174. info "Install on demand: npx playwright install chromium"
  175. if [ "$OS" = "Linux" ]; then
  176. info " npx playwright install-deps (system libs, needs sudo)"
  177. fi
  178. fi
  179. # ── 7. Tectonic (LaTeX engine, optional) ──
  180. echo ""
  181. echo "--- Tectonic (LaTeX→PDF, optional) ---"
  182. BUNDLED="$SCRIPT_DIR/tectonic"
  183. if [ -x "$BUNDLED" ]; then
  184. if [ "$OS" = "Darwin" ] && [ "$ARCH" = "arm64" ]; then
  185. ok "tectonic (bundled, macOS arm64)"
  186. else
  187. warn "bundled tectonic is macOS arm64 only — cannot run on $OS $ARCH"
  188. if command -v tectonic &>/dev/null; then
  189. TEC_VER=$(tectonic --version 2>&1 | head -1)
  190. ok "tectonic (system: $TEC_VER)"
  191. else
  192. fail "tectonic not in PATH"
  193. case "$OS" in
  194. Darwin) info "Install: brew install tectonic" ;;
  195. Linux) info "Install: conda install -c conda-forge tectonic"
  196. info " or: curl -fsSL https://drop-sh.fullyjustified.net | sh" ;;
  197. MINGW*|MSYS*|CYGWIN*) info "Install: scoop install tectonic / choco install tectonic" ;;
  198. esac
  199. fi
  200. fi
  201. elif command -v tectonic &>/dev/null; then
  202. TEC_VER=$(tectonic --version 2>&1 | head -1)
  203. ok "tectonic ($TEC_VER)"
  204. elif [ -x "$HOME/tectonic" ]; then
  205. ok "tectonic (~/tectonic)"
  206. else
  207. warn "tectonic not installed (needed only for LaTeX/academic PDFs)"
  208. case "$OS" in
  209. Darwin) info "Install: brew install tectonic" ;;
  210. Linux) info "Install: conda install -c conda-forge tectonic"
  211. info " or: curl -fsSL https://drop-sh.fullyjustified.net | sh" ;;
  212. MINGW*|MSYS*|CYGWIN*) info "Install: scoop install tectonic / choco install tectonic" ;;
  213. esac
  214. fi
  215. # ── LibreOffice:统一检查,安装方式由执行环境决定 ──
  216. echo "--- LibreOffice ---"
  217. _LO_PLUGIN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")/../../.." && pwd)"
  218. if "$PY" "$_LO_PLUGIN_DIR/scripts/zcode_libreoffice.py" check; then
  219. ok "LibreOffice ready"
  220. else
  221. warn "LibreOffice is not ready; retain current files and follow the launcher message."
  222. # 桌面可用性只决定引导文案,程序通过 PATH 查找。
  223. info "Installation guidance (desktop: ask whether to wait or install): $_LO_PLUGIN_DIR/skills/pdf/env_setup/libreoffice-unmanaged.md"
  224. fi
  225. # ── 9. CJK Fonts ──
  226. echo ""
  227. echo "--- CJK Fonts ---"
  228. FONT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)/fonts"
  229. if [ -d "$FONT_DIR" ]; then
  230. FONT_COUNT=$(find "$FONT_DIR" -name "*.ttf" -o -name "*.otf" 2>/dev/null | head -20 | wc -l | tr -d ' ')
  231. ok "fonts directory ($FONT_COUNT font files in $FONT_DIR)"
  232. else
  233. warn "no fonts/ directory found — CJK PDFs may have missing glyphs"
  234. info "Expected at: $FONT_DIR"
  235. fi
  236. # Check system CJK fonts
  237. if [ "$OS" = "Darwin" ]; then
  238. if ls /System/Library/Fonts/PingFang.ttc &>/dev/null 2>&1 \
  239. || ls /System/Library/Fonts/STHeiti*.ttc &>/dev/null 2>&1 \
  240. || ls "$HOME/Library/Fonts/"*SimHei* &>/dev/null 2>&1; then
  241. ok "macOS CJK system fonts available"
  242. else
  243. warn "no common CJK system fonts found"
  244. fi
  245. elif [ "$OS" = "Linux" ]; then
  246. if fc-list :lang=zh 2>/dev/null | head -1 | grep -q .; then
  247. ok "system CJK fonts available (fc-list)"
  248. else
  249. warn "no CJK fonts found. Install: sudo apt install fonts-noto-cjk"
  250. fi
  251. fi
  252. # ── Summary ──
  253. echo ""
  254. echo "============================================"
  255. echo " Setup complete."
  256. echo " Run '\"$PY\" pdf.py env.check' for detailed status."
  257. echo " Run '\"$PY\" pdf.py env.fix' to auto-install Python deps."
  258. echo "============================================"