Skip to content

Commit

Permalink
feat(): introduce 'bpkg-realpath'
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed May 28, 2022
1 parent 80e3da8 commit 1588f1e
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 5 deletions.
1 change: 1 addition & 0 deletions bpkg-realpath
11 changes: 10 additions & 1 deletion lib/env/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)"
Expand Down
11 changes: 10 additions & 1 deletion lib/install/install.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions lib/package/package.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
34 changes: 34 additions & 0 deletions lib/realpath/realpath.sh
Original file line number Diff line number Diff line change
@@ -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
11 changes: 10 additions & 1 deletion lib/run/run.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ CMDS+=("suggest")
CMDS+=("term")
CMDS+=("update")
CMDS+=("utils")
CMDS+=("realpath")

make_install () {
local source
Expand Down

0 comments on commit 1588f1e

Please sign in to comment.