Skip to content

Instantly share code, notes, and snippets.

@krrrr38
Last active August 29, 2015 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krrrr38/10004322 to your computer and use it in GitHub Desktop.
Save krrrr38/10004322 to your computer and use it in GitHub Desktop.
play version manager based on svm(https://github.com/yuroyoro/svm)
#!/usr/bin/env bash
# Copyright (c) 2011 Tomohito Ozaki(yuroyoro)
# 2014 Ken Kaizu(krrrr38)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. The name of the author may not be used to endorse or promote
# products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NEXT_VERSION="2.2.2"
AVAILABLE_VERSIONS=`cat <<EOS
1.0
1.0.1
1.0.2
1.0.2.1
1.0.3
1.0.3.1
1.0.3.2
1.0.3.3
1.0.3.4
1.1
1.1.1
1.1.2
1.1.3
1.2
1.2.1
1.2.2
1.2.3
1.2.4
1.2.5
1.2.5.1
1.2.5.2
1.2.5.3
1.2.6
1.2.7
2.0
2.0.1
2.0.2
2.0.3
2.0.4
2.0.5
2.0.6
2.0.7
2.0.8
2.1.0
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5
2.2.0
2.2.1
2.2.2
EOS`
BASE_DIR="${PLAY_INSTALLED_DIR}"
if [ -z "${BASE_DIR}" ] ; then
BASE_DIR="${HOME}/.playvm"
fi
if [ ! -d "${BASE_DIR}" ] ; then
mkdir -p "${BASE_DIR}"
fi
path_included=0
path=$PATH:
while [ $path ]
do
p=${path%%:*}
if [ "${BASE_DIR}/current/rt" = "${p}" ] ; then
path_included=1
fi
path=${path#*:}
done
if [ $path_included -eq 0 ] ; then
echo "warning: You don't have ${BASE_DIR}/current/rt in your PATH."
fi
usage() {
cat << EOS
Usage :
playvm [Action] [Play-Version] [Options]
Action :
-h|help - show this usage information
-c|current - show the currently use play version
-l|list - show the play version installed in playvm_path(default is $HOME/.playvm)
-v|versions - show the abalabe play version not installed
-i|install - install specific play version
-r|remove|uninstall - uninstall specific play version and remove their sources
-s|switch|-u|use - setup to use a specific play version
(any strings) - setup to use specific play version(shortcut of playvm switch)
EOS
}
yes_no() {
MSG=$1
while :
do
echo -n "${MSG} y/N: "
read ans
case $ans in
[yY]|[yY][eE][sS]) return 0 ;;
[nN]|[nN][oO]) return 1 ;;
esac
done
}
list(){
case "`uname`" in
Darwin*) find ${BASE_DIR} -maxdepth 1 -d -print0 | xargs -0 basename | grep 'play-' | sed 's/play-//' ;;
*) find ${BASE_DIR} -maxdepth 1 -depth -printf "%f\n" | grep 'play-' | sed 's/play-//' ;;
esac
}
versions(){
case "`uname`" in
Darwin*) installed_versions=`find ${BASE_DIR} -maxdepth 1 -d -print0 | xargs -0 basename | grep 'play' | sed 's/play-//'` ;;
*) installed_versions=`find ${BASE_DIR} -maxdepth 1 -depth -printf "%f\n" | grep 'play' | sed 's/play-//'` ;;
esac
for v in ${AVAILABLE_VERSIONS}
do
for i in ${installed_versions}
do
if [ "${v}" = "${i}" ] ; then
installed=1
fi
done
if [ -z "${installed}" ] ;then
echo "${v}"
fi
unset installed
done
cat << EOS
more previous releases
see http://www.playframework-ja.org/download
EOS
}
download(){
url="$1"
echo "Downloading $url"
if which wget > /dev/null 2>&1; then
wget "$url"
elif which curl > /dev/null 2>&1; then
curl -O -L "$url"
else
echo >&2 "wget or curl is required"
return 1
fi
if [ $? -ne 0 ]; then
echo >&2 "Failed to download $url"
return 1
fi
}
install(){
version=$1
echo "Trying to installing version of ${version}."
dist_url="http://downloads.typesafe.com/play"
version_dir="${BASE_DIR}/play-${version}"
if [ -d "${version_dir}" ]
then
echo "${version_dir} is already exisits."
yes_no "Do you want to replace this version?"
if [ $? -ne 0 ]; then
rm -rf "${version_dir}"
else
return 0;
fi
fi
mkdir -p "${version_dir}"
cd "${version_dir}"
echo "in ${version_dir}"
download "${dist_url}/${version}/play-${version}.zip"
if [ $? -ne 0 ]; then
cd "${PWD}"
rm -rf "${version_dir}"
return 1;
fi
unzip play-${version}.zip
mv play-${version} rt
rm play-${version}.zip
cd "${PWD}"
echo "Successfully installed version of ${version}."
echo "installed directory is ${version_dir}"
return 0;
}
uninstall(){
version=$1
version_dir="${BASE_DIR}/play-${version}"
get_current current_ver
if [ "${version}" = "${current_ver}" ] ; then
yes_no "Do you want to remove currently version [${version}] ?"
if [ $? -ne 0 ]; then
return 0;
else
read_next_version next_version
fi
fi
rm -rf "${version_dir}"
echo -n "removed version ${version}"
if [ ! -z "${next_version}" ];then
switch "${next_version}"
fi
echo "Successfully uninstalled version of ${version}"
}
read_next_version(){
list
get_stable stable
echo ""
echo -n "Please input currently set version.[${stable}]"
read next_version
if [ -z "${next_version}" ];then
next_version="${stable}"
fi
eval $1="${next_version}"
return 0;
}
confirm_switch(){
version=$1
yes_no "Do you want to change current runtime version to ${version}?"
if [ $? -eq 0 ]; then
switch "${version}"
return 0;
fi
return 1;
}
switch(){
version=$1
if [ -z "${version}" ] ; then
read_next_version version
fi
get_current current_ver
if [ "${version}" = "${current_ver}" ] ; then
echo "Nothing to do..."
return 0;
fi
version_dir="${BASE_DIR}/play-${version}"
if [ ! -d "${version_dir}" ] ; then
echo "Version ${version} is not installed."
yes_no "Do you want to try to install version of ${version}?"
if [ $? -eq 0 ]; then
install "${version}"
if [ $? -ne 0 ]; then
return 1;
fi
else
return 0;
fi
fi
if [ -h "${BASE_DIR}/current" ] ; then
rm "${BASE_DIR}/current"
fi
ln -fs "play-${version}" "${BASE_DIR}/current"
echo "currently version is $1"
}
current(){
get_current currnet_version
echo "currently version is ${currnet_version}"
}
get_current(){
currnet_version=`readlink "${BASE_DIR}/current" | sed 's/play-//'`
eval $1="${currnet_version}"
return 0;
}
cmd="$1"
if [ -z "${cmd}" ] ; then
current
else
case "$cmd" in
-h|help) usage ;;
-c|current) current ;;
-l|list) list
current
;;
-v|versions) versions ;;
-i|install) install $2
if [ $? -eq 0 ]; then
confirm_switch $2
fi
;;
-r|remove|uninstall) uninstall $2 ;;
-s|-u|switch|use) switch $2 ;;
* ) switch "${cmd}" ;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment