#!/usr/bin/env bash unset rvm_default_flag rvm_wrapper_name source "$rvm_scripts_path"/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_rubies_path/default" ]] && return 1 # Open for suggestions to a better way of doing this... alias_check_result="$( \. \"$rvm_scripts_path/initialize\" \. \"$rvm_scripts_path/selector\" export rvm_ruby_string=\"$1\" __rvm_ruby_string > /dev/null 2>&1 echo "$?" )" if [[ "0" == "$alias_check_result" ]]; then "$rvm_scripts_path"/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() { if [[ -z "$alias_name" ]]; then "$rvm_scripts_path"/log "error" "\nusage: 'rvm alias show [alias_name]'\n" result=1 return fi expanded_alias_name="$("$rvm_scripts_path"/db "$rvm_config_path/alias" "$alias_name")" if [[ -z "$expanded_alias_name" ]]; then "$rvm_scripts_path"/log "error" "\nUnknown alias name: '$alias_name'\n" result=1 else result=0 if [[ -n "$gemset_name" ]] ; then printf "${expanded_alias_name}${rvm_gemset_separator}${gemset_name}\n" else printf "$expanded_alias_name\n" fi fi unset expanded_alias_name } alias_delete() { echo "Deleting alias: $alias_name" for link in "$rvm_rubies_path/$alias_name" ; do if [[ -L "$link" ]] ; then rm -f $link ; fi done "$rvm_scripts_path"/db "$rvm_config_path/alias" "$alias_name" "delete" } alias_create() { rvm_environment_identifier="${rvm_environment_identifier:-""}" alias_name="${alias_name:-""}" rvm_ruby_string="${rvm_ruby_string:-""}" if 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_scripts_path"/log "error" "\nusage: 'rvm alias [alias_name] [ruby_string]'\n" result=1 else 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_scripts_path"/log "error" "\nUnknown ruby string '$rvm_ruby_string' specified\n" result=1 return fi final_environment_identifier="$(__rvm_environment_identifier)" "$rvm_scripts_path"/log "info" "Creating alias $alias_name for $final_environment_identifier." ln -nfs "$rvm_rubies_path/$rvm_ruby_string" "$rvm_rubies_path/$alias_name" "$rvm_scripts_path"/log "info" "Recording alias $alias_name for $final_environment_identifier." "$rvm_scripts_path"/db "$rvm_config_path/alias" "$alias_name" "$final_environment_identifier" else if [[ -d "$rvm_rubies_path/$alias_name" ]] ; then "$rvm_scripts_path"/log "error" "\n$rvm_rubies_path/$alias_name is taken and is *not* able to be an alias name.\n" result=1 else "$rvm_scripts_path"/log "error" "\n$rvm_rubies_path/$alias_name is already aliased.\n" result=1 fi fi fi } alias_list() { for item in $rvm_rubies_path/* ; do if [[ -L "$item" ]] ; then echo "$(basename "$item") => $("$rvm_scripts_path"/db "$rvm_config_path/alias" "$(basename "$item")")" fi done; unset item } 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_config_path/alias" ]] ; then touch "$rvm_config_path/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 [[ ! -z "$alias_name" ]] ; then rvm_alias="$("$rvm_scripts_path"/db "$rvm_config_path/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 $?