-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
alias
executable file
·226 lines (148 loc) · 4.81 KB
/
alias
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env bash
unset rvm_default_flag rvm_wrapper_name
source "$rvm_path/scripts/base"
usage() {
printf "
Usage:
rvm alias [action] [arguments]
Examples:
rvm alias create [alias_name] [ruby]
rvm alias delete [alias_name]
rvm alias show [alias_name]
rvm alias list
"
}
alias_conflicts_with_ruby() {
# If default exists, we should return true.
[[ "$1" == "default" && ! -L "$rvm_path/rubies/default" ]] && return 1
# Open for suggestions to a better way of doing this...
alias_check_result="$(
\. "$rvm_path/scripts/initialize"
\. "$rvm_path/scripts/selector"
export rvm_ruby_string=\"$1\"
__rvm_ruby_string > /dev/null 2>&1
echo "$?"
)"
if [[ "0" == "$alias_check_result" ]]; then
"$rvm_path/scripts"/log "error" \
"\nYou have attempted to create an alias called '$1', which is recognized as a rvm ruby.\n"
return 0
fi
return 1
unset alias_check_result
}
alias_show() {
local expanded_alias_name
if [[ -z "$alias_name" ]]; then
"$rvm_path/scripts"/log "error" "\nusage: 'rvm alias show [alias_name]'\n"
result=1
return
fi
expanded_alias_name="$("$rvm_path/scripts"/db "$rvm_path/config/alias" "$alias_name")"
if [[ -z "$expanded_alias_name" ]]; then
"$rvm_path/scripts"/log "error" "\nUnknown alias name: '$alias_name'\n"
result=1
else
result=0
if [[ -n "$gemset_name" ]] ; then
printf "${rvm_gemset_separator:-"@"}${rvm_gemset_separator:-"@"}${gemset_name}\n"
else
printf "$expanded_alias_name\n"
fi
fi
}
alias_delete() {
echo "Deleting alias: $alias_name"
for link in "$rvm_path/rubies/$alias_name" ; do
if [[ -L "$link" ]] ; then rm -f $link ; fi
done
"$rvm_path/scripts"/db "$rvm_path/config/alias" "$alias_name" "delete"
}
alias_create() {
alias_name="${alias_name:-""}"
rvm_ruby_string="${rvm_ruby_string:-""}"
if [[ "default" != "$alias_name" ]] && alias_conflicts_with_ruby "$alias_name"; then
# Force it to an empty alias name to trigger the usage.
alias_name=""
fi
if [[ -z "${rvm_environment_identifier:-""}" || -z "$alias_name" ]] ; then
"$rvm_path/scripts"/log "error" \
"\nusage: 'rvm alias [alias_name] [ruby_string]'\n"
result=1
else
if [[ "default" = "$alias_name" ]] ; then
rvm_alias=""
fi
if [[ -z "$rvm_alias" ]] ; then
rvm_ruby_string="$rvm_environment_identifier"
if [[ -z "$rvm_alias_expanded" ]]; then
rvm_expanding_aliases=1
__rvm_become
unset rvm_expanding_aliases
else
rvm_ruby_string="$rvm_environment_identifier"
fi
if [[ -z "$rvm_ruby_string" ]]; then
"$rvm_path/scripts"/log "error" \
"\nUnknown ruby string '$rvm_ruby_string' specified\n"
result=1
return $result
fi
final_environment_identifier="${rvm_environment_identifier:-$(__rvm_environment_identifier)}"
"$rvm_path/scripts"/log "info" \
"Creating alias $alias_name for $final_environment_identifier."
ln -nfs "$rvm_path/rubies/$rvm_ruby_string" "$rvm_path/rubies/$alias_name"
"$rvm_path/scripts"/log "info" \
"Recording alias $alias_name for $final_environment_identifier."
"$rvm_path/scripts"/db "$rvm_path/config/alias" "$alias_name" "$final_environment_identifier"
else
if [[ -d "$rvm_path/rubies/$alias_name" ]] ; then
"$rvm_path/scripts"/log "error" \
"\n$rvm_path/rubies/$alias_name is taken and is *not* able to be an alias name.\n"
result=1
else
"$rvm_path/scripts"/log "error" \
"\n$rvm_path/rubies/$alias_name is already aliased.\n"
result=1
fi
fi
fi
}
alias_list() {
local item items
items=($(cd "$rvm_path/rubies" ; find . -maxdepth 1 -mindepth 1 -type l | sed -e 's#./##'))
for item in "${items[@]}" ; do
echo "$(basename "$item") => $("$rvm_path/scripts"/db "$rvm_path/config/alias" "$(basename "$item")")"
done
}
args=($*)
action="${args[0]:-""}"
alias_name="${args[1]:-""}"
rvm_environment_identifier="${args[2]:-""}"
args="$(echo ${args[@]:3}) " # Strip trailing / leading / extra spacing.
result=0
if [[ ! -f "$rvm_path/config/alias" ]] ; then touch "$rvm_path/config/alias" ; fi
if printf "$alias_name" | grep -q "${rvm_gemset_separator:-"@"}" ; then
gemset_name="${alias_name/*${rvm_gemset_separator:-"@"}/}"
alias_name="${alias_name/${rvm_gemset_separator:-"@"}*/}"
else
gemset_name=""
fi
if [[ -n "$alias_name" ]] ; then
rvm_alias="$("$rvm_path/scripts/db" "$rvm_path/config/alias" "$alias_name")"
fi
if [[ "$action" = "delete" ]] ; then
alias_delete
elif [[ "$action" = "create" ]] ; then
alias_create
elif [[ "$action" = "list" ]] ; then
alias_list
elif [[ "$action" = "show" ]]; then
alias_show
elif [[ "$action" = "help" ]]; then
usage
else
usage
exit 1
fi
exit $?