Skip to content

Commit d970cec

Browse files
committed
Updated kvm to the latest versions
1 parent 9b76381 commit d970cec

File tree

2 files changed

+186
-109
lines changed

2 files changed

+186
-109
lines changed

kvm.ps1

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ param(
1010
[switch] $x64 = $false,
1111
[switch] $svr50 = $false,
1212
[switch] $svrc50 = $false,
13+
[alias("a")]
14+
[string] $alias = $null,
1315
[parameter(Position=1, ValueFromRemainingArguments=$true)]
1416
[string[]]$args=@()
1517
)
@@ -18,12 +20,18 @@ $userKrePath = $env:USERPROFILE + "\.kre"
1820
$userKrePackages = $userKrePath + "\packages"
1921
$globalKrePath = $env:ProgramFiles + "\KRE"
2022
$globalKrePackages = $globalKrePath + "\packages"
23+
$feed = $env:KRE_NUGET_API_URL
24+
25+
if (!$feed)
26+
{
27+
$feed = "https://www.myget.org/F/aspnetvnext/api/v2";
28+
}
2129

2230
$scriptPath = $myInvocation.MyCommand.Definition
2331

2432
function Kvm-Help {
2533
@"
26-
K Runtime Environment Version Manager - Build 538
34+
K Runtime Environment Version Manager - Build 10002
2735
2836
USAGE: kvm <command> [options]
2937
@@ -35,29 +43,34 @@ kvm upgrade [-x86][-x64] [-svr50][-svrc50] [-g|-global] [-proxy <ADDRESS>]
3543
-f|-force upgrade even if latest is already installed
3644
-proxy <ADDRESS> use given address as proxy when accessing remote server
3745
38-
kvm install <semver>|<alias>|<nupkg> [-x86][-x64] [-svr50][-svrc50] [-g|-global]
39-
install requested KRE from feed
46+
kvm install <semver>|<alias>|<nupkg>|latest [-x86][-x64] [-svr50][-svrc50] [-a|-alias <alias>] [-g|-global] [-f|-force]
47+
<semver>|<alias> install requested KRE from feed
48+
<nupkg> install requested KRE from package on local filesystem
49+
latest install latest KRE from feed
4050
add KRE bin to path of current command line
51+
-p|-persistent add KRE bin to PATH environment variables persistently
52+
-a|-alias <alias> set alias <alias> for requested KRE on install
4153
-g|-global install to machine-wide location
4254
-f|-force install even if specified version is already installed
4355
4456
kvm use <semver>|<alias>|none [-x86][-x64] [-svr50][-svrc50] [-p|-persistent] [-g|-global]
45-
<semver>|<alias> add KRE bin to path of current command line
57+
<semver>|<alias> add KRE bin to path of current command line
4658
none remove KRE bin from path of current command line
4759
-p|-persistent add KRE bin to PATH environment variables persistently
4860
-g|-global combined with -p to change machine PATH instead of user PATH
4961
5062
kvm list
51-
list KRE versions installed
63+
list KRE versions installed
5264
5365
kvm alias
5466
list KRE aliases which have been defined
5567
5668
kvm alias <alias>
5769
display value of named alias
5870
59-
kvm alias <alias> <semver> [-x86][-x64] [-svr50][-svrc50]
60-
set alias to specific version
71+
kvm alias <alias> <semver>|<alias> [-x86][-x64] [-svr50][-svrc50]
72+
<alias> The name of the alias to set
73+
<semver>|<alias> The KRE version to set the alias to. Alternatively use the version of the specified alias
6174
6275
"@ | Write-Host
6376
}
@@ -109,21 +122,19 @@ function Kvm-Global-Setup {
109122

110123
function Kvm-Global-Upgrade {
111124
$persistent = $true
125+
$alias="default"
112126
If (Needs-Elevation) {
113127
$arguments = "& '$scriptPath' upgrade -global $(Requested-Switches)"
114128
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments -Wait
115129
break
116130
}
117-
$version = Kvm-Find-Latest (Requested-Platform "svr50") (Requested-Architecture "x86")
118-
Kvm-Global-Install $version
119-
Kvm-Alias-Set "default" $version
131+
Kvm-Global-Install "latest"
120132
}
121133

122134
function Kvm-Upgrade {
123135
$persistent = $true
124-
$version = Kvm-Find-Latest (Requested-Platform "svr50") (Requested-Architecture "x86")
125-
Kvm-Install $version
126-
Kvm-Alias-Set "default" $version
136+
$alias="default"
137+
Kvm-Install "latest"
127138
}
128139

129140
function Add-Proxy-If-Specified {
@@ -152,30 +163,26 @@ param(
152163
)
153164
Write-Host "Determining latest version"
154165

155-
$url = "https://www.myget.org/F/aspnetvnext/api/v2/GetUpdates()?packageIds=%27KRE-$platform-$architecture%27&versions=%270.0%27&includePrerelease=true&includeAllVersions=false"
166+
$url = "$feed/GetUpdates()?packageIds=%27KRE-$platform-$architecture%27&versions=%270.0%27&includePrerelease=true&includeAllVersions=false"
156167

157168
$wc = New-Object System.Net.WebClient
158169
$wc.Credentials = new-object System.Net.NetworkCredential("aspnetreadonly", "4d8a2d9c-7b80-4162-9978-47e918c9658c")
159170
Add-Proxy-If-Specified($wc)
160171
[xml]$xml = $wc.DownloadString($url)
161172

162-
$version = Select-Xml "//d:Version" -Namespace @{d='http://schemas.microsoft.com/ado/2007/08/dataservices'} $xml
173+
$version = Select-Xml "//d:Version" -Namespace @{d='http://schemas.microsoft.com/ado/2007/08/dataservices'} $xml
163174

164175
return $version
165176
}
166177

167-
function Kvm-Install-Latest {
168-
Kvm-Install (Kvm-Find-Latest (Requested-Platform "svr50") (Requested-Architecture "x86"))
169-
}
170-
171178
function Do-Kvm-Download {
172179
param(
173180
[string] $kreFullName,
174181
[string] $packagesFolder
175182
)
176183
$parts = $kreFullName.Split(".", 2)
177184

178-
$url = "https://www.myget.org/F/aspnetvnext/api/v2/package/" + $parts[0] + "/" + $parts[1]
185+
$url = "$feed/package/" + $parts[0] + "/" + $parts[1]
179186
$kreFolder = Join-Path $packagesFolder $kreFullName
180187
$kreFile = Join-Path $kreFolder "$kreFullName.nupkg"
181188

@@ -189,13 +196,13 @@ param(
189196
}
190197
}
191198

192-
Write-Host "Downloading" $kreFullName "from https://www.myget.org/F/aspnetvnext/api/v2/"
199+
Write-Host "Downloading" $kreFullName "from $feed"
193200

194201
#Downloading to temp location
195202
$kreTempDownload = Join-Path $packagesFolder "temp"
196203
$tempKreFile = Join-Path $kreTempDownload "$kreFullName.nupkg"
197204

198-
if(Test-Path $kreTempDownload) {
205+
if(Test-Path $kreTempDownload) {
199206
del "$kreTempDownload\*" -recurse
200207
} else {
201208
md $kreTempDownload -Force | Out-Null
@@ -208,7 +215,7 @@ param(
208215

209216
Do-Kvm-Unpack $tempKreFile $kreTempDownload
210217

211-
md $kreFolder -Force | Out-Null
218+
md $kreFolder -Force | Out-Null
212219
Write-Host "Installing to $kreFolder"
213220
mv "$kreTempDownload\*" $kreFolder
214221
}
@@ -233,7 +240,7 @@ param(
233240
#make it a nupkg again
234241
Rename-Item $kreZip $kreFile
235242
}
236-
243+
237244
If (Test-Path ($kreFolder + "\[Content_Types].xml")) {
238245
Remove-Item ($kreFolder + "\[Content_Types].xml")
239246
}
@@ -256,10 +263,17 @@ param(
256263
break
257264
}
258265

266+
if ($versionOrAlias -eq "latest") {
267+
$versionOrAlias = Kvm-Find-Latest (Requested-Platform "svr50") (Requested-Architecture "x86")
268+
}
269+
259270
$kreFullName = Requested-VersionOrAlias $versionOrAlias
260271

261272
Do-Kvm-Download $kreFullName $globalKrePackages
262273
Kvm-Use $versionOrAlias
274+
if (![string]::IsNullOrWhiteSpace($alias)) {
275+
Kvm-Alias-Set $alias $versionOrAlias
276+
}
263277
}
264278

265279
function Kvm-Install {
@@ -276,36 +290,44 @@ param(
276290
del $kreFolder -Recurse -Force
277291
$folderExists = $false;
278292
}
279-
293+
280294
if($folderExists) {
281295
Write-Host "Target folder '$kreFolder' already exists"
282296
} else {
283297
$tempUnpackFolder = Join-Path $userKrePackages "temp"
284298
$tempKreFile = Join-Path $tempUnpackFolder "$kreFullName.nupkg"
285-
286-
if(Test-Path $tempUnpackFolder) {
299+
300+
if(Test-Path $tempUnpackFolder) {
287301
del "$tempUnpackFolder\*" -recurse
288302
} else {
289303
md $tempUnpackFolder -Force | Out-Null
290304
}
291305
copy $versionOrAlias $tempKreFile
292-
306+
293307
Do-Kvm-Unpack $tempKreFile $tempUnpackFolder
294308
md $kreFolder -Force | Out-Null
295309
Write-Host "Installing to $kreFolder"
296310
mv "$tempUnpackFolder\*" $kreFolder
297311
}
298312

299-
$kreBin = "$kreFolder\bin"
300-
Write-Host "Adding" $kreBin "to process PATH"
301-
Set-Path (Change-Path $env:Path $kreBin ($globalKrePackages, $userKrePackages))
302-
}
303-
else
304-
{
305-
$kreFullName = Requested-VersionOrAlias $versionOrAlias
313+
$packageVersion = Package-Version $kreFullName
314+
Kvm-Use $packageVersion
315+
if (![string]::IsNullOrWhiteSpace($alias)) {
316+
Kvm-Alias-Set $alias $packageVersion
317+
}
318+
}
319+
else
320+
{
321+
if ($versionOrAlias -eq "latest") {
322+
$versionOrAlias = Kvm-Find-Latest (Requested-Platform "svr50") (Requested-Architecture "x86")
323+
}
324+
$kreFullName = Requested-VersionOrAlias $versionOrAlias
306325

307326
Do-Kvm-Download $kreFullName $userKrePackages
308327
Kvm-Use $versionOrAlias
328+
if (![string]::IsNullOrWhiteSpace($alias)) {
329+
Kvm-Alias-Set $alias $versionOrAlias
330+
}
309331
}
310332
}
311333

@@ -325,7 +347,7 @@ function Kvm-List {
325347
}
326348

327349
filter List-Parts {
328-
$hasBin = Test-Path($_.FullName+"\bin")
350+
$hasBin = Test-Path($_.FullName+"\bin")
329351
if (!$hasBin) {
330352
return
331353
}
@@ -363,7 +385,7 @@ param(
363385
Write-Host "Removing KRE from process PATH"
364386
Set-Path (Change-Path $env:Path "" ($globalKrePackages, $userKrePackages))
365387

366-
if ($persistent) {
388+
if ($persistent) {
367389
Write-Host "Removing KRE from machine PATH"
368390
$machinePath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
369391
$machinePath = Change-Path $machinePath "" ($globalKrePackages, $userKrePackages)
@@ -378,7 +400,7 @@ param(
378400
if ($kreBin -eq $null) {
379401
Write-Host "Cannot find $kreFullName, do you need to run 'kvm install $versionOrAlias'?"
380402
return
381-
}
403+
}
382404

383405
Write-Host "Adding" $kreBin "to process PATH"
384406
Set-Path (Change-Path $env:Path $kreBin ($globalKrePackages, $userKrePackages))
@@ -399,7 +421,7 @@ param(
399421
Write-Host "Removing KRE from process PATH"
400422
Set-Path (Change-Path $env:Path "" ($globalKrePackages, $userKrePackages))
401423

402-
if ($persistent) {
424+
if ($persistent) {
403425
Write-Host "Removing KRE from user PATH"
404426
$userPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
405427
$userPath = Change-Path $userPath "" ($globalKrePackages, $userKrePackages)
@@ -414,12 +436,12 @@ param(
414436
if ($kreBin -eq $null) {
415437
Write-Host "Cannot find $kreFullName, do you need to run 'kvm install $versionOrAlias'?"
416438
return
417-
}
439+
}
418440

419441
Write-Host "Adding" $kreBin "to process PATH"
420442
Set-Path (Change-Path $env:Path $kreBin ($globalKrePackages, $userKrePackages))
421443

422-
if ($persistent) {
444+
if ($persistent) {
423445
Write-Host "Adding $kreBin to user PATH"
424446
$userPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
425447
$userPath = Change-Path $userPath $kreBin ($globalKrePackages, $userKrePackages)
@@ -438,15 +460,20 @@ param(
438460
[string] $name
439461
)
440462
md ($userKrePath + "\alias\") -Force | Out-Null
441-
Write-Host "Alias '$name' is set to" (Get-Content ($userKrePath + "\alias\" + $name + ".txt"))
463+
$aliasFilePath=$userKrePath + "\alias\" + $name + ".txt"
464+
if (!(Test-Path $aliasFilePath)) {
465+
Write-Host "Alias '$name' does not exist"
466+
} else {
467+
Write-Host "Alias '$name' is set to" (Get-Content ($userKrePath + "\alias\" + $name + ".txt"))
468+
}
442469
}
443470

444471
function Kvm-Alias-Set {
445472
param(
446473
[string] $name,
447474
[string] $value
448475
)
449-
$kreFullName = "KRE-" + (Requested-Platform "svr50") + "-" + (Requested-Architecture "x86") + "." + $value
476+
$kreFullName = Requested-VersionOrAlias $value
450477

451478
Write-Host "Setting alias '$name' to '$kreFullName'"
452479
md ($userKrePath + "\alias\") -Force | Out-Null
@@ -471,6 +498,13 @@ param(
471498
return $null
472499
}
473500

501+
function Package-Version() {
502+
param(
503+
[string] $kreFullName
504+
)
505+
return $kreFullName -replace '[^.]*.(.*)', '$1'
506+
}
507+
474508
function Requested-VersionOrAlias() {
475509
param(
476510
[string] $versionOrAlias
@@ -496,7 +530,7 @@ param(
496530
)
497531
if ($svr50 -and $svrc50) {
498532
Throw "This command cannot accept both -svr50 and -svrc50"
499-
}
533+
}
500534
if ($svr50) {
501535
return "svr50"
502536
}
@@ -512,7 +546,7 @@ param(
512546
)
513547
if ($x86 -and $x64) {
514548
Throw "This command cannot accept both -x86 and -x64"
515-
}
549+
}
516550
if ($x86) {
517551
return "x86"
518552
}
@@ -534,7 +568,7 @@ param(
534568
foreach($removePath in $removePaths) {
535569
if ($portion.StartsWith($removePath)) {
536570
$skip = $true
537-
}
571+
}
538572
}
539573
if (!$skip) {
540574
$newPath = $newPath + ";" + $portion
@@ -574,7 +608,6 @@ function Requested-Switches() {
574608
switch -wildcard ($command + " " + $args.Count) {
575609
"setup 0" {Kvm-Global-Setup}
576610
"upgrade 0" {Kvm-Global-Upgrade}
577-
# "install 0" {Kvm-Global-Install-Latest}
578611
"install 1" {Kvm-Global-Install $args[0]}
579612
# "list 0" {Kvm-Global-List}
580613
"use 1" {Kvm-Global-Use $args[0]}
@@ -584,7 +617,6 @@ function Requested-Switches() {
584617
switch -wildcard ($command + " " + $args.Count) {
585618
"setup 0" {Kvm-Global-Setup}
586619
"upgrade 0" {Kvm-Upgrade}
587-
# "install 0" {Kvm-Install-Latest}
588620
"install 1" {Kvm-Install $args[0]}
589621
"list 0" {Kvm-List}
590622
"use 1" {Kvm-Use $args[0]}

0 commit comments

Comments
 (0)