-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrsvm.sh
606 lines (542 loc) · 14.5 KB
/
rsvm.sh
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
# Rust Version Manager
# ====================
#
# To use the rsvm command source this file from your bash profile.
RSVM_VERSION="0.5.1"
RSVM_NIGHTLY_PATTERN="nightly(\.[0-9]+)?"
RSVM_BETA_PATTERN="beta(\.[0-9]+)?"
RSVM_NORMAL_PATTERN="[0-9]+\.[0-9]+(\.[0-9]+)?(-(alpha|beta)(\.[0-9]*)?)?"
RSVM_RC_PATTERN="$RSVM_NORMAL_PATTERN-rc(\.[0-9]+)?"
RSVM_VERSION_PATTERN="($RSVM_NIGHTLY_PATTERN|$RSVM_NORMAL_PATTERN|$RSVM_RC_PATTERN|$RSVM_BETA_PATTERN)"
RSVM_LAST_INSTALLED_VERSION=
if [ -n "$ZSH_VERSION" ]
then
RSVM_SCRIPT=${(%):-%N}
RSVM_SCRIPT="$(cd -P "$(dirname "$RSVM_SCRIPT")" && pwd)/$(basename "$RSVM_SCRIPT")"
else
RSVM_SCRIPT=${BASH_SOURCE[0]}
fi
RSVM_ARCH=`uname -m`
RSVM_OSTYPE=`uname -s`
case $RSVM_OSTYPE in
Linux)
RSVM_PLATFORM=$RSVM_ARCH-unknown-linux-gnu
;;
Darwin)
RSVM_PLATFORM=$RSVM_ARCH-apple-darwin
;;
*)
;;
esac
# Auto detect the RSVM_DIR
if [ ! -d "$RSVM_DIR" ]
then
export RSVM_DIR=$(cd $(dirname ${BASH_SOURCE[0]:-$0}) && pwd)
fi
rsvm_initialize()
{
if [ ! -d "$RSVM_DIR/versions" ]
then
mkdir -p "$RSVM_DIR/versions"
fi
if [ ! -f "$RSVM_DIR/.rsvm_version" ]
then
touch "$RSVM_DIR/.rsvm_version"
fi
local rsvm_version=$(cat "$RSVM_DIR/.rsvm_version")
if [ -z "$rsvm_version" ]
then
local DIRECTORIES=$(find "$RSVM_DIR" -maxdepth 1 -mindepth 1 -type d -exec basename '{}' \; \
| sort \
| grep -E "^$RSVM_VERSION_PATTERN")
mkdir -p "$RSVM_DIR/versions"
for line in $(echo $DIRECTORIES | tr " " "\n")
do
mv "$RSVM_DIR/$line" "$RSVM_DIR/versions"
done
fi
echo "1" > "$RSVM_DIR/.rsvm_version"
}
rsvm_check_etag()
{
# Check which md5sum to use
if [ -f "$(which md5sum)" ]; then
MD5=md5sum
elif [ -f "$(which md5)" ]; then
MD5=md5
else
echo "md5sum not found!"
exit 1
fi
if [ -f $2.etag ]
then
curl -s -I -H "If-None-Match:$(cat $2.etag)" $1 | grep 304 | wc -l
elif [ -f $2 ]
then
local ETAG=$($MD5 $2 | awk '{print $1}')
curl -s -I -H "If-None-Match:\"$ETAG\"" $1 | grep 304 | wc -l
else
echo 0
fi
}
rsvm_file_download()
{
local OPTS
# download custom etag
if [ "$3" = true ]
then
curl -I -s $1 | grep ETag | awk '{print $2}' > $2.etag
OPTS='-#'
else
OPTS='-s'
fi
#echo $1 $2
if [ $(rsvm_check_etag $1 $2) = 0 ]
then
# not match etag; new download
curl $OPTS -o $2 $1
else
# match etag; resume download
curl $OPTS -o $2 -C - $1
fi
}
rsvm_append_path()
{
local newpath
if [[ ":$1:" != *":$2:"* ]];
then
newpath="${1:+"$1:"}$2"
else
newpath="$1"
fi
echo $newpath
}
export LD_LIBRARY_PATH=$(rsvm_append_path $LD_LIBRARY_PATH "$RSVM_DIR/current/dist/lib")
export DYLD_LIBRARY_PATH=$(rsvm_append_path $DYLD_LIBRARY_PATH "$RSVM_DIR/current/dist/lib")
export MANPATH=$(rsvm_append_path $MANPATH "$RSVM_DIR/current/dist/share/man")
export RSVM_SRC_PATH="$RSVM_DIR/current/src/rustc-source/src"
if [ -e "$RSVM_SRC_PATH" ]
then
export RUST_SRC_PATH="$RSVM_SRC_PATH"
else
unset RUST_SRC_PATH
fi
export CARGO_HOME="$RSVM_DIR/current/cargo"
export RUSTUP_HOME="$RSVM_DIR/current/rustup"
export PATH=$(rsvm_append_path $PATH "$RSVM_DIR/current/dist/bin")
export PATH=$(rsvm_append_path $PATH "$CARGO_HOME/bin")
rsvm_use()
{
if [ -e "$RSVM_DIR/versions/$1" ]
then
echo -n "Activating rust $1 ... "
rm -rf "$RSVM_DIR/current"
ln -s "$RSVM_DIR/versions/$1" "$RSVM_DIR/current"
source $RSVM_SCRIPT
echo "done"
else
echo "The specified version $1 of rust is not installed..."
echo "You might want to install it with the following command:"
echo ""
echo "rsvm install $1"
fi
}
rsvm_current()
{
if [ ! -e "$RSVM_DIR/current" ]
then
echo "N/A"
return
fi
target=`echo $(readlink "$RSVM_DIR/current"|tr "/" "\n")`
echo ${target[@]} | awk '{print$NF}'
}
rsvm_ls()
{
DIRECTORIES=$(find "$RSVM_DIR/versions" -maxdepth 1 -mindepth 1 -type d -exec basename '{}' \; \
| sort \
| grep -E "^$RSVM_VERSION_PATTERN")
echo "Installed versions:"
echo ""
if [ $(grep -E -o "^$RSVM_VERSION_PATTERN" <<< "$DIRECTORIES" | wc -l) = 0 ]
then
echo ' - None';
else
for line in $(echo $DIRECTORIES | tr " " "\n")
do
if [ `rsvm_current` = "$line" ]
then
echo " => $line"
else
echo " - $line"
fi
done
fi
}
rsvm_init_folder_structure()
{
echo -n "Creating the respective folders for rust $1 ... "
mkdir -p "$RSVM_DIR/versions/$1/src"
mkdir -p "$RSVM_DIR/versions/$1/dist"
echo "done"
}
rsvm_install()
{
local CURRENT_DIR=`pwd`
local target=$1
local with_rustc_source=$2
local dirname
local url_prefix
local LAST_VERSION
local RUSTUP_CHANNEL
if [ ${1: -3} = '-rc' ]
then
url_prefix='/staging/dist'
target=${1%%-rc}
fi
if [[ $1 = "nightly" ]] || [[ $1 = "beta" ]] || [ ${1: -3} = '-rc' ]
then
# if same version reuse directory
LAST_VERSION=$(rsvm_ls|grep $1|tail -n 1|awk '{print $2}')
if [ $(rsvm_check_etag \
"https://static.rust-lang.org/dist$url_prfix/rust-$target-$RSVM_PLATFORM.tar.gz" \
"$RSVM_DIR/versions/$LAST_VERSION/src/rust-$target-$RSVM_PLATFORM.tar.gz") = 1 ]
then
dirname=$LAST_VERSION
else
dirname=$1.`date "+%Y%m%d%H%M%S"`
fi
if [[ $1 = "nightly" ]]
then
RUSTUP_CHANNEL=$1
else
RUSTUP_CHANNEL="beta"
fi
else
dirname=$1
RUSTUP_CHANNEL=$1
fi
rsvm_init_folder_structure $dirname
local SRC="$RSVM_DIR/versions/$dirname/src"
local DIST="$RSVM_DIR/versions/$dirname/dist"
local CARGO="$RSVM_DIR/versions/$dirname/cargo"
local RUSTUP="$RSVM_DIR/versions/$dirname/rustup"
cd $SRC
if [ -z $RSVM_PLATFORM ]
then
echo "rsvm: Not support this platform, $RSVM_OSTYPE"
return
fi
echo "Downloading sources for rust $dirname ... "
rsvm_file_download \
"https://static.rust-lang.org/dist$url_prfix/rust-$target-$RSVM_PLATFORM.tar.gz" \
"rust-$target-$RSVM_PLATFORM.tar.gz" \
true
if [ -e "rust-$target" ]
then
echo "Sources for rust $dirname already extracted ..."
else
echo -n "Extracting source ... "
tar -xzf "rust-$target-$RSVM_PLATFORM.tar.gz"
mv "rust-$target-$RSVM_PLATFORM" "rust-$target"
echo "done"
fi
if [ "$with_rustc_source" = true ]
then
echo "Downloading sources for rustc sourcecode $dirname ... "
rsvm_file_download \
"https://static.rust-lang.org/dist$url_prfix/rustc-$target-src.tar.gz" \
"rustc-$target-src.tar.gz" \
true
if [ -e "rustc-source" ]
then
echo "Sources for rustc $dirname already extracted ..."
else
echo -n "Extracting source ... "
tar -xzf "rustc-$target-src.tar.gz"
mv "rustc-$target" "rustc-source"
fi
fi
if [ ! -f $SRC/rust-$target/bin/cargo ] && [ ! -f $SRC/rust-$target/cargo/bin/cargo ]
then
echo "Downloading sources for cargo nightly ... "
rsvm_file_download \
"https://static.rust-lang.org/cargo-dist/cargo-nightly-$RSVM_PLATFORM.tar.gz" \
"cargo-nightly-$RSVM_PLATFORM.tar.gz" \
true
echo -n "Extracting source ... "
tar -xzf "cargo-nightly-$RSVM_PLATFORM.tar.gz"
mv "cargo-nightly-$RSVM_PLATFORM" "cargo-nightly"
echo "done"
cd "$SRC/cargo-nightly"
sh install.sh --prefix=$DIST
fi
cd "$SRC/rust-$target"
sh install.sh --prefix=$DIST
if [ ! -f $DIST/lib/rustlib/multirust-channel-manifest.toml ]
then
echo "Downloading channel manifest ... "
rsvm_file_download \
"https://static.rust-lang.org/dist/channel-rust-${RUSTUP_CHANNEL}.toml" \
"multirust-channel-manifest.toml"
echo "done"
cp multirust-channel-manifest.toml $DIST/lib/rustlib/multirust-channel-manifest.toml
fi
if [ ! -f $DIST/lib/rustlib/multirust-config.toml ]
then
cat << EOF > $DIST/lib/rustlib/multirust-config.toml
config_version = "1"
[[components]]
pkg = "rustc"
target = "$RSVM_PLATFORM"
[[components]]
pkg = "rust-std"
target = "$RSVM_PLATFORM"
[[components]]
pkg = "cargo"
target = "$RSVM_PLATFORM"
[[components]]
pkg = "rust-docs"
target = "$RSVM_PLATFORM"
EOF
fi
if [ ! -f $DIST/bin/rustup ]
then
echo "Downloading rustup ... "
rsvm_file_download \
"https://static.rust-lang.org/rustup/dist/$RSVM_PLATFORM/rustup-init" \
"rustup"
echo "done"
cp rustup $DIST/bin/rustup
chmod +x $DIST/bin/rustup
fi
mkdir -p $RUSTUP/toolchains
ln -s $DIST $RUSTUP/toolchains/${RUSTUP_CHANNEL}-${RSVM_PLATFORM}
cat << EOF > $RUSTUP/settings.toml
default_host_triple = "x86_64-unknown-linux-gnu"
default_toolchain = "nightly-x86_64-unknown-linux-gnu"
telemetry = false
version = "12"
[overrides]
EOF
echo ""
echo "And we are done. Have fun using rust $dirname."
cd $CURRENT_DIR
RSVM_LAST_INSTALLED_VERSION=$dirname
}
rsvm_ls_remote()
{
local VERSIONS
local STABLE_VERSION
if [ -z $RSVM_PLATFORM ]
then
echo "rsvm: Not support this platform, $RSVM_OSTYPE"
return
fi
STABLE_VERSION=$(rsvm_ls_channel stable)
rsvm_file_download https://static.rust-lang.org/dist/index.txt "$RSVM_DIR/cache/index.txt"
VERSIONS=$(cat "$RSVM_DIR/cache/index.txt" \
| command grep -E -o "^/dist/rust-$RSVM_NORMAL_PATTERN-$RSVM_PLATFORM.tar.gz" \
| command grep -E -o "$RSVM_VERSION_PATTERN" \
| command sort \
| command uniq)
for VERSION in $VERSIONS;
do
if [ "$STABLE_VERSION" = "$VERSION" ]
then
continue
fi
echo $VERSION
done
echo $STABLE_VERSION
rsvm_ls_channel staging
rsvm_ls_channel beta
rsvm_ls_channel nightly
}
rsvm_ls_channel()
{
local VERSIONS
local POSTFIX
if [ -z $RSVM_PLATFORM ]
then
echo "rsvm: Not support this platform, $RSVM_OSTYPE"
return
fi
case $1 in
staging|rc)
POSTFIX='-rc'
rsvm_file_download https://static.rust-lang.org/dist/staging/dist/channel-rust-stable "$RSVM_DIR/cache/channel-rust-staging"
VERSIONS=$(cat "$RSVM_DIR/cache/channel-rust-staging" \
| command grep -E -o "rust-$RSVM_VERSION_PATTERN-$RSVM_PLATFORM.tar.gz" \
| command grep -E -o "$RSVM_VERSION_PATTERN" \
| command sort \
| command uniq)
;;
stable|beta|nightly)
rsvm_file_download https://static.rust-lang.org/dist/channel-rust-$1 "$RSVM_DIR/cache/channel-rust-$1"
VERSIONS=$(cat "$RSVM_DIR/cache/channel-rust-$1" \
| command grep -E -o "rust-$RSVM_VERSION_PATTERN-$RSVM_PLATFORM.tar.gz" \
| command grep -E -o "$RSVM_VERSION_PATTERN" \
| command sort \
| command uniq)
;;
*)
echo "rsvm: Not support this channel, $1"
return
;;
esac
for VERSION in $VERSIONS;
do
echo $VERSION$POSTFIX
done
}
rsvm_uninstall()
{
if [ `rsvm_current` = "$1" ]
then
echo "rsvm: Cannot uninstall currently-active version, $1"
return
fi
if [ ! -d "$RSVM_DIR/versions/$1" ]
then
echo "$1 version is not installed yet..."
return
fi
echo "uninstall $1 ..."
case $RSVM_OSTYPE in
Darwin)
rm -ri "$RSVM_DIR/versions/$1"
;;
*)
rm -rI "$RSVM_DIR/versions/$1"
;;
esac
}
rsvm()
{
rsvm_initialize
case $1 in
""|help|--help|-h)
echo ''
echo 'Rust Version Manager'
echo '===================='
echo ''
echo 'Usage:'
echo ''
echo ' rsvm help | --help | -h Show this message.'
echo ' rsvm install <version> Download and install a <version>.'
echo ' <version> could be for example "0.12.0".'
echo ' rsvm uninstall <version> Uninstall a <version>.'
echo ' rsvm use <version> Activate <version> for now and the future.'
echo ' rsvm ls | list List all installed versions of rust.'
echo ' rsvm ls-remote List remote versions available for install.'
echo ' rsvm ls-channel Print a channel version available for install.'
echo ''
echo "Current version: $RSVM_VERSION"
;;
--version|-v)
echo "v$RSVM_VERSION"
;;
install)
if [ -z "$2" ]
then
# whoops. no version found!
echo "Please define a version of rust!"
echo ""
echo "Example:"
echo " rsvm install 0.12.0"
elif ([[ "$2" =~ ^$RSVM_VERSION_PATTERN$ ]])
then
local version=$2
local with_rustc_source=true
for i in ${@:3:${#@}}
do
case $i in
--dry)
echo "Would install rust $version"
RSVM_LAST_INSTALLED_VERSION=$version
rsvm_use $RSVM_LAST_INSTALLED_VERSION
exit
;;
--without-rustc-source)
with_rustc_source=false
;;
*)
;;
esac
done
rsvm_install "$version" "$with_rustc_source"
rsvm_use $RSVM_LAST_INSTALLED_VERSION
else
# the version was defined in a the wrong format.
echo "You defined a version of rust in a wrong format!"
echo "Please use either <major>.<minor> or <major>.<minor>.<patch>."
echo ""
echo "Example:"
echo " rsvm install 0.12.0"
fi
;;
ls|list)
rsvm_ls
;;
ls-remote)
rsvm_ls_remote
;;
ls-channel)
if [ -z "$2" ]
then
# whoops. no channel found!
echo "Please define a channel of rust!"
echo ""
echo "Example:"
echo " rsvm ls-channel stable"
else
rsvm_ls_channel $2
fi
;;
use)
if [ -z "$2" ]
then
# whoops. no version found!
echo "Please define a version of rust!"
echo ""
echo "Example:"
echo " rsvm use 0.12.0"
elif ([[ "$2" =~ ^$RSVM_VERSION_PATTERN$ ]])
then
rsvm_use "$2"
else
# the version was defined in a the wrong format.
echo "You defined a version of rust in a wrong format!"
echo "Please use either <major>.<minor> or <major>.<minor>.<patch>."
echo ""
echo "Example:"
echo " rsvm use 0.12.0"
fi
;;
uninstall)
if [ -z "$2" ]
then
# whoops. no version found!
echo "Please define a version of rust!"
echo ""
echo "Example:"
echo " rsvm use 0.12.0"
elif ([[ "$2" =~ ^$RSVM_VERSION_PATTERN$ ]])
then
rsvm_uninstall "$2"
else
# the version was defined in a the wrong format.
echo "You defined a version of rust in a wrong format!"
echo "Please use either <major>.<minor> or <major>.<minor>.<patch>."
echo ""
echo "Example:"
echo " rsvm uninstall 0.12.0"
fi
;;
*)
rsvm
esac
echo ''
}
# vim: et ts=2 sw=2