Skip to content

Commit

Permalink
feat: add gen_source_guard
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Feb 12, 2022
1 parent 3712749 commit 6fe70c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bin/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*]
indent_size = 4
36 changes: 36 additions & 0 deletions bin/gen_source_guard
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
#
# gen a source guard code snippet for a shell lib file.
#
set -eEuo pipefail

p_uuid_gen() {
if command -v uuidgen &>/dev/null; then
uuidgen
else
python3 -c 'import uuid; print(str(uuid.uuid4()).upper())'
fi
}

new_source_guard_var_name() {
local uuid
uuid=$(p_uuid_gen)
uuid=${uuid//-/_}

echo "__source_guard_$uuid"
}

new_source_guard() {
local source_guard_var_name
source_guard_var_name="$(new_source_guard_var_name)"

cat <<EOF
#_ source guard start _#
[ -z "\${$source_guard_var_name:+dummy}" ] || return 0
$source_guard_var_name="\$(dirname "\$(readlink -f "\${BASH_SOURCE[0]}")")"
readonly $source_guard_var_name
#_ source guard end _#
EOF
}

new_source_guard

0 comments on commit 6fe70c7

Please sign in to comment.