Skip to content

Commit

Permalink
Resolve calls to symlinked 'np'
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpurra committed Jan 21, 2015
1 parent 5f4619e commit fdf77a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
29 changes: 28 additions & 1 deletion src/np
@@ -1,4 +1,31 @@
#!/usr/bin/env bash
set -e

exec "${BASH_SOURCE%/*}/np.sh" "$@"
function crossplatformReadlink {
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
# http://stackoverflow.com/a/1116890
TARGET_FILE="$1"

cd `dirname $TARGET_FILE`
TARGET_FILE=`basename $TARGET_FILE`

# Iterate down a (possible) chain of symlinks
while [ -L "$TARGET_FILE" ]
do
TARGET_FILE=`readlink $TARGET_FILE`
cd `dirname $TARGET_FILE`
TARGET_FILE=`basename $TARGET_FILE`
done

# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
PHYS_DIR=`pwd -P`
RESULT=$PHYS_DIR/$TARGET_FILE
echo "$RESULT"
}

# Must be executed in this file.
# Used to recursively call np and to find files to `source`.
NP_SOURCE=$(crossplatformReadlink "$BASH_SOURCE")

exec "${NP_SOURCE%/*}/np.sh" "$@"
12 changes: 10 additions & 2 deletions src/np.sh
Expand Up @@ -9,6 +9,14 @@ action=""

executable="np-${action}.sh"

[[ -z $(which "$executable") ]] && { echo -E "np: '${action}' is not a action." 1>&2; exit 1; }
if [[ -z $(which "$executable") ]];
then
executable="${BASH_SOURCE%/*}/${executable}"

exec "$executable" "$@"
if [[ -z $(which "$executable") ]];
then
echo -E "np: '${action}' is not a action." 1>&2; exit 1;
fi
fi

exec "$executable" "$@"

0 comments on commit fdf77a6

Please sign in to comment.