setup.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
  12. ok() { echo -e " ${GREEN}✓${NC} $1"; }
  13. fail() { echo -e " ${RED}✗${NC} $1"; }
  14. warn() { echo -e " ${YELLOW}○${NC} $1"; }
  15. info() { echo -e " ${BLUE}→${NC} $1"; }
  16. echo "============================================"
  17. echo " DOCX Skill — Environment Setup"
  18. echo "============================================"
  19. echo ""
  20. OS="$(uname -s)"
  21. ARCH="$(uname -m)"
  22. echo "Platform: $OS $ARCH"
  23. echo ""
  24. # ── 0. macOS: Homebrew ──
  25. if [ "$OS" = "Darwin" ]; then
  26. echo "--- Homebrew (macOS package manager) ---"
  27. if command -v brew &>/dev/null; then
  28. BREW_VER=$(brew --version 2>/dev/null | head -1)
  29. ok "brew ($BREW_VER)"
  30. else
  31. fail "brew not found — Node.js install needs Homebrew on macOS"
  32. info "Install: /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
  33. fi
  34. echo ""
  35. fi
  36. # ── 1. Node.js (docx-js runs on Node) ──
  37. echo "--- Node.js ---"
  38. if command -v node &>/dev/null; then
  39. NODE_VER=$(node --version)
  40. ok "node ($NODE_VER)"
  41. else
  42. fail "node not found (required — docx generation uses docx-js on Node)"
  43. case "$OS" in
  44. Darwin) info "Install: brew install node" ;;
  45. Linux) info "Install: curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -"
  46. info " sudo apt install -y nodejs" ;;
  47. *) info "Install: https://nodejs.org/" ;;
  48. esac
  49. fi
  50. # ── 2. npm ──
  51. echo ""
  52. echo "--- npm ---"
  53. if command -v npm &>/dev/null; then
  54. NPM_VER=$(npm --version 2>/dev/null)
  55. ok "npm ($NPM_VER)"
  56. else
  57. fail "npm not found"
  58. case "$OS" in
  59. Darwin) info "Install: brew install node (includes npm)" ;;
  60. Linux) info "Install: comes with nodejs" ;;
  61. *) info "Install: https://nodejs.org/" ;;
  62. esac
  63. fi
  64. # ── 3. npm package: docx ──
  65. echo ""
  66. echo "--- npm Packages ---"
  67. if node -e "require('docx')" 2>/dev/null || npm list -g docx &>/dev/null; then
  68. DOCX_VER=$(node -e "try{console.log(require('docx/package.json').version)}catch(e){console.log('installed')}" 2>/dev/null)
  69. ok "docx ($DOCX_VER)"
  70. else
  71. fail "docx not installed"
  72. info "Install: npm install -g docx"
  73. echo ""
  74. if [ -t 0 ]; then
  75. read -p " Install now? [Y/n] " -n 1 -r REPLY
  76. echo ""
  77. REPLY=${REPLY:-Y}
  78. else
  79. warn "Non-interactive mode — skipping auto-install."
  80. REPLY=N
  81. fi
  82. if [[ ! $REPLY =~ ^[Nn]$ ]]; then
  83. npm install -g docx 2>/dev/null && ok "Installed: docx" || fail "npm install failed"
  84. fi
  85. fi
  86. # ── 4. Python 3 (post-processing scripts) ──
  87. echo ""
  88. echo "--- Python (post-processing) ---"
  89. if command -v python3 &>/dev/null; then
  90. PY_VER=$(python3 --version 2>&1)
  91. ok "python3 ($PY_VER)"
  92. if [ "$OS" = "Darwin" ]; then
  93. PY_PATH=$(which python3 2>/dev/null)
  94. if [[ "$PY_PATH" == "/usr/bin/python3" ]]; then
  95. warn "Using macOS system Python (limited). Recommend: brew install python3"
  96. fi
  97. fi
  98. else
  99. fail "python3 not found"
  100. case "$OS" in
  101. Darwin) info "Install: brew install python3" ;;
  102. Linux) info "Install: sudo apt install python3 python3-pip (Debian/Ubuntu)"
  103. info " sudo dnf install python3 python3-pip (Fedora/RHEL)" ;;
  104. *) info "Install: https://www.python.org/downloads/" ;;
  105. esac
  106. fi
  107. # ── 5. pip ──
  108. echo ""
  109. echo "--- pip ---"
  110. if python3 -m pip --version &>/dev/null 2>&1; then
  111. PIP_VER=$(python3 -m pip --version 2>/dev/null | head -1)
  112. ok "pip ($PIP_VER)"
  113. else
  114. fail "pip not found"
  115. case "$OS" in
  116. Darwin) info "Install: python3 -m ensurepip --upgrade"
  117. info " or: brew install python3 (includes pip)" ;;
  118. Linux) info "Install: sudo apt install python3-pip (Debian/Ubuntu)" ;;
  119. *) info "Install: python3 -m ensurepip --upgrade" ;;
  120. esac
  121. fi
  122. # ── 6. Python packages ──
  123. echo ""
  124. echo "--- Python Packages ---"
  125. PY_PKGS=(
  126. "defusedxml:defusedxml"
  127. )
  128. MISSING_PY=()
  129. for entry in "${PY_PKGS[@]}"; do
  130. mod="${entry%%:*}"
  131. pkg="${entry##*:}"
  132. if python3 -c "import $mod" 2>/dev/null; then
  133. ver=$(python3 -c "import $mod; print(getattr($mod, '__version__', 'installed'))" 2>/dev/null)
  134. ok "$pkg ($ver)"
  135. else
  136. fail "$pkg not installed"
  137. MISSING_PY+=("$pkg")
  138. fi
  139. done
  140. if [ ${#MISSING_PY[@]} -gt 0 ]; then
  141. echo ""
  142. if [ -t 0 ]; then
  143. read -p " Install missing Python packages? [Y/n] " -n 1 -r REPLY
  144. echo ""
  145. REPLY=${REPLY:-Y}
  146. else
  147. warn "Non-interactive mode — skipping auto-install. Run interactively or install manually."
  148. REPLY=N
  149. fi
  150. if [[ ! $REPLY =~ ^[Nn]$ ]]; then
  151. python3 -m pip install -q "${MISSING_PY[@]}" 2>/dev/null \
  152. || python3 -m pip install -q --user "${MISSING_PY[@]}" 2>/dev/null \
  153. || python3 -m pip install -q --break-system-packages "${MISSING_PY[@]}" 2>/dev/null \
  154. || { fail "pip install failed. Try manually: pip install ${MISSING_PY[*]}"; }
  155. ok "Installed: ${MISSING_PY[*]}"
  156. fi
  157. fi
  158. # ── LibreOffice (install-on-demand, NOT substitutable) ──
  159. echo ""
  160. echo "--- LibreOffice (.doc→.docx, DOCX→PDF, visual verification) ---"
  161. if command -v soffice &>/dev/null; then
  162. LO_VER=$(soffice --version 2>/dev/null | head -1)
  163. ok "libreoffice ($LO_VER)"
  164. else
  165. warn "libreoffice not installed — REQUIRED for .doc→.docx, DOCX→PDF, visual checks"
  166. info "You MUST install it. FORBIDDEN: using Word/WPS/Pages instead of installing,"
  167. info "or skipping the PDF/visual check. A large download is NOT a reason to skip."
  168. info "REQUIRED: explain the necessity to the user, say the package is large, THEN install."
  169. info "PREFERRED source (fast in China, current build): Tsinghua TUNA mirror"
  170. info " https://mirrors.tuna.tsinghua.edu.cn/libreoffice/libreoffice/stable/26.8.0/"
  171. info " Pick the subdir for this platform/arch ($(uname -m)):"
  172. info " Linux deb -> deb/x86_64/ or deb/aarch64/ (tar -xzf, then dpkg -i */DEBS/*.deb)"
  173. info " Linux rpm -> rpm/x86_64/"
  174. info " macOS -> mac/aarch64/ (Apple Silicon) or mac/x86_64/ (Intel) (.dmg)"
  175. info " If 26.8.0 is gone, pick the newest version under .../libreoffice/stable/"
  176. info "Package-manager FALLBACK (only if the mirror is unreachable):"
  177. case "$(uname -s)" in
  178. Darwin) info " brew install --cask libreoffice" ;;
  179. Linux) info " sudo apt install libreoffice-core (Debian/Ubuntu)" ;;
  180. esac
  181. info "AFTER installing, REGISTER the binary on PATH -- the method differs per OS:"
  182. case "$OS" in
  183. Darwin) info " sudo ln -sf /Applications/LibreOffice.app/Contents/MacOS/soffice /usr/local/bin/soffice"
  184. info " (Apple Silicon: use /opt/homebrew/bin/soffice if /usr/local/bin is not on PATH)" ;;
  185. Linux) info " sudo ln -sf /opt/libreoffice*/program/soffice /usr/local/bin/soffice" ;;
  186. esac
  187. info " then re-verify: soffice --version"
  188. info "An install left off PATH looks 'not installed' next run and gets reinstalled"
  189. info "needlessly. If soffice already exists on disk but is off PATH, just link it."
  190. info "Another program is allowed ONLY after an install attempt has FAILED. Report it."
  191. info "No install attempt = no substitute program."
  192. fi
  193. # ── Summary ──
  194. echo ""
  195. echo "============================================"
  196. echo " Setup complete."
  197. echo " Core: Node.js + docx (npm)"
  198. echo " Post-processing: Python + defusedxml"
  199. echo " On-demand (not substitutable): LibreOffice"
  200. echo "============================================"