This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install
executable file
·270 lines (227 loc) · 7.87 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
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
#!/usr/bin/env bash
# ----------------------------------------------------------------------------------------------------------------------
# ls-git - https://github.com/eth-p/ls-git/ | MIT License | Copyright (C) 2018 Ethan P. (eth-p)
# ----------------------------------------------------------------------------------------------------------------------
# Copyright (c) 2018 Ethan P. (eth-p)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ----------------------------------------------------------------------------------------------------------------------
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALL_CACHE="${HERE}/.installed"
# ----------------------------------------------------------------------------------------------------------------------
A_ERR="\x1B[31m"
A_MSG="\x1B[35m"
A_RST="\x1B[0m"
A_AMEND="\x1B[A\x1B[J"
A_ALT_ENTER="\x1B[?1049h\x1B[2J\x1B[H"
A_ALT_EXIT="\x1B[?1049l"
# ----------------------------------------------------------------------------------------------------------------------
# Arguments:
arg_to="$([ -f "$INSTALL_CACHE" ] && cat "$INSTALL_CACHE" || echo "$HOME/.bin")"
arg_update=false
arg_deps=false
arg_minify=false
arg_depcheck=true
argv=()
argv_original=("$@")
# Argument Parser:
argparse_key=''
argparse_val=''
for argparse in "$@" ''; do
[ "$argparse_key" = '--' ] && { argv+=("$argparse"); continue; };
case "$argparse" in
'--') argparse_key='--'; continue;;
'--'*'='*) argparse_key="$(sed 's/=.*$//' <<< "$argparse")"; argparse_val="$(sed 's/^.*=//' <<< "$argparse")";;
'--'*) argparse_key="$argparse";;
*) argparse_val="$argparse";;
esac
case "$argparse_key" in
'') argv+=("$argparse_val");;
'--to') [ "$argparse" != "$argparse_key" ] && arg_to="$argparse_val";;
'--deps') arg_deps=true; argparse_key='';;
'--update') arg_update=true; argparse_key='';;
'--minify') arg_minify=true; argparse_key='';;
'--no-depcheck') arg_depcheck=false; argparse_key='';;
*) printf "${A_ERR}Illegal argument: %s${A_RST}\n" "$argparse_key";;
esac
[ "$argparse" != "$argparse_key" ] && argparse_key=''
done
# ----------------------------------------------------------------------------------------------------------------------
# Utility Functions:
start() {
printf "${A_MSG}%s${A_RST}\n" "$1" 1>&2
}
err() {
printf "%$(tput cols)s" '' | tr ' ' '-' 1>&2
printf "${A_ERR}ERROR: %s${A_RST}\n" "$1" 1>&2
}
fatal() {
printf "%$(tput cols)s" '' | tr ' ' '-' 1>&2
printf "${A_ERR}FATAL: %s${A_RST}\n" "$1" 1>&2
exit 1
}
contains() {
local search="$1"
local element
for element in "${@:2}"; do
[ "$element" = "$search" ] && return 0
done
return 1
}
abspath() {
python -c "import os,sys; print os.path.abspath(sys.argv[1])" "$1" || realpath "$1" || {
fatal "Please install python to use this installer."
exit 2
}
}
# ----------------------------------------------------------------------------------------------------------------------
# Dependency Check Functions:
__cache_depcheck_perl=()
__cache_depcheck=()
depcheck_perl() {
local module="$1"
contains "$module" "${__cache_depcheck_perl[@]}" && return 0
__cache_depcheck_perl+=("$module")
# Check if the module can be loaded in Perl.
printf "Checking for module %-30s " "'$module'..."
if echo "use $module;" | perl &>/dev/null; then
echo "OK"
return 0
else
echo "Missing"
fi
# Check if the module can't be installed by the script.
[ "$arg_deps" = "true" ] || fatal "Missing module."
# Attempt to install the module.
printf "${A_ALT_ENTER}"
start "Installing module: ${module}"
[ -d "${HERE}/.install-logs" ] || mkdir -p "${HERE}/.install-logs"
PERL_MM_USE_DEFAULT=1 cpan install -T "$module" | tee "${HERE}/.install-logs/${module}";
if [ "$?" -ne 0 ]; then
err "Install failed."
read -s
printf "${A_ALT_EXIT}"
exit 1
fi
printf "${A_ALT_EXIT}"
printf "${A_AMEND}"
printf "Checking for module %-30s Installed\n" "'$module'..."
}
depcheck() {
local program="$1"
contains "$program" "${__cache_depcheck[@]}" && return 0
__cache_depcheck+=("$program")
# Check if the program exists in the path.
printf "Checking for dependency %-26s " "'$program'..."
if command -v "$1" &>/dev/null; then
echo "OK"
return 0
fi
# Fail.
echo "Missing"
fatal "Missing dependency."
exit 1
}
# ----------------------------------------------------------------------------------------------------------------------
# Preproccessing:
__cache__preprocess_included=()
preprocess() {
local srcfile="$(abspath "$1")"
local srcdir="$(abspath "$2")"
# Don't include twice.
contains "$srcfile" "${__cache__preprocess_included[@]}" && return 0
__cache__preprocess_included+=("$srcfile")
# Preprocess.
while IFS='' read -r line; do
case "$line" in
'#!'*) {
# Shebang comment.
echo "$line"
};;
'#@'*) {
# Copyright comment.
echo "#${line:2}"
};;
'#'*) {
# Regular comment.
:
};;
'use FindBin;');;
'require'*) {
# Use directive.
file="${srcdir}/$(sed "s/^require *(\{0,1\}['\"]\\\$FindBin::RealBin\/\([A-Za-z0-9\\/\.\\-]\{1,\}\).*\$/\1/" <<< "$line")"
echo "- Embedding: $file" 1>&2
if [ -f "$file" ]; then
echo "# <PREPROCESSOR> INCLUDE: $file"
preprocess "$file" "$srcdir"
echo "# <PREPROCESSOR> END: $file"
continue
else
fatal "Failed to embed file: $file"
fi
};;
*) {
# Regular line.
echo "$line"
};;
esac
done < "$srcfile"
}
# ----------------------------------------------------------------------------------------------------------------------
# Update:
if [ "$arg_update" = "true" ]; then
start "Updating..."
git -C "$HERE" remote update >/dev/null
if [ "$(git -C "$HERE" rev-parse @)" != "$(git -C "$HERE" rev-parse '@{u}')" ] && [ "$(git -C "$HERE" rev-parse @)" = "$(git -C "$HERE" merge-base @ '@{u}')" ]; then
start "Update found."
exec bash "${BASH_SOURCE[0]}" "${argv_original[@]}"
return $?
fi
fi
# ----------------------------------------------------------------------------------------------------------------------
# Install:
INSTALL="$arg_to"
SOURCE="${HERE}/src/main.pl"
echo "$INSTALL" > "$INSTALL_CACHE"
[ -z "$arg_to" ] && fatal "Invalid usage of '--to' argument."
# Preprocessing.
start "Preprocessing..."
tempfile="$(mktemp "$(mktemp -d)"/'ls-git.XXXXXX')"
preprocess "$SOURCE" "$(dirname "$SOURCE")" > "$tempfile"
# Command dependencies.
start "Checking dependencies..."
depcheck 'cpan'
depcheck 'git'
depcheck 'perl'
depcheck 'tput'
# Module dependencies.
while read -r module; do
depcheck_perl "$module"
done < <(grep "^use [A-Z][A-Za-z\:]\{1,\}" "$tempfile" | sed 's/use \([A-Za-z\:]\{1,\}\).*/\1/' | sort -r )
# Install.
start "Installing..."
echo "Installing to: $INSTALL"
if [ "$arg_minify" = "true" ] && command -v perltidy &>/dev/null; then
# Install minified.
perltidy --mangle --delete-all-comments "$tempfile" -o "$INSTALL"
else
# Install normal.
cp "$tempfile" "$INSTALL"
fi
chmod 755 "$INSTALL"