forked from rvm/rvm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rvm-installer
executable file
·248 lines (188 loc) · 5.23 KB
/
rvm-installer
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
#!/usr/bin/env bash
log() { printf "$*\n" ; return $? ; }
fail() { log "\nERROR: $*\n" ; exit 1 ; }
usage()
{
printf "
Usage
rvm-installer [options] [action]
Options
--branch <name> - Install RVM head, from named branch
--version <head|latest|x.y.z> - Install RVM version [head|latest|x.y.z]
--trace - used to debug the installer script
Actions
help - Display CLI help (this output)
"
}
fetch_version()
{
version=$(curl -B "${rvm_releases_url}/latest-version.txt" 2>/dev/null)
}
fetch_md5()
{
md5=$(curl -B "${rvm_releases_url}/rvm-${version}.tar.gz.md5" 2>/dev/null)
}
md5_match()
{
local archive="$1"
case "$(uname)" in
Darwin|FreeBSD)
archive_md5="$(/sbin/md5 -q "${archive}")"
;;
OpenBSD)
archive_md5="$(/bin/md5 -q "${archive}")"
;;
Linux|*)
archive_md5="$(md5sum "${archive}" | awk '{print $1}')"
;;
esac
[[ "$archive_md5" == "$md5" ]]
return $?
}
install_release()
{
archive="$rvm_archives_path/rvm-${version}.tar.gz"
fetch_md5
if [[ -s $archive ]] && ! md5_match ; then
# Remove old installs, if they exist and have incorrect md5.
[[ -f "$rvm_archives_path/rvm-${version}.tar.gz" ]] &&
rm -f "$rvm_archives_path/rvm-${version}.tar.gz"
fi
curl -L "${rvm_releases_url}/rvm-${version}.tar.gz" -o "$archive"
if ! md5_match "$archive" ; then
fail "ERROR:
Archive package downloaded does not match it's calculated md5 checksum ${md5}:
$rvm_archives_path/rvm-${version}.tar.gz
Retry the installation and/or check your networking setup.
Halting installation.
"
fi
tar zoxf "${rvm_archives_path}/rvm-${version}.tar.gz" -C "$rvm_src_path/"
cd "$rvm_src_path/rvm-${version}"
}
install_head()
{
local remote="origin"
if [[ -d "${rvm_src_path}/rvm/.git" ]] ; then
builtin cd "${rvm_src_path}/rvm/"
if [[ -z "$(git branch | awk "/$branch$/")" ]] ; then
if ! git checkout -b "$branch" --track "$remote/$branch" 2>/dev/null ; then
fail "$remote $branch remote branch not found."
fi
elif [[ -z "$(git branch | awk "/\* $branch$/{print \$2}")" ]] ; then
if ! git checkout $branch 2>/dev/null ; then
fail "Unable to checkout $branch."
fi
fi
git pull --rebase origin $branch
else
builtin cd "${rvm_src_path}"
if ! git clone --depth 1 git://github.com/wayneeseguin/rvm.git ; then
if ! git clone https://github.com/wayneeseguin/rvm.git ; then
fail "Unable to clone the RVM repository, attempted both git:// and https://"
fi
fi
fi
builtin cd "${rvm_src_path}/rvm/"
return 0
}
shopt -s extglob
set -o errtrace
set -o errexit
true ${rvm_ignore_rvmrc:=0}
if [[ $UID -eq 0 ]] ; then
[[ $rvm_ignore_rvmrc -eq 0 && -s /etc/rvmrc ]] && source /etc/rvmrc
true "${rvm_path:="/usr/local/rvm"}"
else
[[ $rvm_ignore_rvmrc -eq 0 && -s "$HOME/.rvmrc" ]] && source "$HOME/.rvmrc"
if [[ -z "${rvm_path:-}" || "${rvm_path}" = "/usr/local/rvm" ]] ; then
rvm_path="$HOME/.rvm"
fi
fi
# Parse CLI arguments.
while [[ $# -gt 0 ]] ; do
token="$1" ; shift
case "$token" in
--path)
if [[ -n "${1:-}" ]] ; then
rvm_path="$1" ; shift
else
fail "--path must be followed by a path."
fi
;;
--trace)
set -o xtrace
export rvm_trace_flag=1
;;
--branch) # Install RVM from a given branch
if [[ -n "${1:-}" ]] ; then
version="head"
branch="$1" ; shift
else
fail "--branch must be followed by a branchname."
fi
;;
--version)
case "$1" in
+([[:digit:]]).+([[:digit:]]).+([[:digit:]]))
version="$1" ; shift
;;
lastest|stable)
version="latest" ; shift
;;
head|master)
version="head" ; shift
branch="master"
;;
*)
fail "--version must be followed by a vaild version number x.y.z"
;;
esac
;;
+([[:digit:]]).+([[:digit:]]).+([[:digit:]]))
version="$token"
;;
help|usage)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
true "${version:=head}"
export PS4="+ \${BASH_SOURCE##\${rvm_path:-}} : \${FUNCNAME[0]:+\${FUNCNAME[0]}()} \${LINENO} > "
rvm_src_path="$rvm_path/src"
rvm_archives_path="$rvm_path/archives"
rvm_releases_url="http://rvm.beginrescueend.com/releases"
rvm_prefix="$(dirname $rvm_path)/"
for dir in "$rvm_src_path" "$rvm_archives_path" ; do
[[ ! -d "$dir" ]] && mkdir -p "$dir/"
done
# Perform the actual installation, first we obtain the source using whichever
# means was specified, if any. Defaults to head.
case "${version}" in
head)
install_head
;;
latest)
fetch_version
install_release
;;
+([[:digit:]]).+([[:digit:]]).+([[:digit:]])) # x.y.z
install_release
;;
*)
fail "Something went wrong, unrecognized version '$version'"
;;
esac
# No matter which one we are doing we install the same way, using the RVM
# installer script.
chmod +x ./scripts/install
flags=""
[[ ${rvm_trace_flag:-0} -eq 1 ]] && flags="$flags --trace"
[[ ${rvm_debug_flag:-0} -eq 1 ]] && flags="$flags --debug"
# Now we yield to the RVM installer.
exec ./scripts/install ${flags} --prefix "$rvm_prefix" --path "$rvm_path"