-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcals.sh
executable file
·105 lines (86 loc) · 3.28 KB
/
cals.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
#!/usr/bin/env bash
####################################################################################################
# CaLS = Custom and Local Scripts
# Powerful environment to write custom private scripts
#
# aks. means AnKor Custom Scripts
# akl. means AnKor Local Scripts
####################################################################################################
if [[ -z ${AK_CALS_CUSTOM_SCRIPTS_PATH+x} ]]; then
declare -r AK_CALS_CUSTOM_SCRIPTS_PATH="${AK_SCRIPT_PATH}/custom-scripts"
export AK_CALS_CUSTOM_SCRIPTS_PATH
fi
if [[ -z ${AK_CALS_LOCAL_SCRIPTS_PATH+x} ]]; then
declare -r AK_CALS_LOCAL_SCRIPTS_PATH="${AK_SCRIPT_PATH}/local-scripts"
export AK_CALS_LOCAL_SCRIPTS_PATH
fi
declare __AK_CALS_BINS_PATH="${AK_SCRIPT_PATH}/.bin"
function __ak.cals.loadScriptsDir() {
local -r scriptsPath="${1}"
local -r prefix="${2}"
if [[ -z "${scriptsPath}" ]]; then
echo "ERROR! argument 'scriptsPath' is required" >&2
return 1
fi
if [[ -z "${prefix}" ]]; then
echo "ERROR! argument 'prefix' is required" >&2
fi
if [[ ! -d "${scriptsPath}" ]]; then
mkdir -p "${scriptsPath}"
fi
if [[ ":$PATH:" != *":${__AK_CALS_BINS_PATH}:"* ]]; then
PATH="${PATH}:${__AK_CALS_BINS_PATH}"
fi
local -a commandList=()
local commandFullPath
local fileFullPath
local folderFullPath
# Linking single-file commands
for fileFullPath in $(find "${scriptsPath}" -maxdepth 1 -type f -name "*.sh"); do
local fileName=$(basename "${fileFullPath%.sh}")
local commandName="${prefix}${fileName}"
local commandFullPath="${__AK_CALS_BINS_PATH}/${commandName}"
commandList=("${commandList[@]}" "${commandName}")
__ak.cals.checkCommandLink "${fileFullPath}" "${commandFullPath}"
done
# Linking folder commands
for folderFullPath in $(find "${scriptsPath}" -maxdepth 1 -type d -name "*"); do
local fileName=$(basename "${folderFullPath}")
local commandName="${prefix}${fileName}"
local commandFullPath="${__AK_CALS_BINS_PATH}/${commandName}"
fileFullPath="${folderFullPath}/index.sh"
if [[ -f "${fileFullPath}" ]]; then
commandList=("${commandList[@]}" "${commandName}")
__ak.cals.checkCommandLink "${fileFullPath}" "${commandFullPath}"
fi
done
# Clear old links
for commandFullPath in $(find "${__AK_CALS_BINS_PATH}" -type f -name "${prefix}*"); do
if ! ak.array.inArray "$(basename "${commandFullPath}")" "${commandList[@]}"; then
rm -f "${commandFullPath}"
fi
done
}
function __ak.cals.checkCommandLink() {
local -r fileFullPath="${1}"
local -r commandFullPath="${2}"
local -r fileName=$(basename "${fileFullPath}")
local -r command=$(basename "${commandFullPath}")
if [[ ! -f "${commandFullPath}" ]]; then
{
echo "#!/usr/bin/env bash"
echo
echo "declare -r AK_CALS_PATH=\"${fileFullPath}\""
# shellcheck disable=SC2016
echo 'declare -r AK_CALS_DIR=$(dirname "${AK_CALS_PATH}")'
echo "declare -r AK_CALS_COMMAND=\"${command}\""
echo
echo "source \"${AK_SCRIPT_PATH}/index.sh\""
echo "source \"${fileFullPath}\" \"\${@}\""
} > "${commandFullPath}"
chmod +x "${commandFullPath}"
fi
}
__ak.cals.loadScriptsDir "${AK_CALS_CUSTOM_SCRIPTS_PATH}" "aks."
__ak.cals.loadScriptsDir "${AK_CALS_LOCAL_SCRIPTS_PATH}" "akl."
unset __AK_CALS_BINS_PATH