Skip to content

Commit

Permalink
refactor: use command realpath instead of function `portableReadLin…
Browse files Browse the repository at this point in the history
…k` without losing portability 🔗
  • Loading branch information
oldratlee committed Feb 18, 2024
1 parent 4ad6b5b commit 55d7c15
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 98 deletions.
39 changes: 9 additions & 30 deletions bin/ap
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,13 @@ die() {
exit 1
}

# How can I get the behavior of GNU's readlink -f on a Mac?
# https://stackoverflow.com/questions/1055671
portableReadLink() {
local file=$1 uname

uname=$(uname)
case "$uname" in
Linux* | CYGWIN* | MINGW*)
readlink -f -- "$file"
;;
Darwin*)
local py_args=(-c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$file")
if command -v greadlink >/dev/null; then
greadlink -f -- "$file"
elif command -v python3 >/dev/null; then
python3 "${py_args[@]}"
elif command -v python >/dev/null; then
python "${py_args[@]}"
else
die "fail to find command(greadlink/python3/python) to get absolute path!"
fi
;;
*)
die "NOT support uname($uname)!"
;;
esac
# `realpath` command existed on Linux and macOS, return resolved physical path
# - realpath command on macOS do NOT support option `-e`;
# combined `[ -e $file ]` to check file existence first.
# - How can I get the behavior of GNU's readlink -f on a Mac?
# https://stackoverflow.com/questions/1055671
realpath() {
[ -e "$1" ] && command realpath -- "$1"
}

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

for f in "${files[@]}"; do
if [ -e "$f" ]; then
portableReadLink "$f"
else
realpath "$f" || {
redPrint "error: $f does not exists!" >&2
has_error=true
fi
}
done

# set exit status
Expand Down
36 changes: 9 additions & 27 deletions bin/cp-into-docker-run
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,13 @@ isAbsolutePath() {
[[ "$1" =~ ^/ ]]
}

# How can I get the behavior of GNU's readlink -f on a Mac?
# https://stackoverflow.com/questions/1055671
portableReadLink() {
local file=$1 uname

uname=$(uname)
case "$uname" in
Linux* | CYGWIN* | MINGW*)
readlink -f -- "$file"
;;
Darwin*)
local py_args=(-c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$file")
if command -v greadlink >/dev/null; then
greadlink -f -- "$file"
elif command -v python3 >/dev/null; then
python3 "${py_args[@]}"
elif command -v python >/dev/null; then
python "${py_args[@]}"
else
die "fail to find command(greadlink/python3/python) for readlink!"
fi
;;
*)
die "NOT support uname($uname)!"
;;
esac
# `realpath` command existed on Linux and macOS, return resolved physical path
# - realpath command on macOS do NOT support option `-e`;
# combined `[ -e $file ]` to check file existence first.
# - How can I get the behavior of GNU's readlink -f on a Mac?
# https://stackoverflow.com/questions/1055671
realpath() {
[ -e "$1" ] && command realpath -- "$1"
}

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

run_command=$(which "$specified_run_command")
fi
run_command=$(portableReadLink "$run_command")

run_command=$(realpath "$run_command")
readonly run_command run_command_base_name=${run_command##*/}

run_timestamp=$(date "+%Y%m%d_%H%M%S")
Expand Down
39 changes: 9 additions & 30 deletions bin/xpf
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,22 @@ set -eEuo pipefail
# util functions
################################################################################

# How can I get the behavior of GNU's readlink -f on a Mac?
# https://stackoverflow.com/questions/1055671
portableReadLink() {
local file=$1 uname

uname=$(uname)
case "$uname" in
Linux* | CYGWIN* | MINGW*)
readlink -f -- "$file"
;;
Darwin*)
local py_args=(-c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$file")
if command -v greadlink >/dev/null; then
greadlink -f -- "$file"
elif command -v python3 >/dev/null; then
python3 "${py_args[@]}"
elif command -v python >/dev/null; then
python "${py_args[@]}"
else
echo "fail to find command(greadlink/python3/python) for readlink!" >&2
exit 1
fi
;;
*)
echo "not support uname($uname)!" >&2
exit 1
;;
esac
# `realpath` command existed on Linux and macOS, return resolved physical path
# - realpath command on macOS do NOT support option `-e`;
# combined `[ -e $file ]` to check file existence first.
# - How can I get the behavior of GNU's readlink -f on a Mac?
# https://stackoverflow.com/questions/1055671
realpath() {
[ -e "$1" ] && command realpath -- "$1"
}

################################################################################
# biz logic
################################################################################

# DO NOT inline THIS_SCRIPT into BASE_DIR, because sub-shell:
# BASE_DIR=$(dirname -- "$(portableReadLink "${BASH_SOURCE[0]}")")
THIS_SCRIPT=$(portableReadLink "${BASH_SOURCE[0]}")
# BASE_DIR=$(dirname -- "$(realpath "${BASH_SOURCE[0]}")")
THIS_SCRIPT=$(realpath "${BASH_SOURCE[0]}")
BASE_DIR=$(dirname -- "$THIS_SCRIPT")

# shellcheck disable=SC1091
Expand Down
9 changes: 4 additions & 5 deletions test-cases/integration-test.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#!/usr/bin/env bash
set -eEuo pipefail

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

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

################################################################################
# common util functions
Expand Down
6 changes: 5 additions & 1 deletion test-cases/lint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/usr/bin/env bash
set -eEuo pipefail

realpath() {
[ -e "$1" ] && command realpath -- "$1"
}

# cd to the root of the project
cd "$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")"/..
cd "$(dirname -- "$(realpath "${BASH_SOURCE[0]}")")"/..

find bin lib legacy-bin -type f |
grep -Pv '/show-duplicate-java-classes$' |
Expand Down
9 changes: 4 additions & 5 deletions test-cases/uq_test.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#!/usr/bin/env bash
set -eEuo pipefail

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

BASE=$(dirname -- "$($READLINK_CMD -f -- "${BASH_SOURCE[0]}")")
BASE=$(dirname -- "$(realpath "${BASH_SOURCE[0]}")")
cd "$BASE"

#################################################
Expand Down

0 comments on commit 55d7c15

Please sign in to comment.