env 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #!/usr/bin/env bash
  2. #
  3. # Environment manipulation functions.
  4. #
  5. __rvm_nuke_rvm_variables()
  6. {
  7. unset rvm_head_flag $(env | __rvm_awk -F= '/^rvm_/{print $1" "}')
  8. }
  9. # Unset ruby-specific variables
  10. __rvm_unset_ruby_variables()
  11. {
  12. # unset rvm_ruby_flag $(env | __rvm_awk -F= '/^rvm_ruby_/{printf $1" "}')
  13. unset rvm_env_string rvm_ruby_string rvm_ruby_strings rvm_ruby_binary rvm_ruby_gem_home rvm_ruby_gem_path rvm_ruby_home rvm_ruby_interpreter rvm_ruby_irbrc rvm_ruby_log_path rvm_ruby_major_version rvm_ruby_minor_version rvm_ruby_package_name rvm_ruby_patch_level rvm_ruby_release_version rvm_ruby_repo_url rvm_ruby_repo_branch rvm_ruby_revision rvm_ruby_selected_flag rvm_ruby_tag rvm_ruby_version rvm_head_flag rvm_ruby_package_file rvm_ruby_configure rvm_ruby_name rvm_ruby_url rvm_ruby_global_gems_path rvm_ruby_args rvm_ruby_name rvm_llvm_flag rvm_ruby_repo_tag
  14. __rvm_load_rvmrc # restore important variables
  15. }
  16. # TODO: Should be able to...
  17. # Unset both rvm variables as well as ruby-specific variables
  18. # Preserve gemset if 'rvm_sticky' is set
  19. # (persist gemset unless clear is explicitly called).
  20. __rvm_cleanse_variables()
  21. {
  22. __rvm_unset_ruby_variables
  23. if [[ ${rvm_sticky_flag:-0} -eq 1 ]]
  24. then export rvm_gemset_name
  25. else unset rvm_gemset_name
  26. fi
  27. # arrays
  28. unset rvm_configure_flags rvm_patch_names rvm_make_flags
  29. # variables
  30. unset rvm_env_string rvm_ruby_string rvm_action rvm_error_message rvm_force_flag rvm_debug_flag rvm_delete_flag rvm_summary_flag rvm_json_flag rvm_yaml_flag rvm_file_name rvm_user_flag rvm_system_flag rvm_install_flag rvm_llvm_flag rvm_sticky_flag rvm_rvmrc_flag rvm_gems_flag rvm_docs_flag rvm_ruby_alias rvm_static_flag rvm_archive_extension rvm_hook rvm_ruby_name rvm_remote_flag
  31. # rvm_gemsets_path rvm_user_path rvm_wrappers_path rvm_patches_path rvm_docs_path rvm_examples_path rvm_rubies_path rvm_usr_path rvm_src_path rvm_tmp_path rvm_lib_path rvm_repos_path rvm_log_path rvm_help_path rvm_environments_path rvm_archives_path
  32. __rvm_load_rvmrc # restore important variables
  33. }
  34. # Add bin path if not present
  35. __rvm_conditionally_add_bin_path()
  36. {
  37. [[ ":${PATH}:" == *":${rvm_bin_path}:"* ]] ||
  38. {
  39. if [[ "${rvm_ruby_string:-"system"}" == "system" && -z "$GEM_HOME" ]]
  40. then PATH="$PATH:${rvm_bin_path}"
  41. else PATH="${rvm_bin_path}:$PATH"
  42. fi
  43. }
  44. }
  45. __rvm_load_environment()
  46. {
  47. \typeset __hook
  48. if
  49. [[ -f "$rvm_environments_path/$1" ]]
  50. then
  51. # Restore the environment
  52. unset GEM_HOME GEM_PATH
  53. __rvm_remove_rvm_from_path ; __rvm_conditionally_add_bin_path
  54. # source the environment file
  55. \. "$rvm_environments_path/$1"
  56. rvm_hook="after_use"
  57. if [[ -n "${rvm_scripts_path:-}" || -n "${rvm_path:-}" ]]
  58. then source "${rvm_scripts_path:-$rvm_path/scripts}/hook"
  59. fi
  60. # clear the PATH cache
  61. builtin hash -r
  62. else
  63. __rvm_use "$1"
  64. fi
  65. }
  66. __rvm_export()
  67. {
  68. # extract the variable name from the first arg.
  69. \typeset name
  70. name=${1%%\=*}
  71. # store the current value, to be restored later.
  72. builtin export rvm_old_$name=${!name}
  73. # pass-through the return value of the builtin.
  74. export "$@"
  75. return $?
  76. }
  77. __rvm_unset_exports()
  78. {
  79. \typeset wrap_name name value
  80. \typeset -a __variables_list
  81. __rvm_read_lines __variables_list <<< "$(
  82. printenv | __rvm_sed '/^rvm_old_.*=/ { s/=.*$//; p; }; d;'
  83. )"
  84. for wrap_name in "${__variables_list[@]}"
  85. do
  86. eval "value=\"\${${wrap_name}}\""
  87. name=${wrap_name#rvm_old_}
  88. if [[ -n "${value:-}" ]]
  89. then export $name="${value}"
  90. else unset $name
  91. fi
  92. unset $wrap_name
  93. done
  94. }
  95. __rvm_fix_path_from_gem_path()
  96. {
  97. [[ -n "${GEM_PATH:-}" ]] || return 0
  98. export PATH
  99. \typeset IFS _iterator_path
  100. \typeset -a _gem_path _new_path
  101. IFS=:
  102. _gem_path=()
  103. _new_path=()
  104. __rvm_custom_separated_array _gem_path : "${GEM_PATH}"
  105. for _iterator_path in "${_gem_path[@]}"
  106. do
  107. _new_path+=( "${_iterator_path}/bin" )
  108. done
  109. _new_path+=( "${MY_RUBY_HOME:-${GEM_HOME/gems/rubies}}/bin" )
  110. _new_path+=( "${rvm_bin_path}" )
  111. PATH="${_new_path[*]}:$PATH"
  112. builtin hash -r
  113. }
  114. # Clean all rvm items out of the current working path.
  115. __rvm_remove_rvm_from_path()
  116. {
  117. \typeset local_rvm_path
  118. __rvm_remove_from_path "${rvm_path%/}/*"
  119. __rvm_remove_from_path "${rvm_gems_path%/}/*"
  120. __rvm_remove_from_path "${rvm_bin_path}" #TODO: this might be dangerous if rvm is available in some common path
  121. while local_rvm_path="$( __rvm_which rvm 2>/dev/null )"
  122. do __rvm_remove_from_path "${local_rvm_path%/*}"
  123. done
  124. builtin hash -r
  125. }
  126. __rvm_switch()
  127. {
  128. \typeset new_rvm_path new_rvm_bin_path
  129. (( $# )) && [[ -z "$1" ]] && shift || true # skip first empty argument
  130. (( $# )) && [[ -n "$1" ]] && [[ -d "$1" || -d "${1%/*}" ]] && [[ ! -f "$1" ]] ||
  131. {
  132. rvm_error "No valid path given."
  133. return 1
  134. }
  135. [[ "${rvm_path}" != "${new_rvm_path}" ]] ||
  136. {
  137. rvm_warn "Already there!"
  138. return 2
  139. }
  140. rvm_log "Switching ${rvm_path} => ${1}"
  141. new_rvm_path="${1%/}"
  142. new_rvm_bin_path="${2:-$new_rvm_path/bin}"
  143. new_rvm_bin_path="${new_rvm_bin_path%/}"
  144. __rvm_use_system
  145. __rvm_remove_from_path "${rvm_path%/}/*"
  146. rvm_reload_flag=1
  147. rvm_path="${new_rvm_path}"
  148. rvm_bin_path="${new_rvm_bin_path}"
  149. rvm_scripts_path="${rvm_path}/scripts"
  150. rvm_environments_path="${rvm_path}/environments"
  151. __rvm_remove_from_path "${rvm_path%/}/*"
  152. __rvm_add_to_path prepend "${rvm_bin_path}"
  153. }
  154. __rvm_unload_action()
  155. {
  156. \typeset _element IFS
  157. \typeset -a _list
  158. IFS=$'\n'
  159. _list=( $( \command \cat ${2:--} | sort -u ) )
  160. for _element in "${_list[@]}"
  161. do $1 "${_element}"
  162. done
  163. }
  164. __function_unset()
  165. {
  166. if [[ -n "${ZSH_VERSION:-}" ]]
  167. then unset -f "$1"
  168. else unset "$1"
  169. fi
  170. }
  171. __rvm_unload()
  172. {
  173. \typeset _element
  174. \typeset -a _list
  175. #PATH
  176. __rvm_remove_rvm_from_path
  177. # fpath
  178. if [[ -n "${ZSH_VERSION:-}" ]]
  179. then
  180. __rvm_remove_from_array fpath "$rvm_path/scripts/extras/completion.zsh" "${fpath[@]}"
  181. fi
  182. # aliases
  183. __rvm_unload_action unalias <<< "$(
  184. if [[ -n "${ZSH_VERSION:-}" ]]
  185. then alias | __rvm_awk -F"=" '/rvm/ {print $1}'
  186. else alias | __rvm_awk -F"[= ]" '/rvm/ {print $2}'
  187. fi
  188. )"
  189. # variables
  190. __rvm_unload_action unset <<< "$(
  191. set |
  192. __rvm_awk -F"=" 'BEGIN{v=0;} /^[a-zA-Z_][a-zA-Z0-9_]*=/{v=1;} v==1&&$2~/^['\''\$]/{v=2;}
  193. v==1&&$2~/^\(/{v=3;} v==2&&/'\''$/&&!/'\'\''$/{v=1;} v==3&&/\)$/{v=1;} v{print;} v==1{v=0;}' |
  194. __rvm_awk -F"=" '/^[^ ]*(RUBY|GEM|IRB|gem|rubies|rvm)[^ ]*=/ {print $1} /^[^ ]*=.*rvm/ {print $1}' |
  195. __rvm_grep -vE "^PROMPT|^prompt|^PS|^BASH_SOURCE|^PATH"
  196. )"
  197. # functions
  198. __rvm_unload_action __function_unset <<< "$(
  199. \typeset -f | __rvm_awk '$2=="()" {fun=$1} /rvm/{print fun}' | sort -u | __rvm_grep -v __rvm_unload_action
  200. )"
  201. if
  202. [[ -n "${ZSH_VERSION:-}" ]]
  203. then
  204. unset -f __rvm_unload_action
  205. unset -f __function_unset
  206. if
  207. [[ -n "${_comp_dumpfile:-}" ]]
  208. then
  209. \command \rm -f "$_comp_dumpfile"
  210. compinit -d "$_comp_dumpfile"
  211. fi
  212. else
  213. unset __rvm_unload_action __function_unset
  214. fi
  215. }
  216. __rvm_ruby_config_get()
  217. {
  218. \typeset variable_name ruby_path
  219. variable_name="$1"
  220. ruby_path="${2:-$rvm_ruby_home/bin/ruby}"
  221. # mruby doesn't support -r requires or RbConfig
  222. __rvm_string_match "$ruby_path" "*mruby*" && return
  223. case "${variable_name:---all}" in
  224. (--all)
  225. "$ruby_path" -rrbconfig -e 'puts RbConfig::CONFIG.sort.map{|k,v| "#{k}: #{v}" }' 2>/dev/null || return $?
  226. ;;
  227. (*)
  228. "$ruby_path" -rrbconfig -e 'puts RbConfig::CONFIG["'"$variable_name"'"]' 2>/dev/null || return $?
  229. ;;
  230. esac
  231. }
  232. __rvm_env_print()
  233. {
  234. environment_file_path="$rvm_environments_path/$(__rvm_env_string)"
  235. # Echo the path or environment file.
  236. if
  237. [[ "$rvm_path_flag" == "1" || "$*" == *"--path"* ]]
  238. then
  239. echo "$environment_file_path"
  240. elif
  241. [[ "$rvm_cron_flag" == "1" || "$*" == *"--cron"* ]]
  242. then
  243. \command \cat "$environment_file_path" |
  244. __rvm_grep -Eo "[^ ]+=[^;]+" |
  245. __rvm_sed -e 's/\$PATH/'"${PATH//\//\\/}"'/' -e 's/\${PATH}/'"${PATH//\//\\/}"'/'
  246. else
  247. \command \cat "$environment_file_path"
  248. fi
  249. }