forked from taylor/kiex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kiex
executable file
·449 lines (392 loc) · 11.3 KB
/
kiex
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
#!/usr/bin/env bash
base_path="$HOME/.kiex"
build_path="${base_path}/builds"
elixirs_path="${base_path}/elixirs"
elixirs_source_path="\$HOME/.kiex/elixirs"
kiex_url="https://raw.github.com/taylor/kiex/master/kiex"
erlang_min_release_full="R16B"
erlang_min_release=16
erlang_min_release_canidate="B"
erlang_min_release_canidate_n=""
SYSTEM=$(uname -s)
if [ "$SYSTEM" = "Linux" ] ; then
if [ "$(grep -ic Ubuntu /etc/issue)" = 1 ] ; then
DISTRO="Ubuntu"
elif [ "$(grep -ic Centos /etc/issue)" = 1 ] ; then
DISTRO=Centos
elif [ "$([[ -f /etc/inittab ]] && grep -ic Gentoo /etc/inittab)" = 3 ] ; then
DISTRO=Gentoo
elif [ "$( grep -ic DISTRIB_ID=Arch /etc/lsb-release)" = 1 ] ; then
DISTRO=Arch
else
echo "Unknown Linux distribution"
fi
elif [ "$SYSTEM" = "Darwin" ] ; then
#DISTRO=$(uname -r)
DISTRO=OSX
else
echo "Unsupported system $SYSTEM"
fi
function usage() {
me=$(basename $0)
#echo "$me <list [known]|install <version>|use <version>>"
echo "$me commands:"
printf "%b" " list - shows currently installed elixirs\n"
printf "%b" " list known - shows available elixirs releases\n"
printf "%b" " install <version> - installs the given release version\n"
printf "%b" " use <version> - use the given version for this shell\n"
printf "%b" " shell <version> - use the given version for this shell\n"
printf "%b" " default <version> - sets default version to be used\n"
printf "%b" " implode - removes kiex and all installed elixirs\n"
printf "%b" " selfupdate - update kiex itself\n"
}
# NOTE: work-around for differences in readlink on BSD and Linux
# Based on the Stack Overflow answer http://stackoverflow.com/a/1116890
function readlink_f() {
TARGET_FILE="$1"
cd `dirname "$TARGET_FILE"`
TARGET_FILE=`basename "$TARGET_FILE"`
# Iterate down a (possible) chain of symlinks
while [ -L "$TARGET_FILE" ] ; do
TARGET_FILE=`readlink "$TARGET_FILE"`
cd `dirname "$TARGET_FILE"`
TARGET_FILE=`basename "$TARGET_FILE"`
done
# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
PHYS_DIR=`pwd -P`
RESULT="${PHYS_DIR}/${TARGET_FILE}"
echo $RESULT
}
function check_erlang_release() {
if [ "$SYSTEM" = "Linux" ] ; then
erlang_release_file="$(dirname $(readlink -f $(which erl)))/../releases/RELEASES"
elif [ "$SYSTEM" = "Darwin" ] ; then
erlang_release_file="$(dirname $(readlink_f $(which erl)))/../releases/RELEASES"
else
echo "Unknown system $SYSTEM"
return 1
fi
if [ ! -e "$erlang_release_file" ] ; then
echo "Failed to find the erlang release file!" 1>&2
return 1
fi
erlang_release=($(awk -F, 'NR==1 {gsub(/"/,"",$3);print $3}' "$erlang_release_file" | sed 's/R\([0-9]*\)\([A-Z]\)/\1 \2 /'))
r="${erlang_release[0]}"
c="${erlang_release[1]}"
cn="${erlang_release[2]}"
#echo "erlang release: ${erlang_release[*]}, r: $r, c: $c, n: $cn"
#elixir_req_str="Elixir requires Erlang ${erlang_min_release_full} or later"
if [ -z "$r" ] ; then
echo "Failed to extract the erlang release!" 1>&2
false
elif (( $r > $erlang_min_release )) ; then
true
elif (( $r < $erlang_min_release )) ; then
false
elif (( $r == $erlang_min_release )) ; then
if [ -z "$erlang_min_release_canidate" ] ; then
true
elif [ -n "$erlang_min_release_canidate" -a -z "$c" ] ; then
false
elif [ "$erlang_min_release_canidate" == "A" -a "$c" == "B" ] ; then
true
elif [ "$erlang_min_release_canidate" == "$c" ] ; then
if [ -n "$erlang_min_release_canidate_n" ] ; then
if [ -z "$cn" ] ; then
false
elif (( $cn < $erlang_min_release_canidate_n )) ; then
false
else
true
fi
else
true
fi
else
# unknown character after release...
false
fi
fi
}
function erlang_bin_found() {
if [ -z "$(which erl 2> /dev/null)" ] ; then
echo "Erlang binary not found" 1>&2
false
else
true
fi
}
function exit_on_unmet_prereqs() {
deps_check
if [ ! "$?" = 0 ]
then
echo "Elixir requires Erlang ${erlang_min_release_full} or later"
echo "Install via:"
echo " * Download - https://www.erlang-solutions.com/downloads/download-erlang-otp"
echo " http://www.erlang.org/download.html"
echo " * Kerl - https://github.com/spawngrid/kerl"
echo " * Package manager, OS X brew install erlang, Ubuntu/Debian/CentOS see erlang-solutions, Arch see AUR"
exit 1
fi
}
function deps_check() {
erlang_bin_found && check_erlang_release
}
function setup() {
mkdir -p "${base_path}/"{builds,elixirs}
}
if [ ! -d "${base_path}/builds" -o ! -d "${base_path}/elixirs" ] ; then
setup
fi
function kiex_implode() {
echo -n "Type YES to remove the kiex install and all elixirs: "
read resp
if [ ! "$resp" = "YES" ] ; then
echo "Aborting!"
exit 0
fi
rm -rf "$HOME/.kiex"
echo "Please remove the kiex source lines from your startup script. It looks like:"
echo " [[ -s \"\$HOME/.kiex/scripts/kiex\" ]] && source \"\$HOME/.kiex/scripts/kiex\""
}
function self_update() {
if [ "$SYSTEM" = "Linux" ] ; then
mypath=$(dirname $(readlink -f $0))
elif [ "$SYSTEM" = "Darwin" ] ; then
mypath=$(dirname $(readlink_f $0))
else
echo "Unknown system $SYSTEM"
return 1
fi
cd $mypath
D=$(date +%Y%m%d.%H%M)
cp kiex kiex.bak-$D
curl -qs -o kiex "$kiex_url"
chmod +x kiex
echo "kiex installed in $mypath is now updated"
}
function get_known_elixir_releases() {
x=$(curl -H "Accept: application/json" -qs https://api.github.com/repos/elixir-lang/elixir/releases |grep '"tag_name":')
x=${x//\"tag_name\":/}
x=${x//[\",]/}
x=${x//v/}
echo "$x"
}
function list_known() {
echo "Getting the available releases from https://github.com/elixir-lang/elixir/releases"
echo
elixir_releases=$(get_known_elixir_releases)
#elixir_releases=$x
echo "Known Elixir releases: "
printf "%b" "${elixir_releases}\n"
}
function valid_known_release() {
ver="$1"
get_known_elixir_releases | grep -q "$1" 2> /dev/null
echo $?
}
function install_elixir() {
version="$1"
if [ ! "$(valid_known_release ${version})" = 0 ] ; then
echo "Unknown Elixir version $version"
echo "Use 'kiex list known' see known versions"
exit 1
fi
exit_on_unmet_prereqs
install_path="${elixirs_path}/elixir-${version}"
cd "$build_path"
git clone https://github.com/elixir-lang/elixir.git elixir-git 2> /dev/null
cd elixir-git
git checkout "v${version}"
make clean compile
mkdir -p "$install_path"
make "PREFIX=$install_path" install
echo "Installed Elixir version $version"
create_env_file "$version"
echo "Load the elixir environment file with: "
printf "%b" " "
printf "%b" "source ${elixirs_source_path}/elixir-${version}.env\n"
}
function create_env_file() {
new_ver="$1"
new_file="${elixirs_path}/elixir-${new_ver}.env"
new_path="${elixirs_path}/elixir-${new_ver}/bin"
new_mix_path="${elixirs_path}/elixir-${new_ver}/ebin"
if [ -f "$new_file" ] ; then
mv -f "$new_file" "${new_file}.old"
fi
cat <<EOF> $new_file
export ELIXIR_VERSION="$new_ver"
export PATH="${elixirs_source_path}/elixir-${new_ver}/bin:\$PATH"
EOF
#export MIX_PATH="${elixirs_source_path}/dynamo-${new_ver}/ebin:\$MIX_PATH"
#export MIX_PATH="${elixirs_source_path}/elixir-${new_ver}/ebin:\$MIX_PATH"
}
function find_elixir_ver() {
target_elixir="$1"
elixirs=($(
cd "$elixirs_path"
find . -maxdepth 1 -mindepth 1 -type d 2> /dev/null | sort
))
#TODO: reset version to nothing if problems arise
for version in "${elixirs[@]//.\/}"
do
if [[ ! -x "$elixirs_path/$version/bin/elixir" ]]
then
continue
elif [[ ! -f "$elixirs_path/${version}.env" ]]
then
continue
elif [ ! "$version" = "$target_elixir" -a ! "${version}" = "elixir-${target_elixir}" ]
then
continue
else
break
fi
done
echo $version
}
function use_elixir() {
target_elixir="$1"
version=$(find_elixir_ver "$target_elixir")
echo "Load the elixir environment file with: "
printf "%b" " "
printf "%b" "source ${elixirs_source_path}/${version}.env\n"
}
function elixir_subshell() {
target_elixir="$1"
version=$(find_elixir_ver "$target_elixir")
echo "Starting sub-shell with elixir version $version"
source "${elixirs_path}/${version}.env"
exec $SHELL
}
function set_default_elixir() {
target_elixir="$1"
version=$(find_elixir_ver "$target_elixir")
if [ -e ${elixirs_path}/.default ] ; then
if [ -L ${elixirs_path}/.default ] ; then
rm ${elixirs_path}/.default
else
echo "The file ${elixirs_path}/.default should not exists! Fix and try again."
exit 1
fi
fi
ln -s "${elixirs_path}/${version}.env" "${elixirs_path}/.default"
echo "Default Elixir version set to $version"
echo "Load right now with: source $elixirs_source_path/.default"
}
function list_elixirs() {
typeset current_elixir elixirs version selected system_elixir system_version \
default_elixir string binary
if [ -e "$elixirs_path"/.default ] ; then
default_elixir="elixir-$(source $elixirs_path/.default ; echo $ELIXIR_VERSION)"
fi
if [ -n "$(which iex)" ] ; then
v=$(iex --version 2> /dev/null|grep -v 'Warning: could not run')
current_elixir="elixir-${v/Elixir /}"
fi
elixirs=($(
cd "$elixirs_path"
find . -maxdepth 1 -mindepth 1 -type d 2> /dev/null | sort
#find . -maxdepth 1 -mindepth 1 -type d | sed -e 's#./##g'
))
#for version in "${elixirs//.\/}"
for version in "${elixirs[@]//.\/}"
do
if [[ ! -x "$elixirs_path/$version/bin/elixir" ]]
then
continue
fi
if [[ "$version" = "$current_elixir" && "$version" = "$default_elixir" ]]
then
printf "%b" "=* "
elif [[ "$version" = "$current_elixir" ]]
then
printf "%b" "=> "
elif [[ "$version" = "$default_elixir" ]]
then
printf "%b" " * "
else
printf "%b" " "
fi
printf "%b" "$version"
printf "%b" "\n"
done
if (( ${#elixirs[@]} == 0 ))
then
printf "%b" "
# No kiex elixirs installed yet. Try 'kiex install <version>'.
"
else
if [[ -z "${default_elixir}" ]]
then
printf "%b" "
# Default elixir not set. Try 'kiex default <version>'.
"
fi
printf "%b" "
# => - current
# =* - current && default
# * - default
"
fi
printf "%b" "\n"
}
action="$1"
shift
if [ -z "$action" ] ; then
usage
exit 0
fi
case $action in
list)
if [ -z "$1" ]
then
printf "%b" "\nkiex elixirs\n\n"
list_elixirs
elif [ "$1" = "known" -o "$1" = "releases" ]
then
list_known
else
usage
exit 1
fi
;;
install)
[[ -z "$1" ]] && usage && exit 1
install_elixir "$1"
;;
use)
[[ -z "$1" ]] && usage && exit 1
use_elixir "$1"
;;
default)
[[ -z "$1" ]] && usage && exit 1
set_default_elixir "$1"
;;
shell)
[[ -z "$1" ]] && usage && exit 1
elixir_subshell "$1"
;;
envfile)
[[ -z "$1" ]] && exit 1
create_env_file "$1"
;;
selfupdate|update_self)
self_update
exit 0
;;
implode)
kiex_implode
exit 0
;;
depscheck|checkdeps)
deps_check
[[ $? = 0 ]] && echo "Dependencies met" || ( echo "Dependencies not met" && exit_on_unmet_prereqs )
;;
*)
usage
exit
;;
esac