diff --git a/bpkg-realpath b/bpkg-realpath new file mode 120000 index 0000000..0efe7bb --- /dev/null +++ b/bpkg-realpath @@ -0,0 +1 @@ +lib/realpath/realpath.sh \ No newline at end of file diff --git a/lib/env/env.sh b/lib/env/env.sh index b4d701c..3f4df86 100755 --- a/lib/env/env.sh +++ b/lib/env/env.sh @@ -9,6 +9,15 @@ else source "$(which bpkg-package)" fi +if ! type -f bpkg-realpath &>/dev/null; then + echo "error: bpkg-realpath not found, aborting" + exit 1 +else + # shellcheck disable=SC2230 + # shellcheck source=lib/realpath/realpath.sh + source "$(which bpkg-realpath)" +fi + # Include config rc file if found BPKG_USER_CONFIG_FILE="$HOME/.bpkgrc" @@ -37,7 +46,7 @@ export BPKG_PACKAGE_USER export BPKG_PACKAGE_NAME="$(bpkg_package name 2>/dev/null)" export BPKG_PACKAGE_REPO="$(bpkg_package repo 2>/dev/null)" export BPKG_PACKAGE_PATH="$(bpkg_package_path)" -export BPKG_PACKAGE_ROOT="$(realpath "$(dirname "$BPKG_PACKAGE_PATH")")" +export BPKG_PACKAGE_ROOT="$(bpkg_realpath "$(dirname "$BPKG_PACKAGE_PATH")")" export BPKG_PACKAGE_DEPS="$BPKG_PACKAGE_ROOT/deps" export BPKG_PACKAGE_VERSION="$(bpkg_package version 2>/dev/null)" export BPKG_PACKAGE_DESCRIPTION="$(bpkg_package description 2>/dev/null)" diff --git a/lib/install/install.sh b/lib/install/install.sh index 2fe133c..01287ab 100755 --- a/lib/install/install.sh +++ b/lib/install/install.sh @@ -1,5 +1,14 @@ #!/usr/bin/env bash +if ! type -f bpkg-realpath &>/dev/null; then + echo "error: bpkg-realpath not found, aborting" + exit 1 +else + # shellcheck disable=SC2230 + # shellcheck source=lib/realpath/realpath.sh + source "$(which bpkg-realpath)" +fi + if ! type -f bpkg-utils &>/dev/null; then echo "error: bpkg-utils not found, aborting" exit 1 @@ -211,7 +220,7 @@ bpkg_install () { echo for pkg in "${pkgs[@]}"; do - if test -d "$(realpath "$pkg" 2>/dev/null)"; then + if test -d "$(bpkg_realpath "$pkg" 2>/dev/null)"; then if ! (cd "$pkg" && bpkg_getdeps); then return 1 fi diff --git a/lib/package/package.sh b/lib/package/package.sh index 4ac0106..b0214a1 100755 --- a/lib/package/package.sh +++ b/lib/package/package.sh @@ -1,11 +1,20 @@ #!/usr/bin/env bash +if ! type -f bpkg-realpath &>/dev/null; then + echo "error: bpkg-realpath not found, aborting" + exit 1 +else + # shellcheck disable=SC2230 + # shellcheck source=lib/realpath/realpath.sh + source "$(which bpkg-realpath)" +fi + BPKG_JSON="$(which bpkg-json)" if [ -z "$BPKG_JSON" ]; then - BPKG_JSON="$(realpath "$0/../JSON/JSON.sh")" + BPKG_JSON="$(bpkg_realpath "$0/../JSON/JSON.sh")" else - BPKG_JSON="$(realpath "$BPKG_JSON")" + BPKG_JSON="$(bpkg_realpath "$BPKG_JSON")" fi ## output usage diff --git a/lib/realpath/realpath.sh b/lib/realpath/realpath.sh new file mode 100755 index 0000000..a71c24c --- /dev/null +++ b/lib/realpath/realpath.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +function bpkg_realpath () { + local target="$1" + + if [ -n "$(which realpath 2>/dev/null)" ]; then + realpath "$@" + return $? + fi + + if test -d "$target"; then + cd "$target" || return $? + pwd + elif test -f "$target"; then + # file + if [[ $target = /* ]]; then + echo "$target" + elif [[ $target == */* ]]; then + cd "${1%/*}" || return $? + echo "$(pwd)/${1##*/}" + else + echo "$(pwd)/$target" + fi + fi + + return 0 +} + +if [[ ${BASH_SOURCE[0]} != "$0" ]]; then + export -f bpkg_realpath +else + bpkg_realpath "$@" + exit $? +fi diff --git a/lib/run/run.sh b/lib/run/run.sh index a8a5dda..d7b1357 100755 --- a/lib/run/run.sh +++ b/lib/run/run.sh @@ -1,5 +1,14 @@ #!/usr/bin/env bash +if ! type -f bpkg-realpath &>/dev/null; then + echo "error: bpkg-realpath not found, aborting" + exit 1 +else + # shellcheck disable=SC2230 + # shellcheck source=lib/realpath/realpath.sh + source "$(which bpkg-realpath)" +fi + if ! type -f bpkg-utils &>/dev/null; then echo "error: bpkg-utils not found, aborting" exit 1 @@ -172,7 +181,7 @@ bpkg_run () { if which "$name" 2>/dev/null; then : elif test -f "$1"; then - realpath "$1" + bpkg_realpath "$1" elif [ -n "$1" ] && which "$1" 2>/dev/null; then : fi diff --git a/setup.sh b/setup.sh index 64aa83b..d63781a 100755 --- a/setup.sh +++ b/setup.sh @@ -89,6 +89,7 @@ CMDS+=("suggest") CMDS+=("term") CMDS+=("update") CMDS+=("utils") +CMDS+=("realpath") make_install () { local source