Skip to content

Commit

Permalink
Add java-select
Browse files Browse the repository at this point in the history
Utility, which simplifies switch between multiple versions of Java.
  • Loading branch information
Godin committed Mar 26, 2015
1 parent 21fe005 commit 517217f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
56 changes: 56 additions & 0 deletions java-select/bin/java-select
@@ -0,0 +1,56 @@
#!/bin/sh

BASE_DIR="$HOME/.java-select"

die() {
echo >&2 "$@"
exit 1
}

# On Mac "echo -n" doesn't work in sh without COMMAND_MODE=legacy (man 5 compat).
# This workaround allows to use echo from GNU coreutils.
echo() {
`which echo` "$@"
}

change() {
[ "$#" -eq 1 ] || die "1 argument required, $# provided"
target="${1}"

if [[ -d "${BASE_DIR}/versions/${target}" ]]; then
local tmpf="${BASE_DIR}/current.new"
ln -s "${BASE_DIR}/versions/${target}" "${tmpf}" || die "Unable to create temporary symlink"
if ! mv -T "${tmpf}" "${BASE_DIR}/current"; then
die "Unable to replace symlink with ${target}"
fi
java -version
else
die "Target '${target}' doesn't appear to be valid!"
fi
}

list() {
local current=$(readlink "${BASE_DIR}/current")
current="${current##*/}"

for candidate in $(ls "${BASE_DIR}/versions") ; do
if [[ -d "${BASE_DIR}/versions/${candidate}" ]]; then
if [ $1 -eq 1 ]; then
if [[ "${current}" = "${candidate}" ]]; then
echo -e -n " \e[1;33m*\e[0m "
else
echo -n " "
fi
fi
echo "${candidate}"
fi
done
}

if [ "$1" == "--completion" ]; then
list 0
elif [ "$#" -eq 1 ]; then
change $1
else
list 1
fi
12 changes: 12 additions & 0 deletions java-select/java-select.zshrc
@@ -0,0 +1,12 @@
export JAVA_HOME="${HOME}/.java-select/current"
export JDK_HOME="${JAVA_HOME}"
export JAVAC="${JAVA_HOME}/bin/javac"
export PATH="${JAVA_HOME}/bin:${DOTFILES}/java-select/bin:${PATH}"

_java_select() {
local completions
completions="$(java-select --completion)"
reply=("${(ps:\n:)completions}")
}

compctl -K _java_select java-select

0 comments on commit 517217f

Please sign in to comment.