forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvm.sh
432 lines (359 loc) · 16 KB
/
kvm.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# kvm.sh
# Source this file from your .bash-profile or script to use
# "Constants"
_KVM_BUILDNUMBER="10319"
_KVM_RUNTIME_PACKAGE_NAME="kre"
_KVM_RUNTIME_FRIENDLY_NAME="K Runtime"
_KVM_RUNTIME_SHORT_NAME="KRE"
_KVM_RUNTIME_FOLDER_NAME=".k"
_KVM_COMMAND_NAME="kvm"
_KVM_VERSION_MANAGER_NAME="K Version Manager"
_KVM_DEFAULT_FEED="https://www.nuget.org/api/v2"
_KVM_HOME_VAR_NAME="KRE_HOME"
__kvm_has() {
type "$1" > /dev/null 2>&1
return $?
}
if __kvm_has "unsetopt"; then
unsetopt nomatch 2>/dev/null
fi
if [ -z "$KVM_USER_HOME" ]; then
eval KVM_USER_HOME="~/$_KVM_RUNTIME_FOLDER_NAME"
fi
_KVM_USER_PACKAGES="$KVM_USER_HOME/runtimes"
_KVM_ALIAS_DIR="$KVM_USER_HOME/alias"
if [ -z "$KRE_FEED" ]; then
KRE_FEED="$_KVM_DEFAULT_FEED"
fi
__kvm_find_latest() {
local platform="mono"
if ! __kvm_has "curl"; then
echo "$_KVM_COMMAND_NAME needs curl to proceed." >&2;
return 1
fi
local url="$KRE_FEED/GetUpdates()?packageIds=%27$_KVM_RUNTIME_PACKAGE_NAME-$platform%27&versions=%270.0%27&includePrerelease=true&includeAllVersions=false"
xml="$(curl $url 2>/dev/null)"
echo $xml | grep \<[a-zA-Z]:Version\>* >> /dev/null || return 1
version="$(echo $xml | sed 's/.*<[a-zA-Z]:Version>\([^<]*\).*/\1/')"
echo $version
}
__kvm_strip_path() {
echo "$1" | sed -e "s#$_KVM_USER_PACKAGES/[^/]*$2[^:]*:##g" -e "s#:$_KVM_USER_PACKAGES/[^/]*$2[^:]*##g" -e "s#$_KVM_USER_PACKAGES/[^/]*$2[^:]*##g"
}
__kvm_prepend_path() {
if [ -z "$1" ]; then
echo "$2"
else
echo "$2:$1"
fi
}
__kvm_package_version() {
local runtimeFullName="$1"
echo "$runtimeFullName" | sed "s/[^.]*.\(.*\)/\1/"
}
__kvm_package_name() {
local runtimeFullName="$1"
echo "$runtimeFullName" | sed "s/\([^.]*\).*/\1/"
}
__kvm_package_runtime() {
local runtimeFullName="$1"
echo "$runtimeFullName" | sed "s/$_KVM_RUNTIME_PACKAGE_NAME-\([^.-]*\).*/\1/"
}
__kvm_download() {
local runtimeFullName="$1"
local runtimeFolder="$2"
local pkgName=$(__kvm_package_name "$runtimeFullName")
local pkgVersion=$(__kvm_package_version "$runtimeFullName")
local url="$KRE_FEED/package/$pkgName/$pkgVersion"
local runtimeFile="$runtimeFolder/$runtimeFullName.nupkg"
if [ -e "$runtimeFolder" ]; then
echo "$runtimeFullName already installed."
return 0
fi
echo "Downloading $runtimeFullName from $KRE_FEED"
if ! __kvm_has "curl"; then
echo "$_KVM_COMMAND_NAME needs curl to proceed." >&2;
return 1
fi
mkdir -p "$runtimeFolder" > /dev/null 2>&1
local httpResult=$(curl -L -D - "$url" -o "$runtimeFile" 2>/dev/null | grep "^HTTP/1.1" | head -n 1 | sed "s/HTTP.1.1 \([0-9]*\).*/\1/")
[[ $httpResult == "404" ]] && echo "$runtimeFullName was not found in repository $KRE_FEED" && return 1
[[ $httpResult != "302" && $httpResult != "200" ]] && echo "HTTP Error $httpResult fetching $runtimeFullName from $KRE_FEED" && return 1
__kvm_unpack $runtimeFile $runtimeFolder
return $?
}
__kvm_unpack() {
local runtimeFile="$1"
local runtimeFolder="$2"
echo "Installing to $runtimeFolder"
if ! __kvm_has "unzip"; then
echo "$_KVM_COMMAND_NAME needs unzip to proceed." >&2;
return 1
fi
unzip $runtimeFile -d $runtimeFolder > /dev/null 2>&1
[ -e "$runtimeFolder/[Content_Types].xml" ] && rm "$runtimeFolder/[Content_Types].xml"
[ -e "$runtimeFolder/_rels/" ] && rm -rf "$runtimeFolder/_rels/"
[ -e "$runtimeFolder/package/" ] && rm -rf "$runtimeFolder/_package/"
[ -e "$runtimeFile" ] && rm -f "$runtimeFile"
#Set shell commands as executable
find "$runtimeFolder/bin/" -type f \
-exec sh -c "head -c 11 {} | grep '/bin/bash' > /dev/null" \; -print | xargs chmod 775
}
__kvm_requested_version_or_alias() {
local versionOrAlias="$1"
local runtimeBin=$(__kvm_locate_runtime_bin_from_full_name "$versionOrAlias")
# If the name specified is an existing package, just use it as is
if [ -n "$runtimeBin" ]; then
echo "$versionOrAlias"
else
if [ -e "$_KVM_ALIAS_DIR/$versionOrAlias.alias" ]; then
local runtimeFullName=$(cat "$_KVM_ALIAS_DIR/$versionOrAlias.alias")
local pkgName=$(echo $runtimeFullName | sed "s/\([^.]*\).*/\1/")
local pkgVersion=$(echo $runtimeFullName | sed "s/[^.]*.\(.*\)/\1/")
local pkgPlatform=$(echo "$pkgName" | sed "s/kre-\([^.-]*\).*/\1/")
else
local pkgVersion=$versionOrAlias
local pkgPlatform="mono"
fi
echo "$_KVM_RUNTIME_PACKAGE_NAME-$pkgPlatform.$pkgVersion"
fi
}
# This will be more relevant if we support global installs
__kvm_locate_runtime_bin_from_full_name() {
local runtimeFullName=$1
[ -e "$_KVM_USER_PACKAGES/$runtimeFullName/bin" ] && echo "$_KVM_USER_PACKAGES/$runtimeFullName/bin" && return
}
kvm()
{
if [ $# -lt 1 ]; then
$_KVM_COMMAND_NAME help
return
fi
case $1 in
"help" )
echo ""
echo "$_KVM_VERSION_MANAGER_NAME - Build $_KVM_BUILDNUMBER"
echo ""
echo "USAGE: $_KVM_COMMAND_NAME <command> [options]"
echo ""
echo "$_KVM_COMMAND_NAME upgrade"
echo "install latest $_KVM_RUNTIME_SHORT_NAME from feed"
echo "add $_KVM_RUNTIME_SHORT_NAME bin to path of current command line"
echo "set installed version as default"
echo ""
echo "$_KVM_COMMAND_NAME install <semver>|<alias>|<nupkg>|latest [-a|-alias <alias>] [-p -persistent]"
echo "<semver>|<alias> install requested $_KVM_RUNTIME_SHORT_NAME from feed"
echo "<nupkg> install requested $_KVM_RUNTIME_SHORT_NAME from local package on filesystem"
echo "latest install latest version of $_KVM_RUNTIME_SHORT_NAME from feed"
echo "-a|-alias <alias> set alias <alias> for requested $_KVM_RUNTIME_SHORT_NAME on install"
echo "-p -persistent set installed version as default"
echo "add $_KVM_RUNTIME_SHORT_NAME bin to path of current command line"
echo ""
echo "$_KVM_COMMAND_NAME use <semver>|<alias>|<package>|none [-p -persistent]"
echo "<semver>|<alias>|<package> add $_KVM_RUNTIME_SHORT_NAME bin to path of current command line "
echo "none remove $_KVM_RUNTIME_SHORT_NAME bin from path of current command line"
echo "-p -persistent set selected version as default"
echo ""
echo "$_KVM_COMMAND_NAME list"
echo "list $_KVM_RUNTIME_SHORT_NAME versions installed "
echo ""
echo "$_KVM_COMMAND_NAME alias"
echo "list $_KVM_RUNTIME_SHORT_NAME aliases which have been defined"
echo ""
echo "$_KVM_COMMAND_NAME alias <alias>"
echo "display value of the specified alias"
echo ""
echo "$_KVM_COMMAND_NAME alias <alias> <semver>|<alias>|<package>"
echo "<alias> the name of the alias to set"
echo "<semver>|<alias>|<package> the $_KVM_RUNTIME_SHORT_NAME version to set the alias to. Alternatively use the version of the specified alias"
echo ""
echo "$_KVM_COMMAND_NAME unalias <alias>"
echo "remove the specified alias"
echo ""
;;
"upgrade" )
[ $# -ne 1 ] && kvm help && return
$_KVM_COMMAND_NAME install latest -p
;;
"install" )
[ $# -lt 2 ] && kvm help && return
shift
local persistent=
local versionOrAlias=
local alias=
while [ $# -ne 0 ]
do
if [[ $1 == "-p" || $1 == "-persistent" ]]; then
local persistent="-p"
elif [[ $1 == "-a" || $1 == "-alias" ]]; then
local alias=$2
shift
elif [[ -n $1 ]]; then
[[ -n $versionOrAlias ]] && echo "Invalid option $1" && kvm help && return 1
local versionOrAlias=$1
fi
shift
done
if [[ "$versionOrAlias" == "latest" ]]; then
echo "Determining latest version"
versionOrAlias=$(__kvm_find_latest)
[[ $? == 1 ]] && echo "Error: Could not find latest version from feed $KRE_FEED" && return 1
echo "Latest version is $versionOrAlias"
fi
if [[ "$versionOrAlias" == *.nupkg ]]; then
local runtimeFullName=$(basename $versionOrAlias | sed "s/\(.*\)\.nupkg/\1/")
local runtimeVersion=$(__kvm_package_version "$runtimeFullName")
local runtimeFolder="$_KVM_USER_PACKAGES/$runtimeFullName"
local runtimeFile="$runtimeFolder/$runtimeFullName.nupkg"
if [ -e "$runtimeFolder" ]; then
echo "$runtimeFullName already installed"
else
mkdir "$runtimeFolder" > /dev/null 2>&1
cp -a "$versionOrAlias" "$runtimeFile"
__kvm_unpack "$runtimeFile" "$runtimeFolder"
[[ $? == 1 ]] && return 1
fi
$_KVM_COMMAND_NAME use "$runtimeVersion" "$persistent"
[[ -n $alias ]] && kvm alias "$alias" "$runtimeVersion"
else
local runtimeFullName="$(__kvm_requested_version_or_alias $versionOrAlias)"
local runtimeFolder="$_KVM_USER_PACKAGES/$runtimeFullName"
__kvm_download "$runtimeFullName" "$runtimeFolder"
[[ $? == 1 ]] && return 1
$_KVM_COMMAND_NAME use "$versionOrAlias" "$persistent"
[[ -n $alias ]] && kvm alias "$alias" "$versionOrAlias"
fi
;;
"use" )
[ $# -gt 3 ] && $_KVM_COMMAND_NAME help && return
[ $# -lt 2 ] && $_KVM_COMMAND_NAME help && return
shift
local persistent=
while [ $# -ne 0 ]
do
if [[ $1 == "-p" || $1 == "-persistent" ]]; then
local persistent="true"
elif [[ -n $1 ]]; then
local versionOrAlias=$1
fi
shift
done
if [[ $versionOrAlias == "none" ]]; then
echo "Removing $_KVM_RUNTIME_SHORT_NAME from process PATH"
# Strip other version from PATH
PATH=$(__kvm_strip_path "$PATH" "/bin")
if [[ -n $persistent && -e "$_KVM_ALIAS_DIR/default.alias" ]]; then
echo "Setting default $_KVM_RUNTIME_SHORT_NAME to none"
rm "$_KVM_ALIAS_DIR/default.alias"
fi
return 0
fi
local runtimeFullName=$(__kvm_requested_version_or_alias "$versionOrAlias")
local runtimeBin=$(__kvm_locate_runtime_bin_from_full_name "$runtimeFullName")
if [[ -z $runtimeBin ]]; then
echo "Cannot find $runtimeFullName, do you need to run '$_KVM_COMMAND_NAME install $versionOrAlias'?"
return 1
fi
echo "Adding" $runtimeBin "to process PATH"
PATH=$(__kvm_strip_path "$PATH" "/bin")
PATH=$(__kvm_prepend_path "$PATH" "$runtimeBin")
if [[ -n $persistent ]]; then
local runtimeVersion=$(__kvm_package_version "$runtimeFullName")
$_KVM_COMMAND_NAME alias default "$runtimeVersion"
fi
;;
"alias" )
[[ $# -gt 3 ]] && kvm help && return
[[ ! -e "$_KVM_ALIAS_DIR/" ]] && mkdir "$_KVM_ALIAS_DIR/" > /dev/null
if [[ $# == 1 ]]; then
echo ""
local format="%-20s %s\n"
printf "$format" "Alias" "Name"
printf "$format" "-----" "----"
if [ -d "$_KVM_ALIAS_DIR" ]; then
for __kvm_file in $(find "$_KVM_ALIAS_DIR" -name *.alias); do
local alias="$(basename $__kvm_file | sed 's/\.alias//')"
local name="$(cat $__kvm_file)"
printf "$format" "$alias" "$name"
done
fi
echo ""
return
fi
local name="$2"
if [[ $# == 2 ]]; then
[[ ! -e "$_KVM_ALIAS_DIR/$name.alias" ]] && echo "There is no alias called '$name'" && return
cat "$_KVM_ALIAS_DIR/$name.alias"
echo ""
return
fi
local runtimeFullName=$(__kvm_requested_version_or_alias "$3")
[[ ! -d "$_KVM_USER_PACKAGES/$runtimeFullName" ]] && echo "$runtimeFullName is not an installed $_KVM_RUNTIME_SHORT_NAME version" && return 1
local action="Setting"
[[ -e "$_KVM_ALIAS_DIR/$name.alias" ]] && action="Updating"
echo "$action alias '$name' to '$runtimeFullName'"
echo "$runtimeFullName" > "$_KVM_ALIAS_DIR/$name.alias"
;;
"unalias" )
[[ $# -ne 2 ]] && kvm help && return
local name=$2
local aliasPath="$_KVM_ALIAS_DIR/$name.alias"
[[ ! -e "$aliasPath" ]] && echo "Cannot remove alias, '$name' is not a valid alias name" && return 1
echo "Removing alias $name"
rm "$aliasPath" >> /dev/null 2>&1
;;
"list" )
[[ $# -gt 2 ]] && kvm help && return
[[ ! -d $_KVM_USER_PACKAGES ]] && echo "$_KVM_RUNTIME_FRIENDLY_NAME is not installed." && return 1
local searchGlob="$_KVM_RUNTIME_PACKAGE_NAME-*"
if [ $# == 2 ]; then
local versionOrAlias=$2
local searchGlob=$(__kvm_requested_version_or_alias "$versionOrAlias")
fi
echo ""
# Separate empty array declaration from initialization
# to avoid potential ZSH error: local:217: maximum nested function level reached
local arr
arr=()
# Z shell array-index starts at one.
local i=1
local format="%-20s %s\n"
if [ -d "$_KVM_ALIAS_DIR" ]; then
for __kvm_file in $(find "$_KVM_ALIAS_DIR" -name *.alias); do
arr[$i]="$(basename $__kvm_file | sed 's/\.alias//')/$(cat $__kvm_file)"
let i+=1
done
fi
local formatString="%-6s %-20s %-7s %-20s %s\n"
printf "$formatString" "Active" "Version" "Runtime" "Location" "Alias"
printf "$formatString" "------" "-------" "-------" "--------" "-----"
local formattedHome=`(echo $_KVM_USER_PACKAGES | sed s=$HOME=~=g)`
for f in $(find $_KVM_USER_PACKAGES -name "$searchGlob" \( -type d -or -type l \) -prune -exec basename {} \;); do
local active=""
[[ $PATH == *"$_KVM_USER_PACKAGES/$f/bin"* ]] && local active=" *"
local pkgName=$(__kvm_package_runtime "$f")
local pkgVersion=$(__kvm_package_version "$f")
local alias=""
local delim=""
for i in "${arr[@]}"; do
temp="$_KVM_RUNTIME_PACKAGE_NAME-$pkgName.$pkgVersion"
temp2="$_KVM_RUNTIME_PACKAGE_NAME-$pkgName-x86.$pkgVersion"
if [[ ${i#*/} == $temp || ${i#*/} == $temp2 ]]; then
alias+="$delim${i%/*}"
delim=", "
fi
done
printf "$formatString" "$active" "$pkgVersion" "$pkgName" "$formattedHome" "$alias"
[[ $# == 2 ]] && echo "" && return 0
done
echo ""
[[ $# == 2 ]] && echo "$versionOrAlias not found" && return 1
;;
*)
echo "Unknown command $1"
return 1
esac
return 0
}
# Generate the command function using the constant defined above.
$_KVM_COMMAND_NAME list default >/dev/null && $_KVM_COMMAND_NAME use default >/dev/null || true