upgrade 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env bash
  2. unset GREP_OPTIONS
  3. usage()
  4. {
  5. printf "%b" "
  6. Usage:
  7. rvm upgrade {source-ruby} [destination-ruby] [--force]
  8. Description:
  9. Upgrades the specified (already installed) source ruby given to the
  10. given destination ruby version. Will migrate gemsets, wrappers, aliases
  11. and environment files.
  12. To upgrade rvm itself you want 'rvm get'.
  13. Examples:
  14. $ rvm upgrade 1.9.2-p136 1.9.2-p180
  15. $ rvm upgrade ree-2011.01 ree-2011-02
  16. "
  17. }
  18. confirm()
  19. {
  20. if (( ${rvm_force_flag:-0} > 0 ))
  21. then return 0
  22. fi
  23. \typeset confirmation_response
  24. printf "%b" "$1 (Y/n): "
  25. read -r confirmation_response
  26. if [[ -n "$confirmation_response" ]]
  27. then
  28. echo $confirmation_response | __rvm_grep -i '^y\|^Y' >/dev/null 2>&1
  29. fi
  30. }
  31. die_with_error()
  32. {
  33. rvm_error "$1"
  34. exit "${2:-1}"
  35. }
  36. expand_ruby_name()
  37. (
  38. source "$rvm_scripts_path/functions/tools"
  39. tools_strings "${1%%@*}"
  40. )
  41. existing_ruby_patch()
  42. {
  43. {
  44. __rvm_list_strings | __rvm_grep -E "^ruby-$1|^$1$" ||
  45. (
  46. rvm_ruby_string="$1"
  47. __rvm_ruby_string
  48. __rvm_list_strings | __rvm_grep -E "^${rvm_ruby_interpreter}-${rvm_ruby_version}(-.*|$)" ||
  49. __rvm_list_strings | __rvm_grep "^${rvm_ruby_interpreter}-"
  50. )
  51. } | __rvm_version_sort | __rvm_tail -n 1
  52. }
  53. highest_ruby_patch()
  54. {
  55. \typeset patch_level _version
  56. (
  57. rvm_ruby_string=$1
  58. __rvm_ruby_string
  59. patch_level="$(
  60. __rvm_db "${rvm_ruby_interpreter}_${rvm_ruby_version}_patch_level"
  61. )"
  62. _version="$(
  63. __rvm_db "${rvm_ruby_interpreter}_version"
  64. )"
  65. if
  66. [[ -n "${patch_level:-""}" ]]
  67. then
  68. rvm_ruby_patch_level="${patch_level}"
  69. echo ${rvm_ruby_interpreter}-${rvm_ruby_version}-${rvm_ruby_patch_level}
  70. elif
  71. [[ -n "${_version:-""}" ]]
  72. then
  73. echo ${rvm_ruby_interpreter}-${_version}
  74. else
  75. echo ${rvm_ruby_interpreter}
  76. fi
  77. )
  78. }
  79. upgrade_ruby()
  80. {
  81. [[ -n "$expanded_source" ]] || die_with_error "The source ruby was not a valid ruby string."
  82. [[ -n "$expanded_destination" ]] || die_with_error "The destination ruby was not a valid ruby string."
  83. confirm "Are you sure you wish to upgrade from $expanded_source to $expanded_destination?" ||
  84. die_with_error "Cancelling upgrade."
  85. [[ -d "$rvm_rubies_path/$expanded_destination" ]] ||
  86. {
  87. rvm_log "Installing new ruby $expanded_destination"
  88. __rvm_run_wrapper manage install "$expanded_destination" ||
  89. die_with_error "Unable to install ruby $expanded_destination. Please install it manually to continue." $?
  90. }
  91. rvm_log "Migrating gems from $expanded_source to $expanded_destination"
  92. "$rvm_scripts_path/migrate" "$expanded_source" "$expanded_destination" || die_with_error "Error migrating gems." "$result"
  93. rvm_log "Upgrade complete!"
  94. }
  95. source_ruby="${args[__array_start]:-}"
  96. destination_ruby="${args[__array_start+1]:-}"
  97. expanded_source="$(existing_ruby_patch "$source_ruby")"
  98. if
  99. [[ -n "$source_ruby" && -z "$destination_ruby" ]]
  100. then
  101. highest_source="$(highest_ruby_patch "$(expand_ruby_name "$source_ruby")")"
  102. if [[ "${expanded_source}" != "${highest_source}" ]]
  103. then destination_ruby="$(expand_ruby_name "$highest_source")"
  104. fi
  105. fi
  106. if
  107. [[ -z "$source_ruby" || -z "$destination_ruby" ]]
  108. then
  109. usage >&2
  110. exit 1
  111. elif
  112. [[ "help" == "$source_ruby" ]]
  113. then
  114. usage
  115. else
  116. expanded_destination="$(expand_ruby_name "$destination_ruby")"
  117. upgrade_ruby
  118. fi