Permalink
Browse files

Implement rvm migrate

  • Loading branch information...
1 parent 32c8c8e commit 602fdd69a879d07917310f451ca5596abe4d176a @Sutto Sutto committed Aug 10, 2010
Showing with 83 additions and 8 deletions.
  1. +2 −0 README
  2. +2 −8 scripts/cli
  3. +79 −0 scripts/migrate
View
2 README
@@ -91,6 +91,8 @@ Action
notes - Display notes, with operating system specifics.
snapshot - Let's your backup / restore an rvm installation in a lightweight manner.
+
+ migrate - Let's you migrate all gemsets from one ruby to another.
Implementation
View
@@ -120,12 +120,6 @@ __rvm_parse_args() {
rvm_parse_break=1
;;
- docs|alias|rubygems)
- rvm_action="$rvm_token"
- rvm_ruby_args="$(__rvm_quote_args "$@")"
- rvm_parse_break=1
- ;;
-
-S)
rvm_action="ruby"
rvm_ruby_args="$rvm_token $(__rvm_quote_args "$@")"
@@ -138,7 +132,7 @@ __rvm_parse_args() {
rvm_parse_break=1
;;
- exec|cleanup|tools|disk-usage|snapshot|repair)
+ docs|alias|rubygems|exec|cleanup|tools|disk-usage|snapshot|repair|migrate)
rvm_action="$rvm_token"
rvm_ruby_args="$(__rvm_quote_args "$@")"
rvm_parse_break=1
@@ -543,7 +537,7 @@ rvm() {
unset old_rvm_ruby_string
;;
- cleanup|tools|snapshot|disk-usage|repair|alias|docs|rubygems)
+ cleanup|tools|snapshot|disk-usage|repair|alias|docs|rubygems|migrate)
__rvm_run_script "$rvm_action"
result=$?
;;
View
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+
+unset GREP_OPTIONS
+
+source "$rvm_scripts_path/base"
+
+usage() {
+ echo "Usage: rvm migrate from to" >&2
+ echo "Moves all gemsets from one ruby to another." >&2
+ exit 1
+}
+
+confirm() {
+ printf "$1 (Y/n): "
+ local confirmation_response
+ read -r confirmation_response
+ [[ -z "$confirmation_response" ]] || echo "$confirmation_response" | grep -qi '^y'
+}
+
+expand_ruby_name() {
+ $rvm_scripts_path/tools strings "$1" | awk -F"$rvm_gemset_separator" '{print $1}'
+}
+
+migrate_rubies() {
+ expanded_source="$(expand_ruby_name "$source_ruby")"
+ expanded_destination="$(expand_ruby_name "$destination_ruby")"
+
+ if [[ -z "$expanded_source" ]]; then
+ echo "Could not expand source ruby '$source_ruby'" >&2
+ return 1
+ elif [[ -z "$expanded_destination" ]]; then
+ echo "Could not expand destination ruby '$destination_ruby'" >&2
+ return 1
+ elif [[ "$expanded_destination" = "$expanded_source" ]]; then
+ echo "Source and Destination Ruby are the same ($expanded_destination)" >&2
+ return 1
+ elif [[ ! -d "$rvm_rubies_path/$expanded_source" ]]; then
+ echo "Ruby '$expanded_source' is not installed - please install it first." >&2
+ return 1
+ elif [[ ! -d "$rvm_rubies_path/$expanded_destination" ]]; then
+ echo "Ruby '$expanded_destination' is not installed - please install it first." >&2
+ return 1
+ fi
+
+ echo "Are you sure you wish to MOVE gems from $expanded_source to $expanded_destination?"
+ confirm "This will overwrite existing gems in $expanded_destination and remove them from $expanded_source" || return 1
+
+ echo "Moving gemsets..."
+
+ while read -r origin_gemset; do
+ gemset_name="$(echo "$origin_gemset" | awk -F"$rvm_gemset_separator" '{print $2}')"
+ destination_gemset="$expanded_destination"
+ if [[ -n "$gemset_name" ]]; then
+ destination_gemset="$destination_gemset$rvm_gemset_separator$gemset_name"
+ fi
+ echo "Moving $origin_gemset to $destination_gemset"
+ rm -rf "$rvm_gems_path/$destination_gemset"
+ mv "$rvm_gems_path/$origin_gemset" "$rvm_gems_path/$destination_gemset"
+ echo "Making gemset $destination_gemset pristine."
+ __rvm_run_with_env "gemset.pristine" "$destination_gemset" "rvm gemset pristine"
+ done < <($rvm_scripts_path/list gemsets strings | grep "^$expanded_source")
+ unset origin_gemset destination_gemset gemset_name
+
+ if confirm "Do you also wish to completely remove $expanded_source (inc. archive)?"; then
+ __rvm_run_with_env "rvm.remove" "$expanded_source" "rvm remove $expanded_source --archive --gems"
+ fi
+
+ echo "Successfully migrate from $expanded_source to $expanded_destination"
+
+}
+
+if [[ -z "$1" || "$#" != 2 ]]; then
+ usage
+fi
+
+source_ruby="$1"; shift
+destination_ruby="$1"; shift
+
+migrate_rubies

0 comments on commit 602fdd6

Please sign in to comment.