tools 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/env bash
  2. source "$rvm_scripts_path/base"
  3. source "$rvm_scripts_path/functions/tools"
  4. usage()
  5. {
  6. echo "Usage: rvm tools {identifier,path-identifier,strings,user}" 1>&2
  7. return 1
  8. }
  9. # Return the identifier that's current in use.
  10. tools_identifier()
  11. {
  12. __rvm_env_string
  13. }
  14. tools_path_identifier()
  15. {
  16. if [[ -z "$1" || ! -d "$1" ]]; then
  17. echo "Usage: rvm tools path-identifier 'path-to-check'"
  18. return 1
  19. fi
  20. __rvm_cd "$1"
  21. __rvm_do_with_env_before
  22. rvm_promptless=1 __rvm_project_rvmrc >/dev/null 2>&1
  23. rvmrc_result="$?"
  24. __rvm_env_string
  25. __rvm_do_with_env_after
  26. return $rvmrc_result
  27. }
  28. tools_user_usage()
  29. {
  30. \typeset msg
  31. for msg in "$@"
  32. do
  33. rvm_error "$msg"
  34. done
  35. rvm_error "Usage: rvm user [[gemsets] [rubies] [hooks] [pkgs] [wrappers] [all] [--skel]|none]"
  36. }
  37. tools_user_setup_path()
  38. {
  39. tools_user_setup "$1" "rvm_$2_path" "\${HOME}/.rvm/$2"
  40. }
  41. tools_user_setup()
  42. ( # subprocess cause we change the HOME
  43. \typeset _target _var_name _user_value
  44. _target="$1"
  45. _var_name="$2"
  46. _user_value="$3"
  47. # detect _var_name in config
  48. if [[ -f "${_target}/.rvmrc" ]] && __rvm_grep "^export ${_var_name}=" "${_target}/.rvmrc" > /dev/null
  49. then
  50. # if defined read path
  51. _user_value="$( __rvm_grep "^export ${_var_name}=" "${_target}/.rvmrc" | __rvm_sed "s/^export ${_var_name}=//" )"
  52. else
  53. # if not defined - define it
  54. echo "export ${_var_name}=\"${_user_value}\"" >> "${_target}/.rvmrc"
  55. fi
  56. # set home to _target, so --skel works fine
  57. HOME="${_target}"
  58. # resolve the stored path
  59. eval "_user_value=\"${_user_value}\""
  60. # ensure the defined path exists
  61. [[ -d "${_user_value}" ]] || mkdir -p "${_user_value}"
  62. # create empty db files for rvm_user_value
  63. if [[ "$_var_name" == "rvm_user_path" ]]
  64. then
  65. \typeset _file
  66. for _file in db md5 rvmrcs sha512
  67. do
  68. [[ -f "${_user_value}/${_file}" ]] || touch "${_user_value}/${_file}"
  69. done
  70. fi
  71. )
  72. tools_user_none()
  73. {
  74. __rvm_sed_i "$1" -E -e "/^(export )?rvm_[a-z_]+_path=/ d" -e "/^(export )?rvm_create_flag=/ d"
  75. }
  76. tools_user()
  77. {
  78. \typeset item dir target
  79. \typeset -a selection
  80. selection=()
  81. for item in $@
  82. do
  83. case "$item" in
  84. all) selection+=( gemsets rubies hooks pkgs wrappers userdb log ) ;;
  85. gemsets) selection+=( gemsets userdb log ) ;;
  86. --skel) rvm_skel_flag=1 ;;
  87. rubies|hooks|pkgs|userdb|log|wrappers) selection+=( "$item" ) ;;
  88. (none)
  89. tools_user_none "${HOME}/.rvmrc"
  90. return $?
  91. ;;
  92. *)
  93. tools_user_usage "Unrecognized option '$item'."
  94. return 1
  95. ;;
  96. esac
  97. done
  98. if (( ${#selection[@]} == 0 ))
  99. then
  100. tools_user_usage
  101. return 1
  102. fi
  103. if [[ ${rvm_skel_flag:-0} == 1 ]] && (( UID ))
  104. then
  105. tools_user_usage "The --skel flag should be run as root: rvmsudo rvm user $@."
  106. return 1
  107. fi
  108. [[ ${rvm_skel_flag:-0} == 1 ]] && target=/etc/skel || target="${HOME}"
  109. if [[ ! -w "${target}" ]] || [[ -d "${target}/.rvm" && ! -w "${target}/.rvm" ]]
  110. then
  111. tools_user_usage "Directory '${target}' or '${target}/.rvm' is not writable for current user."
  112. return 1
  113. fi
  114. if [[ -f "${target}/.rvmrc" && ! -w "${target}/.rvmrc" ]]
  115. then
  116. tools_user_usage "Configuration file '${target}/.rvmrc' is not writable for current user."
  117. return 1
  118. fi
  119. for item in "${selection[@]}"
  120. do
  121. case "$item" in
  122. rubies)
  123. for dir in archives bin environments gems gems_cache log repos rubies rubygems src tmp wrappers user
  124. do
  125. tools_user_setup_path "${target}" $dir
  126. done
  127. tools_user_setup "${target}" rvm_create_flag 1
  128. ;;
  129. gemsets)
  130. for dir in environments gems gems_cache wrappers
  131. do
  132. tools_user_setup_path "${target}" $dir
  133. done
  134. tools_user_setup "${target}" rvm_create_flag 1
  135. ;;
  136. wrappers|hooks|pkgs|log)
  137. tools_user_setup_path "${target}" "$item"
  138. ;;
  139. userdb)
  140. tools_user_setup_path "${target}" user
  141. ;;
  142. esac
  143. done
  144. }
  145. tools_mirror()
  146. {
  147. \typeset file="$rvm_user_path/db"
  148. if
  149. [[ -f "$file" ]] &&
  150. __rvm_grep "^ruby_url=" "$file" >/dev/null
  151. then
  152. if
  153. (( ${rvm_force_flag:-0} == 1 ))
  154. then
  155. __rvm_sed_i "${file}" -e "s/^ruby_${n}_url=.*$/ruby_${n}_url=https:\/\/www.mirrorservice.org\/sites\/ftp.ruby-lang.org\/pub\/ruby\/${n}/"
  156. else
  157. rvm_warn "Some settings already exist, use 'rvm --force tools mirror' to overwrite."
  158. fi
  159. else
  160. printf "ruby_url=https://www.mirrorservice.org/sites/ftp.ruby-lang.org/pub/ruby\n" >> "$file"
  161. fi
  162. }
  163. tools_rvm_env()
  164. {
  165. \typeset script
  166. rvm_log "# use shebang: #!/usr/bin/$1-rvm-env 1.9.3"
  167. for script in $@
  168. do
  169. if builtin command -v ${script} >/dev/null
  170. then
  171. __rvm_sudo ln -nfs $rvm_bin_path/rvm-shell /usr/bin/${script}-rvm-env &&
  172. rvm_log "Created link '/usr/bin/${script}-rvm-env'." ||
  173. rvm_error "Cannot create link '/usr/bin/${script}-rvm-env'."
  174. else
  175. rvm_error "There is no command/script '${script}' in system."
  176. fi
  177. done
  178. }
  179. args=($*)
  180. action="${args[0]}"
  181. args="$(echo ${args[@]:1})" # Strip trailing / leading / extra spacing.
  182. [[ -z "$action" ]] && usage
  183. case "$action" in
  184. identifier) tools_identifier ;;
  185. path-identifier) tools_path_identifier "$args" ;;
  186. strings) tools_strings "$args" ;;
  187. mirror) tools_mirror ;;
  188. user) __rvm_call_with_restored_umask tools_user "$args" ;;
  189. rvm-env) tools_rvm_env $args ;;
  190. *) usage ;;
  191. esac