-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivate.sh
335 lines (307 loc) · 8.34 KB
/
activate.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
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
#!/bin/sh
# Csh is not supported, primarily due to lack of functions.
# > csh -l or csh -i
[ "$0" = 'csh' ] && \
printf '%s\n' 'koopa does not support csh.' && \
exit 1
# Tcsh is not supported, primarily due to lack of functions.
# > tcsh -l or tcsh -i
[ "$0" = 'tcsh' ] && \
printf '%s\n' 'koopa does not support tcsh.' && \
exit 1
__koopa_activate_usage() {
# """
# Koopa activation usage triggered by '--help' flag.
# @note Updated 2021-10-25.
# """
cat << END
usage: activate [--help|-h]
Activate koopa.
supported environment variables:
KOOPA_FORCE=1
Force activation inside of non-interactive shells.
Not generally recommended, but used by koopa installer.
KOOPA_MINIMAL=1
Minimal mode.
Simply load koopa programs into PATH.
Skips additional program and shell configuration.
KOOPA_SKIP=1
Skip activation in current shell session.
Recommended for users who want to selectively disable activation
of shared koopa installation.
KOOPA_TEST=1
Enable verbose test mode.
Used for Travis CI checks.
details:
Bash or Zsh is currently recommended.
Also supports Ash, Busybox, and Dash POSIX shells.
For system-wide configuration on Linux, this should be called inside
'/etc/profile.d/zzz-koopa.sh', owned by root.
Sourcing of POSIX shell scripts via '.' (POSIX) or 'source' (bash, zsh)
requires that arguments are passed in at the beginning of the call, rather
than as positional arguments or flags. Refer to the working examples.
examples:
# Default mode.
. /usr/local/koopa/activate
# Minimal mode.
export KOOPA_MINIMAL=1
. /usr/local/koopa/activate
END
}
__koopa_bash_source() {
# """
# Bash source file location.
# @note Updated 2021-05-07.
# """
# shellcheck disable=SC3028,SC3054
__koopa_print "${BASH_SOURCE[0]}"
return 0
}
__koopa_check_zsh() {
# """
# Check that current Zsh configuration is supported.
# @note Updated 2023-03-21.
#
# Zsh currently requires presence of '~/.zshrc' for clean activation.
# This check will intentionally force early return when activation is
# attempted from '/etc/profile.d'.
#
# Note that sourcing in '/etc/profile' doesn't return script path in
# '0', which is commonly recommended online in place of 'BASH_SOURCE'.
# '0' in this case instead returns '_src_etc_profile'.
#
# This approach covers both '_src_etc_profile' and '_src_etc_profile_d'.
#
# @seealso
# - https://stackoverflow.com/a/23259585/3911732
[ -n "${ZSH_VERSION:-}" ] || return 0
return 0
}
__koopa_export_koopa_prefix() {
# """
# Export 'KOOPA_PREFIX' variable.
# @note Updated 2023-03-09.
# """
__kvar_shell="$(__koopa_shell_name)"
__kvar_script="$("__koopa_${__kvar_shell}_source")"
if [ ! -e "$__kvar_script" ]
then
__koopa_warn 'Failed to locate koopa activate script.'
return 1
fi
# Note that running realpath on the file instead of the directory will
# properly resolve '~/.config/koopa/activate' symlink case.
if [ -L "$__kvar_script" ]
then
__kvar_script="$(__koopa_realpath "$__kvar_script")"
fi
__kvar_prefix="$(__koopa_realpath "$(dirname "$__kvar_script")")"
KOOPA_PREFIX="$__kvar_prefix"
export KOOPA_PREFIX
unset -v __kvar_prefix __kvar_script __kvar_shell
return 0
}
__koopa_export_koopa_subshell() {
# """
# Export 'KOOPA_SUBSHELL' variable.
# @note Updated 2021-05-26.
#
# This function evaluates whether 'KOOPA_PREFIX' is defined, which should be
# the case only inside a subshell.
# """
[ -z "${KOOPA_PREFIX:-}" ] && return 0
KOOPA_SUBSHELL=1
export KOOPA_SUBSHELL
return 0
}
__koopa_header() {
# """
# Shared shell header file location.
# @note Updated 2023-05-18.
# """
__kvar_shell="$(__koopa_shell_name)"
__kvar_file="${KOOPA_PREFIX:?}/lang/${__kvar_shell}/include/header.sh"
[ -f "$__kvar_file" ] || return 1
__koopa_print "$__kvar_file"
unset -v __kvar_file __kvar_shell
return 0
}
__koopa_is_installed() {
# """
# Are all of the requested programs installed?
# @note Updated 2023-03-09.
# """
for __kvar_cmd in "$@"
do
command -v "$__kvar_cmd" >/dev/null || return 1
done
unset -v __kvar_cmd
return 0
}
__koopa_is_interactive() {
# """
# Is the current shell interactive?
# @note Updated 2021-10-25.
# """
__koopa_str_detect "$-" 'i'
}
__koopa_posix_source() {
# """
# POSIX source file location.
# @note Updated 2023-03-09.
#
# POSIX doesn't support file path resolution of sourced dot scripts.
# """
__kvar_prefix="${KOOPA_PREFIX:-}"
if [ -z "$__kvar_prefix" ] && [ -d '/opt/koopa' ]
then
__kvar_prefix='/opt/koopa'
fi
if [ ! -d "$__kvar_prefix" ]
then
__koopa_warn \
'Failed to locate koopa activation script.' \
"Required 'KOOPA_PREFIX' variable is unset."
return 1
fi
__koopa_print "${__kvar_prefix}/activate"
unset -v __kvar_prefix
return 0
}
__koopa_preflight() {
# """
# Run pre-flight checks.
# @note Updated 2021-10-25.
# """
[ "${KOOPA_SKIP:-0}" -eq 1 ] && return 1
[ "${KOOPA_FORCE:-0}" -eq 1 ] && return 0
__koopa_check_zsh || return 1
__koopa_is_interactive || return 1
return 0
}
__koopa_print() {
# """
# Print a string.
# @note Updated 2023-03-09.
# """
for __kvar_string in "$@"
do
printf '%b\n' "$__kvar_string"
done
unset -v __kvar_string
return 0
}
__koopa_realpath() {
# """
# Resolve file path.
# @note Updated 2023-03-23.
# """
for __kvar_arg in "$@"
do
__kvar_string="$( \
readlink -f "$__kvar_arg" \
2>/dev/null \
|| true \
)"
if [ -z "$__kvar_string" ]
then
__kvar_string="$( \
perl -MCwd -le \
'print Cwd::abs_path shift' \
"$__kvar_arg" \
2>/dev/null \
|| true \
)"
fi
if [ -z "$__kvar_string" ]
then
__kvar_string="$( \
python3 -c \
"import os; print(os.path.realpath('${__kvar_arg}'))" \
2>/dev/null \
|| true \
)"
fi
if [ -z "$__kvar_string" ]
then
unset -v __kvar_arg _kvar_string
return 1
fi
__koopa_print "$__kvar_string"
done
unset -v __kvar_arg __kvar_string
return 0
}
__koopa_shell_name() {
# """
# Shell name.
# @note Updated 2023-05-18.
# """
if [ -n "${BASH_VERSION:-}" ]
then
__kvar_string='bash'
elif [ -n "${ZSH_VERSION:-}" ]
then
__kvar_string='zsh'
else
__kvar_string='sh'
fi
__koopa_print "$__kvar_string"
unset -v __kvar_string
return 0
}
__koopa_str_detect() {
# """
# Evaluate whether a string contains a desired value.
# @note Updated 2023-03-10.
# """
test "${1#*"$2"}" != "$1"
}
__koopa_warn() {
# """
# Print a warning message to the console.
# @note Updated 2021-05-14.
# """
for __kvar_string in "$@"
do
printf '%b\n' "$__kvar_string" >&2
done
unset -v __kvar_string
return 0
}
__koopa_zsh_source() {
# """
# Zsh source file location.
# @note Updated 2021-11-18.
#
# Use '%x' not '%N' when called inside function.
# https://stackoverflow.com/a/23259585/3911732
# """
# shellcheck disable=SC2296
__koopa_print "${(%):-%x}"
return 0
}
__koopa_activate() {
# """
# Activate koopa bootloader inside shell session.
# @note Updated 2022-09-02.
# """
case "${1:-}" in
'--help' | '-h')
__koopa_activate_usage
return 0
;;
esac
__koopa_preflight || return 0
__koopa_export_koopa_subshell || return 1
__koopa_export_koopa_prefix || return 1
KOOPA_ACTIVATE="${KOOPA_ACTIVATE:-1}"
export KOOPA_ACTIVATE
# shellcheck source=/dev/null
. "$(__koopa_header)" || return 1
unset -v KOOPA_ACTIVATE
return 0
}
__koopa_activate "$@"
# NOTE Don't attempt to unset functions here, can cause hash table warnings
# with active interactive Zsh session.