environment 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #!/usr/bin/env bash
  2. __rvm_env_string()
  3. {
  4. \typeset _string
  5. _string="${GEM_HOME:-}"
  6. _string="${_string##*/}"
  7. printf "%b" "${_string:-system}\n"
  8. }
  9. __rvm_expand_ruby_string()
  10. {
  11. \typeset string current_ruby
  12. string="$1"
  13. case "${string:-all}" in
  14. all)
  15. __rvm_list_strings | \command \tr ' ' "\n"
  16. ;;
  17. all-gemsets)
  18. __rvm_list_gemset_strings
  19. ;;
  20. default-with-rvmrc|rvmrc)
  21. "$rvm_scripts_path/tools" path-identifier "$PWD"
  22. ;;
  23. all-rubies|rubies)
  24. __rvm_list_strings
  25. ;;
  26. current-ruby|gemsets)
  27. current_ruby="$(__rvm_env_string)"
  28. current_ruby="${current_ruby%@*}"
  29. rvm_silence_logging=1 "$rvm_scripts_path/gemsets" list strings \
  30. | __rvm_sed "s/ (default)//; s/^/$current_ruby${rvm_gemset_separator:-@}/ ; s/@default// ;"
  31. ;;
  32. current)
  33. __rvm_env_string
  34. ;;
  35. aliases)
  36. __rvm_awk -F= '{print $string}' < "$rvm_path/config/alias"
  37. ;;
  38. *)
  39. __rvm_ruby_strings_exist $( echo "$string" | \command \tr "," "\n" | __rvm_strip )
  40. ;;
  41. esac
  42. }
  43. __rvm_become()
  44. {
  45. # set rvm_rvmrc_flag=0 to not create .rvmrc in random places of code
  46. \typeset string rvm_rvmrc_flag
  47. string="$1"
  48. rvm_rvmrc_flag=0
  49. [[ -n "$string" ]] && {
  50. rvm_ruby_string="$string"
  51. rvm_gemset_name=""
  52. }
  53. __rvm_use >/dev/null || return $?
  54. rvm_ruby_string="${rvm_ruby_string}${rvm_gemset_name:+${rvm_gemset_separator:-'@'}}${rvm_gemset_name:-}"
  55. return 0
  56. }
  57. __rvm_ensure_has_environment_files()
  58. {
  59. \typeset file_name variable value environment_id __path __gem_home
  60. __gem_home="${rvm_ruby_gem_home}"
  61. file_name="${__gem_home}/environment"
  62. __path=""
  63. if [[ "${__gem_home##*@}" != "global" ]]
  64. then __path+="${__gem_home}/bin:"
  65. fi
  66. __path+="${rvm_ruby_global_gems_path}/bin:${rvm_ruby_home}/bin"
  67. \command \rm -f "$file_name"
  68. \command \mkdir -p "${__gem_home}/wrappers" "${rvm_environments_path}" "${rvm_wrappers_path}"
  69. printf "%b" "export PATH=\"${__path}:\$PATH\"\n" > "$file_name"
  70. for variable in GEM_HOME GEM_PATH MY_RUBY_HOME IRBRC MAGLEV_HOME RBXOPT RUBY_VERSION
  71. do
  72. eval "value=\${${variable}:-""}"
  73. if [[ -n "$value" ]]
  74. then printf "export %b='%b'\n" "${variable}" "${value}" >> "$file_name"
  75. else printf "unset %b\n" "${variable}" >> "$file_name"
  76. fi
  77. done
  78. environment_id="${__gem_home##*/}"
  79. [[ -L "${rvm_environments_path}/${environment_id}" ]] ||
  80. {
  81. if [[ -f "${rvm_environments_path}/${environment_id}" ]]
  82. then rm -rf "${rvm_environments_path}/${environment_id}"
  83. fi
  84. ln -nfs "${__gem_home}/environment" "${rvm_environments_path}/${environment_id}"
  85. }
  86. ln -nfs "${__gem_home}/wrappers" "$rvm_wrappers_path/${environment_id}"
  87. return 0
  88. }
  89. # Runs a command in a given env.
  90. __rvm_with()
  91. (
  92. unset rvm_rvmrc_flag
  93. export rvm_create_flag=1
  94. export rvm_delete_flag=0
  95. export rvm_internal_use_flag=1
  96. export rvm_use_flag=0
  97. __rvm_use "$1" || return $?
  98. shift
  99. "$@" || return $?
  100. )
  101. __variables_definition()
  102. {
  103. \typeset -a __variables_list __array_list
  104. \typeset __method
  105. __method="$1"
  106. # NOTE: Removing rvm_bin_path here causes system wide installations to generate
  107. # a corrupt PATH, breaking the RVM installation.
  108. __variables_list=(
  109. rvm_head_flag rvm_ruby_selected_flag rvm_user_install_flag rvm_path_flag rvm_cron_flag
  110. rvm_static_flag rvm_default_flag rvm_loaded_flag rvm_llvm_flag rvm_skip_autoreconf_flag rvm_dynamic_extensions_flag
  111. rvm_18_flag rvm_19_flag rvm_20_flag rvm_21_flag
  112. rvm_force_autoconf_flag rvm_dump_environment_flag rvm_curl_flags rvm_rubygems_version
  113. rvm_verbose_flag rvm_debug_flag rvm_trace_flag __array_start rvm_skip_pristine_flag
  114. rvm_create_flag rvm_remove_flag rvm_movable_flag rvm_archive_flag
  115. rvm_gemdir_flag rvm_reload_flag rvm_auto_reload_flag rvm_disable_binary_flag
  116. rvm_ignore_gemsets_flag rvm_skip_gemsets_flag rvm_install_on_use_flag rvm_remote_flag
  117. rvm_verify_downloads_flag rvm_skip_openssl_flag rvm_gems_cache_path rvm_gems_path
  118. rvm_man_path rvm_ruby_gem_path rvm_ruby_log_path rvm_gems_cache_path rvm_archives_path
  119. rvm_docs_path rvm_environments_path rvm_examples_path rvm_gems_path rvm_gemsets_path
  120. rvm_help_path rvm_hooks_path rvm_lib_path rvm_log_path rvm_patches_path rvm_repos_path
  121. rvm_rubies_path rvm_scripts_path rvm_src_path rvm_tmp_path rvm_user_path rvm_usr_path
  122. rvm_wrappers_path rvm_stored_errexit rvm_ruby_strings rvm_ruby_binary
  123. rvm_ruby_gem_home rvm_ruby_home rvm_ruby_interpreter rvm_ruby_irbrc rvm_ruby_major_version
  124. rvm_ruby_minor_version rvm_ruby_package_name rvm_ruby_patch_level rvm_ruby_release_version
  125. rvm_ruby_repo_url rvm_ruby_repo_branch rvm_ruby_revision rvm_ruby_tag rvm_ruby_sha rvm_ruby_repo_tag
  126. rvm_ruby_version rvm_ruby_package_file rvm_ruby_name rvm_ruby_name rvm_ruby_args
  127. rvm_ruby_user_tag rvm_ruby_patch detected_rvm_ruby_name __rvm_env_loaded next_token
  128. rvm_error_message rvm_gemset_name rvm_parse_break rvm_token rvm_action rvm_export_args
  129. rvm_gemset_separator rvm_expanding_aliases rvm_tar_command rvm_tar_options
  130. rvm_patch_original_pwd rvm_project_rvmrc rvm_archive_extension rvm_autoinstall_bundler_flag
  131. rvm_codesign_identity rvm_expected_gemset_name
  132. rvm_without_gems rvm_with_gems rvm_with_default_gems
  133. rvm_ignore_dotfiles_flag rvm_fuzzy_flag rvm_autolibs_flag
  134. rvm_autolibs_flag_number rvm_autolibs_flag_runner rvm_quiet_curl_flag rvm_max_time_flag
  135. rvm_error_clr rvm_warn_clr rvm_debug_clr rvm_notify_clr rvm_code_clr rvm_comment_clr rvm_reset_clr
  136. rvm_error_color rvm_warn_color rvm_debug_color rvm_notify_color rvm_code_color rvm_comment_color rvm_reset_color
  137. rvm_log_timestamp rvm_log_filesystem rvm_log_namelen rvm_show_log_lines_on_error
  138. )
  139. __array_list=(
  140. rvm_patch_names rvm_ree_options rvm_autoconf_flags rvm_architectures
  141. )
  142. case "${__method}" in
  143. (export) true ;;
  144. (unset) unset "${__array_list[@]}" || true ;;
  145. (*)
  146. rvm_error "Unknown action given to __variables_definition: ${__method}"
  147. return 1
  148. ;;
  149. esac
  150. ${__method} "${__variables_list[@]}" || true
  151. # prevent errors with bash "set -a", see https://github.com/rvm/rvm/issues/2872
  152. if [[ -n "${BASH_VERSION:-}" ]]
  153. then export -fn __rvm_select_version_variables __rvm_ruby_string_parse_ __rvm_rm_rf_verbose __rvm_parse_args __rvm_ruby_string_find __rvm_file_load_env __rvm_remove_without_gems 2>/dev/null || true
  154. fi
  155. }
  156. # Set shell options that RVM needs temporarily, these are reverted by __rvm_teardown.
  157. # see the top of ./scripts/initialize for settings that are needed all the time.
  158. # Setup must be always called after initialize, otherwise it does nothing ... except exporting.
  159. __rvm_setup()
  160. {
  161. __variables_definition export
  162. # Setup only on first load.
  163. if (( __rvm_env_loaded != 1 ))
  164. then return 0
  165. fi
  166. if
  167. [[ -n "${BASH_VERSION:-}" ]] && ! __function_on_stack cd pushd popd
  168. then
  169. export rvm_shell_nounset
  170. if __rvm_has_opt "nounset"
  171. then rvm_bash_nounset=1
  172. else rvm_bash_nounset=0
  173. fi
  174. set +o nounset
  175. _rvm_old_traps=$( trap | __rvm_grep -E 'EXIT|HUP|INT|QUIT|TERM' || true )
  176. trap '__rvm_teardown_final ; set +x' EXIT HUP INT QUIT TERM
  177. fi
  178. if [[ -n "${ZSH_VERSION:-}" ]]
  179. then
  180. export rvm_zsh_clobber rvm_zsh_nomatch
  181. # Set clobber for zsh users, for compatibility with bash's append operator ( >> file ) behavior
  182. if setopt | __rvm_grep -s '^noclobber$' >/dev/null 2>&1
  183. then rvm_zsh_clobber=0
  184. else rvm_zsh_clobber=1
  185. fi
  186. setopt clobber
  187. # Set no_nomatch so globs that don't match any files don't print out a warning
  188. if setopt | __rvm_grep -s '^nonomatch$' >/dev/null 2>&1
  189. then rvm_zsh_nomatch=0
  190. else rvm_zsh_nomatch=1
  191. fi
  192. setopt no_nomatch
  193. fi
  194. }
  195. __rvm_teardown()
  196. {
  197. if builtin command -v __rvm_cleanup_tmp >/dev/null 2>&1
  198. then __rvm_cleanup_tmp
  199. fi
  200. export __rvm_env_loaded
  201. # if __rvm_env_loaded is not set - detect it via rvm_tmp_path
  202. : __rvm_env_loaded:${__rvm_env_loaded:=${rvm_tmp_path:+1}}:
  203. # if not loaded then fallback to 0
  204. : __rvm_env_loaded:${__rvm_env_loaded:=0}:
  205. # decrease load count counter
  206. : __rvm_env_loaded:$(( __rvm_env_loaded-=1 )):
  207. # skip teardown when already done or when not yet finished
  208. if [[ -z "${rvm_tmp_path:-}" ]] || (( __rvm_env_loaded > 0 ))
  209. then return 0
  210. fi
  211. if
  212. [[ -n "${BASH_VERSION:-}" ]]
  213. then
  214. trap - EXIT HUP INT QUIT TERM # Clear all traps, we do not want to go into an loop.
  215. if
  216. [[ -n "${_rvm_old_traps:-}" ]]
  217. then
  218. eval "${_rvm_old_traps}"
  219. fi
  220. (( rvm_bash_nounset == 1 )) && set -o nounset
  221. unset rvm_bash_nounset
  222. fi
  223. if
  224. [[ -n "${ZSH_VERSION:-""}" ]]
  225. then
  226. # If rvm_zsh_clobber is 0 then "setopt" contained "noclobber" before rvm performed "setopt clobber".
  227. (( rvm_zsh_clobber == 0 )) && setopt noclobber
  228. # If rvm_zsh_nomatch is 0 then "setopt" contained "nonomatch" before rvm performed "setopt nonomatch".
  229. (( rvm_zsh_nomatch == 0 )) || setopt nomatch
  230. unset rvm_zsh_clobber rvm_zsh_nomatch
  231. fi
  232. if
  233. [[ -n "${rvm_stored_umask:-}" ]]
  234. then
  235. umask ${rvm_stored_umask}
  236. unset rvm_stored_umask
  237. fi
  238. if builtin command -v __rvm_cleanup_download >/dev/null 2>&1
  239. then __rvm_cleanup_download
  240. fi
  241. if [[ "${rvm_stored_errexit:-""}" == "1" ]]
  242. then set -e
  243. fi
  244. __variables_definition unset
  245. unset _system_arch _system_name _system_type _system_version
  246. return 0
  247. }
  248. __rvm_teardown_if_broken()
  249. {
  250. if
  251. __function_on_stack __rvm_load_project_config ||
  252. __function_on_stack __rvm_with
  253. then
  254. true # inside of loading .rvmrc / __rvm_with
  255. elif
  256. (( ${__rvm_env_loaded:-0} > 0 ))
  257. then
  258. __rvm_teardown_final
  259. fi
  260. }
  261. __rvm_teardown_final()
  262. {
  263. __rvm_env_loaded=1
  264. unset __rvm_project_rvmrc_lock
  265. __rvm_teardown
  266. }
  267. __rvm_do_with_env_before()
  268. {
  269. if [[ -n "${rvm_scripts_path:-}" || -n "${rvm_path:-}" ]]
  270. then
  271. # Load env - setup all required variables, __rvm_teardown is called on the end
  272. source "${rvm_scripts_path:-"$rvm_path/scripts"}/initialize"
  273. __rvm_setup
  274. fi
  275. }
  276. __rvm_do_with_env_after()
  277. {
  278. __rvm_teardown
  279. }
  280. __rvm_do_with_env()
  281. {
  282. \typeset result
  283. __rvm_do_with_env_before
  284. "$@"
  285. result=$?
  286. __rvm_do_with_env_after
  287. return ${result:-0}
  288. }
  289. __rvm_conditionally_do_with_env()
  290. {
  291. if (( __rvm_env_loaded > 0 ))
  292. then
  293. "$@"
  294. else
  295. __rvm_do_with_env "$@"
  296. fi
  297. }
  298. __rvm_ensure_is_a_function()
  299. {
  300. if [[ ${rvm_reload_flag:=0} == 1 ]] || ! is_a_function rvm
  301. then
  302. for script in functions/version functions/selector cd functions/cli cli override_gem
  303. do
  304. if [[ -f "$rvm_scripts_path/$script" ]]
  305. then
  306. source "$rvm_scripts_path/$script"
  307. else
  308. printf "%b" \
  309. "WARNING:
  310. Could not source '$rvm_scripts_path/$script' as file does not exist.
  311. RVM will likely not work as expected.\n"
  312. fi
  313. done
  314. fi
  315. }
  316. __rvm_call_with_restored_umask()
  317. {
  318. rvm_umask="$(umask)"
  319. if [[ -n "${rvm_stored_umask:-}" ]];
  320. then
  321. umask ${rvm_stored_umask}
  322. fi
  323. "$@"
  324. umask "${rvm_umask}"
  325. unset rvm_umask
  326. }