-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcvm.sh
executable file
·308 lines (247 loc) · 7.33 KB
/
cvm.sh
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env bash
CVM_VERSION="0.1.0"
cvm_usage() {
echo "Composer Version Manager (v$CVM_VERSION)"
echo ""
echo "Usage:"
echo " cvm COMMAND [OPTIONS] [ARGUMENTS]"
echo ""
echo "Options:"
echo " -h, --help Show this message"
echo " -v, --version Display the application version"
echo " -r, --reload Reload cvm in the current shell"
echo ""
echo "Commands:"
echo " current Show the current version of Composer"
echo " install Install a specific version of Composer"
echo " update Update an installed version of Composer"
echo " remove Remove a specific version of Composer"
echo " use Use a specific version of Composer"
echo " list Show installed versions of Composer"
}
cvm_get_version() {
VERSION="$1"
if [ "$VERSION" = "" ] && [ -f ".cvmrc" ]; then
VERSION=$(cat .cvmrc)
fi
case "$VERSION" in
"" | "stable" | "latest-stable")
VERSION="stable"
;;
"preview" | "latest-preview")
VERSION="preview"
;;
"snapshot" | "latest")
VERSION="latest"
;;
"1" | "1.x" | "latest-1.x")
VERSION="1.x"
;;
"2" | "2.x" | "latest-2.x")
VERSION="2.x"
;;
esac
echo "$VERSION"
}
cvm_reload() {
source "${CVM_DIR}/cvm.sh"
}
cvm_get_url() {
VERSION=$(cvm_get_version "$1")
BASE_URL="https://getcomposer.org"
case "$VERSION" in
"stable" | "preview" | "1.x" | "2.x")
VERSION="latest-$VERSION"
;;
esac
if [ "$VERSION" = "latest" ]; then
URL="${BASE_URL}/composer.phar"
else
URL="${BASE_URL}/download/${VERSION}/composer.phar"
fi
echo "$URL"
}
cvm_use() {
VERSION=$(cvm_get_version "$1")
if [ "$VERSION" = "system" ]; then
unalias composer 2>/dev/null
export CVM_COMPOSER_VERSION=""
export CVM_COMPOSER_PHAR=""
return 0
fi
FILE="${CVM_DIR}/versions/${VERSION}/composer.phar"
if [ -f "$FILE" ]; then
alias composer="$FILE"
else
echo "Composer version ${VERSION} is not installed"
return 1
fi
export CVM_COMPOSER_VERSION="$VERSION"
export CVM_COMPOSER_PHAR="$FILE"
echo "Using $(composer --version)"
}
cvm_install() {
VERSION=$(cvm_get_version "$1")
URL=$(cvm_get_url "$1")
# Stage the download so we can grab the exact version
STAGING_DIR="${CVM_DIR}/versions/.staging/${VERSION}"
STAGING_FILE="${STAGING_DIR}/composer.phar"
echo "Downloading Composer version ${VERSION}"
rm -rf "$STAGING_DIR"
mkdir -p "$STAGING_DIR"
curl -o "$STAGING_FILE" "$URL" >/dev/null 2>/dev/null
chmod +x "$STAGING_FILE"
STAGING_VERSION=$("$STAGING_FILE" --version 2>/dev/null | cut -d" " -f3)
if [ -z "$STAGING_VERSION" ]; then
echo "Failed to download Composer version ${VERSION}"
rm -rf "$STAGING_DIR"
return 1
fi
HUMAN_VERSION="$VERSION"
if [ "$STAGING_VERSION" != "$VERSION" ]; then
HUMAN_VERSION="${VERSION} (${STAGING_VERSION})"
fi
# Make sure the staging version isn't already installed
INSTALL_DIR="${CVM_DIR}/versions/${STAGING_VERSION}"
INSTALL_FILE="${INSTALL_DIR}/composer.phar"
if [ -f "$INSTALL_FILE" ]; then
echo "Composer version ${HUMAN_VERSION} is already installed"
rm -rf "$STAGING_DIR"
return 1
fi
# Install the staging version and clean up
echo "Installing Composer version ${HUMAN_VERSION}"
mv "$STAGING_DIR" "$INSTALL_DIR"
if [ "$STAGING_VERSION" != "$VERSION" ]; then
ALIAS_DIR="${CVM_DIR}/versions/${VERSION}"
rm -rf "$ALIAS_DIR" 2>/dev/null
ln -s "$INSTALL_DIR" "$ALIAS_DIR"
fi
cvm_use "$VERSION"
}
cvm_update() {
VERSION=$(cvm_get_version "$1")
URL=$(cvm_get_url "$1")
STAGING_DIR="${CVM_DIR}/versions/.staging/${VERSION}"
STAGING_FILE="${STAGING_DIR}/composer.phar"
INSTALL_DIR="${CVM_DIR}/versions/${VERSION}"
INSTALL_FILE="${INSTALL_DIR}/composer.phar"
if [ ! -f "$INSTALL_FILE" ]; then
echo "Composer version ${VERSION} is not installed"
return 1
fi
case "$VERSION" in
"stable" | "preview" | "latest" | "1.x" | "2.x")
echo "Updating Composer version ${VERSION}"
;;
*)
echo "Composer version ${VERSION} is not eligible for updates, please try one of stable, preview, latest, 1.x, 2.x"
return 1
;;
esac
mkdir -p "$INSTALL_DIR"
curl -o "$INSTALL_FILE" "$URL" >/dev/null 2>/dev/null
chmod +x "$INSTALL_FILE"
cvm_use "$VERSION"
}
cvm_remove() {
VERSION=$(cvm_get_version "$1")
if [ "$VERSION" = "system" ]; then
echo "Refusing to remove system Composer"
return 1
fi
DIR="${CVM_DIR}/versions/${VERSION}"
if [ ! -d "$DIR" ]; then
echo "Composer version ${VERSION} is not installed"
return 1
fi
CURRENT="$CVM_COMPOSER_PHAR"
TARGET="${CVM_DIR}/versions/${VERSION}/composer.phar"
rm -rf "$DIR" 2>/dev/null # brutal efficiency?
find -L "${CVM_DIR}/versions" -type l -exec rm -f {} \;
echo "Removed Composer version ${VERSION}"
if [ ! -f "$CURRENT" ]; then
cvm_use "system"
fi
}
cvm_current() {
if [ -f "$CVM_COMPOSER_PHAR" ]; then
echo "Using $("$CVM_COMPOSER_PHAR" --version)"
return 0
fi
COMMAND=$(command -v composer)
if [ -z "$CVM_COMPOSER_PHAR" ] && [ ! -z "$COMMAND" ]; then
echo "Using system $(composer --version)"
return 0
fi
echo "Composer not found"
}
cvm_list() {
for V in $(command ls "${CVM_DIR}/versions"); do
if [ ! -L "${CVM_DIR}/versions/${V}" ]; then
echo "$V"
fi
done
for V in $(command ls "${CVM_DIR}/versions"); do
if [ -L "${CVM_DIR}/versions/${V}" ]; then
T=$(readlink "${CVM_DIR}/versions/${V}")
echo "$V -> $(basename "$T")"
fi
done
}
cvm() {
if [ "$1" = "" ]; then
cvm_usage
return 0
fi
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print tolower($1)}'`
case $PARAM in
"--help" | "-h")
cvm_usage
return 0
;;
"--version" | "-v")
echo "$CVM_VERSION"
return 0
;;
"--reload" | "-r")
cvm_reload
return 0
;;
"current")
cvm_current
return 0
;;
"install")
shift
cvm_install $@
return 0
;;
"update")
shift
cvm_update $@
return 0
;;
"remove" | "uninstall")
shift
cvm_remove $@
return 0
;;
"use")
shift
cvm_use $@
return 0
;;
"list")
cvm_list
return 0
;;
*)
echo "Unknown Option: ${PARAM}"
return 1
;;
esac
shift
done
}