# Copyright 1999-2009 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Id$ DESCRIPTION="Manage Ruby on Rails versions" MAINTAINER="graaff@gentoo.org" SVN_DATE='$Date: $' VERSION=$(svn_date_to_version "${SVN_DATE}" ) RAILS="${EROOT}/usr/bin/rails" # find a list of rails symlink targets, best first find_targets() { for f in \ ${RAILS}-4.0* \ ${RAILS}-3.2* \ ${RAILS}-3.1* \ ${RAILS}-3.0* \ ${RAILS}-2.3* \ ; do if [[ -f ${f} ]] ; then echo $(basename ${f} ) fi done } # try to remove the rails symlink remove_symlink() { rm "${RAILS}" &>/dev/null } # determine the version get_version() { local basename="$(basename $(canonicalise ${RAILS} ) )" local version=${basename#*-} echo ${version} } # set the rails symlink set_symlink() { target=${1} if is_number "${target}" && [[ ${target} -ge 1 ]] ; then targets=( $(find_targets ) ) target=${targets[$(( ${target} - 1 ))]} fi if [[ -f "${ROOT}/usr/bin/${target}" ]] ; then remove_symlink ln -s "${ROOT}/usr/bin/${target}" "${RAILS}" || \ die "Couldn't set ${target} symlink" else die -q "Target \"${1}\" doesn't appear to be valid!" fi } set_symlink_default() { set_symlink 1 } set_symlink_by_slot() { slot=${1} local target=$(ls ${RAILS}-${slot}* 2>/dev/null) if [[ -n ${target} ]]; then remove_symlink ln -s ${target} ${RAILS} else die -q "No providers available for slot ${slot}" fi } ### show action ### describe_show() { echo "Manage Ruby on Rails versions" } do_show() { if [[ ${#} -gt 0 ]]; then die -q "Usage error. No parameters allowed" fi write_list_start "Current Ruby on Rails version:" if [[ -L "${RAILS}" ]] ; then write_kv_list_entry "$(basename $(canonicalise ${RAILS} ) )" "" elif [[ -e "${RAILS}" ]] ; then write_kv_list_entry "(not a symlink)" "" else write_kv_list_entry "(unset)" "" fi } ### list action ### describe_list() { echo "List available Ruby on Rails versions" } do_list() { if [[ ${#} -gt 0 ]]; then die -q "Usage error: no parameters allowed" fi local i targets targets=( $(find_targets) ) for (( i = 0; i < ${#targets[@]}; i++ )); do [[ ${targets[i]} = \ $(basename "$(canonicalise "${ROOT}/usr/bin/rails")") ]] \ && targets[i]=$(highlight_marker "${targets[i]}") done write_list_start "Available Ruby on Rails versions:" write_numbered_list -m "(none found)" "${targets[@]}" } ### set action ### describe_set() { echo "Set a new Ruby on Rails version" } describe_set_options() { echo "target : Target name or number (from 'list' action)" } describe_set_parameters() { echo "" } do_set() { if [[ $# -gt 1 ]]; then die -q "Too many parameters. Expected only 1." fi local version=${1} if [[ -z ${version} ]] ; then die -q "You didn't give me a version name" elif [[ -L "${RAILS}" ]] ; then if ! remove_symlink ; then die -q "Can't remove existing rails binary" elif ! set_symlink "${1}" ; then die -q "Can't set new rails binary" fi elif [[ -e "${RAILS}" ]] ; then write_warning_msg "Can't set a new rails provider. There's a file in the way at ${RAILS}. You can try removing it manually, and then re-running this command." else set_symlink ${version} || die -q "Wasn't able to set a new provider" fi } ### update action ### describe_update() { echo "Updates the rails symlink to the latest version available" } do_update() { # Only set a symlink if there are any version of rails present local target=1 local targets=( $(find_targets ) ) if [[ -z ${targets[@]} ]] ; then remove_symlink fi target=${targets[$(( ${target} - 1 ))]} if [[ -f "${ROOT}/usr/bin/${target}" ]] ; then set_symlink_default fi do_show } # vim: set ft=eselect :