forked from dojoengine/dojo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdojoup
More file actions
executable file
·902 lines (750 loc) · 25.3 KB
/
dojoup
File metadata and controls
executable file
·902 lines (750 loc) · 25.3 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
#!/usr/bin/env bash
# Check if the platform is Windows
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
echo "Note: Dojoup does not support Powershell or Cmd on Windows."
echo "Please use Git BASH (https://gitforwindows.org/) or WSL (https://learn.microsoft.com/en-us/windows/wsl/install)."
fi
set -e
HOME_DIR=${XDG_CONFIG_HOME:-$HOME}
BASE_DIR=${DOJO_DIR-"$HOME_DIR/.dojo"}
DEFAULT_TOOLCHAIN_DIR="$BASE_DIR/bin"
INSTALLATION_DIR="$BASE_DIR/installed"
TOOLCHAIN_DIR="$INSTALLATION_DIR/toolchain"
COMPONENTS_DIR="$INSTALLATION_DIR/components"
# Repository information
DOJO_REPO="dojoengine/dojo"
KATANA_REPO="dojoengine/katana"
TORII_REPO="dojoengine/torii"
# Toolchain versions compatibility registry
VERSIONS_JSON_URL="https://raw.githubusercontent.com/dojoengine/dojo/refs/heads/main/versions.json"
# All components
COMPONENT_BINARY=("sozo" "torii" "katana")
# The last dojo version that is still using the 'legacy' format ie all binaries are included in the monorepo
MIN_NEW_DOJO_VERSION=1.5.0
get_repo_for_component() {
local component=$1
# Mapping of components and their repositories
COMPONENT_REPO_MAPPING=(
"sozo:$DOJO_REPO"
"torii:$TORII_REPO"
"katana:$KATANA_REPO"
)
for component_repo in "${COMPONENT_REPO_MAPPING[@]}"; do
local component_part="${component_repo%%:*}"
local repo_part="${component_repo#*:}"
if [ "$component_part" = "$component" ]; then
echo "$repo_part"
return 0
fi
done
return 1
}
set_default_version() {
local version=$1
requested_toolchain_path=$TOOLCHAIN_DIR/$version
# Return error if the directory path to the requested toolchain doesn't exist
if [[ ! -d "$requested_toolchain_path" ]]; then
err "The requested toolchain \`$version\` is not installed."
fi
# Do nothing if DEFAULT_TOOLCHAIN_DIR is already pointing to the requested toolchain
if [[ -L "$DEFAULT_TOOLCHAIN_DIR" ]] && [[ "$(readlink -f "$DEFAULT_TOOLCHAIN_DIR")" == "$requested_toolchain_path" ]]; then
return
fi
if [ -d "$DEFAULT_TOOLCHAIN_DIR" ]; then
rm -rf "$DEFAULT_TOOLCHAIN_DIR"
fi
ln -sf "$requested_toolchain_path" "$DEFAULT_TOOLCHAIN_DIR"
}
# Function to get the active Dojo version
get_default_version() {
if [ -d "$DEFAULT_TOOLCHAIN_DIR" ] && [ -L "$DEFAULT_TOOLCHAIN_DIR" ]; then
# Read the link and get the last segment of the path (the toolchain name ie version)
# eg .dojo/installed/toolchain/1.4.0
active_path=$(readlink -f "$DEFAULT_TOOLCHAIN_DIR")
toolchain=$(basename "$active_path")
echo "$toolchain"
return 0
else
return 1
fi
}
get_active_component_path(){
local component=$1
if [ -d "$DEFAULT_TOOLCHAIN_DIR" ]; then
echo "$(readlink -f "$DEFAULT_TOOLCHAIN_DIR/$component")"
return 0
else
return 1
fi
}
usage() {
cat 1>&2 <<'EOF'
The Dojo toolchain installer
USAGE:
dojoup [COMMAND]
COMMANDS:
component Manage individual Dojo components
default Switch to a specific installed version
install Install Dojo
list List all installed versions
show Display the default Dojo version
EOF
}
show_version() {
# Get current global version
local active_version=$(get_default_version)
if [ -n "$active_version" ]; then
printf "version: $active_version\n"
printf "components:\n"
# Show details for each binary
for component in "${COMPONENT_BINARY[@]}"; do
component_path=$(get_active_component_path "$component")
bin_version=$(get_component_version "$component_path")
say " $component: $bin_version"
done
else
say "No active Dojo version set. Use 'dojoup install' to install Dojo or 'dojoup default <version>' to set an active version."
fi
}
get_component_version() {
local bin_path=$1
if [ -n "$bin_path" ]; then
# Extract version by taking everything after the first space without validating the format
local bin_version=$("$bin_path" --version 2>/dev/null | head -1 | cut -d' ' -f2 || echo "unknown")
echo "$bin_version"
else
return 1
fi
}
# List all installed versions from the toolchain directory
list_toolchains() {
default_version=$(get_default_version)
if [ -d "$TOOLCHAIN_DIR" ]; then
for toolchain in $(ls -1 "$TOOLCHAIN_DIR" 2>/dev/null); do
if [ "$toolchain" = "$default_version" ]; then
say "$toolchain (default)"
else
say "$toolchain"
fi
done
fi
}
use_version() {
local version=$1
# Check if version was provided
if [ -z "$version" ]; then
show_version
return 0
fi
# Set default version and update symlinks
set_default_version "$version"
say "now using dojo toolchain: $version"
}
detect_platform_arch() {
# Determine platform
local platform="$(uname -s)"
local ext="tar.gz"
case $platform in
Linux)
platform="linux"
;;
Darwin)
platform="darwin"
;;
MINGW*)
ext="zip"
platform="win32"
;;
*)
err "unsupported platform: $platform"
;;
esac
# Determine architecture
local architecture="$(uname -m)"
if [ "${architecture}" = "x86_64" ]; then
# Redirect stderr to /dev/null to avoid printing errors if non Rosetta.
if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null)" = "1" ]; then
architecture="arm64" # Rosetta.
else
architecture="amd64" # Intel.
fi
elif [ "${architecture}" = "arm64" ] ||[ "${architecture}" = "aarch64" ] ; then
architecture="arm64" # Arm.
else
architecture="amd64" # Amd.
fi
# Return values as a space-separated string
echo "$platform $ext $architecture"
}
install_dojo() {
need_cmd jq
need_cmd curl
# Process options for installation
# If the first argument is provided and doesn't start with a dash, treat it as the version
if [[ $1 && ! $1 =~ ^- ]]; then
DOJOUP_VERSION=$1
shift
fi
# Process any remaining options
while [[ $1 ]]; do
case $1 in
--) shift; break;;
-h|--help)
usage_install
exit 0
;;
*)
warn "unknown install option: $1"
usage_install
exit 1
;;
esac
shift
done
# Default to the main dojo repo
DOJOUP_REPO=${DOJOUP_REPO-$DOJO_REPO}
# Store user specified version separately
DOJOUP_USER_VERSION=${DOJOUP_VERSION}
# Install by downloading binaries
DOJOUP_VERSION=${DOJOUP_VERSION-stable}
DOJOUP_TAG=$DOJOUP_VERSION
# Normalize versions (handle channels, versions without v prefix)
if [[ "$DOJOUP_VERSION" == "stable" ]]; then
# Fetch the list of releases from the GitHub API and filter out `prerelease`` releases and `alpha`` releases
DOJOUP_TAG=$(curl -s "https://api.github.com/repos/${DOJO_REPO}/releases" \
| grep -oE '"tag_name": "[^"]*"|"prerelease": (true|false)' \
| grep -B1 '"prerelease": false' \
| grep '"tag_name":' \
| grep -oE '"v[0-9]*\.[0-9]*\.[0-9]*(-rc\.[0-9]*)?"' \
| tr -d '"' \
| head -n 1)
DOJOUP_VERSION=$DOJOUP_TAG
elif [[ "$DOJOUP_VERSION" == [[:digit:]]* ]]; then
# Add v prefix
DOJOUP_VERSION="v${DOJOUP_VERSION}"
DOJOUP_TAG="${DOJOUP_VERSION}"
fi
say "installing dojo (version ${DOJOUP_VERSION}, tag ${DOJOUP_TAG})"
# Get platform and architecture information
read -r PLATFORM EXT ARCHITECTURE <<< "$(detect_platform_arch)"
VERSIONS_REGISTRY=$(mktemp)
# Try to download the latest versions.json
say "syncing dojo toolchain..."
if ! download "$VERSIONS_JSON_URL" "$VERSIONS_REGISTRY" 2>/dev/null; then
# If no local copy exists either, error out
rm -f "$VERSIONS_REGISTRY"
err "failed to sync toolchain versions."
fi
# Parse the versions.json file to get compatible versions
# Remove the 'v' prefix from DOJOUP_VERSION for json lookup
DOJO_VERSION_NO_V=$(echo "$DOJOUP_VERSION" | sed 's/^v//')
# Track which archives we've already downloaded to avoid redundancy
DOWNLOADED_ARCHIVES=""
INSTALLED_BINARIES=()
# get the latest component version that is compatible with the given dojo version
get_compatible_version() {
local binary=$1
compatible_version=$(jq -r --arg v "$DOJO_VERSION_NO_V" --arg b "$binary" '.[$v][$b][0]' "$VERSIONS_REGISTRY")
if [ "$compatible_version" = "null" ]; then
err "No compatible version found for ${binary}"
else
echo "$compatible_version"
fi
}
# for backward compatibitliy with the legacy monorepo format
do_legacy_install() {
# Dojo repo binaries use dojo version
bin_version=$DOJO_VERSION_NO_V
bin_tag=$DOJOUP_TAG
RELEASE_URL="https://github.com/${DOJO_REPO}/releases/download/${bin_tag}/"
local bin_path
ARCHIVE_NAME="dojo_${bin_tag}_${PLATFORM}_${ARCHITECTURE}.$EXT"
BIN_ARCHIVE_URL="${RELEASE_URL}${ARCHIVE_NAME}"
EXTRACT_DIR=$(mktemp -d)
# Check if the version exists in the repository
if ! curl --output /dev/null --silent --head --fail "$BIN_ARCHIVE_URL"; then
err "Version ${bin_tag} for ${component} does not exist."
fi
# Download and extract the binaries archive
if [ "$PLATFORM" = "win32" ]; then
tmp="$(mktemp -d 2>/dev/null || echo ".")/dojo.zip"
ensure download "$BIN_ARCHIVE_URL" "$tmp"
ensure unzip "$tmp" -d "$EXTRACT_DIR"
rm -f "$tmp"
else
ensure download "$BIN_ARCHIVE_URL" | ensure tar -xzC "$EXTRACT_DIR"
fi
for component in "${COMPONENT_BINARY[@]}"; do
say "installing $component"
# the path to the extracted the component binary
extracted_bin_dir="$EXTRACT_DIR/$component"
component_bin_path="$COMPONENTS_DIR/$component/$bin_version"
# Create directory for this binary version
ensure mkdir -p "$component_bin_path"
# Move only this binary to its version directory
if [ -f "$extracted_bin_dir" ]; then
ensure cp "$extracted_bin_dir" "$component_bin_path/"
else
err "Version ${bin_tag} for ${component} is not found in the downloaded archive file."
fi
# Only check for Scarb version if we're installing sozo
if [ "$component" = "sozo" ]; then
check_scarb $component_bin_path
fi
bin_path="$component_bin_path/$component"
INSTALLED_BINARIES+=("$bin_path:$component")
done
}
do_install() {
for component in "${COMPONENT_BINARY[@]}"; do
say "installing $component"
# Determine the version of each component that is compatible with the given Dojo version
repo=$(get_repo_for_component "$component")
if [ "$repo" = "$DOJO_REPO" ]; then
# Dojo repo binaries use dojo version
bin_version=$DOJO_VERSION_NO_V
bin_tag=$DOJOUP_TAG
else
bin_version=$(get_compatible_version "$component")
bin_tag="v$bin_version"
fi
# Create directory for this binary version
component_bin_path="$COMPONENTS_DIR/$component/$bin_version"
ensure mkdir -p "$component_bin_path"
# Compute the URL of the release tarball
RELEASE_URL="https://github.com/${repo}/releases/download/${bin_tag}/"
local bin_path
# Download the component binary
if [ "$repo" = "$DOJO_REPO" ]; then
# For dojo repo, all binaries are in a single archive
ARCHIVE_NAME="dojo_${bin_tag}_${PLATFORM}_${ARCHITECTURE}.$EXT"
BIN_ARCHIVE_URL="${RELEASE_URL}${ARCHIVE_NAME}"
# Check if we've already downloaded this archive
if [[ "$DOWNLOADED_ARCHIVES" != *"$ARCHIVE_NAME"* ]]; then
EXTRACT_DIR=$(mktemp -d)
# Check if the version exists in the repository
if ! curl --output /dev/null --silent --head --fail "$BIN_ARCHIVE_URL"; then
err "Version ${bin_tag} for ${component} does not exist."
fi
# Download and extract the binaries archive
if [ "$PLATFORM" = "win32" ]; then
tmp="$(mktemp -d 2>/dev/null || echo ".")/dojo.zip"
ensure download "$BIN_ARCHIVE_URL" "$tmp"
ensure unzip "$tmp" -d "$EXTRACT_DIR"
rm -f "$tmp"
else
ensure download "$BIN_ARCHIVE_URL" | ensure tar -xzC "$EXTRACT_DIR"
fi
# Remember that we've downloaded this archive
DOWNLOADED_ARCHIVES="$DOWNLOADED_ARCHIVES $ARCHIVE_NAME"
fi
# Move only this binary to its version directory
if [ -f "$EXTRACT_DIR/$component" ]; then
ensure cp "$EXTRACT_DIR/$component" "$component_bin_path/"
else
err "Version ${bin_tag} for ${component} is not found in the downloaded archive file."
fi
bin_path="$component_bin_path/$component"
else
# For other repos like katana, each binary has its own archive
BIN_ARCHIVE_URL="${RELEASE_URL}${component}_${bin_tag}_${PLATFORM}_${ARCHITECTURE}.$EXT"
# Check if the version exists in the repository
if ! curl --output /dev/null --silent --head --fail "$BIN_ARCHIVE_URL"; then
err "Version ${bin_tag} for ${component} does not exist."
fi
# Download and extract the binaries archive
if [ "$PLATFORM" = "win32" ]; then
tmp="$(mktemp -d 2>/dev/null || echo ".")/dojo.zip"
ensure download "$BIN_ARCHIVE_URL" "$tmp"
ensure unzip "$tmp" -d "$component_bin_path"
rm -f "$tmp"
else
ensure download "$BIN_ARCHIVE_URL" | ensure tar -xzC "$component_bin_path"
fi
bin_path="$component_bin_path/$component"
fi
INSTALLED_BINARIES+=("$bin_path:$component")
# Only check for Scarb version if we're installing sozo
if [ "$component" = "sozo" ]; then
check_scarb $bin_path
fi
done
}
if [ "$(version_is_bigger_than "$DOJO_VERSION_NO_V" "$MIN_NEW_DOJO_VERSION" )" -ne -1 ]; then
do_install
else
do_legacy_install
fi
# Create version-specific directory in DOJO_DIR for symlinks
TOOLCHAIN_VER_DIR="$TOOLCHAIN_DIR/$DOJO_VERSION_NO_V"
ensure mkdir -p "$TOOLCHAIN_VER_DIR"
# Create symlinks for all installed binaries in the version-specific directory
for binary_entry in "${INSTALLED_BINARIES[@]}"; do
bin_path="${binary_entry%%:*}"
bin_name="${binary_entry##*:}"
# Create symlink to the binary
if [ -f "$bin_path" ]; then
ln -sf "$bin_path" "$TOOLCHAIN_VER_DIR/$bin_name"
fi
done
# Set the global version to the one we just installed
set_default_version "$DOJO_VERSION_NO_V"
say "done!"
welcome_msg
cleanup() {
# Clean up temporary files
if [ -n "$EXTRACT_DIR" ] && [ -d "$EXTRACT_DIR" ]; then
rm -rf "$EXTRACT_DIR"
fi
if [ -f "$VERSIONS_REGISTRY" ]; then
rm -f "$VERSIONS_REGISTRY"
fi
}
cleanup
}
usage_install() {
cat 1>&2 <<'EOF'
Install the Dojo toolchain.
USAGE:
dojoup install [VERSION]
ARGS:
VERSION The version to install (if not specified, defaults to the latest stable version)
OPTIONS:
-h, --help Print help information
EOF
}
say() {
printf "%s\n" "$1"
}
warn() {
say "warning: ${1}" >&2
}
err() {
say "$1" >&2
exit 1
}
need_cmd() {
if ! check_cmd "$1"; then
err "need '$1' (command not found)"
fi
}
check_cmd() {
command -v "$1" &>/dev/null
}
# Run a command that should never fail. If the command fails execution
# will immediately terminate with an error showing the failing
# command.
ensure() {
if ! "$@"; then err "command failed: $*"; fi
}
# Downloads $1 into $2 or stdout
download() {
if [ "$2" ]; then
# output into $2
if check_cmd curl; then
curl -#o "$2" -L "$1"
else
wget --show-progress -qO "$2" "$1"
fi
else
# output to stdout
if check_cmd curl; then
curl -#L "$1"
else
wget --show-progress -qO- "$1"
fi
fi
}
# Function to check mutual exclusivity of options
check_exclusive_options() {
local options=("$@")
local count=0
local set_option=""
for option in "${options[@]}"; do
if [ -n "${!option}" ]; then
((count++))
set_option="$option"
fi
done
if [ "$count" -gt 1 ]; then
err "only one of ${options[*]} can be specified"
elif [ "$count" -eq 1 ]; then
echo "$set_option"
fi
}
# Welcome message printed after having installed Dojo.
welcome_msg() {
dojo='\033[1;34m'
title='\033[0;32m'
emphasis='\033[0;34m'
command='\033[0;31m'
clear='\033[0m'
printf "
═════════════════════════════════════════════════════════════════════════
██████╗ ██╗ ██╗ █████╗ ██╗ ██╗ ██████╗
██╔═══██╗██║ ██║██╔══██╗╚██╗ ██╔╝██╔═══██╗
██║ ██║███████║███████║ ╚████╔╝ ██║ ██║
██║ ██║██╔══██║██╔══██║ ╚██╔╝ ██║ ██║
╚██████╔╝██║ ██║██║ ██║ ██║ ╚██████╔╝
╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═════╝
Repo : https://github.com/dojoengine/dojo
Book : https://book.dojoengine.org/
Chat : https://discord.gg/dojoengine
https://t.me/dojoengine
Congratulations on successfully installing ${dojo}Dojo${clear} ${DOJOUP_VERSION}! 🥷
For more info on getting started, check out the Dojo Starter guide: https://book.dojoengine.org/tutorials/dojo-starter
═════════════════════════════════════════════════════════════════════════
"
}
check_scarb() {
local sozo_path=$1
# Extracting the scarb version from the output of 'sozo --version'
if [ -n "$sozo_path" ] && [ -f "$sozo_path" ]; then
scarb_version=$(echo "$($sozo_path --version)" | grep -o 'scarb: [0-9.]*' | cut -d ' ' -f 2)
# Check if scarb is already installed
if [ -n "$scarb_version" ] && [ "$(scarb --version 2>/dev/null)" != "scarb $scarb_version" ]; then
# Check if scarb is managed by asdf
if command -v asdf &> /dev/null; then
if asdf list | grep -q "scarb"; then
# Check if default version is set
if ! asdf current scarb &> /dev/null; then
# Try newer asdf command first, fall back to older command if it fails.
if ! asdf set scarb $scarb_version &> /dev/null; then
asdf global scarb $scarb_version
fi
fi
else
# Install scarb using asdf
asdf plugin add scarb
asdf install scarb $scarb_version
fi
else
# Install scarb using the install script
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh -s -- -v $scarb_version
fi
fi
fi
}
# Function to compare version strings (like "1.2.3" vs "1.3.0")
# Takes two version strings as input and compares them semantically
# Returns: -1 if version1 < version2, 0 if equal, 1 if version1 > version2
# This is useful for determining if a newer version is available or for sorting versions
version_is_bigger_than() {
if [[ $# -ne 2 ]]; then
echo "Usage: compare_versions version1 version2"
echo "Returns: -1 if version1 < version2, 0 if equal, 1 if version1 > version2"
return 2
fi
local v1=($1)
local v2=($2)
# Split versions by dots
IFS='.' read -ra v1_parts <<< "$1"
IFS='.' read -ra v2_parts <<< "$2"
# Compare each component
for i in "${!v1_parts[@]}"; do
# If we've reached the end of v2 but v1 has more parts, v1 is greater
if [[ -z "${v2_parts[i]}" ]]; then
echo 1
return 0
fi
# Compare the numeric values
if (( v1_parts[i] > v2_parts[i] )); then
echo 1
return 0
elif (( v1_parts[i] < v2_parts[i] )); then
echo -1
return 0
fi
done
# If we get here and v2 has more parts, v2 is greater
if [[ "${#v1_parts[@]}" -lt "${#v2_parts[@]}" ]]; then
echo -1
return 0
fi
# Versions are equal
echo 0
return 0
}
install_component() {
need_cmd jq
need_cmd curl
# Check if component name and version are provided
if [ $# -lt 2 ]; then
usage_component
exit 1
fi
local component=$1
local version=$2
shift 2
# Process any remaining options
while [[ $1 ]]; do
case $1 in
--) shift; break;;
-h|--help)
usage_component
exit 0
;;
*)
warn "unknown component option: $1"
usage_component
exit 1
;;
esac
shift
done
# Validate component name
if [[ ! " ${COMPONENT_BINARY[@]} " =~ " ${component} " ]]; then
err "Invalid component name: $component. Valid components are: ${COMPONENT_BINARY[*]}"
fi
# Normalize version (handle versions without v prefix)
if [[ "$version" == [[:digit:]]* ]]; then
version="v${version}"
fi
say "installing $component (version ${version})"
# Get platform and architecture information
read -r PLATFORM EXT ARCHITECTURE <<< "$(detect_platform_arch)"
# Get the repository for the component
repo=$(get_repo_for_component "$component")
if [ -z "$repo" ]; then
err "Could not determine repository for component: $component"
fi
# Create directory for this binary version
component_bin_path="$COMPONENTS_DIR/$component/$version"
ensure mkdir -p "$component_bin_path"
# Compute the URL of the release tarball
RELEASE_URL="https://github.com/${repo}/releases/download/${version}/"
local bin_path
# Download the component binary
if [ "$repo" = "$DOJO_REPO" ]; then
# For dojo repo, all binaries are in a single archive
ARCHIVE_NAME="dojo_${version}_${PLATFORM}_${ARCHITECTURE}.$EXT"
BIN_ARCHIVE_URL="${RELEASE_URL}${ARCHIVE_NAME}"
EXTRACT_DIR=$(mktemp -d)
# Check if the version exists in the repository
if ! curl --output /dev/null --silent --head --fail "$BIN_ARCHIVE_URL"; then
err "Version ${version} for ${component} does not exist."
fi
# Download and extract the binaries archive
if [ "$PLATFORM" = "win32" ]; then
tmp="$(mktemp -d 2>/dev/null || echo ".")/dojo.zip"
ensure download "$BIN_ARCHIVE_URL" "$tmp"
ensure unzip "$tmp" -d "$EXTRACT_DIR"
rm -f "$tmp"
else
ensure download "$BIN_ARCHIVE_URL" | ensure tar -xzC "$EXTRACT_DIR"
fi
# Move only this binary to its version directory
if [ -f "$EXTRACT_DIR/$component" ]; then
ensure cp "$EXTRACT_DIR/$component" "$component_bin_path/"
else
err "Version ${version} for ${component} is not found in the downloaded archive file."
fi
bin_path="$component_bin_path/$component"
else
# For other repos like katana, each binary has its own archive
BIN_ARCHIVE_URL="${RELEASE_URL}${component}_${version}_${PLATFORM}_${ARCHITECTURE}.$EXT"
# Check if the version exists in the repository
if ! curl --output /dev/null --silent --head --fail "$BIN_ARCHIVE_URL"; then
err "Version ${version} for ${component} does not exist."
fi
# Download and extract the binaries archive
if [ "$PLATFORM" = "win32" ]; then
tmp="$(mktemp -d 2>/dev/null || echo ".")/dojo.zip"
ensure download "$BIN_ARCHIVE_URL" "$tmp"
ensure unzip "$tmp" -d "$component_bin_path"
rm -f "$tmp"
else
ensure download "$BIN_ARCHIVE_URL" | ensure tar -xzC "$component_bin_path"
fi
bin_path="$component_bin_path/$component"
fi
# Only check for Scarb version if we're installing sozo
if [ "$component" = "sozo" ]; then
check_scarb $bin_path
fi
# Create symlink in the default toolchain directory
if [ -f "$bin_path" ]; then
ln -sf "$bin_path" "$DEFAULT_TOOLCHAIN_DIR/$component"
fi
say "done!"
cleanup() {
# Clean up temporary files
if [ -n "$EXTRACT_DIR" ] && [ -d "$EXTRACT_DIR" ]; then
rm -rf "$EXTRACT_DIR"
fi
}
cleanup
}
usage_component() {
cat 1>&2 <<'EOF'
Manage individual Dojo components.
USAGE:
dojoup component <COMMAND>
COMMANDS:
add Add a specific component version
help Print this message or the help of the given subcommand(s)
USAGE:
dojoup component add <COMPONENT> <VERSION>
ARGS:
COMPONENT The component to install (sozo, torii, katana)
VERSION The version to install
OPTIONS:
-h, --help Print help information
EOF
}
main() {
# Process commands
if [ $# -eq 0 ]; then
# If no command provided, default to the backward compatible behavior
# which is installing the latest version of Dojo.
install_dojo "$@"
fi
if [ ! -d "$BASE_DIR" ]; then
ensure mkdir -p "$BASE_DIR"
fi
# First argument is the command
COMMAND=$1
shift
case $COMMAND in
show)
show_version
exit 0
;;
install)
install_dojo "$@"
;;
default)
use_version "$@"
;;
list)
list_toolchains
;;
component)
if [ $# -eq 0 ]; then
usage_component
exit 0
fi
SUBCOMMAND=$1
shift
case $SUBCOMMAND in
add)
install_component "$@"
;;
help|-h|--help)
usage_component
exit 0
;;
*)
warn "unknown component command: $SUBCOMMAND"
usage_component
exit 1
;;
esac
;;
help|-h|--help)
usage
exit 0
;;
esac
}
main "$@" || exit 1