Skip to content

Commit 9cb216c

Browse files
committed
Fix realpath not available on macOS
1 parent 4e3c605 commit 9cb216c

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

lib/tfenv-exec.sh

+27-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,38 @@
22

33
set -uo pipefail;
44

5+
function realpath-relative-to() {
6+
# A basic implementation of GNU `realpath --relative-to=$1 $2`
7+
# that can also be used on macOS.
8+
9+
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
10+
readlink_f() {
11+
local target_file="${1}";
12+
local file_name;
13+
14+
while [ "${target_file}" != "" ]; do
15+
cd "$(dirname "$target_file")" || early_death "Failed to 'cd \$(${target_file%/*})'";
16+
file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"'";
17+
target_file="$(readlink "${file_name}")";
18+
done;
19+
20+
echo "$(pwd -P)/${file_name}";
21+
};
22+
23+
local relative_to="$(readlink_f "${1}")";
24+
local path="$(readlink_f "${2}")";
25+
26+
echo "${path#"${relative_to}/"}";
27+
return 0;
28+
}
29+
export -f realpath-relative-to;
30+
531
function tfenv-exec() {
632
for _arg in ${@:1}; do
733
if [[ "${_arg}" == -chdir=* ]]; then
834
chdir="${_arg#-chdir=}";
935
log 'debug' "Found -chdir arg: ${chdir}";
10-
export TFENV_DIR="${PWD}/$(realpath --relative-to="${PWD}" "$chdir")";
36+
export TFENV_DIR="${PWD}/$(realpath-relative-to "${PWD}" "${chdir}")";
1137
log 'debug' "Setting TFENV_DIR to: ${TFENV_DIR}";
1238
fi;
1339
done;

0 commit comments

Comments
 (0)