Skip to content

Commit 55d7c15

Browse files
committed
refactor: use command realpath instead of function portableReadLink without losing portability 🔗
1 parent 4ad6b5b commit 55d7c15

File tree

6 files changed

+40
-98
lines changed

6 files changed

+40
-98
lines changed

bin/ap

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,13 @@ die() {
3535
exit 1
3636
}
3737

38-
# How can I get the behavior of GNU's readlink -f on a Mac?
39-
# https://stackoverflow.com/questions/1055671
40-
portableReadLink() {
41-
local file=$1 uname
42-
43-
uname=$(uname)
44-
case "$uname" in
45-
Linux* | CYGWIN* | MINGW*)
46-
readlink -f -- "$file"
47-
;;
48-
Darwin*)
49-
local py_args=(-c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$file")
50-
if command -v greadlink >/dev/null; then
51-
greadlink -f -- "$file"
52-
elif command -v python3 >/dev/null; then
53-
python3 "${py_args[@]}"
54-
elif command -v python >/dev/null; then
55-
python "${py_args[@]}"
56-
else
57-
die "fail to find command(greadlink/python3/python) to get absolute path!"
58-
fi
59-
;;
60-
*)
61-
die "NOT support uname($uname)!"
62-
;;
63-
esac
38+
# `realpath` command existed on Linux and macOS, return resolved physical path
39+
# - realpath command on macOS do NOT support option `-e`;
40+
# combined `[ -e $file ]` to check file existence first.
41+
# - How can I get the behavior of GNU's readlink -f on a Mac?
42+
# https://stackoverflow.com/questions/1055671
43+
realpath() {
44+
[ -e "$1" ] && command realpath -- "$1"
6445
}
6546

6647
usage() {
@@ -131,12 +112,10 @@ readonly files=("${files[@]:-.}")
131112
has_error=false
132113

133114
for f in "${files[@]}"; do
134-
if [ -e "$f" ]; then
135-
portableReadLink "$f"
136-
else
115+
realpath "$f" || {
137116
redPrint "error: $f does not exists!" >&2
138117
has_error=true
139-
fi
118+
}
140119
done
141120

142121
# set exit status

bin/cp-into-docker-run

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,13 @@ isAbsolutePath() {
3636
[[ "$1" =~ ^/ ]]
3737
}
3838

39-
# How can I get the behavior of GNU's readlink -f on a Mac?
40-
# https://stackoverflow.com/questions/1055671
41-
portableReadLink() {
42-
local file=$1 uname
43-
44-
uname=$(uname)
45-
case "$uname" in
46-
Linux* | CYGWIN* | MINGW*)
47-
readlink -f -- "$file"
48-
;;
49-
Darwin*)
50-
local py_args=(-c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$file")
51-
if command -v greadlink >/dev/null; then
52-
greadlink -f -- "$file"
53-
elif command -v python3 >/dev/null; then
54-
python3 "${py_args[@]}"
55-
elif command -v python >/dev/null; then
56-
python "${py_args[@]}"
57-
else
58-
die "fail to find command(greadlink/python3/python) for readlink!"
59-
fi
60-
;;
61-
*)
62-
die "NOT support uname($uname)!"
63-
;;
64-
esac
39+
# `realpath` command existed on Linux and macOS, return resolved physical path
40+
# - realpath command on macOS do NOT support option `-e`;
41+
# combined `[ -e $file ]` to check file existence first.
42+
# - How can I get the behavior of GNU's readlink -f on a Mac?
43+
# https://stackoverflow.com/questions/1055671
44+
realpath() {
45+
[ -e "$1" ] && command realpath -- "$1"
6546
}
6647

