-
Notifications
You must be signed in to change notification settings - Fork 1
/
_bash_config.sh
executable file
·183 lines (133 loc) · 3.51 KB
/
_bash_config.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
#!/bin/bash
# Needed Boilerplate to use this bash config in a script:
# scriptPath="$(realpath -s "${BASH_SOURCE[0]}")"
# source "./_bash_config.sh"
# run() {
# # <Your code>
# }
# run_bash run $@
# PUBLIC
# Last command
##############################
run_bash() {
exportIsolation "$@"
}
# PUBLIC
# Execute within run
##############################
setSuccessToken() {
cd "$unlinkedOwnDirPath"
mkdir -p "$currentTmp"
local -r succesToken="$currentTmp/successToken"
echo "$(date --rfc-3339=seconds)" > "$succesToken"
cd "$scriptDir"
}
# Usage: if [[ "$(isSuccessTokenSet)" == "false" ]]; then
isSuccessTokenSet() {
cd "$unlinkedOwnDirPath"
local -r succesToken="$currentTmp/successToken"
if [[ -e "$succesToken" ]]; then
echo "true"
else
echo "false"
fi
cd "$scriptDir"
}
# To use this function in a project, remove "readonly" declarations of variables in _project_config.sh to avoid errors.
# A project is a directory which contains _bash_config.sh, _platform_config.sh and _project_config.sh.
# Invoked other projects won't be affected.
changePlatformTo() {
newPlatform="$1"
if [[ "$currentPlatform" != "$newPlatform" ]]; then
setCurrentPlatform "$newPlatform"
source "$unlinkedOwnDirPath/_project_config.sh"
fi
}
clearCurrentPlatform() {
cd "$unlinkedOwnDirPath"
rm -rdf "$currentTarget"
mkdir -p "$currentTarget"
rm -rdf "$currentTmp"
mkdir -p "$currentTmp"
cd "$scriptDir"
}
# PRIVATE
# Executed within run
##############################
exportIsolation() {
# this implements export isolation.
# exports within the withinExportIsolation function won't be
# exported to this outer environment.
# exports will only passed to invoked scripts.
withinExportIsolation "$@" &
my_pid=$!
wait $my_pid
}
withinExportIsolation() {
loadConfig
loggedRunner "$@"
}
loadConfig() {
source "$unlinkedOwnDirPath/_platform_config.sh"
source "$unlinkedOwnDirPath/_project_config.sh"
}
loggedRunner() {
if [[ -z ${level+x} ]]; then
level="1"
export level
fi
echo "---started($level): $relativeScriptPath"
((level+=1))
export level
local -r the_run="$@"
$the_run
((level-=1))
echo "--finished($level): $relativeScriptPath"
}
# PRIVATE
# Used while sourcing
##############################
setupVariables() {
local -r ownFullPath="$(realpath -s "${BASH_SOURCE[0]}")"
local -r unlinkedOwnFullPath="$(readlink -f "$ownFullPath")"
readonly unlinkedOwnDirPath="$(dirname "$unlinkedOwnFullPath")"
}
setRelativeScriptPath() {
if [[ -z ${firstInvokedSkriptPath+x} ]]; then
#"CurrentPlatform is unset"
firstInvokedSkriptPath="$scriptDir"
# export is needed to pass variable to invoked skripts
export firstInvokedSkriptPath
echo "Info: set and exported firstInvokedSkriptPath=$firstInvokedSkriptPath"
fi
local -r scriptName="$(basename "$scriptPath")"
if [[ "$firstInvokedSkriptPath" == "$scriptDir" ]]; then
local -r relativeFullPath="./$scriptName"
else
local -r relativeDirPath="$(realpath --relative-to="$firstInvokedSkriptPath" "$scriptDir")"
local -r relativeFullPath="./$relativeDirPath/$scriptName"
fi
readonly relativeScriptPath="$relativeFullPath"
}
setupErrorHandling() {
#add -x for debugging
set -Eeuo pipefail errexit
trap on_err ERR INT TERM
}
on_err() {
errorCode="$?"
magicNum="22"
if [[ "$errorCode" -ne "$magicNum" ]] ;then
echo -e "\nError occurred in: $relativeScriptPath" >&2
fi
exit "$magicNum"
}
# PRIVATE
# Executed while sourcing
##############################
whileSourcing() {
setupVariables
setRelativeScriptPath
setupErrorHandling
}
whileSourcing