Skip to content

Commit 6fe70c7

Browse files
committed
feat: add gen_source_guard
1 parent 3712749 commit 6fe70c7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

bin/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*]
2+
indent_size = 4

bin/gen_source_guard

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
#
3+
# gen a source guard code snippet for a shell lib file.
4+
#
5+
set -eEuo pipefail
6+
7+
p_uuid_gen() {
8+
if command -v uuidgen &>/dev/null; then
9+
uuidgen
10+
else
11+
python3 -c 'import uuid; print(str(uuid.uuid4()).upper())'
12+
fi
13+
}
14+
15+
new_source_guard_var_name() {
16+
local uuid
17+
uuid=$(p_uuid_gen)
18+
uuid=${uuid//-/_}
19+
20+
echo "__source_guard_$uuid"
21+
}
22+
23+
new_source_guard() {
24+
local source_guard_var_name
25+
source_guard_var_name="$(new_source_guard_var_name)"
26+
27+
cat <<EOF
28+
#_ source guard start _#
29+
[ -z "\${$source_guard_var_name:+dummy}" ] || return 0
30+
$source_guard_var_name="\$(dirname "\$(readlink -f "\${BASH_SOURCE[0]}")")"
31+
readonly $source_guard_var_name
32+
#_ source guard end _#
33+
EOF
34+
}
35+
36+
new_source_guard

0 commit comments

Comments
 (0)