-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild
More file actions
executable file
·1054 lines (897 loc) · 35 KB
/
Copy pathbuild
File metadata and controls
executable file
·1054 lines (897 loc) · 35 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# ============================================================================
# KalamDB Unified Build Tool
# ============================================================================
# Interactive build tool for creating releases, binaries, and Docker images
#
# Usage:
# ./tools/build # Interactive mode
# ./tools/build --help # Show help
# ./tools/build --batch # Non-interactive mode (uses defaults/flags)
#
# Batch mode flags:
# --batch # Enable non-interactive mode
# --platforms <list> # Comma-separated platforms (e.g., linux-aarch64,macos-aarch64)
# --all-platforms # Build all platforms
# --docker # Build Docker image
# --docker-push # Build and push Docker image
# --github-release # Create GitHub release
# --version-tag <tag> # Version tag (default: v<version>)
#
# Examples:
# ./tools/build --batch --platforms linux-aarch64,macos-aarch64 --docker-push
# ./tools/build --batch --all-platforms --github-release --docker-push
#
# Features:
# - Auto-detects version from Cargo.toml
# - Multi-platform binary builds
# - GitHub release creation
# - Docker Hub publishing
# ============================================================================
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
DOCKER_DIR="$ROOT_DIR/docker/build"
DIST_DIR="$ROOT_DIR/dist"
REPO_OWNER="jamals86"
REPO_NAME="KalamDB"
DOCKERHUB_REPO="jamals86/kalamdb"
PROFILE="release-dist"
CROSS_PROFILE="docker" # Use docker profile for cross-compilation (less memory intensive)
# Platform configurations: name:rust_target:file_ext
PLATFORMS=(
"linux-x86_64:x86_64-unknown-linux-gnu:"
"linux-aarch64:aarch64-unknown-linux-gnu:"
"macos-x86_64:x86_64-apple-darwin:"
"macos-aarch64:aarch64-apple-darwin:"
"windows-x86_64:x86_64-pc-windows-gnu:.exe"
)
# Docker platform configurations
DOCKER_PLATFORMS=(
"linux-amd64:linux/amd64"
"linux-arm64:linux/arm64"
)
# ============================================================================
# Helper Functions
# ============================================================================
print_header() {
echo ""
echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║${NC}${BOLD} KalamDB Build Tool ${NC}${BLUE}║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}"
echo ""
}
print_step() {
echo -e "${CYAN}▶${NC} $1"
}
print_success() {
echo -e "${GREEN}✓${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
print_error() {
echo -e "${RED}✗${NC} $1"
}
print_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
# Show help
show_help() {
cat << EOF
${BOLD}KalamDB Build Tool${NC}
Interactive build tool for creating releases, binaries, and Docker images.
${BOLD}USAGE:${NC}
./tools/build Run in interactive mode
./tools/build --help Show this help message
./tools/build --batch Run in non-interactive batch mode
${BOLD}BATCH MODE FLAGS:${NC}
--batch Enable non-interactive mode
--platforms <list> Comma-separated platforms to build
Available: linux-x86_64, linux-aarch64, macos-x86_64,
macos-aarch64, windows-x86_64
--all-platforms Build all platforms
--docker Build Docker image (local only)
--docker-push Build and push Docker image to Docker Hub
--github-release Create GitHub release
--version-tag <tag> Version tag (default: v<version from Cargo.toml>)
${BOLD}EXAMPLES:${NC}
# Build Linux ARM, macOS ARM, and push Docker
./tools/build --batch --platforms linux-aarch64,macos-aarch64 --docker-push
# Build all platforms and create GitHub release
./tools/build --batch --all-platforms --github-release
# Full release: all platforms, GitHub, and Docker
./tools/build --batch --all-platforms --github-release --docker-push
${BOLD}FEATURES:${NC}
• Auto-detects version from Cargo.toml
• Multi-platform binary builds (Linux, macOS, Windows)
• GitHub release creation with checksums
• Docker Hub publishing (multi-arch)
${BOLD}PREREQUISITES:${NC}
• Rust toolchain installed
• GitHub CLI (gh) for releases: brew install gh && gh auth login
• Docker for container builds: https://docker.com
• cross for cross-compilation: cargo install cross
${BOLD}SUPPORTED PLATFORMS:${NC}
Binaries:
• linux-x86_64 - Linux Intel/AMD 64-bit
• linux-aarch64 - Linux ARM 64-bit
• macos-x86_64 - macOS Intel
• macos-aarch64 - macOS Apple Silicon
• windows-x86_64 - Windows 64-bit
Docker:
• linux-amd64 - Docker for x86_64
• linux-arm64 - Docker for ARM64
EOF
exit 0
}
# Extract version from Cargo.toml
get_version() {
grep '^version = ' "$ROOT_DIR/Cargo.toml" | head -1 | sed 's/version = "\(.*\)"/\1/'
}
# Prompt for yes/no
prompt_yes_no() {
local prompt="$1"
local default="${2:-n}"
local yn
if [[ "$default" == "y" ]]; then
read -p "$(echo -e "${CYAN}?${NC} $prompt [Y/n]: ")" yn
yn="${yn:-y}"
else
read -p "$(echo -e "${CYAN}?${NC} $prompt [y/N]: ")" yn
yn="${yn:-n}"
fi
[[ "$yn" =~ ^[Yy] ]]
}
# Multi-select menu (text-based, more reliable)
multi_select() {
local prompt="$1"
shift
local options=("$@")
local num_options=${#options[@]}
local selected=()
# Initialize all as selected (1 = selected, 0 = not selected)
for ((i=0; i<num_options; i++)); do
selected+=("1")
done
while true; do
echo ""
echo -e "${CYAN}?${NC} $prompt"
echo -e " ${BLUE}(Enter numbers to toggle, 'a' for all, 'n' for none, press Enter when done)${NC}"
echo ""
for ((i=0; i<num_options; i++)); do
local check
if [[ "${selected[$i]}" == "1" ]]; then
check="${GREEN}◉${NC}"
else
check="○"
fi
echo -e " ${BOLD}$((i+1))${NC}) ${check} ${options[$i]}"
done
echo ""
read -p "$(echo -e "${CYAN}>${NC} ")" input
case "$input" in
"")
# Done - exit loop (just press Enter)
break
;;
a|A|all)
# Select all
for ((i=0; i<num_options; i++)); do
selected[$i]="1"
done
clear_lines $((num_options + 6))
;;
n|N|none)
# Deselect all
for ((i=0; i<num_options; i++)); do
selected[$i]="0"
done
clear_lines $((num_options + 6))
;;
*)
# Toggle specific numbers (comma or space separated)
local nums
IFS=', ' read -ra nums <<< "$input"
for num in "${nums[@]}"; do
if [[ "$num" =~ ^[0-9]+$ ]] && [[ $num -ge 1 ]] && [[ $num -le $num_options ]]; then
local idx=$((num - 1))
if [[ "${selected[$idx]}" == "1" ]]; then
selected[$idx]="0"
else
selected[$idx]="1"
fi
fi
done
clear_lines $((num_options + 6))
;;
esac
done
# Show final selection
echo -e "${GREEN}Selected:${NC}"
# Return selected indices
SELECTED_INDICES=()
for ((i=0; i<num_options; i++)); do
if [[ "${selected[$i]}" == "1" ]]; then
SELECTED_INDICES+=("$i")
echo -e " ${GREEN}✓${NC} ${options[$i]}"
fi
done
echo ""
}
# Clear N lines above cursor
clear_lines() {
local count=$1
for ((i=0; i<count; i++)); do
tput cuu1 # Move up
tput el # Clear line
done
}
# Single select menu (text-based)
single_select() {
local prompt="$1"
shift
local options=("$@")
local num_options=${#options[@]}
echo ""
echo -e "${CYAN}?${NC} $prompt"
echo ""
for ((i=0; i<num_options; i++)); do
echo -e " ${BOLD}$((i+1))${NC}) ${options[$i]}"
done
echo ""
while true; do
read -p "$(echo -e "${CYAN}>${NC} Enter number [1-$num_options]: ")" input
if [[ "$input" =~ ^[0-9]+$ ]] && [[ $input -ge 1 ]] && [[ $input -le $num_options ]]; then
SELECTED_INDEX=$((input - 1))
echo -e "${GREEN}Selected:${NC} ${options[$SELECTED_INDEX]}"
echo ""
break
else
echo -e "${RED}Invalid selection. Enter a number between 1 and $num_options${NC}"
fi
done
}
# ============================================================================
# Build Functions
# ============================================================================
# Generate version.toml with git info for build scripts
generate_version_toml() {
local version_file="$ROOT_DIR/version.toml"
# Get git info
local git_commit
local git_branch
local build_date
git_commit=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
git_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
build_date=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
# Write version.toml
cat > "$version_file" << EOF
# Auto-generated version info - DO NOT EDIT MANUALLY
# Generated by tools/build at $build_date
[version]
git_commit_hash = "$git_commit"
git_branch = "$git_branch"
build_date = "$build_date"
EOF
print_success "Generated version.toml (commit: $git_commit, branch: $git_branch)"
}
setup_macos_env() {
if [[ "$(uname -s)" == "Darwin" ]]; then
if [ -d "$(xcode-select -p 2>/dev/null)/Toolchains/XcodeDefault.xctoolchain/usr/lib" ]; then
XCODE_LIB="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain/usr/lib"
export DYLD_LIBRARY_PATH="${XCODE_LIB}:${DYLD_LIBRARY_PATH:-}"
export LIBCLANG_PATH="${XCODE_LIB}"
export BINDGEN_EXTRA_CLANG_ARGS="-I$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain/usr/include"
elif [ -d "/opt/homebrew/opt/llvm/lib" ]; then
export DYLD_LIBRARY_PATH="/opt/homebrew/opt/llvm/lib:${DYLD_LIBRARY_PATH:-}"
export LIBCLANG_PATH="/opt/homebrew/opt/llvm/lib"
elif [ -d "/usr/local/opt/llvm/lib" ]; then
export DYLD_LIBRARY_PATH="/usr/local/opt/llvm/lib:${DYLD_LIBRARY_PATH:-}"
export LIBCLANG_PATH="/usr/local/opt/llvm/lib"
fi
fi
}
build_platform() {
local platform_name="$1"
local rust_target="$2"
local file_ext="$3"
local version="$4"
print_step "Building for ${BOLD}$platform_name${NC}..."
local cli_bin="kalam${file_ext}"
local server_bin="kalamdb-server${file_ext}"
local output_dir="$DIST_DIR/$version"
mkdir -p "$output_dir"
# Add target
rustup target add "$rust_target" 2>/dev/null || true
# Determine if cross-compilation is needed
local build_cmd="cargo"
local current_target=""
case "$(uname -s)-$(uname -m)" in
Darwin-arm64) current_target="aarch64-apple-darwin" ;;
Darwin-x86_64) current_target="x86_64-apple-darwin" ;;
Linux-x86_64) current_target="x86_64-unknown-linux-gnu" ;;
Linux-aarch64) current_target="aarch64-unknown-linux-gnu" ;;
esac
# Check if cross-compilation is needed
local needs_cross=false
case "$(uname -s)" in
Darwin)
# macOS can only natively build for macOS targets
if [[ "$rust_target" == *"linux"* ]] || [[ "$rust_target" == *"windows"* ]]; then
needs_cross=true
fi
;;
Linux)
# Linux can only natively build for Linux targets (same arch)
if [[ "$rust_target" == *"darwin"* ]] || [[ "$rust_target" == *"windows"* ]]; then
needs_cross=true
elif [[ "$rust_target" != "$current_target" ]]; then
needs_cross=true
fi
;;
esac
if [[ "$needs_cross" == true ]]; then
if command -v cross &> /dev/null; then
build_cmd="cross"
else
print_error "Cannot build $platform_name on $(uname -s) without 'cross' tool"
print_info "Install cross: cargo install cross"
print_info "Note: cross requires Docker to be running"
return 1
fi
fi
# Select profile based on whether we're cross-compiling
local selected_profile="$PROFILE"
if [[ "$build_cmd" == "cross" ]]; then
selected_profile="$CROSS_PROFILE"
print_info "Using '$CROSS_PROFILE' profile for cross-compilation (less memory intensive)"
# cross uses buildkit/buildx by default. If buildx isn't available, fall back.
if command -v docker &> /dev/null; then
if ! docker buildx version &> /dev/null; then
print_warning "docker buildx not available; setting CROSS_CONTAINER_ENGINE_NO_BUILDKIT=1"
export CROSS_CONTAINER_ENGINE_NO_BUILDKIT=1
fi
fi
fi
local build_dir="$ROOT_DIR/target/${rust_target}/${selected_profile}"
# Build with full output
echo ""
cd "$ROOT_DIR"
if ! $build_cmd build --profile "$selected_profile" --target "$rust_target"; then
print_error "Build command failed for $platform_name"
return 1
fi
echo ""
# Check and copy binaries
if [[ ! -f "$build_dir/$cli_bin" ]] || [[ ! -f "$build_dir/$server_bin" ]]; then
print_error "Build failed for $platform_name"
return 1
fi
local cli_output="kalam-${version}-${platform_name}${file_ext}"
local server_output="kalamdb-server-${version}-${platform_name}${file_ext}"
cp "$build_dir/$cli_bin" "$output_dir/$cli_output"
cp "$build_dir/$server_bin" "$output_dir/$server_output"
# Make executable
if [[ -z "$file_ext" ]]; then
chmod +x "$output_dir/$cli_output"
chmod +x "$output_dir/$server_output"
fi
# Create archives
cd "$output_dir"
if [[ "$platform_name" == *"windows"* ]]; then
if command -v zip &> /dev/null; then
zip -q "${cli_output%.exe}.zip" "$cli_output"
zip -q "${server_output%.exe}.zip" "$server_output"
fi
else
tar -czf "${cli_output}.tar.gz" "$cli_output"
tar -czf "${server_output}.tar.gz" "$server_output"
fi
cd "$ROOT_DIR"
print_success "Built $platform_name"
}
generate_checksums() {
local version="$1"
local output_dir="$DIST_DIR/$version"
cd "$output_dir"
if command -v sha256sum &> /dev/null; then
sha256sum *.tar.gz *.zip 2>/dev/null > SHA256SUMS || true
elif command -v shasum &> /dev/null; then
shasum -a 256 *.tar.gz *.zip 2>/dev/null > SHA256SUMS || true
fi
cd "$ROOT_DIR"
}
create_github_release() {
local version="$1"
local tag="$2"
local output_dir="$DIST_DIR/$version"
print_step "Creating GitHub release ${BOLD}$tag${NC}..."
# Check if release exists
if gh release view "$tag" --repo "$REPO_OWNER/$REPO_NAME" &> /dev/null; then
if [[ "$BATCH_MODE" == true ]]; then
# In batch mode, auto-delete existing release
print_warning "Release $tag already exists. Deleting in batch mode..."
gh release delete "$tag" --repo "$REPO_OWNER/$REPO_NAME" --yes
elif prompt_yes_no "Release $tag already exists. Delete and recreate?"; then
gh release delete "$tag" --repo "$REPO_OWNER/$REPO_NAME" --yes
else
print_warning "Skipping GitHub release"
return 0
fi
fi
# Create release notes
local release_notes="## KalamDB $tag
### Installation
#### Linux/macOS
\`\`\`bash
# Download and extract
wget https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$tag/kalamdb-server-$version-<platform>.tar.gz
tar -xzf kalamdb-server-$version-<platform>.tar.gz
chmod +x kalamdb-server-$version-<platform>
sudo mv kalamdb-server-$version-<platform> /usr/local/bin/kalamdb-server
\`\`\`
#### Windows
Download the \`.zip\` file and extract to a directory in your PATH.
### Checksums
SHA256 checksums available in \`SHA256SUMS\`.
"
# Collect files to upload (only existing files)
local upload_files=()
for f in "$output_dir"/*.tar.gz "$output_dir"/*.zip; do
[[ -f "$f" ]] && upload_files+=("$f")
done
[[ -f "$output_dir/SHA256SUMS" ]] && upload_files+=("$output_dir/SHA256SUMS")
if [[ ${#upload_files[@]} -eq 0 ]]; then
print_error "No files to upload for release"
return 1
fi
echo ""
print_info "Uploading ${#upload_files[@]} files..."
for f in "${upload_files[@]}"; do
echo " • $(basename "$f")"
done
echo ""
if ! gh release create "$tag" \
--repo "$REPO_OWNER/$REPO_NAME" \
--title "KalamDB $tag" \
--notes "$release_notes" \
--draft \
"${upload_files[@]}"; then
print_error "Failed to create GitHub release"
return 1
fi
print_success "GitHub draft release created: https://github.com/$REPO_OWNER/$REPO_NAME/releases/tag/$tag"
}
build_docker_image() {
local version="$1"
local push="${2:-false}" # Optional: push after build (needed for multi-arch)
print_step "Building Docker image ${BOLD}$DOCKERHUB_REPO:$version${NC}..."
echo ""
cd "$ROOT_DIR"
# Check if buildx is available for multi-platform builds
if docker buildx version &>/dev/null; then
print_info "Using Docker Buildx for multi-architecture build (linux/amd64, linux/arm64)"
# Create/use a builder with multi-platform support
if ! docker buildx inspect kalamdb-builder &>/dev/null; then
print_info "Creating buildx builder..."
docker buildx create --name kalamdb-builder --use --bootstrap
else
docker buildx use kalamdb-builder
fi
# Build command args
local build_args=(
"--platform" "linux/amd64,linux/arm64"
"--tag" "$DOCKERHUB_REPO:$version"
"--tag" "$DOCKERHUB_REPO:latest"
"--file" "$DOCKER_DIR/Dockerfile"
"--progress=plain"
)
# For multi-arch, we need to either push or load
# --load only works for single platform, so we push for multi-arch
if [[ "$push" == "true" ]]; then
build_args+=("--push")
print_info "Building and pushing multi-arch image..."
else
print_warning "Multi-arch build requires --push. Building for local arch only."
build_args=("--tag" "$DOCKERHUB_REPO:$version" "--tag" "$DOCKERHUB_REPO:latest" "--file" "$DOCKER_DIR/Dockerfile" "--load" "--progress=plain")
fi
if ! DOCKER_BUILDKIT=1 docker buildx build "${build_args[@]}" .; then
print_error "Docker build failed"
return 1
fi
else
print_warning "Docker Buildx not available. Building for local architecture only."
print_info "Install buildx for multi-arch support: https://docs.docker.com/buildx/working-with-buildx/"
# Fallback to regular build
if ! DOCKER_BUILDKIT=1 docker build \
--tag "$DOCKERHUB_REPO:$version" \
--tag "$DOCKERHUB_REPO:latest" \
--file "$DOCKER_DIR/Dockerfile" \
--progress=plain \
.; then
print_error "Docker build failed"
return 1
fi
fi
echo ""
print_success "Docker image built: $DOCKERHUB_REPO:$version"
# Show image size (only works for local images)
if docker image inspect "$DOCKERHUB_REPO:$version" &>/dev/null; then
local image_size
image_size=$(docker image inspect "$DOCKERHUB_REPO:$version" --format='{{.Size}}' 2>/dev/null | awk '{printf "%.1f MB", $1/1024/1024}')
print_info "Image size: $image_size"
fi
}
push_docker_image() {
local version="$1"
print_step "Building and pushing multi-arch Docker image to Docker Hub..."
echo ""
# For multi-arch, we need to build and push in one step
# This is because buildx can't load multi-arch images locally
if docker buildx version &>/dev/null; then
# Use the build function with push=true for multi-arch
build_docker_image "$version" "true"
echo ""
print_success "Pushed multi-arch image to Docker Hub: $DOCKERHUB_REPO:$version"
print_info "Platforms: linux/amd64, linux/arm64"
print_info "View at: https://hub.docker.com/r/$DOCKERHUB_REPO"
else
# Fallback: push single-arch image
print_warning "Docker Buildx not available. Pushing single-arch image only."
if ! docker push "$DOCKERHUB_REPO:$version"; then
print_error "Failed to push $DOCKERHUB_REPO:$version"
return 1
fi
print_step "Pushing ${BOLD}$DOCKERHUB_REPO:latest${NC} to Docker Hub..."
if ! docker push "$DOCKERHUB_REPO:latest"; then
print_error "Failed to push $DOCKERHUB_REPO:latest"
return 1
fi
echo ""
print_success "Pushed to Docker Hub: $DOCKERHUB_REPO:$version"
print_info "View at: https://hub.docker.com/r/$DOCKERHUB_REPO"
fi
}
# Check Docker prerequisites
check_docker_prereqs() {
# Check Docker is installed
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed"
print_info "Install from: https://docs.docker.com/get-docker/"
return 1
fi
# Check Docker daemon is running
if ! docker info &> /dev/null; then
print_error "Docker daemon is not running"
print_info "Start Docker Desktop or run: sudo systemctl start docker"
return 1
fi
print_success "Docker daemon is running"
return 0
}
# Check Docker Hub login status
check_docker_login() {
print_step "Checking Docker Hub login status..."
# Try to check if logged in by inspecting config
if docker info 2>/dev/null | grep -qi "Username:"; then
local username
username=$(docker info 2>/dev/null | grep -i "Username:" | awk '{print $2}')
print_success "Logged in to Docker Hub as: $username"
return 0
fi
# Check docker config for credentials
if [[ -f "$HOME/.docker/config.json" ]] && grep -q "index.docker.io" "$HOME/.docker/config.json" 2>/dev/null; then
print_success "Docker Hub credentials found in config"
return 0
fi
# Not logged in - prompt user
print_warning "Not logged in to Docker Hub"
echo ""
if prompt_yes_no "Login to Docker Hub now?"; then
echo ""
if docker login; then
print_success "Docker Hub login successful"
return 0
else
print_error "Docker Hub login failed"
return 1
fi
else
print_warning "Push may fail without login"
return 0
fi
}
# ============================================================================
# Main
# ============================================================================
# Parse command line arguments for batch mode
parse_args() {
BATCH_MODE=false
BATCH_PLATFORMS=""
BATCH_ALL_PLATFORMS=false
BATCH_DOCKER=false
BATCH_DOCKER_PUSH=false
BATCH_GITHUB_RELEASE=false
BATCH_VERSION_TAG=""
while [[ $# -gt 0 ]]; do
case "$1" in
--help|-h)
show_help
;;
--batch)
BATCH_MODE=true
shift
;;
--platforms)
BATCH_PLATFORMS="$2"
shift 2
;;
--all-platforms)
BATCH_ALL_PLATFORMS=true
shift
;;
--docker)
BATCH_DOCKER=true
shift
;;
--docker-push)
BATCH_DOCKER=true
BATCH_DOCKER_PUSH=true
shift
;;
--github-release)
BATCH_GITHUB_RELEASE=true
shift
;;
--version-tag)
BATCH_VERSION_TAG="$2"
shift 2
;;
*)
print_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
}
main() {
# Parse arguments first
parse_args "$@"
# Handle --help (already handled in parse_args, but keep for direct calls)
if [[ "${1:-}" == "--help" ]] || [[ "${1:-}" == "-h" ]]; then
show_help
fi
print_header
# Get version from Cargo.toml
VERSION=$(get_version)
print_info "Detected version from Cargo.toml: ${BOLD}$VERSION${NC}"
echo ""
if [[ "$BATCH_MODE" == true ]]; then
# ========== BATCH MODE ==========
print_info "Running in batch (non-interactive) mode"
echo ""
# Set version tag
VERSION_TAG="${BATCH_VERSION_TAG:-v$VERSION}"
# Determine platforms
SELECTED_PLATFORMS=()
if [[ "$BATCH_ALL_PLATFORMS" == true ]]; then
SELECTED_PLATFORMS=("${PLATFORMS[@]}")
elif [[ -n "$BATCH_PLATFORMS" ]]; then
IFS=',' read -ra requested_platforms <<< "$BATCH_PLATFORMS"
for req_platform in "${requested_platforms[@]}"; do
req_platform=$(echo "$req_platform" | xargs) # trim whitespace
local found=false
for p in "${PLATFORMS[@]}"; do
platform_name="${p%%:*}"
if [[ "$platform_name" == "$req_platform" ]]; then
SELECTED_PLATFORMS+=("$p")
found=true
break
fi
done
if [[ "$found" == false ]]; then
print_error "Unknown platform: $req_platform"
print_info "Available platforms: linux-x86_64, linux-aarch64, macos-x86_64, macos-aarch64, windows-x86_64"
exit 1
fi
done
fi
if [[ ${#SELECTED_PLATFORMS[@]} -eq 0 ]] && [[ "$BATCH_DOCKER" == false ]]; then
print_error "No platforms selected and no Docker build requested"
print_info "Use --platforms, --all-platforms, or --docker/--docker-push"
exit 1
fi
CREATE_GITHUB_RELEASE="$BATCH_GITHUB_RELEASE"
BUILD_DOCKER="$BATCH_DOCKER"
PUSH_DOCKER="$BATCH_DOCKER_PUSH"
# Check prerequisites for GitHub release
if [[ "$CREATE_GITHUB_RELEASE" == true ]]; then
if ! command -v gh &> /dev/null; then
print_error "GitHub CLI (gh) is not installed"
print_info "Install with: brew install gh && gh auth login"
exit 1
fi
if ! gh auth status &> /dev/null; then
print_error "Not logged in to GitHub"
print_info "Run: gh auth login"
exit 1
fi
print_success "GitHub CLI authenticated"
fi
# Check Docker prerequisites
if [[ "$BUILD_DOCKER" == true ]]; then
print_step "Checking Docker prerequisites..."
if ! check_docker_prereqs; then
print_error "Docker prerequisites not met"
exit 1
fi
if [[ "$PUSH_DOCKER" == true ]]; then
# In batch mode, just check if credentials exist (don't prompt)
if [[ -f "$HOME/.docker/config.json" ]] && grep -q "index.docker.io" "$HOME/.docker/config.json" 2>/dev/null; then
print_success "Docker Hub credentials found"
elif docker info 2>/dev/null | grep -qi "Username:"; then
print_success "Logged in to Docker Hub"
else
print_error "Not logged in to Docker Hub. Run 'docker login' first."
exit 1
fi
fi
fi
else
# ========== INTERACTIVE MODE ==========
# Ask for version tag
read -p "$(echo -e "${CYAN}?${NC} Version tag for release [v$VERSION]: ")" VERSION_TAG
VERSION_TAG="${VERSION_TAG:-v$VERSION}"
echo ""
# Select target platforms
platform_names=()
for p in "${PLATFORMS[@]}"; do
platform_names+=("${p%%:*}")
done
multi_select "Select target platforms:" "${platform_names[@]}"
SELECTED_PLATFORMS=()
for idx in "${SELECTED_INDICES[@]}"; do
SELECTED_PLATFORMS+=("${PLATFORMS[$idx]}")
done
if [[ ${#SELECTED_PLATFORMS[@]} -eq 0 ]]; then
print_error "No platforms selected"
exit 1
fi
# Ask about GitHub release
CREATE_GITHUB_RELEASE=false
if prompt_yes_no "Create GitHub release?"; then
CREATE_GITHUB_RELEASE=true
# Check gh CLI
if ! command -v gh &> /dev/null; then
print_error "GitHub CLI (gh) is not installed"
print_info "Install with: brew install gh && gh auth login"
exit 1
fi
if ! gh auth status &> /dev/null; then
print_error "Not logged in to GitHub"
print_info "Run: gh auth login"
exit 1
fi
fi
# Ask about Docker
BUILD_DOCKER=false
PUSH_DOCKER=false
if prompt_yes_no "Build Docker image?"; then
# Run Docker pre-flight checks
echo ""
print_step "Checking Docker prerequisites..."
if ! check_docker_prereqs; then
print_error "Docker prerequisites not met"
exit 1
fi
BUILD_DOCKER=true
if prompt_yes_no "Push to Docker Hub?"; then
check_docker_login
PUSH_DOCKER=true
fi
echo ""
fi
fi
# Summary
echo ""
echo -e "${BLUE}════════════════════════════════════════════════════════════${NC}"
echo -e "${BOLD}Build Summary${NC}"
echo -e "${BLUE}════════════════════════════════════════════════════════════${NC}"
echo -e " Version: ${BOLD}$VERSION${NC}"
echo -e " Tag: ${BOLD}$VERSION_TAG${NC}"
echo -e " Platforms: ${BOLD}${#SELECTED_PLATFORMS[@]}${NC} selected"
for p in "${SELECTED_PLATFORMS[@]}"; do
echo -e " • ${p%%:*}"
done
echo -e " GitHub Release: ${BOLD}$CREATE_GITHUB_RELEASE${NC}"
echo -e " Docker Build: ${BOLD}$BUILD_DOCKER${NC}"
echo -e " Docker Push: ${BOLD}$PUSH_DOCKER${NC}"
echo -e " Profile: ${BOLD}$PROFILE${NC}"
echo -e " Mode: ${BOLD}$(if [[ "$BATCH_MODE" == true ]]; then echo "batch"; else echo "interactive"; fi)${NC}"
echo -e "${BLUE}════════════════════════════════════════════════════════════${NC}"
echo ""
# In batch mode, proceed without asking. In interactive mode, ask for confirmation.
if [[ "$BATCH_MODE" != true ]]; then
if ! prompt_yes_no "Proceed with build?" "y"; then
print_warning "Build cancelled"
exit 0
fi
else
print_info "Proceeding with build (batch mode)..."
fi
echo ""
# Generate version.toml with git info
print_step "Generating version info..."
generate_version_toml
# Setup environment
setup_macos_env
# Clean and create dist directory
rm -rf "$DIST_DIR/$VERSION"
mkdir -p "$DIST_DIR/$VERSION"
# Build each platform
local build_count=0
local fail_count=0
for platform_config in "${SELECTED_PLATFORMS[@]}"; do
IFS=':' read -r name target ext <<< "$platform_config"
if build_platform "$name" "$target" "$ext" "$VERSION"; then
((build_count++))
else
((fail_count++))
fi
done
echo ""