-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
wrapper
executable file
·196 lines (163 loc) · 3.98 KB
/
wrapper
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
#!/usr/bin/env bash
default_flag="$rvm_default_flag"
# Prevent recursion
unset rvm_default_flag rvm_wrapper_name prefix
source "$rvm_scripts_path/base"
source "$rvm_scripts_path/initialize"
usage()
{
printf "
Usage:
rvm wrapper [ruby_string] [wrapper_prefix] [binary[ binary[ ...]]]
Binaries
ruby, gem, rake, irb, rdoc, ri, testrb
Notes
For more information, see 'rvm help wrapper'
Example
# Wrap the spec binary as 'rails3_spec' for 1.9.2@rails3
rvm wrapper 1.9.2@rails3 rails3 spec
# To create a single binary you can do the following,
user$ rvm use --create 1.8.7@ey ; gem install ey
user$ rvm wrapper 1.8.7@ey --no-prefix ey
# So that it is clear I am now in a different env,
user$ rvm 1.9.2
user$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
# And we have the desired result,
user$ ey
Usage:
ey [--help] [--version] COMMAND [ARGS]
...
"
}
wrap()
{
if [[ -n "${file_name:-""}" ]]
then
mkdir -p "$(dirname "$file_name")"
rm -f "$file_name"
if (( UID == 0 ))
then # ... this stuff should not be in here...
path="${rvm_path:-"/usr/local/rvm"}"
else
path="${rvm_path:-"$HOME/.rvm"}"
fi
printf "#!/usr/bin/env bash
if [[ -s \"$path/environments/${environment_identifier}\" ]]
then
source \"$path/environments/${environment_identifier}\"
exec $binary_name \"\$@\"
else
echo \"ERROR: Missing RVM environment file: '$path/environments/${environment_identifier}'\" >&2
exit 1
fi
" > "$file_name"
if [[ -f "$file_name" ]]
then
chmod +x "$file_name"
fi
return 0
else
rvm_error "wrap() : file_name unkown variable for wrap()."
return 1
fi
}
symlink_binary()
{
# Generate the default wrapper with the given binary name.
# We first check if we can wrap the binary and if we were able to,
# we then symlink it into place.
if wrap_binary && [[ -f "$file_name" ]]
then
rm -f "$rvm_bin_path/${prefix}_${binary_name}"
ln -fs "$file_name" "$rvm_bin_path/${prefix}_${binary_name}"
fi
}
wrap_binary()
{
# We wrap when the given binary is in the path or override_check is set to one.
if [[ "$override_check" = "1" ]] || command -v $binary_name > /dev/null
then
wrap
else
rvm_error "Binary '$binary_name' not found."
return 1
fi
}
# Empty ruby string: show usage and exit.
if (( $# == 0 ))
then
usage
exit 1
else
ruby_string="$1"
shift
if (( $# > 0 ))
then
prefix="$1"
shift
fi
fi
if [[ -z "$ruby_string" ]]
then
usage
exit 1
fi
binaries=($@)
override_check=0
# Default the list of binaries to those we use regularily.
if [[ ${#binaries[@]} -eq 0 ]]
then
binaries=(ruby gem irb ri rdoc rake erb testrb)
fi
# Use the correct ruby.
__rvm_become "$ruby_string"
__rvm_ensure_has_environment_files
environment_identifier="$(__rvm_environment_identifier)"
# For each binary, we want to generate the wrapper / symlink
# it to the existing wrapper if needed.
for binary_name in "${binaries[@]}"
do
file_name="$rvm_wrappers_path/${environment_identifier}/${binary_name##*\/}"
if (( ${rvm_default_flag:-0} > 0 ))
then
prefix="default"
fi
if [[ ! -d "$rvm_bin_path" ]]
then
mkdir -p "$rvm_bin_path"
fi
if [[ -z "${prefix:-}" ]]
then
override_check=1
wrap_binary
# Symlink it into place.
if [[ -f "$file_name" ]]
then
if [[ "$binary_name" == "ruby" ]]
then
destination="$rvm_bin_path/$environment_identifier"
else
destination="$rvm_bin_path/${binary_name##*\/}-${environment_identifier}"
fi
rm -f "$destination"
ln -sf "$file_name" "$destination"
fi
elif [[ "--no-prefix" == "$prefix" ]]
then
override_check=1
wrap_binary
if [[ -f "$file_name" ]]
then
destination="$rvm_bin_path/${binary_name##*\/}"
if [[ -s "$destination" ]]
then
rm -f "$destination"
fi
ln -sf "$file_name" "$destination"
fi
else
symlink_binary
fi
done
exit $?