set 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #!/usr/bin/env bash
  2. source "$rvm_scripts_path/base"
  3. rvm_is_not_a_shell_function=0 # for nested invocations of rvm
  4. if [[ -n "${rvm_in_flag}" && -d "${rvm_in_flag}" ]]
  5. then __rvm_cd "${rvm_in_flag}"
  6. fi
  7. __rvm_attempt_single_exec()
  8. {
  9. # Return if we have multiple rubies. or we're not running exec.
  10. if (( ${#rvm_ruby_strings[@]} == 1 ))
  11. then
  12. __rvm_become "$rvm_ruby_strings"
  13. __rvm_load_rvmrc
  14. export rvm_project_rvmrc=0
  15. exec "${args[@]}"
  16. fi
  17. return 1
  18. }
  19. __rvm_ruby_do()
  20. {
  21. # Return on invalid rubies.
  22. __rvm_become "$current_set_ruby" || return 1
  23. rvm_hook="before_do"
  24. source "$rvm_scripts_path/hook"
  25. if [[ -n "$rvm_json_flag" || -n "$rvm_yaml_flag" || -n "$rvm_summary_flag" ]]
  26. then
  27. if [[ ! -d "./log/$rvm_ruby_string/" ]]
  28. then
  29. mkdir -p "./log/$rvm_ruby_string/"
  30. fi
  31. touch "./log/$rvm_ruby_string/$action.log"
  32. "${args[@]}" >> "./log/$rvm_ruby_string/$action.log" 2>&1
  33. else
  34. if (( ${rvm_verbose_flag:-0} > 0 ))
  35. then
  36. current_env="$(__rvm_env_string)"
  37. if [[ "$current_env" != "$current_set_ruby" ]]
  38. then
  39. current_env="$current_set_ruby ($current_env)"
  40. fi
  41. rvm_log "$current_env: $(ruby -v $rvm_ruby_mode | \command \tr "\n" ' ')"
  42. unset current_env
  43. fi
  44. (
  45. __rvm_load_rvmrc
  46. export rvm_project_rvmrc=0
  47. "${args[@]}"
  48. )
  49. fi
  50. result=$?
  51. string=$rvm_ruby_string #$(basename $rvm_ruby_gem_home)
  52. if (( result == 0 ))
  53. then
  54. eval "successes=(${successes[*]} $string)"
  55. else
  56. eval "errors=(${errors[*]} $string)"
  57. fi
  58. eval "rubies=(${rubies[*]} $string)"
  59. eval "statuses=(${statuses[*]} $result)"
  60. unset string
  61. rvm_hook="after_do"
  62. source "$rvm_scripts_path/hook"
  63. __rvm_unset_ruby_variables
  64. }
  65. # Output the summary in a human readable format.
  66. __rvm_summary()
  67. {
  68. export successes errors statuses
  69. summary="\nSummary:\n\n"
  70. if [[ ${#successes[*]} -gt 0 ]]
  71. then
  72. if rvm_pretty_print stdout
  73. then
  74. summary="$summary ${rvm_notify_clr:-}${#successes[*]} successful: $(echo "${successes[*]}" | __rvm_sed 's# #, #g')${rvm_reset_clr:-}\n"
  75. else
  76. summary="$summary ${#successes[*]} successful: $(echo "${successes[*]}" | __rvm_sed 's# #, #g')\n"
  77. fi
  78. fi
  79. if [[ ${#errors[*]} -gt 0 ]] ; then
  80. if rvm_pretty_print stdout
  81. then
  82. summary="$summary ${rvm_error_clr:-}${#errors[*]} errors: $(echo "${errors[*]}" | __rvm_sed 's# #, #g')${rvm_reset_clr:-}\n"
  83. else
  84. summary="$summary ${#errors[*]} errors: $(echo "${errors[*]}" | __rvm_sed 's# #, #g')\n"
  85. fi
  86. fi
  87. total=${#rubies[*]}
  88. [[ -z "${ZSH_VERSION:-}" ]] ; array_start=$?
  89. printf "%b" "$summary" | tee -a log/summary.log
  90. return ${#errors[*]}
  91. }
  92. # Output the summary in a yaml format.
  93. __rvm_yaml()
  94. {
  95. export successes errors statuses
  96. yaml="totals:\n rubies: ${#rubies[*]}\n successes: ${#successes[*]}\n errors: ${#errors[*]}\nsuccesses:"
  97. for var in ${successes[*]} ; do yaml="$yaml\n - $var" ; done
  98. yaml="$yaml\nerrors:"
  99. for var in ${errors[*]} ; do yaml="$yaml\n - $var" ; done
  100. yaml="$yaml\nrubies:"
  101. total=${#rubies[*]}
  102. [[ -z "${ZSH_VERSION:-}" ]] ; array_start=$?
  103. for (( index = $array_start ; index < $total + $array_start ; index++ )) ; do
  104. if [[ ${rvm_debug_flag:-0} -gt 0 ]] ; then
  105. rvm_debug "${rubies[$index]}: ${statuses[$index]}"
  106. fi
  107. yaml="$yaml\n \"${rubies[$index]}\": ${statuses[$index]}"
  108. done
  109. unset index array_start
  110. \command \mkdir -p log
  111. printf "%b" "$yaml" | tee -a log/summary.yaml
  112. return ${#errors[*]}
  113. }
  114. # Output the summary in a json format.
  115. __rvm_json()
  116. {
  117. \typeset index array_start
  118. json="{
  119. \"totals\": { \"rubies\": ${#rubies[*]}, \"successes\": ${#successes[*]}, \"errors\": ${#errors[*]} },
  120. \"successful\": [$(echo \"${successes[*]}\" | __rvm_sed 's# #", "#g' | __rvm_sed 's#\"\"##')],
  121. \"errors\": [$(echo \"${errors[*]}\" | __rvm_sed 's# #", "#g' | __rvm_sed 's#\"\"##')],
  122. \"rubies\": { "
  123. total=${#rubies[*]}
  124. [[ -z "${ZSH_VERSION:-}" ]] ; array_start=$?
  125. for (( index = $array_start ; index < $total + $array_start ; index++ )) ; do
  126. if [[ ${rvm_debug_flag:-0} -gt 0 ]] ; then
  127. rvm_debug "${rubies[$index]}: ${statuses[$index]}"
  128. fi
  129. json="$json\n {\"${rubies[$index]}\": ${statuses[$index]}}"
  130. if (( $index + 1 < $total + $array_start )) ; then json="$json, " ; fi
  131. done
  132. json="$json\n }\n}"
  133. if [[ ! -d log ]] ; then
  134. mkdir -p log
  135. fi
  136. printf "%b" "$json" | tee -a log/summary.json
  137. return ${#errors[*]}
  138. }
  139. # Loop over a set or all rvm installed rubies to perform some action.
  140. # Record the results and report based on CLI selections.
  141. rubies=() ; successes=() ; errors=() ; statuses=()
  142. args=( "$@" )
  143. action="${args[$__array_start]}"
  144. unset args[$__array_start]
  145. args=( "${args[@]}" )
  146. if [[ -z "$action" ]]
  147. then
  148. rvm_error "Action must be specified."
  149. exit 1
  150. elif [[ "$action" != "do" ]]
  151. then
  152. rvm_error "Only 'do' action is allowed."
  153. exit 1
  154. fi
  155. # deprecated 2011.10.22 for RVM 1.9.0, removed 2012.09.13 for RVM 1.16.0
  156. if [[ -z "${rvm_ruby_strings}" ]]
  157. then
  158. rvm_error "\`rvm do ${args[@]}\` was removed, use \`rvm 1.9.2,1.9.3 do ${args[@]}\` or \`rvm all do ${args[@]}\` instead."
  159. exit 1
  160. fi
  161. previous_rvm_ruby_strings="$rvm_ruby_strings"
  162. rvm_ruby_strings=( $( __rvm_expand_ruby_string "$rvm_ruby_strings" ) ) || {
  163. \typeset result=$?
  164. if
  165. [[ $result -eq 2 && -n "${rvm_expected_gemset_name:-${rvm_gemset_name:-}}" ]]
  166. then
  167. rvm_ruby_string="${previous_rvm_ruby_strings## }"
  168. __rvm_ruby_string
  169. rvm_error "Gemset '${rvm_expected_gemset_name:-${rvm_gemset_name:-}}' does not exist, \
  170. 'rvm $rvm_ruby_string do rvm gemset create ${rvm_expected_gemset_name:-${rvm_gemset_name:-}}' first, or append '--create'."
  171. else
  172. rvm_ruby_string="${previous_rvm_ruby_strings## }"
  173. __rvm_ruby_string
  174. rvm_error "Ruby ${rvm_ruby_string} is not installed."
  175. fi
  176. exit $result
  177. }
  178. unset previous_rvm_ruby_strings
  179. __rvm_attempt_single_exec
  180. for current_set_ruby in ${rvm_ruby_strings[@]}
  181. do
  182. __rvm_ruby_do
  183. done
  184. if [[ -n "$rvm_summary_flag" ]] ; then __rvm_summary ; fi
  185. if [[ -n "$rvm_yaml_flag" ]] ; then __rvm_yaml ; fi
  186. if [[ -n "$rvm_json_flag" ]] ; then __rvm_json ; fi
  187. rvm_hook="after_do" ; source "$rvm_scripts_path/hook"
  188. exit ${#errors[*]}