Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "tests/vendor/bats-assert"]
path = tests/vendor/bats-assert
url = https://github.com/ztombol/bats-assert
[submodule "vendor/bashup/realpaths"]
path = vendor/bashup/realpaths
url = https://github.com/bashup/realpaths
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ script:
docker run -it bash:${BASHVER} --version
time docker run -it basher/basher:bash-${BASHVER} --tap /opt/basher/tests
else
if [[ "$TRAVIS_OS_NAME" == osx ]]; then
brew uninstall --force coreutils
fi
git config --global user.email "user@example.com"
git config --global user.name "User Name"
time bats/bin/bats --tap tests
Expand Down
2 changes: 1 addition & 1 deletion bin/basher
21 changes: 0 additions & 21 deletions libexec/basher
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@
#
# Usage: basher <command> [<args>]

resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}

abs_dirname() {
local cwd="$(pwd)"
local path="$1"

while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done

pwd
cd "$cwd"
}

if [ -z "$BASHER_ROOT" ]; then
BASHER_ROOT="$HOME/.basher"
fi
Expand All @@ -47,9 +29,6 @@ if [ -z "$BASHER_INSTALL_MAN" ]; then
fi
export BASHER_INSTALL_MAN

bin_path="$(abs_dirname "$0")"
export PATH="${bin_path}:${PATH}"

command="$1"
case "$command" in
"")
Expand Down
12 changes: 3 additions & 9 deletions libexec/basher-link
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@

set -e

resolve_link() {
if type -p realpath >/dev/null; then
realpath "$1"
else
readlink -f "$1"
fi
}

no_deps="false"

case $1 in
Expand Down Expand Up @@ -62,7 +54,9 @@ if [ ! -d "${BASHER_PACKAGES_PATH}/$namespace" ]; then
fi

# Resolve local package path
directory="$(resolve_link "$directory")"
source basher.realpath
realpath.absolute "$directory"
directory="$REPLY"

ln -s "$directory" "${BASHER_PACKAGES_PATH}/$package"

Expand Down
66 changes: 66 additions & 0 deletions libexec/basher.realpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash
# This file is automatically generated by `make vendor`; do not edit!

realpath.location(){ realpath.follow "$1"; realpath.absolute "$REPLY" ".."; }
realpath.resolved(){ realpath.follow "$1"; realpath.absolute "$REPLY"; }
realpath.dirname() { REPLY=.; ! [[ $1 =~ /+[^/]+/*$ ]] || REPLY="${1%${BASH_REMATCH[0]}}"; REPLY=${REPLY:-/}; }
realpath.basename(){ REPLY=/; ! [[ $1 =~ /*([^/]+)/*$ ]] || REPLY="${BASH_REMATCH[1]}"; }

realpath.follow() {
local target
while [[ -L "$1" ]] && target=$(readlink -- "$1"); do
realpath.dirname "$1"
# Resolve relative to symlink's directory
[[ $REPLY != . && $target != /* ]] && REPLY=$REPLY/$target || REPLY=$target
# Break out if we found a symlink loop
for target; do [[ $REPLY == "$target" ]] && break 2; done
# Add to the loop-detect list and tail-recurse
set -- "$REPLY" "$@"
done
REPLY="$1"
}

realpath.absolute() {
REPLY=$PWD; local eg=extglob; ! shopt -q $eg || eg=; ${eg:+shopt -s $eg}
while (($#)); do case $1 in
//|//[^/]*) REPLY=//; set -- "${1:2}" "${@:2}" ;;
/*) REPLY=/; set -- "${1##+(/)}" "${@:2}" ;;
*/*) set -- "${1%%/*}" "${1##${1%%/*}+(/)}" "${@:2}" ;;
''|.) shift ;;
..) realpath.dirname "$REPLY"; shift ;;
*) REPLY="${REPLY%/}/$1"; shift ;;
esac; done; ${eg:+shopt -u $eg}
}

realpath.canonical() {
realpath.follow "$1"; set -- "$REPLY" # $1 is now resolved
realpath.basename "$1"; set -- "$1" "$REPLY" # $2 = basename $1
realpath.dirname "$1"
[[ $REPLY != "$1" ]] && realpath.canonical "$REPLY"; # recurse unless root
realpath.absolute "$REPLY" "$2"; # combine canon parent w/basename
}

realpath.relative() {
local target=""
realpath.absolute "$1"; set -- "$REPLY" "${@:2}"; realpath.absolute "${2-$PWD}" X
while realpath.dirname "$REPLY"; [[ "$1" != "$REPLY" && "$1" == "${1#${REPLY%/}/}" ]]; do
target=../$target
done
[[ $1 == "$REPLY" ]] && REPLY=${target%/} || REPLY="$target${1#${REPLY%/}/}"
REPLY=${REPLY:-.}
}

# Compute the physical file and directory of the main program

realpath.resolved "$0"
__FILE__=$REPLY

realpath.dirname "$REPLY"
__DIR__=$REPLY

# If we're run as the main program and our name is basher.realpath,
# run basher with our directory at the head of PATH:

if [[ "$0" == "$BASH_SOURCE" && "$__FILE__" == *.realpath && -f "${__FILE__%.realpath}" ]]; then
PATH="$__DIR__:$PATH" exec "${__FILE__%.realpath}" "$@"
fi
15 changes: 15 additions & 0 deletions libexec/basher.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

# Compute the physical file and directory of the main program

realpath.resolved "$0"
__FILE__=$REPLY

realpath.dirname "$REPLY"
__DIR__=$REPLY

# If we're run as the main program and our name is basher.realpath,
# run basher with our directory at the head of PATH:

if [[ "$0" == "$BASH_SOURCE" && "$__FILE__" == *.realpath && -f "${__FILE__%.realpath}" ]]; then
PATH="$__DIR__:$PATH" exec "${__FILE__%.realpath}" "$@"
fi
15 changes: 14 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
test:
bats tests

.PHONY: test
.PHONY: test vendor


# The bootstrap script adds basher's physical directory to PATH:
# It is built from bashup/realpaths + libexec/basher.stub

BOOTSTRAP=libexec/basher.realpath

vendor: $(BOOTSTRAP)

$(BOOTSTRAP): vendor/bashup/realpaths/realpaths libexec/basher.stub
sed -e '2i\
# This file is automatically generated by `make vendor`; do not edit!' $^ >$@
chmod +x $@
9 changes: 2 additions & 7 deletions tests/basher-link.bats
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#!/usr/bin/env bats

load test_helper
source basher.realpath

resolve_link() {
if type -p realpath >/dev/null; then
realpath "$1"
else
readlink -f "$1"
fi
}
resolve_link() { realpath.resolved "$1"; echo "$REPLY"; }

@test "without arguments prints usage" {
run basher-link
Expand Down
1 change: 1 addition & 0 deletions vendor/bashup/realpaths
Submodule realpaths added at 7b209f