build_vscode.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/bin/bash
  2. # Opencode VSCode Extension Build Script
  3. # This script handles the complete build process for the Opencode VSCode extension
  4. set -e
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m' # No Color
  11. # Script directory references
  12. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  13. ROOT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
  14. PLUGIN_DIR="$ROOT_DIR/hosts/vscode-plugin"
  15. echo -e "${BLUE}Opencode VSCode Extension Build Script${NC}"
  16. echo "Plugin directory: $PLUGIN_DIR"
  17. echo "Root directory: $ROOT_DIR"
  18. # --- Package manager helpers ---
  19. PNPM_AVAILABLE=false
  20. RUN_PM="npm run"
  21. INSTALL_PM="npm ci || npm install"
  22. if command -v pnpm >/dev/null 2>&1; then
  23. PNPM_AVAILABLE=true
  24. RUN_PM="pnpm run"
  25. INSTALL_PM="pnpm install --frozen-lockfile"
  26. fi
  27. run_install() {
  28. if $PNPM_AVAILABLE; then
  29. pnpm install --frozen-lockfile
  30. else
  31. npm ci || npm install
  32. fi
  33. }
  34. run_script() {
  35. local script="$1"
  36. if $PNPM_AVAILABLE; then
  37. pnpm run "$script"
  38. else
  39. npm run "$script"
  40. fi
  41. }
  42. print_status() {
  43. echo -e "${GREEN}[INFO]${NC} $1"
  44. }
  45. print_warning() {
  46. echo -e "${YELLOW}[WARN]${NC} $1"
  47. }
  48. print_error() {
  49. echo -e "${RED}[ERROR]${NC} $1"
  50. }
  51. if [ ! -f "$PLUGIN_DIR/package.json" ]; then
  52. print_error "package.json not found. Please run this script from the repository root."
  53. exit 1
  54. fi
  55. BUILD_TYPE="development"
  56. SKIP_BINARIES=false
  57. SKIP_TESTS=false
  58. PACKAGE_ONLY=false
  59. while [[ $# -gt 0 ]]; do
  60. case $1 in
  61. --production)
  62. BUILD_TYPE="production"
  63. shift
  64. ;;
  65. --skip-binaries)
  66. SKIP_BINARIES=true
  67. shift
  68. ;;
  69. --skip-tests)
  70. SKIP_TESTS=true
  71. shift
  72. ;;
  73. --package-only)
  74. PACKAGE_ONLY=true
  75. shift
  76. ;;
  77. --help)
  78. echo "Usage: $0 [OPTIONS]"
  79. echo "Options:"
  80. echo " --production Build for production (default: development)"
  81. echo " --skip-binaries Skip building backend binaries"
  82. echo " --skip-tests Skip running tests"
  83. echo " --package-only Only create the .vsix package (skip compilation)"
  84. echo " --help Show this help message"
  85. exit 0
  86. ;;
  87. *)
  88. print_error "Unknown option: $1"
  89. exit 1
  90. ;;
  91. esac
  92. done
  93. print_status "Building VSCode extension in $BUILD_TYPE mode"
  94. cd "$PLUGIN_DIR"
  95. if [ "$PACKAGE_ONLY" = false ]; then
  96. print_status "Cleaning previous build artifacts..."
  97. set +e
  98. if [ ! -d node_modules ]; then
  99. print_warning "Dependencies not installed; skipping script clean and removing artifacts manually."
  100. rm -rf out
  101. rm -f ./*.vsix
  102. fi
  103. if [ -d node_modules ]; then
  104. run_script clean
  105. if [[ $? -ne 0 ]]; then
  106. print_warning "Clean command failed, applying fallback removal..."
  107. rm -rf out
  108. rm -f ./*.vsix
  109. fi
  110. fi
  111. set -e
  112. fi
  113. if [ "$PACKAGE_ONLY" = false ]; then
  114. print_status "Installing dependencies..."
  115. if ! command -v node >/dev/null 2>&1; then
  116. print_error "Node.js is required but not found in PATH. Please install Node.js."
  117. exit 1
  118. fi
  119. run_install
  120. fi
  121. if [ "$SKIP_BINARIES" = false ] && [ "$PACKAGE_ONLY" = false ]; then
  122. print_status "Building backend binaries..."
  123. "$SCRIPT_DIR/build_opencode.sh"
  124. fi
  125. if [ "$PACKAGE_ONLY" = false ]; then
  126. print_status "Compiling TypeScript..."
  127. if [ "$BUILD_TYPE" = "production" ]; then
  128. run_script compile:production
  129. else
  130. run_script compile
  131. fi
  132. fi
  133. if [ "$PACKAGE_ONLY" = false ]; then
  134. print_status "Running linter..."
  135. set +e
  136. run_script lint
  137. if [[ $? -ne 0 ]]; then
  138. print_warning "Linting failed, continuing with build..."
  139. fi
  140. set -e
  141. fi
  142. if [ "$SKIP_TESTS" = false ] && [ "$PACKAGE_ONLY" = false ]; then
  143. print_status "Running tests..."
  144. set +e
  145. run_script test
  146. if [[ $? -ne 0 ]]; then
  147. print_warning "Tests failed, continuing with build..."
  148. fi
  149. set -e
  150. fi
  151. print_status "Checking for required binaries..."
  152. BINARY_PATHS=(
  153. "resources/bin/windows/amd64/opencode.exe"
  154. "resources/bin/macos/amd64/opencode"
  155. "resources/bin/macos/arm64/opencode"
  156. "resources/bin/linux/amd64/opencode"
  157. "resources/bin/linux/arm64/opencode"
  158. )
  159. MISSING_BINARIES=false
  160. for binary_path in "${BINARY_PATHS[@]}"; do
  161. if [ ! -f "$binary_path" ]; then
  162. print_warning "Missing binary: $binary_path"
  163. MISSING_BINARIES=true
  164. fi
  165. done
  166. if [ "$MISSING_BINARIES" = true ]; then
  167. print_warning "Some binaries are missing. The extension may not work on all platforms."
  168. print_warning "Run '$SCRIPT_DIR/build_opencode.sh' from the root directory to build all binaries."
  169. fi
  170. print_status "Creating VSCode extension package..."
  171. VSCE_CMD="vsce"
  172. if ! command -v vsce >/dev/null 2>&1; then
  173. if command -v npx >/dev/null 2>&1; then
  174. VSCE_CMD="npx -y @vscode/vsce"
  175. else
  176. print_warning "vsce not found and npx unavailable; attempting global install via npm"
  177. npm install -g @vscode/vsce
  178. fi
  179. fi
  180. if [ "$BUILD_TYPE" = "production" ]; then
  181. eval "$VSCE_CMD package --no-dependencies --out 'opencode-vscode-$(date +%Y%m%d-%H%M%S).vsix'"
  182. else
  183. eval "$VSCE_CMD package --pre-release --no-dependencies --out 'opencode-vscode-dev-$(date +%Y%m%d-%H%M%S).vsix'"
  184. fi
  185. print_status "Build completed successfully!"
  186. print_status "Extension package created in: $PLUGIN_DIR"
  187. shopt -s nullglob
  188. VSIX_FILES=( *.vsix )
  189. shopt -u nullglob
  190. if ((${#VSIX_FILES[@]} > 0)); then
  191. echo "Packages created:"
  192. for vsix in "${VSIX_FILES[@]}"; do
  193. echo " $vsix"
  194. done
  195. fi