rvmrc_project 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/usr/bin/env bash
  2. # Checks the rvmrc for the given directory. Note that if
  3. # argument is passed, it will be used instead of pwd.
  4. __rvm_project_rvmrc()
  5. {
  6. export __rvm_project_rvmrc_lock
  7. : __rvm_project_rvmrc_lock:${__rvm_project_rvmrc_lock:=0}
  8. : __rvm_project_rvmrc_lock:$((__rvm_project_rvmrc_lock+=1))
  9. if (( __rvm_project_rvmrc_lock > 1 ))
  10. then return 0 # no nesting
  11. fi
  12. \typeset working_dir found_file rvm_trustworthiness_result save_PATH
  13. # Get the first argument or the pwd.
  14. working_dir="${1:-"$PWD"}"
  15. save_PATH="${PATH}"
  16. while :
  17. do
  18. if
  19. [[ -z "$working_dir" || "$HOME" == "$working_dir" || "${rvm_prefix:-}" == "$working_dir" || "$working_dir" == "." ]]
  20. then
  21. if (( ${rvm_project_rvmrc_default:-0} >= 1 ))
  22. then rvm_previous_environment=default
  23. fi
  24. if [[ -n "${rvm_previous_environment:-""}" ]] && (( ${rvm_project_rvmrc_default:-0} < 2 ))
  25. then __rvm_load_environment "$rvm_previous_environment"
  26. fi
  27. __rvm_file_env_check_unload
  28. unset rvm_current_rvmrc rvm_previous_environment
  29. break
  30. else
  31. if
  32. __rvm_project_dir_check "$working_dir" found_file
  33. then
  34. rvm_trustworthiness_result=0
  35. if
  36. [[ "${found_file}" != "${rvm_current_rvmrc:-""}" ]]
  37. then
  38. __rvm_conditionally_do_with_env __rvm_load_project_config "${found_file}" ||
  39. {
  40. rvm_trustworthiness_result=$?
  41. PATH="${save_PATH}" # restore PATH if project file load failed
  42. unset RVM_PROJECT_PATH
  43. }
  44. fi
  45. unset __rvm_project_rvmrc_lock
  46. return "$rvm_trustworthiness_result"
  47. else
  48. working_dir="${working_dir%/*}"
  49. fi
  50. fi
  51. done
  52. unset __rvm_project_rvmrc_lock
  53. return 1
  54. }
  55. __rvm_load_project_config()
  56. {
  57. rvm_debug "__rvm_load_project_config $1"
  58. \typeset __gemfile _bundle_install
  59. \typeset -a __file_env_variables
  60. __file_env_variables=()
  61. __gemfile=""
  62. rvm_previous_environment="$(__rvm_env_string)"
  63. : rvm_autoinstall_bundler_flag:${rvm_autoinstall_bundler_flag:=0}
  64. case "$1" in
  65. (*/.rvmrc)
  66. __rvmrc_warning_display_for_rvmrc "$1"
  67. if
  68. __rvm_check_rvmrc_trustworthiness "$1"
  69. then
  70. __rvm_remove_rvm_from_path ; __rvm_conditionally_add_bin_path
  71. rvm_current_rvmrc="$1"
  72. __rvm_ensure_is_a_function
  73. unset GEM_HOME GEM_PATH
  74. rvm_ruby_string="${rvm_previous_environment/system/default}" rvm_action=use source "$1" ||
  75. return $?
  76. else
  77. return $?
  78. fi
  79. ;;
  80. (*/.versions.conf)
  81. __rvm_ensure_is_a_function
  82. rvm_current_rvmrc="$1"
  83. rvm_ruby_string="$( \command \tr -d '\r' <"$1" | __rvm_sed -n '/^ruby=/ {s/ruby=//;p;}' | tail -n 1 )"
  84. [[ -n "${rvm_ruby_string}" ]] || return 2
  85. rvm_gemset_name="$( \command \tr -d '\r' <"$1" | __rvm_sed -n '/^ruby-gemset=/ {s/ruby-gemset=//;p;}' | tail -n 1 )"
  86. rvm_create_flag=1 __rvm_use || return 3
  87. __rvm_file_load_env_and_trust "$1" "env-"
  88. _bundle_install="$( \command \tr -d '\r' <"$1" | __rvm_sed -n '/^ruby-bundle-install=/ {s/ruby-bundle-install=//;p;}' )"
  89. if
  90. [[ -n "${_bundle_install}" ]]
  91. then
  92. if [[ -f "${_bundle_install}" ]]
  93. then __gemfile="${_bundle_install}"
  94. else __gemfile="${1%/*}/Gemfile"
  95. fi
  96. fi
  97. ;;
  98. (*/Gemfile)
  99. __rvm_ensure_is_a_function
  100. rvm_current_rvmrc="$1"
  101. rvm_ruby_string="$( \command \tr -d '\r' <"$1" | __rvm_sed -n '/^#ruby=/ {s/#ruby=//;p;}' | tail -n 1 )"
  102. [[ -n "${rvm_ruby_string}" ]] || {
  103. rvm_ruby_string="$(
  104. \command \tr -d '\r' <"$1" |
  105. __rvm_sed -n "s/[[:space:]]+rescue[[:space:]]+nil$//; /^\s*ruby[[:space:](]/ {s/^\s*ruby//; s/[[:space:]()'\"]//g; p;}" |
  106. \tail -n 1
  107. )"
  108. [[ -n "${rvm_ruby_string}" ]] || return 2
  109. rvm_ruby_string="${rvm_ruby_string%%\#*}"
  110. rvm_ruby_string="${rvm_ruby_string/,:engine=>/-}"
  111. rvm_ruby_string="${rvm_ruby_string/,engine:/-}"
  112. rvm_ruby_string="${rvm_ruby_string/,:engine_version=>[^,]*/}"
  113. rvm_ruby_string="${rvm_ruby_string/,engine_version:[^,]*/}"
  114. rvm_ruby_string="${rvm_ruby_string/,:patchlevel=>/-p}"
  115. rvm_ruby_string="${rvm_ruby_string/,patchlevel:/-p}"
  116. } #'
  117. rvm_gemset_name="$( \command \tr -d '\r' <"$1" | __rvm_sed -n '/^#ruby-gemset=/ {s/#ruby-gemset=//;p;}' | tail -n 1 )"
  118. if [[ -z "${rvm_gemset_name:-}" && -f "${1%/*}/.ruby-gemset" ]]
  119. then rvm_gemset_name="$( \command \tr -d '\r' <"${1%/*}/.ruby-gemset" )"
  120. fi
  121. __rvmrc_warning_display_for_Gemfile "$1"
  122. rvm_create_flag=1 __rvm_use || return 3
  123. __rvm_file_load_env_and_trust "$1" "#ruby-env-"
  124. __gemfile="$1"
  125. ;;
  126. (*/.ruby-version|*/.rbfu-version|*/.rbenv-version)
  127. __rvm_ensure_is_a_function
  128. rvm_current_rvmrc="$1"
  129. rvm_ruby_string="$( \command \tr -d '\r' <"$1" )"
  130. if [[ -z "${rvm_ruby_string}" ]]
  131. then return 2
  132. fi
  133. if [[ -f "${1%/*}/.ruby-gemset" ]]
  134. then rvm_gemset_name="$( \command \tr -d '\r' <"${1%/*}/.ruby-gemset" )"
  135. else rvm_gemset_name=""
  136. fi
  137. rvm_create_flag=1 __rvm_use || return 3
  138. __rvm_file_load_env_and_trust "${1%/*}/.ruby-env"
  139. __rvm_file_load_env_and_trust "${1%/*}/.rbenv-vars"
  140. __gemfile="${1%/*}/Gemfile"
  141. ;;
  142. (*)
  143. rvm_error "Unsupported file format for '$1'"
  144. return 1
  145. ;;
  146. esac
  147. __rvm_file_set_env
  148. if
  149. [[ "${rvm_autoinstall_bundler_flag:-0}" == 1 && -n "${__gemfile:-}" && -f "${__gemfile:-}" ]]
  150. then
  151. __rvm_which bundle >/dev/null 2>&1 ||
  152. gem install --remote bundler
  153. bundle install --gemfile="${__gemfile}" | __rvm_grep -vE '^Using|Your bundle is complete'
  154. fi
  155. }
  156. # Detect if project file is in given PATH,
  157. #
  158. # Usage:
  159. # __rvm_project_dir_check <file_or_dir_to_check> [variable_name_to_set [default_value]]
  160. #
  161. # @param file_or_dir_to_check file or directory to check if it's valid project file
  162. # @param variable_name_to_set when project file is found - set this variable to it's location
  163. # @param default_value when no project file found - set variable to this value
  164. # @return 0 - found, >0 - not found
  165. # @env RVM_PROJECT_PATH unsets when not found, sets to project root when found
  166. __rvm_project_dir_check()
  167. {
  168. \typeset _found_file path_to_check variable variable_default
  169. \typeset -a _valid_files
  170. path_to_check="$1"
  171. variable="${2:-}"
  172. variable_default="${3:-}"
  173. _valid_files=(
  174. "$path_to_check"
  175. "$path_to_check/.rvmrc" "$path_to_check/.versions.conf" "$path_to_check/.ruby-version"
  176. "$path_to_check/.rbfu-version" "$path_to_check/.rbenv-version" "$path_to_check/Gemfile"
  177. )
  178. __rvm_find_first_file _found_file "${_valid_files[@]}" || true
  179. if
  180. [[ ! -s "$_found_file" ||
  181. "${_found_file}" == "$HOME/.rvmrc"
  182. ]]
  183. then
  184. _found_file=""
  185. elif
  186. [[ "${_found_file##*/}" == "Gemfile" ]] &&
  187. ! __rvm_grep "^#ruby=" "$_found_file" >/dev/null &&
  188. ! __rvm_grep -E "^\s*ruby" "$_found_file" >/dev/null
  189. then
  190. _found_file=""
  191. fi
  192. if [[ -n "$variable" ]]
  193. then eval "$variable=\"\${_found_file:-$variable_default}\""
  194. fi
  195. if
  196. [[ -n "${_found_file:-$variable_default}" ]]
  197. then
  198. RVM_PROJECT_PATH="${_found_file:-$variable_default}"
  199. RVM_PROJECT_PATH="${RVM_PROJECT_PATH%/*}"
  200. else
  201. \typeset __result=$?
  202. unset RVM_PROJECT_PATH
  203. return $__result
  204. fi
  205. }