rvmrc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env bash
  2. source "${rvm_scripts_path}/functions/rvmrc_env"
  3. source "${rvm_scripts_path}/functions/rvmrc_project"
  4. source "${rvm_scripts_path}/functions/rvmrc_set"
  5. source "${rvm_scripts_path}/functions/rvmrc_to"
  6. source "${rvm_scripts_path}/functions/rvmrc_trust"
  7. source "${rvm_scripts_path}/functions/rvmrc_warning"
  8. __rvm_rvmrc_match_all() [[ "${1:-}" == "all" || "${1:-}" == "all.rvmrcs" || "${1:-}" == "allGemfiles" ]]
  9. __rvm_rvmrc_tools()
  10. {
  11. \typeset rvmrc_action rvmrc_warning_action rvmrc_path saveIFS trust rvmrc_ruby
  12. rvmrc_action="$1"
  13. (( $# )) && shift || true
  14. if
  15. [[ "${rvmrc_action}" == "warning" ]]
  16. then
  17. rvmrc_warning_action="${1:-}"
  18. (( $# )) && shift || true
  19. fi
  20. if
  21. [[ "${rvmrc_action}" == "create" ]]
  22. then
  23. rvmrc_ruby="${1:-${GEM_HOME##*/}}"
  24. rvmrc_path="$(__rvm_cd "$PWD" >/dev/null 2>&1; pwd)/${2:-.rvmrc}"
  25. elif
  26. [[ "$1" == ".rvmrc" ]]
  27. then
  28. rvmrc_path="$PWD/.rvmrc"
  29. elif
  30. [[ "${rvmrc_action}" == "to" || "${rvmrc_action}" == "warning" ]] ||
  31. [[ -n "${1:-}" ]]
  32. then
  33. rvmrc_path="$1"
  34. else
  35. rvmrc_path="$PWD/.rvmrc"
  36. fi
  37. if
  38. [[ "${rvmrc_action}" == "to" ||
  39. "${rvmrc_action}" == "warning" ||
  40. "${rvmrc_action}" == "create"
  41. ]]||
  42. __rvm_rvmrc_match_all "${rvmrc_path:-}" # ignore all*
  43. then
  44. true
  45. else
  46. __rvm_project_dir_check "${rvmrc_path}" rvmrc_path "${rvmrc_path}/.rvmrc"
  47. fi
  48. rvmrc_path="${rvmrc_path//\/\///}"
  49. rvmrc_path="${rvmrc_path%/}"
  50. case "$rvmrc_action" in
  51. warning)
  52. __rvmrc_warning "${rvmrc_warning_action:-}" "$rvmrc_path" || return $?
  53. ;;
  54. to)
  55. __rvm_rvmrc_to "$rvmrc_path" || return $?
  56. ;;
  57. create)
  58. (
  59. rvm_create_flag=1 __rvm_use "${rvmrc_ruby}"
  60. case "${rvmrc_path}" in
  61. (*/.rvmrc|*/--rvmrc) __rvm_set_rvmrc ;;
  62. (*/.ruby-version|*/--ruby-version) __rvm_set_ruby_version ;;
  63. (*/.versions.conf|*/--versions-conf) __rvm_set_versions_conf ;;
  64. (*)
  65. rvm_error "Unrecognized project file format."
  66. return 1
  67. ;;
  68. esac
  69. )
  70. ;;
  71. reset)
  72. __rvm_reset_rvmrc_trust "$rvmrc_path" &&
  73. rvm_log "Reset trust for $rvmrc_path" ||
  74. rvm_error "Reset trust for $rvmrc_path - failed"
  75. ;;
  76. trust)
  77. __rvm_trust_rvmrc "$rvmrc_path" &&
  78. rvm_log "Marked $rvmrc_path as trusted" ||
  79. rvm_error "Marked $rvmrc_path as trusted - failed"
  80. ;;
  81. untrust)
  82. __rvm_untrust_rvmrc "$rvmrc_path" &&
  83. rvm_log "Marked $rvmrc_path as untrusted" ||
  84. rvm_error "Marked $rvmrc_path as untrusted - failed"
  85. ;;
  86. trusted)
  87. __rvm_rvmrc_stored_trust_check "$rvmrc_path" || return $?
  88. ;;
  89. is_trusted)
  90. __rvm_rvmrc_stored_trust_check "$rvmrc_path" >/dev/null
  91. ;;
  92. load)
  93. rvm_current_rvmrc="" rvm_trust_rvmrcs_flag=1 __rvm_project_rvmrc "${rvmrc_path%/.rvmrc}"
  94. ;;
  95. try_to_read_ruby)
  96. __rvm_rvmrc_tools_try_to_read_ruby "$@" || return $?
  97. ;;
  98. *)
  99. rvm_error "Usage: rvm rvmrc {trust,untrust,trusted,load,reset,is_trusted,try_to_read_ruby,create}"
  100. return 1
  101. ;;
  102. esac
  103. return $?
  104. }
  105. __rvm_rvmrc_tools_try_to_read_ruby()
  106. {
  107. case "$rvmrc_path" in
  108. (*/.rvmrc)
  109. # make sure the flag is passed on to sub-process () / $()
  110. if [[ -n "${rvm_trust_rvmrcs_flag:-}" ]]
  111. then export rvm_trust_rvmrcs_flag
  112. fi
  113. rvmrc_path="$(cd "$(dirname "$rvmrc_path")"; pwd)/.rvmrc"
  114. __rvm_rvmrc_tools is_trusted "$(dirname "$rvmrc_path")" .rvmrc ||
  115. (
  116. # subprocess to not mess with current process variables
  117. rvm_promptless=1 __rvm_project_rvmrc "$rvmrc_path" >/dev/null 2>&1
  118. )
  119. if __rvm_rvmrc_tools is_trusted "$(dirname "$rvmrc_path")" .rvmrc
  120. then __rvm_rvmrc_tools_read_ruby "$@" || return $?
  121. else return 1
  122. fi
  123. ;;
  124. (*)
  125. __rvm_rvmrc_tools_read_ruby "$@" || return $?
  126. ;;
  127. esac
  128. }
  129. __rvm_save_variables()
  130. {
  131. \typeset __save_to __key
  132. __save_to="$1"
  133. shift
  134. for __key in "$@"
  135. do
  136. eval "${__save_to}+=( \"\${__key}=\${${__key}}\" )"
  137. done
  138. }
  139. __rvm_rvmrc_tools_read_ruby()
  140. {
  141. \typeset __result
  142. \typeset -a rvmrc_tools_read_ruby
  143. rvmrc_tools_read_ruby=()
  144. __rvm_save_variables rvmrc_tools_read_ruby \
  145. rvm_current_rvmrc result current_result rvm_token next_token \
  146. rvm_action _string
  147. rvm_current_rvmrc=""
  148. rvm_action="${rvm_action:-use}" rvm_trust_rvmrcs_flag=1 __rvm_project_rvmrc "$rvmrc_path" >/dev/null &&
  149. rvm_ruby_string="${GEM_HOME##*/}" &&
  150. rvm_ruby_strings="$rvm_ruby_string" ||
  151. __result=101
  152. __rvm_set_env "" "${rvmrc_tools_read_ruby[@]}"
  153. return ${__result:-0}
  154. }