diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2025-10-13 09:56:31 -0400 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2025-10-13 09:56:31 -0400 |
| commit | 6647f7cb41e821889334e986f55a03770ba4fe8b (patch) | |
| tree | 5111b92752835013441a2f33decfbb805ee59d3e /scripts | |
| parent | Extract out subcmd package to standalone library (diff) | |
| download | cli-6647f7cb41e821889334e986f55a03770ba4fe8b.zip | |
Add build.sh script
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/build.sh | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..c4a5569 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,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 "$@" |