-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
upgrade
executable file
·72 lines (55 loc) · 2.56 KB
/
upgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bash
unset GREP_OPTIONS
source "$rvm_scripts_path/base"
usage() {
echo "Usage: rvm upgrade prefix [specific version]" >&2
echo "Upgrades the latest installed ruby with a given prefix (e.g. ree) to a new version." >&2
exit 1
}
confirm() {
printf "$1 (Y/n): "
local confirmation_response
read -r confirmation_response
[[ -z "$confirmation_response" ]] || echo $confirmation_response | \grep -qi '^y\|^Y'
}
die_with_error() {
"$rvm_scripts_path/log" "fail" "$1"
exit "${2:-1}"
}
expand_ruby_name() {
"$rvm_scripts_path/tools" strings "$1" | awk -F"$rvm_gemset_separator" '{print $1}'
}
expand_existing_ruby() {
local prefix="$(expand_ruby_name "$1" | awk -F'-' '{print $1"-"$2}')"
local ruby_name
while read -r ruby_name; do
if [[ "$ruby_name" != "$expanded_destination"* ]]; then
echo "$ruby_name" ; return 0
fi
done < <("$rvm_scripts_path/list" strings | tr ' ' '\n' | sort -ur | \grep "^$prefix" | \grep -v '-head$' | head -n1)
return 1
}
upgrade_ruby() {
[[ -z "$source_ruby" ]] && die_with_error "Unable to find a source ruby. Please manually provide one."
expanded_source="$(expand_ruby_name "$source_ruby")"
[[ -z "$expanded_source" ]] && die_with_error "The source ruby was not a valid ruby string."
confirm "Are you sure you wish to upgrade from $expanded_source to $expanded_destination?" || die_with_error "Cancelling upgrade."
if [[ ! -d "$rvm_rubies_path/$expanded_destination" ]]; then
"$rvm_scripts_path/log" "info" "Installing new ruby $expanded_destination"
$rvm_bin_path/rvm install "$expanded_destination"
result="$?" ; [[ "$result" -gt 0 ]] && die_with_error "Unable to install ruby $expanded_destination. Please install it manually to continue." "$result"
fi
"$rvm_scripts_path/log" "info" "Migrating gems from $expanded_source to $expanded_destination"
"$rvm_scripts_path/migrate" "$expanded_source" "$expanded_destination"
result="$?" ; [[ "$result" -gt 0 ]] && die_with_error "Error migrating gems." "$result"
"$rvm_scripts_path/log" "info" "Upgrade complete!"
}
args=($*)
destination_ruby="${args[0]}"
source_ruby="${args[1]:-"$(expand_existing_ruby "$destination_ruby")"}"
args="$(echo ${args[@]:2}) " # Strip trailing / leading / extra spacing.
[[ -z "$source_ruby" ]] && usage
expanded_destination="$(expand_ruby_name "$destination_ruby")"
[[ -z "$source_ruby" ]] && die_with_error "The Source ruby was not specified, a valid ruby string, or not found."
[[ -z "$expanded_destination" ]] && die_with_error "The destination ruby was not a valid ruby string, or not found."
upgrade_ruby