-
Notifications
You must be signed in to change notification settings - Fork 9
/
install
executable file
·208 lines (181 loc) · 5.85 KB
/
install
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
#!/bin/bash
# OS directory expected to be on $PATH
#
# Using a common one, often already added to .bashrc or .profile,
# but likely will need to revisit/fix this a few time...
OS_DIR_ON_PATH="$HOME/.local/bin"
# shellcheck disable=SC2088
OS_DIR_ON_PATH_ALT="~/.local/bin"
SUIBASE_DIR="$HOME/suibase"
EXIT_TERMINAL_INSTRUCTION=false
# Utility functions.
setup_error() {
echo "$*" 1>&2
exit 1
}
is_user_root() { [ "${EUID:-$(id -u)}" -eq 0 ]; }
# Function to simply create/update symlinks in $OS_DIR_ON_PATH
#
# Has no effect if symlink already exists and matching.
update_bin_path() {
UBP_TARGET="$SUIBASE_DIR/scripts/$1"
if [ -d "$OS_DIR_ON_PATH" ]; then
if [ ! -L "$OS_DIR_ON_PATH/$1" ]; then
ln -s "$UBP_TARGET" "$OS_DIR_ON_PATH/$1"
echo_info " Symlink added: $OS_DIR_ON_PATH/$1 -> $UBP_TARGET"
else
# Verify link is as intended, if not replace it.
UBP_READLINK=$(readlink -f "$OS_DIR_ON_PATH/$1")
if [[ "$UBP_READLINK" != "$UBP_TARGET" ]]; then
ln -sf "$UBP_TARGET" "$OS_DIR_ON_PATH/$1"
echo_info " Symlink updated: $OS_DIR_ON_PATH/$1 -> $UBP_TARGET"
fi
fi
fi
}
is_local_bin_on_path() {
# Check if the installation dir is in the $PATH variable.
if [[ ":$PATH:" == *":$OS_DIR_ON_PATH:"* ]]; then
true
return
elif [[ ":$PATH:" == *":$OS_DIR_ON_PATH_ALT:"* ]]; then
true
return
fi
false
return
}
setup_local_bin_as_needed() {
# Create $OS_DIR_ON_PATH if it does not exists.
if [ ! -d "$OS_DIR_ON_PATH" ]; then
mkdir -p "$OS_DIR_ON_PATH"
fi
if is_local_bin_on_path; then
return
fi
# Some .profile have code to add $HOME/.local/bin to $PATH
# Source it to see if it fixes it. If yes, then will need to inform
# the user at the end of the installation to close and re-open
# the terminal.
if [ -f "$HOME/.profile" ]; then
# shellcheck source=SCRIPTDIR/../.profile
source "$HOME/.profile"
if is_local_bin_on_path; then
EXIT_TERMINAL_INSTRUCTION=true
return
fi
fi
##### That may work, but affraid to enable it without testing
# if [[ $(uname) == "Darwin" ]]; then
# On macOS, default to .zprofile since catalina, before
# it was .bash_profile.
# if [ -f "$HOME/.zprofile" ]; then
# TARGET_FILE="$HOME/.zprofile"
# else
# if [ -f "$HOME/.bash_profile" ]; then
# TARGET_FILE="$HOME/.bash_profile"
# else
# echo "No profile files found"
# fi
# fi
# if [ -n "$TARGET_FILE" ]; then
# https://unix.stackexchange.com/questions/26047/how-to-correctly-add-a-path-to-path
# echo 'export PATH="${PATH:+${PATH}:}~/.local/bin"' >>"$TARGET_FILE"
# source "$TARGET_FILE"
# fi
# fi
echo_info "Please add $OS_DIR_ON_PATH to your \$PATH variable"
echo_info
echo_info "The installation will not proceed until you complete this step."
echo_info
echo_info "(Please report this to the developer to make the install script better)"
exit 1
}
echo_info() {
if [ "$QUIET_OPT" = false ]; then
echo "$*"
fi
}
main() {
QUIET_OPT=false
while [[ "$#" -gt 0 ]]; do
case $1 in
--quiet) QUIET_OPT=true ;;
*) setup_error "Unknown option '$1'" ;;
esac
shift
done
if [ "$(uname)" = "Darwin" ]; then
# readlink -f is not supported with macOS<12.3
min_ver="12.3"
ver=$(sw_vers -productVersion)
if [ "$(echo -e $min_ver'\n'$ver | sort -V | tail -1)" = "$min_ver" ]; then
if [ "$ver" != "$min_ver" ]; then
echo "Current macOS version: $ver"
echo "macOS version $min_ver or higher is required"
echo "Please upgrade your macOS version"
exit 1
fi
fi
fi
if is_user_root; then
setup_error "Should not install suibase as root (or sudo)"
fi
# Verify that suibase was cloned in user directory.
if [ ! -f "$SUIBASE_DIR/install" ]; then
echo "suibase should be git clone in user home directory [$HOME]"
echo "See https://suibase.io/how-to/install/ for more info."
exit 1
fi
setup_local_bin_as_needed
echo_info "Starting installation of suibase..."
# Build the list of scripts (e.g. "localnet, devnet, tsui...")
local script_list=()
for dir_entry in "$SUIBASE_DIR/"scripts/*; do
if [ -f "$dir_entry" ] && [ -x "$dir_entry" ]; then
script_list+=("$(basename "$dir_entry")")
fi
done
for script_name in "${script_list[@]}"; do
update_bin_path "$script_name"
done
# Final test that the scripts are "callable" by the user without
# specifying a path.
for script_name in "${script_list[@]}"; do
# Call into each script which should return a string echoing... its own name.
# (except for "sui" which could call the ~/.cargo/bin/sui binary)
if [ "$script_name" = "sui" ]; then
continue
fi
test=$($script_name suibase-script-name)
if [ "$test" != "$script_name" ]; then
setup_error "setup of $script_name symlink failed [$test]"
fi
done
# Load suibase common functions/globals to potentially
# do more advanced default workdir creation.
SCRIPT_COMMON_CALLER="$(readlink -f "$0")"
WORKDIR="active"
# shellcheck source=SCRIPTDIR/scripts/common/__globals.sh
source "$SUIBASE_DIR/scripts/common/__globals.sh" "$SCRIPT_COMMON_CALLER" "$WORKDIR"
trap cleanup EXIT
create_cargobin_as_needed
username=$(whoami)
echo_info
echo_info "Suibase is installed now for user [$username]. Great!"
echo_info
echo_info "You can start your localnet with:"
echo_info " \$ localnet start"
echo_info
echo_info "For more info type \"localnet\"."
echo_info
if $EXIT_TERMINAL_INSTRUCTION; then
echo_info "To get started you may need to restart your current shell."
echo_info "This would reload your PATH environment variable to include"
echo_info "suibase's bin directory (\$HOME/.local/bin)."
echo_info
echo_info "To configure your current shell, run:"
echo_info "source \"\$HOME/.profile\""
fi
}
main "$@"