dev_vscode.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/bash
  2. # Opencode VSCode Extension Development Workflow Script
  3. # Provides common development tasks for the Opencode VSCode extension
  4. set -e
  5. RED='\033[0;31m'
  6. GREEN='\033[0;32m'
  7. YELLOW='\033[1;33m'
  8. BLUE='\033[0;34m'
  9. NC='\033[0m'
  10. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  11. ROOT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
  12. PLUGIN_DIR="$ROOT_DIR/hosts/vscode-plugin"
  13. EXTENSION_ID="opencode.opencode"
  14. log_info() {
  15. echo -e "${GREEN}[INFO]${NC} $1"
  16. }
  17. log_warn() {
  18. echo -e "${YELLOW}[WARN]${NC} $1"
  19. }
  20. log_error() {
  21. echo -e "${RED}[ERROR]${NC} $1"
  22. }
  23. echo -e "${BLUE}Opencode VSCode Extension Development Script${NC}"
  24. PNPM_AVAILABLE=false
  25. PM_RUN="npm run"
  26. PM_INSTALL="npm ci || npm install"
  27. if command -v pnpm >/dev/null 2>&1; then
  28. PNPM_AVAILABLE=true
  29. PM_RUN="pnpm run"
  30. PM_INSTALL="pnpm install --frozen-lockfile"
  31. fi
  32. run_install() {
  33. if $PNPM_AVAILABLE; then
  34. pnpm install --frozen-lockfile
  35. else
  36. npm ci || npm install
  37. fi
  38. }
  39. run_script() {
  40. local script="$1"
  41. if $PNPM_AVAILABLE; then
  42. pnpm run "$script"
  43. else
  44. npm run "$script"
  45. fi
  46. }
  47. if [[ ! -f "$PLUGIN_DIR/package.json" ]]; then
  48. log_error "package.json not found. Please run this script from the repository root."
  49. exit 1
  50. fi
  51. cd "$PLUGIN_DIR"
  52. show_help() {
  53. cat <<EOF
  54. Usage: $0 <command>
  55. Commands:
  56. setup Install dependencies and tooling
  57. build Build the extension for development
  58. watch Start TypeScript compiler in watch mode
  59. test Run all tests
  60. test:watch Run tests in watch mode (requires inotifywait on Linux)
  61. lint Run ESLint
  62. lint:fix Run ESLint with auto-fix
  63. clean Clean build artifacts
  64. package Build binaries and create a dev package
  65. install Install the latest .vsix into VSCode
  66. uninstall Uninstall the extension from VSCode
  67. logs Show VSCode extension log folder
  68. debug Show debugging hints
  69. help Show this message
  70. EOF
  71. }
  72. cmd="${1:-help}"
  73. shift || true
  74. case "$cmd" in
  75. setup)
  76. log_info "Setting up development environment..."
  77. if ! command -v node >/dev/null 2>&1; then
  78. log_error "Node.js is required but not found in PATH."
  79. exit 1
  80. fi
  81. log_info "Installing dependencies..."
  82. run_install
  83. if ! command -v vsce >/dev/null 2>&1; then
  84. log_info "Installing vsce..."
  85. npm install --global @vscode/vsce || true
  86. fi
  87. log_info "Setup complete"
  88. ;;
  89. build)
  90. log_info "Building extension..."
  91. run_script compile
  92. ;;
  93. watch)
  94. log_info "Starting TypeScript watch... (Ctrl+C to stop)"
  95. run_script watch
  96. ;;
  97. test)
  98. log_info "Running tests..."
  99. run_script test
  100. ;;
  101. test:watch)
  102. log_info "Running tests in watch mode..."
  103. if command -v inotifywait >/dev/null 2>&1; then
  104. while true; do
  105. run_script test
  106. log_info "Waiting for file changes..."
  107. inotifywait -r -e modify,create,delete src/ --timeout 30 || true
  108. done
  109. else
  110. log_warn "inotifywait not found. Running tests once."
  111. run_script test
  112. fi
  113. ;;
  114. lint)
  115. log_info "Running ESLint..."
  116. run_script lint
  117. ;;
  118. lint:fix)
  119. log_info "Running ESLint with auto-fix..."
  120. npx eslint src --ext ts --fix
  121. ;;
  122. clean)
  123. log_info "Cleaning build artifacts..."
  124. set +e
  125. if [ ! -d node_modules ]; then
  126. log_warn "Dependencies not installed; removing artifacts manually."
  127. rm -rf out
  128. rm -f ./*.vsix
  129. fi
  130. if [ -d node_modules ]; then
  131. run_script clean
  132. local status=$?
  133. if [[ $status -ne 0 ]]; then
  134. log_warn "Clean command failed; applying manual removal."
  135. rm -rf out
  136. rm -f ./*.vsix
  137. fi
  138. fi
  139. set -e
  140. ;;
  141. package)
  142. log_info "Building binaries and packaging..."
  143. "$SCRIPT_DIR/build-vscode.sh" --skip-tests
  144. ;;
  145. install)
  146. log_info "Installing latest .vsix..."
  147. vsix="$(ls -t *.vsix 2>/dev/null | head -n1)"
  148. if [[ -z "$vsix" ]]; then
  149. log_error "No .vsix found. Run '$0 package' first."
  150. exit 1
  151. fi
  152. if ! command -v code >/dev/null 2>&1; then
  153. log_error "VSCode 'code' CLI not found in PATH."
  154. exit 1
  155. fi
  156. code --install-extension "$vsix" --force
  157. log_info "Installed $vsix"
  158. ;;
  159. uninstall)
  160. log_info "Uninstalling extension from VSCode..."
  161. if ! command -v code >/dev/null 2>&1; then
  162. log_error "VSCode 'code' CLI not found in PATH."
  163. exit 1
  164. fi
  165. code --uninstall-extension "$EXTENSION_ID" || log_warn "Extension may not be installed."
  166. ;;
  167. logs)
  168. log_info "VSCode extension logs location:"
  169. if [[ "$OSTYPE" == "darwin"* ]]; then
  170. echo " $HOME/Library/Application Support/Code/logs"
  171. elif [[ "$OSTYPE" == "linux"* ]]; then
  172. echo " $HOME/.config/Code/logs"
  173. else
  174. log_warn "Log path unknown for this OS."
  175. fi
  176. ;;
  177. debug)
  178. log_info "Debugging steps:"
  179. log_info "1. Open project in VSCode"
  180. log_info "2. Go to Run and Debug (Ctrl+Shift+D)"
  181. log_info "3. Select 'Run Extension'"
  182. log_info "4. Press F5"
  183. ;;
  184. help|-h|--help)
  185. show_help
  186. ;;
  187. *)
  188. log_error "Unknown command: $cmd"
  189. echo
  190. show_help
  191. exit 1
  192. ;;
  193. esac