-
Notifications
You must be signed in to change notification settings - Fork 133
/
install
executable file
·242 lines (183 loc) · 5.62 KB
/
install
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
#!/usr/bin/env bash
install_ruby() {
local install_type=$1
local version=$2
local install_path=$3
local ruby_type=$(get_ruby_type $version)
if [ "$TMPDIR" = "" ]; then
local tmp_download_dir=$(mktemp -d)
else
local tmp_download_dir=$TMPDIR
fi
# path to the tar file
local source_path=$(get_download_file_path $install_type $version $tmp_download_dir)
download_source $install_type $version $source_path
# running this in a subshell
# because we don't want to disturb current working dir
(
cd $(dirname $source_path)
if [ "${ruby_type}" = "jruby" ]; then
tar zxf $source_path -C $(dirname $install_path) || exit 1
exit 0
else
tar zxf $source_path || exit 1
fi
# we use untar path because the extracted dir maybe diff
# ideally should be using tar with --strip-components 1
cd $(untar_path $install_type $version $tmp_download_dir)
if [ "$ASDF_PKG_MISSING" != "" ]; then
echo "WARNING: Might use OS-provided pkgs for the following: $ASDF_PKG_MISSING"
fi
local configure_options="$(construct_configure_options $install_path)"
# set in os_based_configure_options
# we unset it here because echo-ing changes the return value of the function
unset ASDF_PKG_MISSING
echo "Building with options: $configure_options"
./configure $configure_options || exit 1
make || exit 1
make install || exit 1
)
}
construct_configure_options() {
local install_path=$1
if [ "$RUBY_CONFIGURE_OPTIONS" = "" ]; then
local configure_options="$(os_based_configure_options) --prefix=$install_path"
if [ "$RUBY_EXTRA_CONFIGURE_OPTIONS" != "" ]; then
configure_options="$configure_options $RUBY_EXTRA_CONFIGURE_OPTIONS"
fi
else
local configure_options="$RUBY_CONFIGURE_OPTIONS --prefix=$install_path"
fi
echo "$configure_options"
}
homebrew_package_path() {
local package_name=$1
if [ "$(brew ls --versions $package_name)" = "" ]; then
echo ""
else
echo "$(brew --prefix $package_name)"
fi
}
exit_if_homebrew_not_installed() {
if [ "$(brew --version 2>/dev/null)" = "" ]; then
echo "ERROR: Please install homebrew for OSX"
exit 1
fi
}
os_based_configure_options() {
local operating_system=$(uname -a)
local configure_options=""
if [[ "$operating_system" =~ "Darwin" ]]; then
exit_if_homebrew_not_installed
local openssl_path=$(homebrew_package_path openssl)
local libyaml_path=$(homebrew_package_path libyaml)
local readline_path=$(homebrew_package_path readline)
else
local openssl_path=/usr
local libyaml_path=/usr
fi
if [ "$openssl_path" = "" ]; then
export ASDF_PKG_MISSING="openssl"
else
configure_options="--with-openssl-dir=$openssl_path"
fi
if [ "$libyaml_path" = "" ]; then
export ASDF_PKG_MISSING="$ASDF_PKG_MISSING libyaml"
else
configure_options="$configure_options --with-libyaml-dir=$libyaml_path"
fi
if [ "$readline_path" != "" ]; then
configure_options="$configure_options --with-readline-dir=$readline_path"
fi
configure_options="$configure_options --enable-shared --disable-install-doc"
echo $configure_options
}
download_source() {
local install_type=$1
local version=$2
local download_path=$3
local download_url=$(get_download_url $install_type $version)
if [ -f $download_path ]; then
rm $download_path
fi
curl -Lo $download_path $download_url
}
get_download_file_path() {
local install_type=$1
local version=$2
local tmp_download_dir=$3
local ruby_type=$(get_ruby_type $version)
local ruby_version=$(get_ruby_version $version)
if [ "${ruby_type}" = "ruby" ]; then
local pkg_name="ruby-${ruby_version}.tar.gz"
elif [ "${ruby_type}" = "jruby" ]; then
local pkg_name="jruby-bin-${ruby_version}.tar.gz"
fi
echo "$tmp_download_dir/$pkg_name"
}
untar_path() {
local install_type=$1
local version=$2
local tmp_download_dir=$3
local ruby_type=$(get_ruby_type $version)
local ruby_version=$(get_ruby_version $version)
if [ "${ruby_type}" = "ruby" ]; then
local dir_name="ruby-${ruby_version}"
elif [ "${ruby_type}" = "jruby" ]; then
local dir_name="jruby-${ruby_version}"
fi
echo "$tmp_download_dir/$dir_name"
}
get_download_url() {
local install_type=$1
local version=$2
local ruby_type=$(get_ruby_type $version)
local ruby_version=$(get_ruby_version $version)
if [ "${ruby_type}" = "ruby" ]; then
echo "https://cache.ruby-lang.org/pub/ruby/ruby-${ruby_version}.tar.gz"
elif [ "${ruby_type}" = "jruby" ]; then
echo "https://s3.amazonaws.com/jruby.org/downloads/${ruby_version}/jruby-bin-${ruby_version}.tar.gz"
fi
}
get_ruby_version() {
IFS='-' read -a version_info <<< "$1"
if [ "${version_info[0]}" = "jruby" ]; then
# jruby
if [ "${#version_info[@]}" -eq 2 ]; then
echo "${version_info[1]}"
else
echo "${version_info[1]}-${version_info[2]}"
fi
else
# ruby
if [ "${#version_info[@]}" -eq 1 ]; then
echo "${version_info[0]}"
else
echo "${version_info[0]}-${version_info[1]}"
fi
fi
}
get_ruby_type() {
IFS='-' read -a version_info <<< "$1"
if [ "${version_info[0]}" = "jruby" ]; then
echo "jruby"
else
echo "ruby"
fi
}
install_default_gems() {
local default_gems="${HOME}/.default-gems"
local gem_bin="${ASDF_INSTALL_PATH}/bin/gem"
if [ ! -f $default_gems ]; then return; fi
echo ""
for name in $(cat $default_gems); do
echo -n "Installing ${name} gem... "
if $gem_bin install $name > /dev/null 2>&1; then
echo "SUCCESS"
else
echo "FAIL"
fi
done
}
install_ruby $ASDF_INSTALL_TYPE $ASDF_INSTALL_VERSION $ASDF_INSTALL_PATH
install_default_gems