-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdvm.sh
executable file
·2232 lines (1870 loc) · 51.9 KB
/
dvm.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
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
# Deno Version Manager
# Copyright (C) 2020 ~ 2025, Chen Su and all contributors.
# A lightweight, and powerful Deno version manager for MacOS, Linux, WSL, and
# Windows with Bash.
{ # Ensure the integrality of this script
export DVM_VERSION="v0.9.1"
######################
## Helper Functions ##
######################
{
################
## Environment ##
################
{
# Check aliases directory, and try to create it if is not existed.
dvm_check_alias_dir() {
if [ ! -d "$DVM_DIR/aliases" ]
then
mkdir -p "$DVM_DIR/aliases"
fi
}
# Gets user profile file path by the shell.
dvm_get_profile_file() {
case "${SHELL##*/}" in
"bash")
DVM_PROFILE_FILE="$HOME/.bashrc"
;;
"zsh")
DVM_PROFILE_FILE="$HOME/.zshrc"
;;
*)
DVM_PROFILE_FILE="$HOME/.profile"
;;
esac
dvm_debug "profile file: $DVM_PROFILE_FILE"
}
# Check whether a command exists or not.
# Parameters:
# $1: command to check.
dvm_has() {
command -v "$1" > /dev/null
}
# Set the environment variables to the default values.
dvm_set_default_env() {
# set default dvm directory
DVM_DIR=${DVM_DIR:-$HOME/.dvm}
# Set modes
DVM_COLOR_MODE=true
DVM_QUIET_MODE=false
DVM_VERBOSE_MODE=false
# Set global variables to default values
DVM_DENO_VERSION=""
DVM_FILE_TYPE=""
DVM_INSTALL_MODE="binary"
DVM_INSTALL_REGISTRY=""
DVM_INSTALL_SKIP_VALIDATION=false
DVM_INSTALL_SKIP_CACHE=false
DVM_LATEST_VERSION=""
DVM_PROFILE_FILE=""
DVM_REMOTE_VERSIONS=""
DVM_REQUEST_RESPONSE=""
DVM_SOURCE=""
DVM_TARGET_NAME=""
DVM_TARGET_TYPE=""
DVM_TARGET_VERSION=""
}
# Remove Deno path from the global environment variable `PATH` that added
# by DVM.
dvm_strip_path() {
echo "$PATH" | tr ":" "\n" | grep -v "$DVM_DIR" | tr "\n" ":"
}
}
######################
## Handle Parameter ##
######################
{
# Check all parameters and try to match available options.
dvm_parse_options() {
while [ "$#" -gt "0" ]
do
case "$1" in
"-q"|"--quiet")
DVM_QUIET_MODE=true
;;
"--color")
DVM_COLOR_MODE=true
;;
"--no-color")
DVM_COLOR_MODE=false
;;
"--verbose")
DVM_VERBOSE_MODE=true
;;
*)
;;
esac
shift
done
}
}
###########
## Input ##
###########
{
# Print a prompt message, and get the confirm (yes or no) from the user
# input.
# Parameters:
# $1: the prompt message.
dvm_confirm_with_prompt() {
local confirm
local prompt
if [ "$#" = 0 ]
then
return
fi
prompt="$1"
echo -n "$prompt (y/n): "
while true
do
read -r confirm
case "$confirm" in
[yY]*)
return 0
;;
[nN]*)
return 1
;;
*)
;;
esac
echo -n "Please type 'y' or 'n': "
done
}
}
###################
## Network Tools ##
###################
{
# Download file from the specific url, and save the file to the specific
# path.
# Parameters:
# - $1: downloading url.
# - $2: the path of downloaded file.
dvm_download_file() {
local url
local file
local cmd
url="$1"
file="$2"
dvm_debug "downloading url: $url"
dvm_debug "download destination file: $file"
if dvm_has curl
then
cmd="curl -LJ $url -o $file"
if [ "$DVM_QUIET_MODE" = true ]
then
cmd="$cmd -s"
elif [ "$DVM_VERBOSE_MODE" = true ]
then
cmd="$cmd -v"
fi
else
dvm_print_error "curl is required."
dvm_failure
return
fi
dvm_debug "download file command: $cmd"
if ! eval "$cmd"
then
dvm_failure
fi
}
# Send a GET request to the specific url, and save response to the
# `DVM_REQUEST_RESPONSE` variable.
# Parameters:
# - $1: request url.
# - $2...: options for curl.
dvm_request() {
local url
# Clear response content.
DVM_REQUEST_RESPONSE=""
if ! dvm_has curl
then
dvm_print_error "curl is required"
dvm_failure
return
fi
url="$1"
shift
cmd="curl -s '$url' $*"
if [ "$DVM_VERBOSE_MODE" = true ]
then
cmd="$cmd -v"
fi
if [ -n "$GITHUB_API_TOKEN" ] && [[ "$url" = "https://api.github.com/"* ]]
then
cmd="$cmd -H \"Authorization: Bearer $GITHUB_API_TOKEN\""
fi
dvm_debug "request url: $url"
dvm_debug "request command: $cmd"
if ! DVM_REQUEST_RESPONSE=$(eval "$cmd")
then
dvm_failure
return
fi
dvm_debug "request response: $DVM_REQUEST_RESPONSE"
}
}
############
## Output ##
############
{
# Print debug message in the verbose mode.
# Parameters:
# - $1...: the message to print.
dvm_debug() {
if [ "$DVM_VERBOSE_MODE" = true ]
then
echo -e "[DEBUG]" "$@"
fi
}
# Print messages without quiet mode.
# Parameters:
# - $1...: the message to print.
dvm_print() {
if [ "$DVM_QUIET_MODE" = true ]
then
return
fi
echo -e "$@"
}
# Print error message with red color text.
# Parameters:
# $1...: the message to print.
dvm_print_error() {
dvm_print_with_color "31" "[ERR]" "$@"
}
# Print warning message with yellow color text.
# Parameters:
# $1...: the message to print.
dvm_print_warning() {
dvm_print_with_color "33" "[WARN]" "$@"
}
# Print message with the specific color.
# Parameters:
# - $1: the color code.
# - $2...: the message to print.
dvm_print_with_color() {
local color="$1"
shift
if [ "$DVM_COLOR_MODE" = true ] && [ -n "$color" ]
then
dvm_print "\x1b[${color}m$*\x1b[0m"
else
dvm_print "$@"
fi
}
}
###################
## Return Status ##
###################
{
# Set return status to true.
dvm_success() {
# execute true to set as success
true
}
# Set return status to false.
dvm_failure() {
# execute false to set as fail
false
}
}
####################
## Version Handle ##
####################
{
# Compare two version number.
# Parameters:
# $1, $2: the version number to compare.
dvm_compare_version() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$2"
}
# Try to getting the active Deno version, and set it to `DVM_DENO_VERSION`
# variable.
dvm_get_current_version() {
local deno_path
local deno_dir
if ! deno_path=$(which deno 2>/dev/null)
then
return
fi
if [[ "$deno_path" != "$DVM_DIR/versions/"* ]]
then
return
fi
deno_dir=${deno_path%/deno}
DVM_DENO_VERSION=${deno_dir##*/}
dvm_debug "active deno version: $DVM_DENO_VERSION"
}
# Try to get a Deno version from the parameters or the .dvmrc file (the
# current directory or the user home directory).
# Parameters:
# $1...: the Deno version.
dvm_get_version() {
local result
local prefix
if ! dvm_get_version_by_param "$@"
then
dvm_get_version_from_dvmrc
fi
if [ -z "$DVM_TARGET_VERSION" ]
then
return
fi
if [[ "$DVM_TARGET_VERSION" != "v"* ]]
then
DVM_TARGET_VERSION="v$DVM_TARGET_VERSION"
fi
result=$(echo "$DVM_TARGET_VERSION" | grep -E "^v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+")
if [ -n "$result" ]
then
return
fi
dvm_debug "$DVM_TARGET_VERSION is a prefix"
dvm_debug "try to get the latest version with the prefix $DVM_TARGET_VERSION"
prefix="$DVM_TARGET_VERSION"\.
DVM_TARGET_VERSION=""
for version in "$DVM_DIR/versions"/*
do
if [[ "$version" == *"$prefix"* ]]
then
result=${version##*/}
dvm_debug "version $result matched to the prefix $DVM_TARGET_VERSION"
fi
done
if [ -z "$result" ]
then
dvm_print_error "no version found with the prefix $DVM_TARGET_VERSION"
dvm_failure
else
dvm_debug "found the latest version $result with the prefix $DVM_TARGET_VERSION"
DVM_TARGET_VERSION="$result"
fi
}
# Try to get a valid and installed Deno version from the parameters.
# Parameters:
# $1...: the Deno version.
dvm_get_version_by_param() {
DVM_TARGET_VERSION=""
while [[ "$1" == "-"* ]]
do
shift
done
if [ "$#" = "0" ]
then
return 1
fi
dvm_debug "try to get version by param: $1"
if [ -f "$DVM_DIR/aliases/$1" ]
then
DVM_TARGET_VERSION=$(head -n 1 "$DVM_DIR/aliases/$1")
if [ ! -f "$DVM_DIR/versions/$DVM_TARGET_VERSION/deno" ]
then
DVM_TARGET_VERSION="$1"
fi
else
DVM_TARGET_VERSION="$1"
fi
}
# Try to read version from .dvmrc file in the current working directory or
# the user home directory.
dvm_get_version_from_dvmrc() {
if dvm_read_dvmrc_file "$PWD" ".dvmrc" || dvm_read_dvmrc_file "$PWD" ".deno-version"
then
return 0
fi
if [ "$PWD" != "$HOME" ]
then
if dvm_read_dvmrc_file "$HOME" ".dvmrc" || dvm_read_dvmrc_file "$HOME" ".deno-version"
then
return 0
fi
fi
return 1
}
# Read .dvmrc or .deno-version file from the specified path, and set it to
# `DVM_TARGET_VERSION` variable if the file is not empty.
# Parameters:
# - $1: path directory
# - $2: file name
dvm_read_dvmrc_file() {
local version
local file_dir="$1"
local filename="$2"
local file="$file_dir/$filename"
if [ -f "$file" ]
then
dvm_debug "$file found"
version=$(head -n 1 "$file")
else
dvm_debug "no $filename found in $file_dir"
return 1
fi
if [ -n "$version" ]
then
dvm_print "Found '$file' with version $version"
DVM_TARGET_VERSION="$version"
return 0
else
dvm_debug "empty $filename file $file_dir"
return 1
fi
}
}
}
###################
## Command alias ##
###################
{
# Set an alias to the specific Deno version, and it will overwrite if the
# alias was created.
# Parameters:
# $1: the alias name to be set.
# $2: the Deno version to alias.
dvm_set_alias() {
local alias_name
local version
dvm_check_alias_dir
while [ "$#" -gt "0" ]
do
case "$1" in
"-"*)
;;
*)
if [ -z "$alias_name" ]
then
alias_name="$1"
elif [ -z "$version" ]
then
version="$1"
fi
esac
shift
done
if [ -z "$alias_name" ] || [ -z "$version" ]
then
dvm_print_help
dvm_failure
return
fi
if [ ! -f "$DVM_DIR/versions/$version/deno" ]
then
dvm_print_error "deno $version is not installed."
dvm_failure
return
fi
echo "$version" > "$DVM_DIR/aliases/$alias_name"
dvm_print "$alias_name -> $version"
}
}
###################
## Command clean ##
###################
{
# Remove version and download caches.
dvm_clean_caches() {
if [ -d "$DVM_DIR/cache" ]
then
rm -rf "$DVM_DIR/cache"
fi
dvm_clean_download_cache
}
# Clean downloading caches in the disk.
dvm_clean_download_cache() {
if [ ! -d "$DVM_DIR/download" ]
then
return
fi
if [ -z "$(ls -A "$DVM_DIR/download")" ]
then
return
fi
for cache_path in "$DVM_DIR/download"/*
do
if [ ! -d "$cache_path" ]
then
continue
fi
[ -f "$cache_path/deno-downloading.zip" ] && rm "$cache_path/deno-downloading.zip"
[ -f "$cache_path/deno-downloading.gz" ] && rm "$cache_path/deno-downloading.gz"
[ -f "$cache_path/deno.zip" ] && rm "$cache_path/deno.zip"
[ -f "$cache_path/deno.gz" ] && rm "$cache_path/deno.gz"
rmdir "$cache_path"
done
}
}
#####################
## Command current ##
#####################
{
# Gets the current Deno version and prints. It will print `system (vX.X.X)`
# if Deno doesn't install by DVM.
dvm_print_current_version() {
local deno_version
if ! dvm_has deno
then
dvm_print "none"
return
fi
dvm_get_current_version
if [ -n "$DVM_DENO_VERSION" ]
then
dvm_print "$DVM_DENO_VERSION"
else
deno_version=$(deno --version | grep "deno" | cut -d " " -f 2)
dvm_print "system (v$deno_version)"
fi
}
}
########################
## Command deactivate ##
########################
{
# Deactivate the active Deno version that added into the global environment
# variable `PATH` by DVM
dvm_deactivate() {
local path_without_dvm
dvm_get_current_version
if [ -z "$DVM_DENO_VERSION" ]
then
dvm_success
return
fi
path_without_dvm=$(dvm_strip_path)
export PATH="$path_without_dvm"
dvm_print "Deno has been deactivated, you can run \"dvm use $DVM_DENO_VERSION\" to restore it."
unset DVM_DENO_VERSION
}
}
####################
## Command doctor ##
####################
{
# Scan the installed versions, and try to finding the invalid versions (the
# versions from path and Deno `-v` option are not same). It'll try to fix the
# invalid versions if it run in the `fix` mode.
# Parameters:
# $1: the mode of the doctor command.
dvm_scan_and_fix_versions() {
local mode
local raw_output
local invalid_message
local corrupted_message
local version
local deno_version
mode="$1"
if [ ! -d "$DVM_DIR/versions" ]
then
return
fi
if [ -z "$(ls -A "$DVM_DIR/versions")" ]
then
return
fi
for version_path in "$DVM_DIR/versions/"*
do
if [ ! -f "$version_path/deno" ]
then
continue
fi
version=${version_path##*/}
raw_output=$("$version_path/deno" --version 2>/dev/null)
if [ -z "$raw_output" ]
then
corrupted_message="$corrupted_message$version\n"
if [ "$mode" = "fix" ]
then
rm -rf "$version_path"
fi
else
deno_version=$(echo "$raw_output" | grep deno | cut -d " " -f 2)
if [ "$version" != "v$deno_version" ]
then
invalid_message="$invalid_message$version -> v$deno_version\n"
if [ "$mode" = "fix" ]
then
mkdir -p "$DVM_DIR/doctor_temp"
mv -f "$version_path" "$DVM_DIR/doctor_temp/v$deno_version"
fi
fi
fi
done
if [ "$mode" = "fix" ]
then
dvm_fix_invalid_versions
else
dvm_print_doctor_message "$invalid_message" "$corrupted_message"
fi
}
# Try to moving the Deno files to the correct path, and remove it if the
# version was existed.
dvm_fix_invalid_versions() {
local version
if [ ! -d "$DVM_DIR/doctor_temp" ]
then
return
fi
for version_path in "$DVM_DIR/doctor_temp/"*
do
version=${version_path##*/}
if [ -d "$DVM_DIR/versions/$version" ]
then
rm -rf "$version_path"
else
mv "$version_path" "$DVM_DIR/versions/$version"
fi
done
rmdir "$DVM_DIR/doctor_temp"
dvm_print "Invalid version(s) has been fixed."
}
# Print the invalid (versions from file and path are not same) and the
# corrupted (unable to run) versions.
# Parameters:
# $1: invalid versions list.
# $2: corrupted versions list.
dvm_print_doctor_message() {
local invalid_message
local corrupted_message
invalid_message="$1"
corrupted_message="$2"
if [ -z "$invalid_message" ] && [ -z "$corrupted_message" ]
then
dvm_print "Everything is ok."
return
fi
if [ -n "$invalid_message" ]
then
dvm_print "Invalid versions:"
dvm_print "$invalid_message"
fi
if [ -n "$corrupted_message" ]
then
dvm_print "Corrupted versions:"
dvm_print "$corrupted_message"
fi
dvm_print "You can run \"dvm doctor --fix\" to fix these errors."
}
}
##################
## Command help ##
##################
{
# Print help messages.
dvm_print_help() {
dvm_print
dvm_print "Deno Version Manager"
dvm_print
dvm_print "Usage:"
dvm_print " dvm install [version | prefix] Download and install by version or the prefix. Install latest or use .dvmrc file if version is omitted."
dvm_print " --registry=<registry> Download and install deno with the specified registry."
dvm_print " --skip-validation Skip version validation before download."
dvm_print " --skip-download-cache Don't use downloaded cache file."
dvm_print " dvm uninstall [name|version] Uninstall a specified version."
dvm_print " dvm use [name|version] Use the specified version that passed by argument or read from .dvmrc."
dvm_print " dvm run <name|version> [args] Run deno on the specified version with arguments."
dvm_print " dvm alias <name> <version> Set an alias name to specified version."
dvm_print " dvm unalias <name|version> Delete the specified alias name."
dvm_print " dvm current Display the current version of Deno."
dvm_print " dvm ls List all installed versions."
dvm_print " dvm ls-remote List all remote versions."
dvm_print " dvm which [current|name|version] Display the path of installed version."
dvm_print " dvm clean Remove all downloaded packages and cached versions."
dvm_print " dvm deactivate Deactivate Deno on current shell."
dvm_print " dvm doctor Scan installed versions and find invalid / corrupted versions."
dvm_print " --fix Scan and fix all invalid / corrupted versions."
dvm_print " dvm upgrade Upgrade dvm itself."
dvm_print " dvm purge Remove dvm from your computer."
dvm_print " dvm help Show this message."
dvm_print
dvm_print "Options:"
dvm_print " -q, --quiet Make outputs more quiet."
dvm_print " --color Print colorful messages."
dvm_print " --no-color Print messages without color."
dvm_print " --verbose Run in verbose mode, it'll print debug messages."
dvm_print
dvm_print "Note:"
dvm_print " <param> is required parameter, [param] is optional parameter."
dvm_print
dvm_print "Examples:"
dvm_print " dvm install v1.0.0"
dvm_print " dvm uninstall v0.42.0"
dvm_print " dvm use v1.0.0"
dvm_print " dvm alias default v1.0.0"
dvm_print " dvm run v1.0.0 app.ts"
dvm_print
}
}
#####################
## Command install ##
#####################
{
# Try to build the binary file of Deno with the specified version, and move
# the build target file to the versions directory.
# Parameters:
# - $1: The Deno version to building.
dvm_build_deno() {
local version
version="$1"
old_dir=$(pwd)
cd "$DVM_DIR/deno_code" || return
git reset --hard HEAD
if ! git checkout "$version" --recurse-submodules
then
dvm_failure
return
fi
cargo clean
if ! cargo build --release
then
dvm_print_error "failed to build deno"
cd "$old_dir" || return
dvm_failure
return
fi
if ! dvm_validate_build_target "$version"
then
cd "$old_dir" || return
dvm_failure
return
elif ! dvm_copy_build_target_to_versions_dir "$version"
then
cd "$old_dir" || return
dvm_failure
return
else
cargo clean
cd "$old_dir" || return
fi
}
# Check the dependencies for building Deno from the source code.
dvm_check_build_dependencies() {
for command in git rustc cargo cc cmake
do
if ! dvm_has "$command"
then
dvm_print_error "$command is required"
dvm_failure
return
fi
done
}
# Try to check the local clone of the source code, and fetch the latest data
# if the local clone is valid. It will delete the source code directory and
# clone it again later if the directory is not the repo of the Deno source
# code.
dvm_check_local_deno_clone() {
local old_dir
old_dir=$(pwd)
cd "$DVM_DIR/deno_code" || return
ret=$(git remote -v | grep "deno.git")
if [ -z "$ret" ]
then
dvm_print_warning "The local clone of Deno source is invalid, trying to re-clone..."
rm -rf "$DVM_DIR/deno_code"
cd "$old_dir" || return
dvm_failure
else
# update repo
git fetch
cd "$old_dir" || return
fi
}
# Clone the source code of Deno into the local directory, and update the
# local clone if it was cloned.
dvm_clone_deno_source() {
if [ -d "$DVM_DIR/deno_code" ]
then
if dvm_check_local_deno_clone
then
return
fi
fi
git clone --recurse-submodules https://github.com/denoland/deno.git "$DVM_DIR/deno_code"
}
# Move the build output file to the versions file.
# Parameters:
# - $1: The Deno version to install.
dvm_copy_build_target_to_versions_dir() {
local version
version="$1"
if ! [ -d "$DVM_DIR/versions/$version" ]
then
mkdir -p "$DVM_DIR/versions/$version"
fi
cp "$DVM_DIR/deno_code/target/release/deno" "$DVM_DIR/versions/$version"
}
# Download Deno with the specific version from GitHub or the specific
# registry (specify by `DVM_INSTALL_REGISTRY` variable). It will download
# Deno to the cache directory, and move it to versions directory after
# completed.
# Parameters:
# - $1: the Deno version to download.
dvm_download_deno() {
local version
local url
local temp_file
local registry
version="$1"
if [ ! -d "$DVM_DIR/download/$version" ]
then
mkdir -p "$DVM_DIR/download/$version"
fi
if [ -z "$DVM_INSTALL_REGISTRY" ]
then
if ! [[ "$version" < "v1.0.1" ]] || [[ "$version" = "v1.0.0" ]]
then
registry="https://dl.deno.land/release"
else
registry="https://github.com/denoland/deno/releases/download"
fi
else
registry="$DVM_INSTALL_REGISTRY"
fi
dvm_debug "registry url: $registry"
url="$registry/$version/$DVM_TARGET_NAME"
temp_file="$DVM_DIR/download/$version/deno-downloading.$DVM_TARGET_TYPE"
if dvm_download_file "$url" "$temp_file"
then
local file_type
file_type=$(file "$temp_file")
if [[ $file_type == *"$DVM_FILE_TYPE"* ]]
then
mv "$temp_file" "$DVM_DIR/download/$version/deno.$DVM_TARGET_TYPE"
return
fi
fi
if [ -f "$temp_file" ]
then
rm "$temp_file"
fi
dvm_print_error "failed to download deno $version."
dvm_failure
}
# Extract the Deno compressed file, and add execute permission to the binary