rvm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #!/usr/bin/env bash
  2. # rvm : Ruby enVironment Manager
  3. # https://rvm.io
  4. # https://github.com/rvm/rvm
  5. # partial duplication marker dkjnkjvnckbjncvbkjnvkj
  6. # prevent from loading in sh shells
  7. if
  8. builtin test -n "${BASH_VERSION:-}" -o -n "${ZSH_VERSION:-}" -o -n "${KSH_VERSION:-}"
  9. then
  10. case "`uname`" in
  11. (CYGWIN*|MINGW*)
  12. __shell_name="`\command \ps -p $$ | \command \awk 'END {print $NF}'` 2>/dev/null" ;;
  13. (SunOS)
  14. __shell_name="`\command \ps -p $$ -o comm=`" ;;
  15. (Linux)
  16. case "$(command uname -o)" in
  17. (Android)
  18. __shell_name="gawk 'BEGIN{RS=\"\"}; NR==1{print; exit}' /proc/$$/cmdline | tr - '\0'" ;;
  19. (*)
  20. __shell_name="`\command \ps -p $$ -o ucomm=`" ;;
  21. esac ;;
  22. (*)
  23. __shell_name="`\command \ps -p $$ -o ucomm=`" ;;
  24. esac
  25. case "$__shell_name" in
  26. (""|dash|sh|ksh|*/dash|*/sh|*/ksh) return 0 ;; # silently stop in sh shells
  27. esac
  28. unset __shell_name
  29. else
  30. return 0
  31. fi
  32. # also duplicated in scripts/base
  33. __rvm_has_opt()
  34. {
  35. if # pre-gnu
  36. [[ -n "${ZSH_VERSION:-}" ]]
  37. then
  38. setopt | GREP_OPTIONS="" \command \grep "^${1:-}$" >/dev/null 2>&1 || return $?
  39. elif # mksh
  40. [[ -n "${KSH_VERSION:-}" ]]
  41. then
  42. set +o | GREP_OPTIONS="" \command \grep "-o ${1:-}$" >/dev/null 2>&1 || return $?
  43. elif # bash
  44. [[ -n "${BASH_VERSION:-}" ]]
  45. then
  46. [[ ":${SHELLOPTS:-}:" == *":${1:-}:"* ]] || return $?
  47. else # what is this?!
  48. return 1
  49. fi
  50. }
  51. # Do not allow sourcing RVM in `sh` - it's not supported
  52. # return 0 to exit from sourcing this script without breaking sh
  53. if __rvm_has_opt "posix"
  54. then return 0
  55. fi
  56. # TODO: Alter the variable names to make sense
  57. \export HOME rvm_prefix rvm_user_install_flag rvm_path
  58. HOME="${HOME%%+(\/)}" # Remove trailing slashes if they exist on HOME
  59. [[ -n "${rvm_stored_umask:-}" ]] || export rvm_stored_umask=$(umask)
  60. if (( ${rvm_ignore_rvmrc:=0} == 0 ))
  61. then
  62. rvm_rvmrc_files=("/etc/rvmrc" "$HOME/.rvmrc")
  63. if [[ -n "${rvm_prefix:-}" ]] && ! [[ "$HOME/.rvmrc" -ef "${rvm_prefix}/.rvmrc" ]]
  64. then rvm_rvmrc_files+=( "${rvm_prefix}/.rvmrc" )
  65. fi
  66. for rvmrc in "${rvm_rvmrc_files[@]}"
  67. do
  68. if [[ -f "$rvmrc" ]]
  69. then
  70. # pre-gnu
  71. if GREP_OPTIONS="" \command \grep '^\s*rvm .*$' "$rvmrc" >/dev/null 2>&1
  72. then
  73. printf "%b" "
  74. Error:
  75. $rvmrc is for rvm settings only.
  76. rvm CLI may NOT be called from within $rvmrc.
  77. Skipping the loading of $rvmrc"
  78. return 1
  79. else
  80. source "$rvmrc"
  81. fi
  82. fi
  83. done
  84. unset rvm_rvmrc_files
  85. fi
  86. # duplication marker jdgkjnfnkjdngjkfnd4fd
  87. # detect rvm_path if not set
  88. if
  89. [[ -z "${rvm_path:-}" ]]
  90. then
  91. if
  92. [[ -n "${BASH_SOURCE:-$_}" && -f "${BASH_SOURCE:-$_}" ]]
  93. then
  94. rvm_path="${BASH_SOURCE:-$_}"
  95. rvm_path="$( \command \cd "${rvm_path%/scripts/rvm}">/dev/null; pwd )"
  96. rvm_prefix=$( dirname $rvm_path )
  97. elif
  98. [[ "${UID:-}" == "0" || "${USER:-}" == "root" ]]
  99. then
  100. if
  101. (( ${rvm_user_install_flag:-0} == 0 ))
  102. then
  103. rvm_prefix="/usr/local"
  104. rvm_path="${rvm_prefix}/rvm"
  105. else
  106. rvm_prefix="$HOME"
  107. rvm_path="${rvm_prefix}/.rvm"
  108. fi
  109. else
  110. if
  111. [[ -d "$HOME/.rvm" && -s "$HOME/.rvm/scripts/rvm" ]]
  112. then
  113. rvm_prefix="$HOME"
  114. rvm_path="${rvm_prefix}/.rvm"
  115. else
  116. rvm_prefix="/usr/local"
  117. rvm_path="${rvm_prefix}/rvm"
  118. fi
  119. fi
  120. else
  121. # remove trailing slashes, btw. %%/ <- does not work as expected
  122. rvm_path="${rvm_path%%+(\/)}"
  123. fi
  124. # guess rvm_prefix if not set
  125. if [[ -z "${rvm_prefix}" ]]
  126. then
  127. rvm_prefix=$( dirname $rvm_path )
  128. fi
  129. # duplication marker kkdfkgnjfndgjkndfjkgnkfjdgn
  130. [[ -n "${rvm_user_install_flag:-}" ]] ||
  131. case "$rvm_path" in
  132. (/usr/local/rvm) rvm_user_install_flag=0 ;;
  133. ($HOME/*|/${USER// /_}*) rvm_user_install_flag=1 ;;
  134. (*) rvm_user_install_flag=0 ;;
  135. esac
  136. export rvm_loaded_flag
  137. if [[ -n "${BASH_VERSION:-}" || -n "${ZSH_VERSION:-}" ]] &&
  138. \typeset -f rvm >/dev/null 2>&1
  139. then
  140. rvm_loaded_flag=1
  141. else
  142. rvm_loaded_flag=0
  143. fi
  144. if
  145. (( ${rvm_loaded_flag:=0} == 0 )) || (( ${rvm_reload_flag:=0} == 1 ))
  146. then
  147. if
  148. [[ -n "${rvm_path}" && -d "$rvm_path" ]]
  149. then
  150. true ${rvm_scripts_path:="$rvm_path/scripts"}
  151. if
  152. [[ ! -f "$rvm_scripts_path/base" ]]
  153. then
  154. printf "%b" "WARNING:
  155. Could not source '$rvm_scripts_path/base' as file does not exist.
  156. RVM will likely not work as expected.\n"
  157. elif
  158. ! source "$rvm_scripts_path/base"
  159. then
  160. printf "%b" "WARNING:
  161. Errors sourcing '$rvm_scripts_path/base'.
  162. RVM will likely not work as expected.\n"
  163. else
  164. __rvm_ensure_is_a_function
  165. __rvm_setup
  166. export rvm_version
  167. rvm_version="$(__rvm_version_installed)"
  168. alias rvm-restart="rvm_reload_flag=1 source '${rvm_scripts_path:-${rvm_path}/scripts}/rvm'"
  169. # Try to load RVM ruby if none loaded yet
  170. __path_to_ruby="$( builtin command -v ruby 2>/dev/null || true )"
  171. if
  172. [[ -z "${__path_to_ruby}" ]] ||
  173. [[ "${__path_to_ruby}" != "${rvm_path}"* ]] ||
  174. [[ "${__path_to_ruby}" == "${rvm_path}/bin/ruby" ]]
  175. then
  176. if
  177. [[ -n "${rvm_environments_path:-}" &&
  178. -s "${rvm_environments_path}/default"
  179. ]]
  180. then
  181. source "${rvm_environments_path}/default"
  182. elif
  183. [[ "${rvm_environments_path:-}" != "${rvm_path}/environments" &&
  184. -s "${rvm_path}/environments/default"
  185. ]]
  186. then
  187. source "${rvm_path}/environments/default"
  188. fi
  189. if
  190. [[ ${rvm_project_rvmrc:-1} -gt 0 ]] &&
  191. ! __function_on_stack __rvm_project_rvmrc
  192. then
  193. # Reload the rvmrc, use promptless ensuring shell processes does not
  194. # prompt if .rvmrc trust value is not stored, revert to default on fail
  195. if
  196. rvm_current_rvmrc=""
  197. rvm_project_rvmrc_default=2 rvm_promptless=1 __rvm_project_rvmrc
  198. then
  199. rvm_hook=after_cd
  200. source "${rvm_scripts_path:-${rvm_path}/scripts}/hook"
  201. fi
  202. fi
  203. elif
  204. [[ "${__path_to_ruby}" == "${rvm_path}"* ]] &&
  205. [[ -z "${GEM_HOME:-}" || -z "${GEM_PATH:-}" ]]
  206. then
  207. echo "
  208. Warning: PATH set to RVM ruby but GEM_HOME and/or GEM_PATH not set, see:
  209. https://github.com/rvm/rvm/issues/3212
  210. " >&2
  211. if
  212. [[ -n "${SUDO_USER:-}" ]]
  213. then
  214. echo "Hint: To fix PATH errors try using 'rvmsudo' instead of 'sudo', see:
  215. https://stackoverflow.com/questions/27784961/received-warning-message-path-set-to-rvm-after-updating-ruby-version-using-rvm/28080063#28080063
  216. " >&2
  217. fi
  218. fi
  219. unset __path_to_ruby
  220. # Makes sure rvm_bin_path is in PATH atleast once.
  221. [[ ":${PATH}:" == *":${rvm_bin_path}:"* ]] || PATH="$PATH:${rvm_bin_path}"
  222. if
  223. (( ${rvm_reload_flag:=0} == 1 ))
  224. then
  225. [[ "${rvm_auto_reload_flag:-0}" == 2 ]] || rvm_notify "RVM reloaded!"
  226. unset __rvm_project_rvmrc_lock
  227. fi
  228. rvm_loaded_flag=1
  229. __rvm_teardown
  230. # Opt-in for custom prompt through by setting:
  231. # rvm_ps1=1
  232. # in either /etc/rvmrc or $HOME/.rvmrc
  233. if
  234. [[ ${rvm_ps1:-0} -eq 1 ]]
  235. then
  236. # Source RVM ps1 functions for a great prompt.
  237. if
  238. [[ -s "$rvm_path/contrib/ps1_functions" ]]
  239. then
  240. source "$rvm_path/contrib/ps1_functions"
  241. elif
  242. [[ -s "/usr/local/rvm/contrib/ps1_functions" ]]
  243. then
  244. source "/usr/local/rvm/contrib/ps1_functions"
  245. fi
  246. if command -v ps1_set >/dev/null 2>&1
  247. then ps1_set
  248. fi
  249. fi
  250. fi
  251. else
  252. printf "%b" "\n\$rvm_path ($rvm_path) does not exist."
  253. fi
  254. unset rvm_prefix_needs_trailing_slash rvm_gems_cache_path rvm_gems_path rvm_project_rvmrc_default rvm_gemset_separator rvm_reload_flag
  255. fi