alias 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env bash
  2. unset rvm_default_flag
  3. source "$rvm_scripts_path/base"
  4. source "$rvm_scripts_path/functions/alias"
  5. usage() {
  6. printf "%b" "
  7. Usage:
  8. rvm alias [action] [arguments]
  9. Examples:
  10. rvm alias create [alias_name] [ruby]
  11. rvm alias delete [alias_name]
  12. rvm alias show [alias_name]
  13. rvm alias list
  14. "
  15. }
  16. alias_create_execute()
  17. {
  18. for _path in "$rvm_rubies_path" "$rvm_gems_path" "$rvm_environments_path" "$rvm_wrappers_path"
  19. do
  20. if [[ -L "$_path/$alias_name" ]]
  21. then \command \rm -fv "$_path/$alias_name" || return $?
  22. fi
  23. \command \ln -fs "$_path/$rvm_ruby_string" "$_path/$alias_name" || return $?
  24. done
  25. __rvm_db_ "$rvm_path/config/alias" "$alias_name" "$rvm_ruby_string"
  26. }
  27. alias_create()
  28. {
  29. alias_name="${alias_name:-""}"
  30. rvm_ruby_string="$rvm_environment_identifier"
  31. rvm_expanding_aliases=1
  32. __rvm_become
  33. unset rvm_expanding_aliases
  34. if
  35. [[ -z "${rvm_environment_identifier:-""}" || -z "$rvm_ruby_string" || -z "$alias_name" ]]
  36. then
  37. rvm_error "usage: 'rvm alias [action] [alias_name] [ruby_string]'"
  38. return 1
  39. elif
  40. [[ ! -L "$rvm_rubies_path/$alias_name" && -d "$rvm_rubies_path/$alias_name" ]]
  41. then
  42. rvm_error "$alias_name is a full ruby name and can not be used as alias name."
  43. return 2
  44. else
  45. __rvm_log_command "alias_create" \
  46. "Creating alias $alias_name for $rvm_ruby_string" \
  47. alias_create_execute
  48. fi
  49. }
  50. alias_list_show()
  51. {
  52. __rvm_cd "$rvm_rubies_path"
  53. __rvm_find . -maxdepth 1 -mindepth 1 -type l | __rvm_sed -e 's#.*/##'
  54. }
  55. alias_list() {
  56. case "${alias_name}" in
  57. (names)
  58. alias_list_show # just show it
  59. ;;
  60. (*)
  61. \typeset -a items
  62. __rvm_read_lines items <(alias_list_show)
  63. \typeset item
  64. for item in "${items[@]}"
  65. do echo "$item => $(__rvm_db_ "$rvm_path/config/alias" "$item")"
  66. done
  67. ;;
  68. esac
  69. }
  70. alias_search_by_target() {
  71. \typeset item items target search
  72. search="${alias_name}@${gemset_name}"
  73. items=($(__rvm_cd "$rvm_rubies_path" ; __rvm_find . -maxdepth 1 -mindepth 1 -type l | __rvm_sed -e 's#./##'))
  74. for item in "${items[@]}"
  75. do
  76. target=$(__rvm_db_ "$rvm_path/config/alias" "$(basename "$item")")
  77. if [[ "${search}" == "${target}" ]]
  78. then
  79. echo "$(basename "$item")"
  80. fi
  81. done
  82. }
  83. args=($*)
  84. action="${args[0]:-""}"
  85. alias_name="${args[1]:-""}"
  86. rvm_environment_identifier="${args[2]:-""}"
  87. shift 3
  88. args="$@"
  89. result=0
  90. if
  91. [[ "$alias_name" == *"/"* ]]
  92. then
  93. printf "%b" "Can not use path separator '/' in alias names."
  94. exit 1
  95. fi
  96. [[ -f "$rvm_path/config/alias" ]] || touch "$rvm_path/config/alias"
  97. if
  98. [[ "$alias_name" == *"${rvm_gemset_separator:-"@"}"* ]]
  99. then
  100. gemset_name="${alias_name/*${rvm_gemset_separator:-"@"}/}"
  101. alias_name="${alias_name/${rvm_gemset_separator:-"@"}*/}"
  102. else
  103. gemset_name=""
  104. fi
  105. case "$action" in
  106. delete|create|list|show|search_by_target)
  107. alias_${action}
  108. ;;
  109. help|usage)
  110. usage
  111. ;;
  112. *)
  113. usage
  114. exit 1
  115. ;;
  116. esac
  117. exit $?