6748
usage() {
@@ -204,7 +185,8 @@ if [ ! -f "$specified_run_command" ]; then
204185

205186
run_command=$(which "$specified_run_command")
206187
fi
207-
run_command=$(portableReadLink "$run_command")
188+
189+
run_command=$(realpath "$run_command")
208190
readonly run_command run_command_base_name=${run_command##*/}
209191

210192
run_timestamp=$(date "+%Y%m%d_%H%M%S")

bin/xpf

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,22 @@ set -eEuo pipefail
1414
# util functions
1515
################################################################################
1616

17-
# How can I get the behavior of GNU's readlink -f on a Mac?
18-
# https://stackoverflow.com/questions/1055671
19-
portableReadLink() {
20-
local file=$1 uname
21-
22-
uname=$(uname)
23-
case "$uname" in
24-
Linux* | CYGWIN* | MINGW*)
25-
readlink -f -- "$file"
26-
;;
27-
Darwin*)
28-
local py_args=(-c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$file")
29-
if command -v greadlink >/dev/null; then
30-
greadlink -f -- "$file"
31-
elif command -v python3 >/dev/null; then
32-
python3 "${py_args[@]}"
33-
elif command -v python >/dev/null; then
34-
python "${py_args[@]}"
35-
else
36-
echo "fail to find command(greadlink/python3/python) for readlink!" >&2
37-
exit 1
38-
fi
39-
;;
40-
*)
41-
echo "not support uname($uname)!" >&2
42-
exit 1
43-
;;
44-
esac
17+
# `realpath` command existed on Linux and macOS, return resolved physical path
18+
# - realpath command on macOS do NOT support option `-e`;
19+
# combined `[ -e $file ]` to check file existence first.
20+
# - How can I get the behavior of GNU's readlink -f on a Mac?
21+
# https://stackoverflow.com/questions/1055671
22+
realpath() {
23+
[ -e "$1" ] && command realpath -- "$1"
4524
}
4625

4726
################################################################################
4827
# biz logic
4928
################################################################################
5029

5130
# DO NOT inline THIS_SCRIPT into BASE_DIR, because sub-shell:
52-
# BASE_DIR=$(dirname -- "$(portableReadLink "${BASH_SOURCE[0]}")")
53-
THIS_SCRIPT=$(portableReadLink "${BASH_SOURCE[0]}")
31+
# BASE_DIR=$(dirname -- "$(realpath "${BASH_SOURCE[0]}")")
32+
THIS_SCRIPT=$(realpath "${BASH_SOURCE[0]}")
5433
BASE_DIR=$(dirname -- "$THIS_SCRIPT")
5534

5635
# shellcheck disable=SC1091

test-cases/integration-test.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#!/usr/bin/env bash
22
set -eEuo pipefail
33

4-
READLINK_CMD=readlink
5-
if command -v greadlink &>/dev/null; then
6-
READLINK_CMD=greadlink
7-
fi
4+
realpath() {
5+
[ -e "$1" ] && command realpath -- "$1"
6+
}
87

9-
cd "$(dirname -- "$($READLINK_CMD -f -- "${BASH_SOURCE[0]}")")"
8+
cd "$(dirname -- "$(realpath "${BASH_SOURCE[0]}")")"
109

1110
################################################################################
1211
# common util functions

test-cases/lint.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#!/usr/bin/env bash
22
set -eEuo pipefail
33

4+
realpath() {
5+
[ -e "$1" ] && command realpath -- "$1"
6+
}
7+
48
# cd to the root of the project
5-
cd "$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")"/..
9+
cd "$(dirname -- "$(realpath "${BASH_SOURCE[0]}")")"/..
610

711
find bin lib legacy-bin -type f |
812
grep -Pv '/show-duplicate-java-classes$' |

test-cases/uq_test.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#!/usr/bin/env bash
22
set -eEuo pipefail
33

4-
READLINK_CMD=readlink
5-
if command -v greadlink &>/dev/null; then
6-
READLINK_CMD=greadlink
7-
fi
4+
realpath() {
5+
[ -e "$1" ] && command realpath -- "$1"
6+
}
87

9-
BASE=$(dirname -- "$($READLINK_CMD -f -- "${BASH_SOURCE[0]}")")
8+
BASE=$(dirname -- "$(realpath "${BASH_SOURCE[0]}")")
109
cd "$BASE"
1110

1211
#################################################

0 commit comments

Comments
 (0)