Skip to content

Commit

Permalink
Merge pull request #14 from nodenv/major-vee
Browse files Browse the repository at this point in the history
Support auto-aliasing major versions
  • Loading branch information
jasonkarns authored May 28, 2017
2 parents 0d9140e + 98add17 commit 5dadf6e
Show file tree
Hide file tree
Showing 4 changed files with 584 additions and 16 deletions.
63 changes: 50 additions & 13 deletions bin/nodenv-alias
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}

# Setup for relative includes. Adapted from:
# http://www.ostricher.com/2014/10/the-right-way-to-get-the-directory-of-a-bash-script/
# http://stackoverflow.com/a/12694189/407845
get_script_dir() {
local dir
local src="${BASH_SOURCE[0]}"

# while $src is a symlink, resolve it
while [ -h "$src" ]; do
dir="${src%/*}"
src="$( resolve_link "$src" )"

# If $src was a relative symlink (so no "/" as prefix),
# need to resolve it relative to the symlink base directory
[[ $src != /* ]] && src="$dir/$src"
done
dir="${src%/*}"

if [ -d "$dir" ]; then
echo "$dir"
else
echo "$PWD"
fi
}

SEMVER_SORT="$(get_script_dir)/../node_modules/sh-semver/semver.sh"

list() {
local exit=1
local link
Expand Down Expand Up @@ -53,14 +80,10 @@ echo_lines_without_symlinks() {
}

sort_versions() {
local patch_start_point
local prefix=""
[[ $1 =~ (^[^.]*-).* ]] && prefix="${BASH_REMATCH[1]}"

case "$1" in
*.*.*) patch_start_point=$((${#1} + 3)) ;; # point_release string length + 1 (0 indexed) + 2 for `-p` separator
*.*) patch_start_point=$((${#1} + 2)) ;; # point_release string length + 1 (0 indexed) + 1 for `.` separator
esac

sort -n -k "1.$patch_start_point"
sed -e "s/^$prefix//" | "$SEMVER_SORT" | sed -e "s/^/$prefix/"
}

auto_for_point() {
Expand All @@ -78,8 +101,22 @@ auto_symlink_point() {
fi
}

point_releases() {
echo_lines_without_symlinks ./*.*.* | sed -e 's/\.[^-.]*$//' -e 's/-[^.]*$//' | sort -u
pre_releases() {
echo_lines_without_symlinks ./*.*.*-*.* | sed -E -e 's/([0-9]+\.[0-9]+\.[0-9]+-.*)\..*/\1/' | sort -u
}

minor_releases() {
echo_lines_without_symlinks ./*.*.* | sed -E -e 's/([0-9]+\.[0-9]+)\.[0-9]+.*/\1/' | sort -u
}

major_releases() {
echo_lines_without_symlinks ./*.*.* | sed -E -e 's/([0-9]+)\.[0-9]+\.[0-9]+.*/\1/' | sort -u | grep -v 0
}

recommended_aliases() {
major_releases
minor_releases
pre_releases
}

abort() {
Expand All @@ -100,7 +137,7 @@ if [ --complete = "$1" ]; then
elif [ "$#" = 0 ]; then
echo --auto
echo --list
{ point_releases; echo_lines_with_symlinks ./*; } | sort -u
{ recommended_aliases; echo_lines_with_symlinks ./*; } | sort -u
fi
exit 0
fi
Expand All @@ -119,8 +156,8 @@ case "$#" in
fi
elif [ --auto = "$2" ]; then
case "$1" in
*.*) cleanup_invalid "$1" && auto_symlink_point "$1" ;;
*) abort "Don't know how to automatically alias $1" ;;
*) cleanup_invalid "$1" && auto_symlink_point "$1" ;;
# *) abort "Don't know how to automatically alias $1" ;;
esac
else
echo "$1 => $2"
Expand All @@ -135,7 +172,7 @@ case "$#" in
;;
--auto | --all)
cleanup_invalid
for point in $(point_releases); do
for point in $(recommended_aliases); do
auto_symlink_point "$point"
done
;;
Expand Down
Loading

0 comments on commit 5dadf6e

Please sign in to comment.