Skip to content

Commit 5f66e68

Browse files
committed
refactor: use command realpath instead of readlink, more portable(support macOS) 🔗
1 parent 57958e4 commit 5f66e68

File tree

8 files changed

+37
-26
lines changed

8 files changed

+37
-26
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ Example:
118118
```sh
119119
$ gen_source_guard
120120
#_ source guard begin _#
121-
[ -z "${__source_guard_0EDD6400_96EC_43E4_871A_E65F6781B828:+has_value}" ] || return 0
122-
__source_guard_0EDD6400_96EC_43E4_871A_E65F6781B828="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
123-
readonly __source_guard_0EDD6400_96EC_43E4_871A_E65F6781B828
121+
[ -n "${__source_guard_0EDD6400_96EC_43E4_871A_E65F6781B828:+has_value}" ] && return
122+
__source_guard_0EDD6400_96EC_43E4_871A_E65F6781B828=$(realpath -- "${BASH_SOURCE[0]}")
123+
# the value of source guard is the canonical dir path of this script
124+
readonly __source_guard_0EDD6400_96EC_43E4_871A_E65F6781B828=${__source_guard_0EDD6400_96EC_43E4_871A_E65F6781B828%/*}
124125
#_ source guard end _#
125126
```

bin/gen_source_guard

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ new_source_guard() {
2626

2727
cat <<EOF
2828
#_ source guard begin _#
29-
[ -z "\${$source_guard_var_name:+has_value}" ] || return 0
30-
$source_guard_var_name="\$(dirname "\$(readlink -f "\${BASH_SOURCE[0]}")")"
31-
readonly $source_guard_var_name
29+
[ -n "\${$source_guard_var_name:+has_value}" ] && return
30+
$source_guard_var_name=\$(realpath -- "\${BASH_SOURCE[0]}")
31+
# the value of source guard is the canonical dir path of this script
32+
readonly $source_guard_var_name=\${$source_guard_var_name%/*}
3233
#_ source guard end _#
3334
EOF
3435
}

lib/common_utils.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
################################################################################
2929
#
3030
#_ source guard begin _#
31-
[ -z "${__source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA:+has_value}" ] || return 0
32-
__source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
33-
readonly __source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA
31+
[ -n "${__source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA:+has_value}" ] && return
32+
__source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA=$(realpath -- "${BASH_SOURCE[0]}")
33+
# the value of source guard is the canonical dir path of this script
34+
readonly __source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA=${__source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA%/*}
3435
#_ source guard end _#
3536

3637
set -eEuo pipefail

lib/java_utils.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
################################################################################
1212
#
1313
#_ source guard begin _#
14-
[ -z "${__source_guard_ED79675F_4289_4394_A843_03D06DB48AFA:+has_value}" ] || return 0
15-
__source_guard_ED79675F_4289_4394_A843_03D06DB48AFA="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
16-
readonly __source_guard_ED79675F_4289_4394_A843_03D06DB48AFA
14+
[ -n "${__source_guard_ED79675F_4289_4394_A843_03D06DB48AFA:+has_value}" ] && return
15+
__source_guard_ED79675F_4289_4394_A843_03D06DB48AFA=$(realpath -- "${BASH_SOURCE[0]}")
16+
# the value of source guard is the canonical dir path of this script
17+
readonly __source_guard_ED79675F_4289_4394_A843_03D06DB48AFA=${__source_guard_ED79675F_4289_4394_A843_03D06DB48AFA%/*}
1718
#_ source guard end _#
1819

1920
set -eEuo pipefail

lib/maven_utils.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
################################################################################
1010
#
1111
#_ source guard begin _#
12-
[ -z "${__source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F:+has_value}" ] || return 0
13-
__source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
14-
readonly __source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F
12+
[ -n "${__source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F:+has_value}" ] && return
13+
__source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F=$(realpath -- "${BASH_SOURCE[0]}")
14+
# the value of source guard is the canonical dir path of this script
15+
readonly __source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F=${__source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F%/*}
1516
#_ source guard end _#
1617

1718
set -eEuo pipefail
@@ -43,7 +44,8 @@ mvu::_find_mvn_cmd_path() {
4344
fi
4445

4546
# 2. find mvnw from parent dirs
46-
local d="$PWD"
47+
local d
48+
d=$(realpath -- "$PWD")
4749
while true; do
4850
local mvnw_path="$d/$maven_wrapper_name"
4951
[ -x "$mvnw_path" ] && {
@@ -53,7 +55,7 @@ mvu::_find_mvn_cmd_path() {
5355
}
5456

5557
[ "/" = "$d" ] && break
56-
d=$(dirname "$d")
58+
d=${d%/*}
5759
done
5860

5961
# 3. find mvn from $PATH

lib/prepare_jdks.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
################################################################################
1616
#
1717
#_ source guard begin _#
18-
[ -z "${__source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465:+has_value}" ] || return 0
19-
__source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
20-
readonly __source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465
18+
[ -n "${__source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465:+has_value}" ] && return
19+
__source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465=$(realpath -- "${BASH_SOURCE[0]}")
20+
# the value of source guard is the canonical dir path of this script
21+
readonly __source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465=${__source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465%/*}
2122
#_ source guard end _#
2223

2324
set -eEuo pipefail

lib/trap_error_info.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
################################################################################
1616
#
1717
#_ source guard begin _#
18-
[ -z "${__source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE:+has_value}" ] || return 0
19-
__source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
20-
readonly __source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE
18+
[ -n "${__source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE:+has_value}" ] && return
19+
__source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE=$(realpath -- "${BASH_SOURCE[0]}")
20+
# the value of source guard is the canonical dir path of this script
21+
readonly __source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE=${__source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE%/*}
2122
#_ source guard end _#
2223

2324
set -eEu -o pipefail -o functrace

templates/integration_test.template.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!/bin/bash
22
set -eEuo pipefail
3-
cd "$(dirname "$(readlink -f "$0")")"
3+
# the canonical path of this script
4+
SELF_PATH=$(realpath -- "$0")
5+
readonly SELF_PATH SELF_DIR=${SELF_PATH%/*}
6+
cd "$SELF_DIR"
47

5-
FIXME BASH_BUDDY_ROOT="$(readlink -f path/to/bash-buddy/dir)"
8+
FIXME BASH_BUDDY_ROOT="$(realpath -- path/to/bash-buddy/dir)"
69
readonly BASH_BUDDY_ROOT
710

811
source "$BASH_BUDDY_ROOT/lib/trap_error_info.sh"
@@ -28,7 +31,7 @@ source "$BASH_BUDDY_ROOT/lib/maven_utils.sh"
2831
# ci build logic
2932
################################################################################
3033

31-
FIXME PROJECT_ROOT_DIR="$(readlink -f /path/to/project/root/dir)"
34+
FIXME PROJECT_ROOT_DIR="$(realpath -- /path/to/project/root/dir)"
3235
cd "$PROJECT_ROOT_DIR"
3336

3437
########################################

0 commit comments

Comments
 (0)