-
Notifications
You must be signed in to change notification settings - Fork 4
/
install.sh
executable file
·195 lines (162 loc) · 3.49 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env bash
# Installation script for Deno Version Manager
# Copyright (C) 2020 ~ 2024, Chen Su and all contributors.
# Ensure the script is downloaded completely
{
dvm_add_into_profile_file() {
local is_dvm_defined
dvm_get_profile_file
is_dvm_defined=$(grep DVM_DIR < "$DVM_PROFILE_FILE")
if [ -n "$is_dvm_defined" ]
then
return
fi
echo "
# Deno Version Manager
export DVM_DIR=\"\$HOME/.dvm\"
[ -f \"\$DVM_DIR/dvm.sh\" ] && . \"\$DVM_DIR/dvm.sh\"
[ -f \"\$DVM_DIR/bash_completion\" ] && . \"\$DVM_DIR/bash_completion\"
" >> "$DVM_PROFILE_FILE"
}
dvm_check_dir() {
if [ ! -d "$DVM_DIR" ]
then
mkdir -p "$DVM_DIR"
else
echo "directory $DVM_DIR already exists."
exit 1
fi
}
dvm_compare_version() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$2"
}
dvm_get_latest_version() {
local request_url
local response
case "$DVM_SOURCE" in
"gitee")
request_url="https://gitee.com/api/v5/repos/ghosind/dvm/releases/latest"
;;
"github"|*)
request_url="https://api.github.com/repos/ghosind/dvm/releases/latest"
;;
esac
if ! dvm_has 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" | sed 's/"/\n/g' | grep tag_name -A 2 | grep v)
}
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_has() {
command -v "$1" > /dev/null
}
dvm_install() {
dvm_check_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
dvm_get_latest_version
dvm_install_latest_version
fi
dvm_add_into_profile_file
echo "DVM has been installed, please restart your terminal or run \`source $DVM_PROFILE_FILE\` to apply changes."
}
dvm_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 ! dvm_has git
then
echo "git is require."
exit 1
fi
cmd="git clone -b $DVM_LATEST_VERSION $git_url $DVM_DIR --depth=1"
if ! ${cmd}
then
echo "failed to download DVM."
exit 1
fi
}
dvm_print_help() {
echo "DVM installation script"
echo
echo "Usage: install.sh [-r <github|gitee>] [-d <dvm_dir>]"
echo
echo "Options:"
echo " -r <github|gitee> Set the repository server, default github."
echo " -d dir Set the dvm install directory, default ~/.dvm."
echo " -h Print help."
echo
echo "Example:"
echo " install.sh -r github -d ~/.dvm"
}
dvm_set_default() {
DVM_DIR=${DVM_DIR:-$HOME/.dvm}
DVM_SOURCE=${DVM_SOURCE:-github}
}
dvm_set_default
while getopts "hr:d:" opt
do
case "$opt" in
"h")
dvm_print_help
exit 0
;;
"r")
if [ "$OPTARG" != "github" ] && [ "$OPTARG" != "gitee" ]
then
dvm_print_help
exit 1
fi
DVM_SOURCE="$OPTARG"
;;
"d")
if [ -z "$OPTARG" ]
then
dvm_print_help
exit 1
fi
DVM_DIR="$OPTARG"
;;
*)
;;
esac
done
if [ "$DVM_SOURCE" != "github" ] && [ "$DVM_SOURCE" != "gitee" ]
then
dvm_print_help
exit 1
fi
dvm_install
}