build_opencode.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # Build opencode for multiple platforms and place binaries in both JetBrains and VSCode plugin resources.
  4. ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
  5. OPENCODE_DIR="$ROOT_DIR/packages/opencode"
  6. DIST_DIR="$OPENCODE_DIR/dist"
  7. JETBRAINS_BIN_DIR="$ROOT_DIR/hosts/jetbrains-plugin/src/main/resources/bin"
  8. VSCODE_BIN_DIR="$ROOT_DIR/hosts/vscode-plugin/resources/bin"
  9. if [[ ! -d "$OPENCODE_DIR" ]]; then
  10. echo "Error: opencode package directory not found at $OPENCODE_DIR" >&2
  11. exit 1
  12. fi
  13. if ! command -v bun >/dev/null 2>&1; then
  14. echo "Error: bun command not found in PATH" >&2
  15. exit 1
  16. fi
  17. prepare_output_dir() {
  18. local dir="$1"
  19. if [[ -z "$dir" ]]; then
  20. echo "Error: output directory path is empty" >&2
  21. exit 1
  22. fi
  23. if [[ "$dir" == "/" ]]; then
  24. echo "Error: refusing to clear root directory" >&2
  25. exit 1
  26. fi
  27. rm -rf "$dir"
  28. mkdir -p "$dir"
  29. }
  30. echo "=> Building opencode distribution"
  31. (
  32. cd "$OPENCODE_DIR"
  33. bun script/build.ts
  34. )
  35. if [[ ! -d "$DIST_DIR" ]]; then
  36. echo "Error: expected dist directory not found at $DIST_DIR" >&2
  37. exit 1
  38. fi
  39. prepare_output_dir "$JETBRAINS_BIN_DIR"
  40. prepare_output_dir "$VSCODE_BIN_DIR"
  41. shopt -s nullglob
  42. dist_entries=("$DIST_DIR"/opencode-*)
  43. if [[ ${#dist_entries[@]} -eq 0 ]]; then
  44. echo "Error: no opencode distribution folders found in $DIST_DIR" >&2
  45. exit 1
  46. fi
  47. for dir in "${dist_entries[@]}"; do
  48. [[ -d "$dir" ]] || continue
  49. name="$(basename "$dir")"
  50. suffix="${name#opencode-}"
  51. if [[ "$suffix" == "$name" ]]; then
  52. echo "Warning: skipping unrecognised dist folder $name" >&2
  53. continue
  54. fi
  55. os="${suffix%%-*}"
  56. arch="${suffix#${os}-}"
  57. if [[ -z "$arch" || "$arch" == "$suffix" ]]; then
  58. echo "Warning: skipping $name due to missing architecture component" >&2
  59. continue
  60. fi
  61. if [[ "$arch" == "x64-baseline" ]]; then
  62. echo "Skipping baseline target $name"
  63. continue
  64. fi
  65. os_dir="$os"
  66. if [[ "$os_dir" == "darwin" ]]; then
  67. os_dir="macos"
  68. fi
  69. arch_dir="$arch"
  70. if [[ "$arch_dir" == "x64" ]]; then
  71. arch_dir="amd64"
  72. fi
  73. binary_src="$dir/bin/opencode"
  74. binary_name="opencode"
  75. if [[ "$os" == "windows" ]]; then
  76. binary_src="$dir/bin/opencode.exe"
  77. binary_name="opencode.exe"
  78. fi
  79. if [[ ! -f "$binary_src" ]]; then
  80. echo "Warning: binary not found at $binary_src" >&2
  81. continue
  82. fi
  83. jetbrains_target="$JETBRAINS_BIN_DIR/$os_dir/$arch_dir"
  84. vscode_target="$VSCODE_BIN_DIR/$os_dir/$arch_dir"
  85. mkdir -p "$jetbrains_target"
  86. mkdir -p "$vscode_target"
  87. cp "$binary_src" "$jetbrains_target/$binary_name"
  88. cp "$binary_src" "$vscode_target/$binary_name"
  89. if [[ "$os" != "windows" ]]; then
  90. chmod +x "$jetbrains_target/$binary_name"
  91. chmod +x "$vscode_target/$binary_name"
  92. fi
  93. echo "=> Prepared binaries for $os/$arch"
  94. done
  95. shopt -u nullglob
  96. echo
  97. echo "All done. Binaries placed under:"
  98. echo " JetBrains: $JETBRAINS_BIN_DIR"
  99. echo " VSCode: $VSCODE_BIN_DIR"
  100. echo "opencode dists remain in $DIST_DIR"