build_opencode.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. OPENCODE_VERSION="${OPENCODE_VERSION:-$(node -p "require('$OPENCODE_DIR/package.json').version")}"
  31. export OPENCODE_VERSION
  32. echo "=> Building opencode distribution (version $OPENCODE_VERSION)"
  33. (
  34. cd "$OPENCODE_DIR"
  35. bun script/build.ts
  36. )
  37. if [[ ! -d "$DIST_DIR" ]]; then
  38. echo "Error: expected dist directory not found at $DIST_DIR" >&2
  39. exit 1
  40. fi
  41. prepare_output_dir "$JETBRAINS_BIN_DIR"
  42. prepare_output_dir "$VSCODE_BIN_DIR"
  43. shopt -s nullglob
  44. dist_entries=("$DIST_DIR"/opencode-*)
  45. if [[ ${#dist_entries[@]} -eq 0 ]]; then
  46. echo "Error: no opencode distribution folders found in $DIST_DIR" >&2
  47. exit 1
  48. fi
  49. for dir in "${dist_entries[@]}"; do
  50. [[ -d "$dir" ]] || continue
  51. name="$(basename "$dir")"
  52. suffix="${name#opencode-}"
  53. if [[ "$suffix" == "$name" ]]; then
  54. echo "Warning: skipping unrecognised dist folder $name" >&2
  55. continue
  56. fi
  57. os="${suffix%%-*}"
  58. arch="${suffix#${os}-}"
  59. if [[ -z "$arch" || "$arch" == "$suffix" ]]; then
  60. echo "Warning: skipping $name due to missing architecture component" >&2
  61. continue
  62. fi
  63. if [[ "$arch" == "x64-baseline" ]]; then
  64. echo "Skipping baseline target $name"
  65. continue
  66. fi
  67. os_dir="$os"
  68. if [[ "$os_dir" == "darwin" ]]; then
  69. os_dir="macos"
  70. fi
  71. arch_dir="$arch"
  72. if [[ "$arch_dir" == "x64" ]]; then
  73. arch_dir="amd64"
  74. fi
  75. binary_src="$dir/bin/opencode"
  76. binary_name="opencode"
  77. if [[ "$os" == "windows" ]]; then
  78. binary_src="$dir/bin/opencode.exe"
  79. binary_name="opencode.exe"
  80. fi
  81. if [[ ! -f "$binary_src" ]]; then
  82. echo "Warning: binary not found at $binary_src" >&2
  83. continue
  84. fi
  85. jetbrains_target="$JETBRAINS_BIN_DIR/$os_dir/$arch_dir"
  86. vscode_target="$VSCODE_BIN_DIR/$os_dir/$arch_dir"
  87. mkdir -p "$jetbrains_target"
  88. mkdir -p "$vscode_target"
  89. cp "$binary_src" "$jetbrains_target/$binary_name"
  90. cp "$binary_src" "$vscode_target/$binary_name"
  91. if [[ "$os" != "windows" ]]; then
  92. chmod +x "$jetbrains_target/$binary_name"
  93. chmod +x "$vscode_target/$binary_name"
  94. fi
  95. echo "=> Prepared binaries for $os/$arch"
  96. done
  97. shopt -u nullglob
  98. echo
  99. echo "All done. Binaries placed under:"
  100. echo " JetBrains: $JETBRAINS_BIN_DIR"
  101. echo " VSCode: $VSCODE_BIN_DIR"
  102. echo "opencode dists remain in $DIST_DIR"