#!/usr/bin/env bash unset rvm_default_flag rvm_wrapper_name source "$rvm_path/scripts/base" usage() { printf " Usage: rvm alias [action] [arguments] Examples: rvm alias create [alias_name] [ruby] rvm alias delete [alias_name] rvm alias show [alias_name] rvm alias list " } alias_conflicts_with_ruby() { # If default exists, we should return true. [[ "$1" == "default" && ! -L "$rvm_path/rubies/default" ]] && return 1 # Open for suggestions to a better way of doing this... alias_check_result="$( \. \"$rvm_path/scripts/initialize\" \. \"$rvm_path/scripts/selector\" export rvm_ruby_string=\"$1\" __rvm_ruby_string > /dev/null 2>&1 echo "$?" )" if [[ "0" == "$alias_check_result" ]]; then "$rvm_path/scripts"/log "error" "\nYou have attempted to create an alias called '$1', which is recognized as a rvm ruby.\n" return 0 fi return 1 unset alias_check_result } alias_show() { local expanded_alias_name if [[ -z "$alias_name" ]]; then "$rvm_path/scripts"/log "error" "\nusage: 'rvm alias show [alias_name]'\n" result=1 return fi expanded_alias_name="$("$rvm_path/scripts"/db "$rvm_path/config/alias" "$alias_name")" if [[ -z "$expanded_alias_name" ]]; then "$rvm_path/scripts"/log "error" "\nUnknown alias name: '$alias_name'\n" result=1 else result=0 if [[ -n "$gemset_name" ]] ; then printf "${rvm_gemset_separator:-"@"}${rvm_gemset_separator:-"@"}${gemset_name}\n" else printf "$expanded_alias_name\n" fi fi } alias_delete() { echo "Deleting alias: $alias_name" for link in "$rvm_path/rubies/$alias_name" ; do if [[ -L "$link" ]] ; then rm -f $link ; fi done "$rvm_path/scripts"/db "$rvm_path/config/alias" "$alias_name" "delete" } alias_create() { alias_name="${alias_name:-""}" rvm_ruby_string="${rvm_ruby_string:-""}" if [[ "default" != "$alias_name" ]] && alias_conflicts_with_ruby "$alias_name"; then # Force it to an empty alias name to trigger the usage. alias_name="" fi if [[ -z "${rvm_environment_identifier:-""}" || -z "$alias_name" ]] ; then "$rvm_path/scripts"/log "error" \ "\nusage: 'rvm alias [alias_name] [ruby_string]'\n" result=1 else if [[ "default" = "$alias_name" ]] ; then rvm_alias="" fi if [[ -z "$rvm_alias" ]] ; then rvm_ruby_string="$rvm_environment_identifier" if [[ -z "$rvm_alias_expanded" ]]; then rvm_expanding_aliases=1 __rvm_become unset rvm_expanding_aliases else rvm_ruby_string="$rvm_environment_identifier" fi if [[ -z "$rvm_ruby_string" ]]; then "$rvm_path/scripts"/log "error" \ "\nUnknown ruby string '$rvm_ruby_string' specified\n" result=1 return $result fi final_environment_identifier="${rvm_environment_identifier:-$(__rvm_environment_identifier)}" "$rvm_path/scripts"/log "info" \ "Creating alias $alias_name for $final_environment_identifier." ln -nfs "$rvm_path/rubies/$rvm_ruby_string" "$rvm_path/rubies/$alias_name" "$rvm_path/scripts"/log "info" \ "Recording alias $alias_name for $final_environment_identifier." "$rvm_path/scripts"/db "$rvm_path/config/alias" "$alias_name" "$final_environment_identifier" else if [[ -d "$rvm_path/rubies/$alias_name" ]] ; then "$rvm_path/scripts"/log "error" \ "\n$rvm_path/rubies/$alias_name is taken and is *not* able to be an alias name.\n" result=1 else "$rvm_path/scripts"/log "error" \ "\n$rvm_path/rubies/$alias_name is already aliased.\n" result=1 fi fi fi } alias_list() { local item items items=($(cd "$rvm_path/rubies" ; find . -maxdepth 1 -mindepth 1 -type l | sed -e 's#./##')) for item in "${items[@]}" ; do echo "$(basename "$item") => $("$rvm_path/scripts"/db "$rvm_path/config/alias" "$(basename "$item")")" done } args=($*) action="${args[0]:-""}" alias_name="${args[1]:-""}" rvm_environment_identifier="${args[2]:-""}" args="$(echo ${args[@]:3}) " # Strip trailing / leading / extra spacing. result=0 if [[ ! -f "$rvm_path/config/alias" ]] ; then touch "$rvm_path/config/alias" ; fi if printf "$alias_name" | grep -q "${rvm_gemset_separator:-"@"}" ; then gemset_name="${alias_name/*${rvm_gemset_separator:-"@"}/}" alias_name="${alias_name/${rvm_gemset_separator:-"@"}*/}" else gemset_name="" fi if [[ -n "$alias_name" ]] ; then rvm_alias="$("$rvm_path/scripts/db" "$rvm_path/config/alias" "$alias_name")" fi if [[ "$action" = "delete" ]] ; then alias_delete elif [[ "$action" = "create" ]] ; then alias_create elif [[ "$action" = "list" ]] ; then alias_list elif [[ "$action" = "show" ]]; then alias_show elif [[ "$action" = "help" ]]; then usage else usage exit 1 fi exit $?