forked from ghosind/dvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·180 lines (149 loc) · 3.01 KB
/
install.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
#!/usr/bin/env bash
get_rc_file() {
case ${SHELL##*/} in
bash)
DVM_RC_FILE="$HOME/.bashrc"
;;
zsh)
DVM_RC_FILE="$HOME/.zshrc"
;;
*)
DVM_RC_FILE="$HOME/.profile"
;;
esac
}
add_nvm_into_rc_file() {
local is_dvm_defined
get_rc_file
is_dvm_defined=$(grep DVM_DIR < "$DVM_RC_FILE")
if [ -n "$is_dvm_defined" ]
then
return
fi
echo "
# Deno Version Manager
export DVM_DIR=\"\$HOME/.dvm\"
export DVM_BIN=\"\$DVM_DIR/bin\"
export PATH=\"\$PATH:\$DVM_BIN\"
[ -f \"\$DVM_DIR/dvm.sh\" ] && alias dvm=\"\$DVM_DIR/dvm.sh\"
[ -f \"\$DVM_DIR/bash_completion\" ] && . \"\$DVM_DIR/bash_completion\"
" >> "$DVM_RC_FILE"
}
get_latest_version() {
local request_url
local response
local field
case "$DVM_SOURCE" in
gitee)
request_url="https://gitee.com/api/v5/repos/ghosind/dvm/releases/latest"
field="6"
;;
github|*)
request_url="https://api.github.com/repos/ghosind/dvm/releases/latest"
field="4"
;;
esac
if [ ! -x "$(command -v curl)" ]
then
echo "Curl is required."
exit 1
fi
if ! response=$(curl -s "$request_url")
then
echo "Failed to get the latest DVM version."
exit 1
fi
DVM_LATEST_VERSION=$(echo "$response" | grep tag_name | cut -d '"' -f $field)
}
install_latest_version() {
local git_url
local cmd
case "$DVM_SOURCE" in
gitee)
git_url="https://gitee.com/ghosind/dvm.git"
;;
github|*)
git_url="https://github.com/ghosind/dvm.git"
;;
esac
if [ ! -x "$(command -v git)" ]
then
echo "git is require."
exit 1
fi
cmd="git clone -b $DVM_LATEST_VERSION $git_url $DVM_DIR"
if ! ${cmd}
then
echo "failed to download DVM."
exit 1
fi
}
set_dvm_dir() {
if [ ! -d "$DVM_DIR" ]
then
mkdir -p "$DVM_DIR"
else
echo "directory $DVM_DIR is existed."
exit 1
fi
}
install_dvm() {
set_dvm_dir
DVM_SCRIPT_DIR=${0%/*}
if [ -f "$DVM_SCRIPT_DIR/dvm.sh" ] &&
[ -d "$DVM_SCRIPT_DIR/.git" ] &&
[ -f "$DVM_SCRIPT_DIR/bash_completion" ]
then
# Copy all files to DVM_DIR
cp -R "$DVM_SCRIPT_DIR/". "$DVM_DIR"
else
get_latest_version
install_latest_version
fi
add_nvm_into_rc_file
echo "DVM has been installed, please restart your terminal or run \`source $DVM_RC_FILE\` to apply changes."
}
set_default() {
DVM_DIR="$HOME/.dvm"
DVM_SOURCE="github"
}
print_help() {
printf "DVM install script
Usage: install.sh [-r <github|gitee>] [-d <dvm_dir>]
Options:
-r <github|gitee> Set the repository server, default github.
-d dir Set the dvm install directory, default ~/.dvm.
-h Print help.
Example:
install.sh -r github -d ~/.dvm
"
}
set_default
while getopts "hr:d:" opt
do
case "$opt" in
h)
print_help
exit 0
;;
r)
if [ "$OPTARG" != "github" ] && [ "$OPTARG" != "gitee" ]
then
print_help
exit 1
fi
DVM_SOURCE="$OPTARG"
;;
d)
if [ -z "$OPTARG" ]
then
print_help
exit 1
fi
DVM_DIR="$OPTARG"
;;
*)
;;
esac
done
install_dvm