-
Notifications
You must be signed in to change notification settings - Fork 1
/
script-loader.zsh
executable file
·48 lines (39 loc) · 2.08 KB
/
script-loader.zsh
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
#!/bin/zsh
# Script sourcing check
test -z "$PS1" \
&& echo -e "This script \033[00;31mshould be sourced\033[0m not executed" && exit 1
#
# == aliasing all the scripts =================================
#
if [ -n "$DOTFILES_SCRIPTS" ]; then
# do a loop over all scriptnames within $DOTFILES_SCRIPTS
eval "DOTFILES_SCRIPTS=($DOTFILES_SCRIPTS)"
for scriptname in ${DOTFILES_SCRIPTS[@]} ; do
# check for existence
if test -f "${DOTFILES_REPO}/scripts/${scriptname}"; then
# check for execution-script
# TODO: find a better way to to destinguish between sourcing and execution
# is executable AND contains text "exec"
if ls -l "${DOTFILES_REPO}/scripts/${scriptname}" | awk '{ print $1 }' | grep 'x' >/dev/null 2>&1 \
&& head -n3 < "${DOTFILES_REPO}/scripts/${scriptname}" | grep "exec" >/dev/null 2>&1 ; then
# creating alias name from scriptname
script_alias="$(echo "${scriptname}" | cut -d. -f1)"
# creating alias for script execution
alias ${script_alias}="${DOTFILES_REPO}/scripts/${scriptname}"
# contains text "sourc" OR is not executable
elif head -n3 < "${DOTFILES_REPO}/scripts/${scriptname}" | grep "sourc" >/dev/null 2>&1 \
|| ! ls -l "${DOTFILES_REPO}/scripts/${scriptname}" | awk '{ print $1 }' | grep 'x' >/dev/null 2>&1 ; then
# creating alias name from scriptname
script_alias="$(echo "${scriptname}" | cut -d. -f1)"
# creating alias for script sourcing
alias ${script_alias}="source ${DOTFILES_REPO}/scripts/${scriptname}"
else
# creating alias name from scriptname
script_alias="$(echo "${scriptname}" | cut -d. -f1)"
# creating alias for script execution
alias ${script_alias}="echo 'WARNING: script-loader error, should this script be sourced? asuming execution!'; \
${DOTFILES_REPO}/scripts/${scriptname}"
fi
fi
done
fi