/
alias
executable file
·237 lines (188 loc) · 5.54 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
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env bash
unset rvm_default_flag rvm_wrapper_name
source "$rvm_scripts_path/base"
usage() {
printf "%b" "
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_show() {
typeset expanded_alias_name
if [[ -z "$alias_name" ]]
then
rvm_log "usage: 'rvm alias show [alias_name]'"
result=1
return
fi
[[ -s "$rvm_path/config/alias" ]] || return 0
expanded_alias_name="$("$rvm_scripts_path"/db "$rvm_path/config/alias" "$alias_name")"
if [[ -z "$expanded_alias_name" ]]; then
rvm_error "Unknown alias name: '$alias_name'"
result=1
else
result=0
if [[ -n "$gemset_name" ]] ; then
printf "%b" "${expanded_alias_name}${rvm_gemset_separator:-"@"}${gemset_name}\n"
else
printf "%b" "${expanded_alias_name}\n"
fi
fi
}
alias_after_delete_default()
{
rvm_log "Deleting default links/files"
for _path in $rvm_bin_path/default_*
do
[[ -f "$_path" ]] && rm -rf ${_path}
done
for wrapper in "$rvm_path"/wrappers/default/*
do
wrapper="${wrapper##*\/}"
if [[ -L "$rvm_bin_path/${wrapper}" ]]
then
rm -f "$rvm_bin_path/${wrapper}"
fi
rm -f "$rvm_bin_path/${wrapper}"
# If the RVM bin path is different from rvm_path/bin, ensure they are
# in sync.
if [[ "${rvm_bin_path}" != "${rvm_path}/bin" ]]
then
rm -f "${rvm_path}/bin/${wrapper}"
fi
done
}
alias_delete() {
rvm_log "Deleting alias: $alias_name"
for _path in "$rvm_rubies_path" "$rvm_environments_path" "$rvm_wrappers_path"
do
[[ -L "$_path/$alias_name" ]] && rm -f "$_path/$alias_name"
done
[[ -s "$rvm_path/config/alias" ]] || return 0
"$rvm_scripts_path"/db "$rvm_path/config/alias" "$alias_name" "delete"
if [[ "default" == "$alias_name" ]] ; then
alias_after_delete_default
fi
}
alias_after_create_default()
{
rvm_log "Creating default links/files"
environment_id="${final_environment_identifier}"
if (( ${rvm_user_install_flag:=0} == 0 ))
then
# Sets up the default wrappers.
"$rvm_scripts_path/wrapper" "$rvm_ruby_string" --no-prefix
else
"$rvm_scripts_path/wrapper" "$rvm_ruby_string" "default"
fi
RUBY_VERSION="$("$rvm_ruby_home/bin/ruby" -v | \sed 's#^\(.*\) (.*$#\1#')"
export GEM_HOME GEM_PATH MY_RUBY_HOME RUBY_VERSION
# Copy wrapper scripts for the newly set default to the RVM bin path.
for wrapper in "$rvm_path"/wrappers/default/*
do
[[ -r "${wrapper}" ]] || continue
if [[ -L "$rvm_bin_path/${wrapper##*\/}" ]]
then
rm -f "$rvm_bin_path/${wrapper##*\/}"
fi
cp -f "$wrapper" "$rvm_bin_path/${wrapper##*\/}"
# If the RVM bin path is different from rvm_path/bin, ensure they are
# in sync.
if [[ "${rvm_bin_path}" != "${rvm_path}/bin" ]]
then
cp -f "${wrapper}" "${rvm_path}/bin/"
fi
done
}
alias_create()
{
alias_name="${alias_name:-""}"
rvm_ruby_string="$rvm_environment_identifier"
rvm_expanding_aliases=1
__rvm_become
unset rvm_expanding_aliases
if
[[ -z "${rvm_environment_identifier:-""}" || -z "$rvm_ruby_string" || -z "$alias_name" ]]
then
rvm_error "usage: 'rvm alias [alias_name] [ruby_string]'"
return 1
elif
[[ ! -L "$rvm_rubies_path/$alias_name" && -d "$rvm_rubies_path/$alias_name" ]]
then
rvm_error "$alias_name is a full ruby name and can not be used as alias name."
return 2
else
final_environment_identifier="${rvm_ruby_string:-$(__rvm_env_string)}"
rvm_log "Creating alias $alias_name for $final_environment_identifier."
for _path in "$rvm_rubies_path" "$rvm_environments_path" "$rvm_wrappers_path"
do
[[ -L "$_path/default" ]] && rm -f "$_path/$alias_name"
\ln -fs "$_path/$rvm_ruby_string" "$_path/$alias_name"
done
rvm_log "Recording alias $alias_name for $final_environment_identifier."
"$rvm_scripts_path"/db "$rvm_path/config/alias" "$alias_name" "$final_environment_identifier"
[[ "default" != "$alias_name" ]] || alias_after_create_default
fi
}
alias_list() {
typeset item
case "${alias_name}" in
(names)
cat - # just show it
;;
(*)
while
read item
do
echo "$item => $("$rvm_scripts_path"/db "$rvm_path/config/alias" "$item")"
done
;;
esac < <(
chpwd_functions="" builtin cd "$rvm_rubies_path"
find . -maxdepth 1 -mindepth 1 -type l | sed -e 's#.*/##'
)
}
alias_search_by_target() {
typeset item items target search
search="${alias_name}@${gemset_name}"
items=($(chpwd_functions="" builtin cd "$rvm_rubies_path" ; find . -maxdepth 1 -mindepth 1 -type l | sed -e 's#./##'))
for item in "${items[@]}"
do
target=$("$rvm_scripts_path"/db "$rvm_path/config/alias" "$(basename "$item")")
if [[ "${search}" == "${target}" ]]
then
echo "$(basename "$item")"
fi
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 "%b" "$alias_name" | GREP_OPTIONS="" \grep "${rvm_gemset_separator:-"@"}" >/dev/null 2>&1 ; then
gemset_name="${alias_name/*${rvm_gemset_separator:-"@"}/}"
alias_name="${alias_name/${rvm_gemset_separator:-"@"}*/}"
else
gemset_name=""
fi
case "$action" in
delete|create|list|show|search_by_target)
alias_${action}
;;
help|usage)
usage
;;
*)
usage
exit 1
;;
esac
exit $?