-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoonbit_install.sh
More file actions
306 lines (242 loc) · 6.97 KB
/
moonbit_install.sh
File metadata and controls
306 lines (242 loc) · 6.97 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
# taken from bun.sh/install
Color_Off=''
# Regular Colors
Red=''
Green=''
Dim='' # White
# Bold
Bold_White=''
Bold_Green=''
if [[ -t 1 ]]; then
# Reset
Color_Off='\033[0m' # Text Reset
# Regular Colors
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Dim='\033[0;2m' # White
# Bold
Bold_Green='\033[1;32m' # Bold Green
Bold_White='\033[1m' # Bold White
fi
error() {
echo -e "${Red}error${Color_Off}: $*" >&2
exit 1
}
info() {
echo -e "${Dim}$* ${Color_Off}"
}
info_bold() {
echo -e "${Bold_White}$* ${Color_Off}"
}
success() {
echo -e "${Green}$* ${Color_Off}"
}
ARGUMENTS=()
for i in "$@"; do
case "$i" in
-h|--help)
HELP=true
;;
# -y|--yes)
# YES=true
# ;;
# -n|--no)
# NO=true
# ;;
-*)
error "Unknown option: $i"
;;
*)
ARGUMENTS+=("$i")
;;
esac
done
if [[ $HELP ]]; then
info_bold "Usage: $0 [options] [arguments]"
echo
echo "Options:"
echo " -h, --help Show this help message and exit"
# echo " -y, --yes always choose yes in confirmation prompt"
# echo " -n, --no always choose no in confirmation prompt"
echo
echo "Arguments:"
echo " None install the latest version of moonbit"
echo " [VERSION] install the specified version of moonbit"
exit 0
fi
if [[ ${#ARGUMENTS[@]} -gt 1 ]]; then
error "Too many arguments"
fi
target=''
case $(uname -ms) in
'Darwin arm64')
target=darwin-aarch64
;;
'Linux x86_64')
target=linux-x86_64
;;
esac
if [[ -z $target ]]; then
error "Unsupported platform: $(uname -ms)"
fi
if [[ -n $MOONBIT_INSTALL_DEV ]]; then
target="$target-dev"
fi
version=${ARGUMENTS[0]:-${MOONBIT_INSTALL_VERSION:-latest}}
version=${version//+/%2B}
CLI_MOONBIT="https://cli.moonbitlang.com"
moonbit_uri="$CLI_MOONBIT/binaries/$version/moonbit-$target.tar.gz"
core_uri="$CLI_MOONBIT/cores/core-$version.tar.gz"
moon_home="${MOON_HOME:-$HOME/.moon}"
bin_dir=$moon_home/bin
exe=$bin_dir/moon
moonbit_dest=$HOME/moonbit.tar.gz
lib_dir=$moon_home/lib
core_dest=$lib_dir/core.tar.gz
if [[ -z $moon_home ]]; then
error "MOON_HOME is not set"
fi
mkdir -p "$moon_home" ||
error "Failed to create directory \"$moon_home\""
echo "Downloading moonbit ..."
curl --fail --location --progress-bar --output "$moonbit_dest" "$moonbit_uri" ||
error "Failed to download moonbit from \"$moonbit_uri\""
rm -rf "${moon_home:?}/bin" ||
error "Failed to remove existing moonbit binaries"
rm -rf "${moon_home:?}/lib" ||
error "Failed to remove existing moonbit libraries"
rm -rf "${moon_home:?}/include" ||
error "Failed to remove existing moonbit includes"
tar xf "$moonbit_dest" --directory="$moon_home" ||
error "Failed to extract moonbit to \"$moon_home\""
if [[ -f "$moon_home/bin/internal/moon-pilot/lib/prompt/moonbitlang.mbt.md" ]]; then
if [[ -f "$moon_home/AGENTS.md" ]]; then
rm -f "$moon_home/AGENTS.md" ||
error "Failed to remove existing $moon_home/AGENTS.md"
fi
ln -s "$moon_home/bin/internal/moon-pilot/lib/prompt/moonbitlang.mbt.md" "$moon_home/AGENTS.md" ||
error "Failed to create symlink for $moon_home/bin/internal/moon-pilot/lib/prompt/moonbitlang.mbt.md"
fi
rm -f "$moonbit_dest" ||
error "Failed to remove \"$moonbit_dest\""
pushd "$bin_dir" >/dev/null || error "Failed to change directory to \"$bin_dir\""
for i in *; do
chmod +x "$i" ||
error "Failed to make \"$i\" executable"
done
chmod +x ./internal/tcc ||
error "Failed to make tcc executable"
popd >/dev/null || error "Failed to change directory to previous directory"
rm -rf "$lib_dir/core" ||
error "Failed to remove existing core"
echo "Downloading core ..."
curl --fail --location --progress-bar --output "$core_dest" "$core_uri" ||
error "Failed to download core from \"$core_uri\""
tar xf "$core_dest" --directory="$lib_dir" ||
error "Failed to extract core to \"$lib_dir\""
rm -f "$core_dest" ||
error "Failed to remove \"$core_dest\""
echo "Bundling core ..."
PATH=$bin_dir $exe bundle --warn-list -a --all --source-dir "$lib_dir"/core ||
error "Failed to bundle core"
if [[ $version == "nightly" ]]; then
PATH=$bin_dir $exe bundle --warn-list -a --target llvm --source-dir "$lib_dir"/core ||
error "Failed to bundle core for llvm backend"
fi
PATH=$bin_dir $exe bundle --warn-list -a --target wasm-gc --source-dir "$lib_dir"/core --quiet ||
error "Failed to bundle core to wasm-gc"
tildify() {
if [[ $1 = $HOME/* ]]; then
local replacement=\~/
echo "${1/$HOME\//$replacement}"
else
echo "$1"
fi
}
success "moonbit was installed successfully to $Bold_Green$(tildify "$moon_home")"
echo "To verify the downloaded binaries, check https://www.moonbitlang.com/download#verifying-binaries for instructions."
echo "To know how to add shell completions, run 'moon shell-completion --help'"
if command -v moon >/dev/null 2>&1; then
echo "Run 'moon help' to get started"
exit 0
fi
tilde_bin_dir=$(tildify "$bin_dir")
bin_env=${bin_dir//\"/\\\"}
refresh_command=''
if [[ $bin_env = $HOME/* ]]; then
bin_env=${bin_env/$HOME\//\$HOME/}
fi
quoted_new_path_env="\"$bin_env:\$PATH\""
case $(basename "$SHELL") in
fish)
commands=(
"fish_add_path \"$bin_env\""
)
fish_config=$HOME/.config/fish/config.fish
tilde_fish_config=$(tildify "$fish_config")
if [[ -w $fish_config ]]; then
{
echo -e "\n# moonbit"
for cmd in "${commands[@]}"; do
echo "$cmd"
done
} >> "$fish_config"
info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_fish_config\""
refresh_command="source $tilde_fish_config"
else
echo "Manually add the directory to $tilde_fish_config (or similar):"
for command in "${commands[@]}"; do
info_bold " $command"
done
fi
;;
zsh)
commands=(
"export PATH=$quoted_new_path_env"
)
zsh_config=$HOME/.zshrc
tilde_zsh_config=$(tildify "$zsh_config")
if [[ -w $zsh_config ]]; then
{
echo -e '\n# moonbit'
for command in "${commands[@]}"; do
echo "$command"
done
} >>"$zsh_config"
info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_zsh_config\""
refresh_command="source $tilde_zsh_config"
else
echo "Manually add the directory to $tilde_zsh_config (or similar):"
for command in "${commands[@]}"; do
info_bold " $command"
done
fi
;;
bash)
commands=(
"export PATH=$quoted_new_path_env"
)
bash_config=$HOME/.bashrc
tilde_bash_config=$(tildify "$bash_config")
if [[ -w $bash_config ]]; then
{
echo -e '\n# moonbit'
for command in "${commands[@]}"; do
echo "$command"
done
} >>"$bash_config"
info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_bash_config\""
refresh_command="source $tilde_bash_config"
else
echo "Manually add the directory to $tilde_bash_config (or similar):"
for command in "${commands[@]}"; do
info_bold " $command"
done
fi
;;
esac
info "To get started, run:"
if [[ $refresh_command ]]; then
info_bold " $refresh_command"
fi
info_bold " moon help"