aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/build.sh
blob: c4a5569f345ec7568060cacfd5f5627b4d7c3abb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env bash
set -eou pipefail

source "$(dirname "${BASH_SOURCE[0]}")/lib/log.sh"
source "$(dirname "${BASH_SOURCE[0]}")/lib/path.sh"

readonly OUTPUT_DIR="${OUTPUT_DIR:-$PATH_PROJECT_DIR/bin}"
readonly BINARY_NAME="bolt"

usage() {
  echo "Usage: $0 [OPTIONS] [TARGET]"
  echo ""
  echo "Build the Bolt CLI binary"
  echo ""
  echo "Options:"
  echo "  -o, --output DIR  Output directory for built binaries (default: ./bin)"
  echo "  -r, --race        Build with race detector enabled"
  echo "  -v, --verbose     Enable verbose build output"
  echo ""
  echo "Targets:"
  echo "  local             Build for current platform (default)"
  echo "  linux             Build for Linux (amd64)"
  echo "  darwin            Build for macOS (amd64)"
  echo "  windows           Build for Windows (amd64)"
  echo "  all               Build for all supported platforms"
  echo ""
  echo "Examples:"
  echo "  $0                    # Build for current platform"
  echo "  $0 linux              # Build for Linux"
  echo "  $0 --race local       # Build with race detector"
  echo "  $0 -o ./dist all      # Build all platforms to ./dist"
}

main() {
  local target="local"
  local race_flag=""
  local verbose_flag=""
  local output_dir="$OUTPUT_DIR"

  parse_args "$@"

  case "$target" in
    local)
      build_local "$race_flag" "$verbose_flag" "$output_dir"
      ;;
    linux)
      build_target "linux" "amd64" "$output_dir"
      ;;
    darwin)
      build_target "darwin" "amd64" "$output_dir"
      ;;
    windows)
      build_target "windows" "amd64" "$output_dir"
      ;;
    all)
      build_all "$output_dir"
      ;;
    *)
      log_error "Invalid target: $target"
      log_newline
      usage
      exit 1
      ;;
  esac
}

parse_args() {
  while [[ $# -gt 0 ]]; do
    case "$1" in
      -h | --help)
        usage
        exit 0
        ;;
      -o | --output)
        if [[ -z "${2:-}" ]]; then
          log_error "Option $1 requires an argument"
          exit 1
        fi
        output_dir="$2"
        shift 2
        ;;
      -r | --race)
        race_flag="-race"
        shift
        ;;
      -v | --verbose)
        verbose_flag="-v"
        shift
        ;;
      -*)
        log_error "Unknown option: $1"
        log_newline
        usage
        exit 1
        ;;
      *)
        target="$1"
        shift
        ;;
    esac
  done
}

build_local() {
  local race_flag="$1"
  local verbose_flag="$2"
  local output_dir="$3"

  log_header "Building for current platform"

  ensure_output_dir "$output_dir"

  local output_path="$output_dir/$BINARY_NAME"
  log_step "Building binary to $output_path"

  # shellcheck disable=SC2086
  if go build $verbose_flag $race_flag -o "$output_path" "$PATH_PROJECT_DIR/cmd/bolt"; then
    log_success "Build completed: $output_path"
    show_binary_info "$output_path"
  else
    log_error "Build failed"
    return 1
  fi
}

build_target() {
  local goos="$1"
  local goarch="$2"
  local output_dir="$3"

  log_header "Building for $goos/$goarch"

  ensure_output_dir "$output_dir"

  local output_path="$output_dir/${BINARY_NAME}-${goos}-${goarch}"
  if [[ "$goos" == "windows" ]]; then
    output_path="${output_path}.exe"
  fi

  log_step "Building binary to $output_path"

  if GOOS="$goos" GOARCH="$goarch" go build -o "$output_path" "$PATH_PROJECT_DIR/cmd/bolt"; then
    log_success "Build completed: $output_path"
    show_binary_info "$output_path"
  else
    log_error "Build failed for $goos/$goarch"
    return 1
  fi
}

build_all() {
  local output_dir="$1"

  log_header "Building for all supported platforms"

  local failed_builds=()

  if ! build_target "linux" "amd64" "$output_dir"; then
    failed_builds+=("linux/amd64")
  fi

  if ! build_target "darwin" "amd64" "$output_dir"; then
    failed_builds+=("darwin/amd64")
  fi

  if ! build_target "windows" "amd64" "$output_dir"; then
    failed_builds+=("windows/amd64")
  fi

  log_newline
  if [[ ${#failed_builds[@]} -eq 0 ]]; then
    log_success "All builds completed successfully!"
  else
    log_error "Failed builds: ${failed_builds[*]}"
    exit 1
  fi
}

ensure_output_dir() {
  local dir="$1"
  if [[ ! -d "$dir" ]]; then
    log_step "Creating output directory: $dir"
    mkdir -p "$dir"
  fi
}

show_binary_info() {
  local binary_path="$1"
  if [[ -f "$binary_path" ]]; then
    local size
    if [[ "$OSTYPE" == "darwin"* ]]; then
      size=$(stat -f%z "$binary_path")
    else
      size=$(stat -c%s "$binary_path" 2>/dev/null || echo "unknown")
    fi

    if [[ "$size" != "unknown" ]]; then
      local size_mb=$((size / 1024 / 1024))
      log_substep "Binary size: ${size_mb}MB (${size} bytes)"
    fi
  fi
}

main "$@"