setup.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/usr/bin/env bash
  2. # ---
  3. # name: docx-setup
  4. # author: Z.AI
  5. # version: "1.0"
  6. # description: Environment setup for the DOCX skill. Checks and installs all required dependencies.
  7. # ---
  8. #
  9. # Installs only dependencies required by the DOCX 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. echo "============================================"
  25. echo " DOCX Skill — Environment Setup"
  26. echo "============================================"
  27. echo ""
  28. OS="$(uname -s)"
  29. ARCH="$(uname -m)"
  30. echo "Platform: $OS $ARCH"
  31. echo ""
  32. # ── 0. macOS: Homebrew ──
  33. if [ "$OS" = "Darwin" ]; then
  34. echo "--- Homebrew (macOS package manager) ---"
  35. if command -v brew &>/dev/null; then
  36. BREW_VER=$(brew --version 2>/dev/null | head -1)
  37. ok "brew ($BREW_VER)"
  38. else
  39. fail "brew not found — Node.js install needs Homebrew on macOS"
  40. info "Install: /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
  41. fi
  42. echo ""
  43. fi
  44. # ── 1. Node.js (docx-js runs on Node) ──
  45. echo "--- Node.js ---"
  46. if command -v node &>/dev/null; then
  47. NODE_VER=$(node --version)
  48. ok "node ($NODE_VER)"
  49. else
  50. fail "node not found (required — docx generation uses docx-js on Node)"
  51. case "$OS" in
  52. Darwin) info "Install: brew install node" ;;
  53. Linux) info "Install: curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -"
  54. info " sudo apt install -y nodejs" ;;
  55. *) info "Install: https://nodejs.org/" ;;
  56. esac
  57. fi
  58. # ── 2. npm ──
  59. echo ""
  60. echo "--- npm ---"
  61. if command -v npm &>/dev/null; then
  62. NPM_VER=$(npm --version 2>/dev/null)
  63. ok "npm ($NPM_VER)"
  64. else
  65. fail "npm not found"
  66. case "$OS" in
  67. Darwin) info "Install: brew install node (includes npm)" ;;
  68. Linux) info "Install: comes with nodejs" ;;
  69. *) info "Install: https://nodejs.org/" ;;
  70. esac
  71. fi
  72. # ── 3. npm package: docx ──
  73. echo ""
  74. echo "--- npm Packages ---"
  75. if node -e "require('docx')" 2>/dev/null || npm list -g docx &>/dev/null; then
  76. DOCX_VER=$(node -e "try{console.log(require('docx/package.json').version)}catch(e){console.log('installed')}" 2>/dev/null)
  77. ok "docx ($DOCX_VER)"
  78. else
  79. fail "docx not installed"
  80. info "Install: npm install -g docx"
  81. echo ""
  82. if [ -t 0 ]; then
  83. read -p " Install now? [Y/n] " -n 1 -r REPLY
  84. echo ""
  85. REPLY=${REPLY:-Y}
  86. else
  87. warn "Non-interactive mode — skipping auto-install."
  88. REPLY=N
  89. fi
  90. if [[ ! $REPLY =~ ^[Nn]$ ]]; then
  91. npm install -g docx 2>/dev/null && ok "Installed: docx" || fail "npm install failed"
  92. fi
  93. fi
  94. # ── 4. Python 3 (post-processing scripts) ──
  95. echo ""
  96. echo "--- Python (post-processing) ---"
  97. if command -v "$PY" &>/dev/null; then
  98. PY_VER=$("$PY" --version 2>&1)
  99. ok "python3 ($PY_VER)"
  100. if [ "$OS" = "Darwin" ]; then
  101. PY_PATH="$PY"
  102. if [[ "$PY_PATH" == "/usr/bin/python3" ]]; then
  103. warn "Using macOS system Python (limited). Recommend: brew install python3"
  104. fi
  105. fi
  106. else
  107. fail "python3 not found"
  108. case "$OS" in
  109. Darwin) info "Install: brew install python3" ;;
  110. Linux) info "Install: sudo apt install python3 python3-pip (Debian/Ubuntu)"
  111. info " sudo dnf install python3 python3-pip (Fedora/RHEL)" ;;
  112. *) info "Install: https://www.python.org/downloads/" ;;
  113. esac
  114. fi
  115. # ── 5. pip ──
  116. echo ""
  117. echo "--- pip ---"
  118. if "$PY" -m pip --version &>/dev/null 2>&1; then
  119. PIP_VER=$("$PY" -m pip --version 2>/dev/null | head -1)
  120. ok "pip ($PIP_VER)"
  121. else
  122. fail "pip not found"
  123. case "$OS" in
  124. Darwin) info "Install: \"$PY\" -m ensurepip --upgrade"
  125. info " or: brew install python3 (includes pip)" ;;
  126. Linux) info "Install: sudo apt install python3-pip (Debian/Ubuntu)" ;;
  127. *) info "Install: \"$PY\" -m ensurepip --upgrade" ;;
  128. esac
  129. fi
  130. # ── 6. Python packages ──
  131. echo ""
  132. echo "--- Python Packages ---"
  133. PY_PKGS=(
  134. "defusedxml:defusedxml"
  135. )
  136. MISSING_PY=()
  137. for entry in "${PY_PKGS[@]}"; do
  138. mod="${entry%%:*}"
  139. pkg="${entry##*:}"
  140. if "$PY" -c "import $mod" 2>/dev/null; then
  141. ver=$("$PY" -c "import $mod; print(getattr($mod, '__version__', 'installed'))" 2>/dev/null)
  142. ok "$pkg ($ver)"
  143. else
  144. fail "$pkg not installed"
  145. MISSING_PY+=("$pkg")
  146. fi
  147. done
  148. if [ ${#MISSING_PY[@]} -gt 0 ]; then
  149. echo ""
  150. if [ -t 0 ]; then
  151. read -p " Install missing Python packages? [Y/n] " -n 1 -r REPLY
  152. echo ""
  153. REPLY=${REPLY:-Y}
  154. else
  155. warn "Non-interactive mode — skipping auto-install. Run interactively or install manually."
  156. REPLY=N
  157. fi
  158. if [[ ! $REPLY =~ ^[Nn]$ ]]; then
  159. "$PY" -m pip install -q "${MISSING_PY[@]}" 2>/dev/null \
  160. || "$PY" -m pip install -q --user "${MISSING_PY[@]}" 2>/dev/null \
  161. || "$PY" -m pip install -q --break-system-packages "${MISSING_PY[@]}" 2>/dev/null \
  162. || { fail "Python package installation failed. Try manually: \"$PY\" -m pip install ${MISSING_PY[*]}"; }
  163. ok "Installed: ${MISSING_PY[*]}"
  164. fi
  165. fi
  166. # ── LibreOffice:统一检查,安装方式由执行环境决定 ──
  167. echo "--- LibreOffice ---"
  168. _LO_PLUGIN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")/../.." && pwd)"
  169. if "$PY" "$_LO_PLUGIN_DIR/scripts/zcode_libreoffice.py" check; then
  170. ok "LibreOffice ready"
  171. else
  172. warn "LibreOffice is not ready; retain current files and follow the launcher message."
  173. # 桌面可用性只决定引导文案,程序通过 PATH 查找。
  174. info "Installation guidance (desktop: ask whether to wait or install): $_LO_PLUGIN_DIR/skills/docx/env_setup/libreoffice-unmanaged.md"
  175. fi
  176. # ── Summary ──
  177. echo ""
  178. echo "============================================"
  179. echo " Setup complete."
  180. echo " Core: Node.js + docx (npm)"
  181. echo " Post-processing: Python + defusedxml"
  182. echo " On-demand (not substitutable): LibreOffice"
  183. echo "============================================"