Updating Kvm in home/dev

This commit is contained in:
Praburaj 2015-01-06 10:23:35 -08:00
parent 0ae0435e98
commit 8e4c1393a7
2 changed files with 321 additions and 191 deletions

441
kvm.ps1
View File

@ -1,36 +1,50 @@
param(
[parameter(Position=0)]
[string] $command,
[string] $proxy,
[switch] $verbosity = $false,
[alias("g")][switch] $global = $false,
[alias("p")][switch] $persistent = $false,
[alias("f")][switch] $force = $false,
[alias("r")][string] $runtime,
[switch] $x86 = $false,
[switch] $amd64 = $false,
[string] $Command,
[string] $Proxy,
[switch] $Verbosity = $false,
[alias("g")][switch] $Global = $false,
[alias("p")][switch] $Persistent = $false,
[alias("f")][switch] $Force = $false,
[alias("r")][string] $Runtime,
[alias("arch")][string] $Architecture,
[switch] $X86 = $false,
[switch] $Amd64 = $false,
#deprecated
[switch] $x64 = $false,
[switch] $X64 = $false,
#deprecated
[switch] $svr50 = $false,
[switch] $Svr50 = $false,
#deprecated
[switch] $svrc50 = $false,
[alias("w")][switch] $wait = $false,
[switch] $Svrc50 = $false,
[alias("w")][switch] $Wait = $false,
[alias("a")]
[string] $alias = $null,
[string] $Alias = $null,
[switch] $NoNative = $false,
[parameter(Position=1, ValueFromRemainingArguments=$true)]
[string[]]$args=@()
[string[]]$Args=@(),
[switch] $Quiet,
[string] $OutputVariable,
[switch] $AssumeElevated
)
$selectedArch=$null;
$defaultArch="x86"
$selectedRuntime=$null
$defaultRuntime="CLR"
$userKrePath = $env:USERPROFILE + "\.kre"
# Get or calculate userKrePath
$userKrePath = $env:USER_KRE_PATH
if(!$userKrePath) { $userKrePath = $env:USERPROFILE + "\.kre" }
$userKrePackages = $userKrePath + "\packages"
$globalKrePath = $env:ProgramFiles + "\KRE"
# Get or calculate globalKrePath
$globalKrePath = $env:GLOBAL_KRE_PATH
if(!$globalKrePath) { $globalKrePath = $env:ProgramFiles + "\KRE" }
$globalKrePackages = $globalKrePath + "\packages"
$feed = $env:KRE_NUGET_API_URL
$feed = $env:KRE_FEED
# In some environments, like Azure Websites, the Write-* cmdlets don't work
$useHostOutputMethods = $true
function String-IsEmptyOrWhitespace([string]$str) {
return [string]::IsNullOrEmpty($str) -or $str.Trim().length -eq 0
@ -41,37 +55,41 @@ if (!$feed)
$feed = "https://www.myget.org/F/aspnetvnext/api/v2";
}
$feed = $feed.TrimEnd("/")
$scriptPath = $myInvocation.MyCommand.Definition
function Kvm-Help {
@"
K Runtime Environment Version Manager - Build 10017
K Runtime Environment Version Manager - Build 10058
USAGE: kvm <command> [options]
kvm upgrade [-x86][-amd64] [-r|-runtime CLR|CoreCLR] [-g|-global] [-f|-force] [-proxy <ADDRESS>]
kvm upgrade [-X86][-Amd64] [-r|-Runtime CLR|CoreCLR] [-g|-Global] [-f|-Force] [-Proxy <ADDRESS>] [-NoNative]
install latest KRE from feed
set 'default' alias to installed version
add KRE bin to user PATH environment variable
-g|-global install to machine-wide location
-f|-force upgrade even if latest is already installed
-proxy <ADDRESS> use given address as proxy when accessing remote server
-g|-Global install to machine-wide location
-f|-Force upgrade even if latest is already installed
-Proxy <ADDRESS> use given address as proxy when accessing remote server
-NoNative Do not generate native images (Effective only for CoreCLR flavors)
kvm install <semver>|<alias>|<nupkg>|latest [-x86][-amd64] [-r|-runtime CLR|CoreCLR] [-a|-alias <alias>] [-g|-global] [-f|-force]
kvm install <semver>|<alias>|<nupkg>|latest [-X86][-Amd64] [-r|-Runtime CLR|CoreCLR] [-a|-Alias <alias>] [-g|-Global] [-f|-Force] [-NoNative]
<semver>|<alias> install requested KRE from feed
<nupkg> install requested KRE from package on local filesystem
latest install latest KRE from feed
add KRE bin to path of current command line
-p|-persistent add KRE bin to PATH environment variables persistently
-a|-alias <alias> set alias <alias> for requested KRE on install
-g|-global install to machine-wide location
-f|-force install even if specified version is already installed
-p|-Persistent add KRE bin to PATH environment variables persistently
-a|-Alias <alias> set alias <alias> for requested KRE on install
-g|-Global install to machine-wide location
-f|-Force install even if specified version is already installed
-NoNative Do not generate native images (Effective only for CoreCLR flavors)
kvm use <semver>|<alias>|none [-x86][-amd64] [-r|-runtime CLR|CoreCLR] [-p|-persistent] [-g|-global]
<semver>|<alias> add KRE bin to path of current command line
none remove KRE bin from path of current command line
-p|-persistent add KRE bin to PATH environment variables persistently
-g|-global combined with -p to change machine PATH instead of user PATH
kvm use <semver>|<alias>|<package>|none [-X86][-Amd64] [-r|-Runtime CLR|CoreCLR] [-p|-Persistent] [-g|-Global]
<semver>|<alias>|<package> add KRE bin to path of current command line
none remove KRE bin from path of current command line
-p|-Persistent add KRE bin to PATH environment variables persistently
-g|-Global combined with -p to change machine PATH instead of user PATH
kvm list
list KRE versions installed
@ -82,14 +100,14 @@ kvm alias
kvm alias <alias>
display value of the specified alias
kvm alias <alias> <semver>|<alias> [-x86][-amd64] [-r|-runtime CLR|CoreCLR]
<alias> The name of the alias to set
<semver>|<alias> The KRE version to set the alias to. Alternatively use the version of the specified alias
kvm alias <alias> <semver>|<alias>|<package> [-X86][-Amd64] [-r|-Runtime CLR|CoreCLR]
<alias> the name of the alias to set
<semver>|<alias>|<package> the KRE version to set the alias to. Alternatively use the version of the specified alias
kvm unalias <alias>
remove the specified alias
"@ -replace "`n","`r`n" | Write-Host
"@ -replace "`n","`r`n" | Console-Write
}
function Kvm-Global-Setup {
@ -99,76 +117,66 @@ function Kvm-Global-Setup {
{
$arguments = "-ExecutionPolicy unrestricted & '$scriptPath' setup -global -wait"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments -Wait
Write-Host "Adding $kvmBinPath to process PATH"
Console-Write "Adding $kvmBinPath to process PATH"
Set-Path (Change-Path $env:Path $kvmBinPath ($kvmBinPath))
Write-Host "Adding $globalKrePath;%USERPROFILE%\.kre to process KRE_HOME"
Console-Write "Adding $globalKrePath;%USERPROFILE%\.kre to process KRE_HOME"
$envKreHome = $env:KRE_HOME
$envKreHome = Change-Path $envKreHome "%USERPROFILE%\.kre" ("%USERPROFILE%\.kre")
$envKreHome = Change-Path $envKreHome $globalKrePath ($globalKrePath)
$env:KRE_HOME = $envKreHome
Write-Host "Setup complete"
Console-Write "Setup complete"
break
}
$scriptFolder = [System.IO.Path]::GetDirectoryName($scriptPath)
Write-Host "Copying file $kvmBinPath\kvm.ps1"
Console-Write "Copying file $kvmBinPath\kvm.ps1"
md $kvmBinPath -Force | Out-Null
copy "$scriptFolder\kvm.ps1" "$kvmBinPath\kvm.ps1"
Write-Host "Copying file $kvmBinPath\kvm.cmd"
Console-Write "Copying file $kvmBinPath\kvm.cmd"
copy "$scriptFolder\kvm.cmd" "$kvmBinPath\kvm.cmd"
Write-Host "Adding $kvmBinPath to process PATH"
Console-Write "Adding $kvmBinPath to process PATH"
Set-Path (Change-Path $env:Path $kvmBinPath ($kvmBinPath))
Write-Host "Adding $kvmBinPath to user PATH"
Console-Write "Adding $kvmBinPath to user PATH"
$userPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
$userPath = Change-Path $userPath $kvmBinPath ($kvmBinPath)
[Environment]::SetEnvironmentVariable("Path", $userPath, [System.EnvironmentVariableTarget]::User)
Write-Host "Adding $globalKrePath;%USERPROFILE%\.kre to process KRE_HOME"
Console-Write "Adding $globalKrePath;%USERPROFILE%\.kre to process KRE_HOME"
$envKreHome = $env:KRE_HOME
$envKreHome = Change-Path $envKreHome "%USERPROFILE%\.kre" ("%USERPROFILE%\.kre")
$envKreHome = Change-Path $envKreHome $globalKrePath ($globalKrePath)
$env:KRE_HOME = $envKreHome
Write-Host "Adding $globalKrePath;%USERPROFILE%\.kre to machine KRE_HOME"
Console-Write "Adding $globalKrePath;%USERPROFILE%\.kre to machine KRE_HOME"
$machineKreHome = [Environment]::GetEnvironmentVariable("KRE_HOME", [System.EnvironmentVariableTarget]::Machine)
$machineKreHome = Change-Path $machineKreHome "%USERPROFILE%\.kre" ("%USERPROFILE%\.kre")
$machineKreHome = Change-Path $machineKreHome $globalKrePath ($globalKrePath)
[Environment]::SetEnvironmentVariable("KRE_HOME", $machineKreHome, [System.EnvironmentVariableTarget]::Machine)
}
function Kvm-Global-Upgrade {
$persistent = $true
$alias="default"
$versionOrAlias = Kvm-Find-Latest $selectedRuntime $selectedArch
If (Needs-Elevation) {
$arguments = "-ExecutionPolicy unrestricted & '$scriptPath' install '$versionOrAlias' -global $(Requested-Switches) -wait"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments -Wait
Kvm-Set-Global-Process-Path $versionOrAlias
break
}
Kvm-Install $versionOrAlias $true
}
function Kvm-Upgrade {
$persistent = $true
$alias="default"
Kvm-Install "latest" $false
param(
[boolean] $isGlobal
)
$Persistent = $true
$Alias="default"
Kvm-Install "latest" $isGlobal
}
function Add-Proxy-If-Specified {
param(
[System.Net.WebClient] $wc
)
if (!$proxy) {
$proxy = $env:http_proxy
if (!$Proxy) {
$Proxy = $env:http_proxy
}
if ($proxy) {
$wp = New-Object System.Net.WebProxy($proxy)
$pb = New-Object UriBuilder($proxy)
if ($Proxy) {
$wp = New-Object System.Net.WebProxy($Proxy)
$pb = New-Object UriBuilder($Proxy)
if (!$pb.UserName) {
$wp.Credentials = [System.Net.CredentialCache]::DefaultCredentials
} else {
@ -183,12 +191,11 @@ param(
[string] $platform,
[string] $architecture
)
Write-Host "Determining latest version"
Console-Write "Determining latest version"
$url = "$feed/GetUpdates()?packageIds=%27KRE-$platform-$architecture%27&versions=%270.0%27&includePrerelease=true&includeAllVersions=false"
$wc = New-Object System.Net.WebClient
$wc.Credentials = new-object System.Net.NetworkCredential("aspnetreadonly", "4d8a2d9c-7b80-4162-9978-47e918c9658c")
Add-Proxy-If-Specified($wc)
[xml]$xml = $wc.DownloadString($url)
@ -213,16 +220,16 @@ param(
$kreFile = Join-Path $kreFolder "$kreFullName.nupkg"
If (Test-Path $kreFolder) {
if($force)
if($Force)
{
rm $kreFolder -Recurse -Force
} else {
Write-Host "$kreFullName already installed."
Console-Write "$kreFullName already installed."
return;
}
}
Write-Host "Downloading" $kreFullName "from $feed"
Console-Write "Downloading $kreFullName from $feed"
#Downloading to temp location
$kreTempDownload = Join-Path $packagesFolder "temp"
@ -235,14 +242,13 @@ param(
}
$wc = New-Object System.Net.WebClient
$wc.Credentials = new-object System.Net.NetworkCredential("aspnetreadonly", "4d8a2d9c-7b80-4162-9978-47e918c9658c")
Add-Proxy-If-Specified($wc)
$wc.DownloadFile($url, $tempKreFile)
Do-Kvm-Unpack $tempKreFile $kreTempDownload
md $kreFolder -Force | Out-Null
Write-Host "Installing to $kreFolder"
Console-Write "Installing to $kreFolder"
mv "$kreTempDownload\*" $kreFolder
Remove-Item "$kreTempDownload" -Force | Out-Null
}
@ -252,20 +258,27 @@ param(
[string] $kreFile,
[string] $kreFolder
)
Write-Host "Unpacking to" $kreFolder
try {
#Shell will not recognize nupkg as a zip and throw, so rename it to zip
$kreZip = [System.IO.Path]::ChangeExtension($kreFile, "zip")
Rename-Item $kreFile $kreZip
#Use the shell to uncompress the nupkg
$shell_app=new-object -com shell.application
$zip_file = $shell_app.namespace($kreZip)
$destination = $shell_app.namespace($kreFolder)
$destination.Copyhere($zip_file.items(), 0x14) #0x4 = don't show UI, 0x10 = overwrite files
}
finally {
#make it a nupkg again
Rename-Item $kreZip $kreFile
Console-Write "Unpacking to $kreFolder"
$compressionLib = [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
if($compressionLib -eq $null) {
try {
# Shell will not recognize nupkg as a zip and throw, so rename it to zip
$kreZip = [System.IO.Path]::ChangeExtension($kreFile, "zip")
Rename-Item $kreFile $kreZip
# Use the shell to uncompress the nupkg
$shell_app=new-object -com shell.application
$zip_file = $shell_app.namespace($kreZip)
$destination = $shell_app.namespace($kreFolder)
$destination.Copyhere($zip_file.items(), 0x14) #0x4 = don't show UI, 0x10 = overwrite files
}
finally {
# make it a nupkg again
Rename-Item $kreZip $kreFile
}
} else {
[System.IO.Compression.ZipFile]::ExtractToDirectory($kreFile, $kreFolder)
}
If (Test-Path ($kreFolder + "\[Content_Types].xml")) {
@ -313,13 +326,13 @@ param(
$kreFolder = "$packageFolder\$kreFullName"
$folderExists = Test-Path $kreFolder
if ($folderExists -and $force) {
if ($folderExists -and $Force) {
del $kreFolder -Recurse -Force
$folderExists = $false;
}
if ($folderExists) {
Write-Host "Target folder '$kreFolder' already exists"
Console-Write "Target folder '$kreFolder' already exists"
} else {
$tempUnpackFolder = Join-Path $packageFolder "temp"
$tempKreFile = Join-Path $tempUnpackFolder "$kreFullName.nupkg"
@ -333,7 +346,7 @@ param(
Do-Kvm-Unpack $tempKreFile $tempUnpackFolder
md $kreFolder -Force | Out-Null
Write-Host "Installing to $kreFolder"
Console-Write "Installing to $kreFolder"
mv "$tempUnpackFolder\*" $kreFolder
Remove-Item "$tempUnpackFolder" -Force | Out-Null
}
@ -341,16 +354,27 @@ param(
$packageVersion = Package-Version $kreFullName
Kvm-Use $packageVersion
if (!$(String-IsEmptyOrWhitespace($alias))) {
Kvm-Alias-Set $alias $packageVersion
if (!$(String-IsEmptyOrWhitespace($Alias))) {
Kvm-Alias-Set $Alias $packageVersion
}
}
else
{
Do-Kvm-Download $kreFullName $packageFolder
Kvm-Use $versionOrAlias
if (!$(String-IsEmptyOrWhitespace($alias))) {
Kvm-Alias-Set "$alias" $versionOrAlias
if (!$(String-IsEmptyOrWhitespace($Alias))) {
Kvm-Alias-Set "$Alias" $versionOrAlias
}
}
if ($kreFullName.Contains("CoreCLR")) {
if ($NoNative) {
Console-Write "Native image generation is skipped"
}
else {
Console-Write "Compiling native images for $kreFullName to improve startup performance..."
Start-Process "k-crossgen" -Wait
Console-Write "Finished native image compilation."
}
}
}
@ -384,7 +408,10 @@ filter List-Parts {
}
$active = $false
foreach($portion in $env:Path.Split(';')) {
if ($portion.StartsWith($_.FullName)) {
# Append \ to the end because otherwise you might see
# multiple active versions if the folders have the same
# name prefix (like 1.0-beta and 1.0)
if ($portion.StartsWith($_.FullName + "\")) {
$active = $true
}
}
@ -393,7 +420,7 @@ filter List-Parts {
$delim=""
foreach($alias in $aliases){
if($_.Name.Split('\', 2).Contains($alias.Name)){
if($_.Name.Split('\', 2) -contains $alias.Name){
$fullAlias += $delim + $alias.Alias
$delim = ", "
}
@ -415,6 +442,8 @@ function Kvm-Global-Use {
param(
[string] $versionOrAlias
)
Validate-Full-Package-Name-Arguments-Combination $versionOrAlias
If (Needs-Elevation) {
$arguments = "-ExecutionPolicy unrestricted & '$scriptPath' use '$versionOrAlias' -global $(Requested-Switches) -wait"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments -Wait
@ -425,8 +454,8 @@ param(
Kvm-Set-Global-Process-Path "$versionOrAlias"
if ($versionOrAlias -eq "none") {
if ($persistent) {
Write-Host "Removing KRE from machine PATH"
if ($Persistent) {
Console-Write "Removing KRE from machine PATH"
$machinePath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
$machinePath = Change-Path $machinePath "" ($globalKrePackages, $userKrePackages)
[Environment]::SetEnvironmentVariable("Path", $machinePath, [System.EnvironmentVariableTarget]::Machine)
@ -437,12 +466,11 @@ param(
$kreFullName = Requested-VersionOrAlias "$versionOrAlias"
$kreBin = Locate-KreBinFromFullName $kreFullName
if ($kreBin -eq $null) {
Write-Host "Cannot find $kreFullName, do you need to run 'kvm install $versionOrAlias'?"
return
throw "Cannot find $kreFullName, do you need to run 'kvm install $versionOrAlias'?"
}
if ($persistent) {
Write-Host "Adding $kreBin to machine PATH"
if ($Persistent) {
Console-Write "Adding $kreBin to machine PATH"
$machinePath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
$machinePath = Change-Path $machinePath $kreBin ($globalKrePackages, $userKrePackages)
[Environment]::SetEnvironmentVariable("Path", $machinePath, [System.EnvironmentVariableTarget]::Machine)
@ -454,7 +482,7 @@ param(
[string] $versionOrAlias
)
if ($versionOrAlias -eq "none") {
Write-Host "Removing KRE from process PATH"
Console-Write "Removing KRE from process PATH"
Set-Path (Change-Path $env:Path "" ($globalKrePackages, $userKrePackages))
return
}
@ -462,11 +490,11 @@ param(
$kreFullName = Requested-VersionOrAlias $versionOrAlias
$kreBin = Locate-KreBinFromFullName $kreFullName
if ($kreBin -eq $null) {
Write-Host "Cannot find $kreFullName, do you need to run 'kvm install $versionOrAlias'?"
Console-Write "Cannot find $kreFullName, do you need to run 'kvm install $versionOrAlias'?"
return
}
Write-Host "Adding" $kreBin "to process PATH"
Console-Write "Adding $kreBin to process PATH"
Set-Path (Change-Path $env:Path $kreBin ($globalKrePackages, $userKrePackages))
}
@ -474,12 +502,14 @@ function Kvm-Use {
param(
[string] $versionOrAlias
)
Validate-Full-Package-Name-Arguments-Combination $versionOrAlias
if ($versionOrAlias -eq "none") {
Write-Host "Removing KRE from process PATH"
Console-Write "Removing KRE from process PATH"
Set-Path (Change-Path $env:Path "" ($globalKrePackages, $userKrePackages))
if ($persistent) {
Write-Host "Removing KRE from user PATH"
if ($Persistent) {
Console-Write "Removing KRE from user PATH"
$userPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
$userPath = Change-Path $userPath "" ($globalKrePackages, $userKrePackages)
[Environment]::SetEnvironmentVariable("Path", $userPath, [System.EnvironmentVariableTarget]::User)
@ -491,15 +521,14 @@ param(
$kreBin = Locate-KreBinFromFullName $kreFullName
if ($kreBin -eq $null) {
Write-Host "Cannot find $kreFullName, do you need to run 'kvm install $versionOrAlias'?"
return
throw "Cannot find $kreFullName, do you need to run 'kvm install $versionOrAlias'?"
}
Write-Host "Adding" $kreBin "to process PATH"
Console-Write "Adding $kreBin to process PATH"
Set-Path (Change-Path $env:Path $kreBin ($globalKrePackages, $userKrePackages))
if ($persistent) {
Write-Host "Adding $kreBin to user PATH"
if ($Persistent) {
Console-Write "Adding $kreBin to user PATH"
$userPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
$userPath = Change-Path $userPath $kreBin ($globalKrePackages, $userKrePackages)
[Environment]::SetEnvironmentVariable("Path", $userPath, [System.EnvironmentVariableTarget]::User)
@ -519,9 +548,11 @@ param(
md ($userKrePath + "\alias\") -Force | Out-Null
$aliasFilePath=$userKrePath + "\alias\" + $name + ".txt"
if (!(Test-Path $aliasFilePath)) {
Write-Host "Alias '$name' does not exist"
Console-Write "Alias '$name' does not exist"
$script:exitCode = 1 # Return non-zero exit code for scripting
} else {
Write-Host "Alias '$name' is set to" (Get-Content ($userKrePath + "\alias\" + $name + ".txt"))
$aliasValue = (Get-Content ($userKrePath + "\alias\" + $name + ".txt"))
Console-Write "Alias '$name' is set to $aliasValue"
}
}
@ -533,7 +564,7 @@ param(
$kreFullName = Requested-VersionOrAlias $value
$aliasFilePath = $userKrePath + "\alias\" + $name + ".txt"
$action = if (Test-Path $aliasFilePath) { "Updating" } else { "Setting" }
Write-Host "$action alias '$name' to '$kreFullName'"
Console-Write "$action alias '$name' to '$kreFullName'"
md ($userKrePath + "\alias\") -Force | Out-Null
$kreFullName | Out-File ($aliasFilePath) ascii
}
@ -544,10 +575,11 @@ param(
)
$aliasPath=$userKrePath + "\alias\" + $name + ".txt"
if (Test-Path -literalPath "$aliasPath") {
Write-Host "Removing alias $name"
Console-Write "Removing alias $name"
Remove-Item -literalPath $aliasPath
} else {
Write-Host "Cannot remove alias, '$name' is not a valid alias name"
Console-Write "Cannot remove alias, '$name' is not a valid alias name"
$script:exitCode = 1 # Return non-zero exit code for scripting
}
}
@ -595,6 +627,15 @@ function Requested-VersionOrAlias() {
param(
[string] $versionOrAlias
)
Validate-Full-Package-Name-Arguments-Combination $versionOrAlias
$kreBin = Locate-KreBinFromFullName $versionOrAlias
# If the name specified is an existing package, just use it as is
if ($kreBin -ne $null) {
return $versionOrAlias
}
If (Test-Path ($userKrePath + "\alias\" + $versionOrAlias + ".txt")) {
$aliasValue = Get-Content ($userKrePath + "\alias\" + $versionOrAlias + ".txt")
$parts = $aliasValue.Split('.', 2)
@ -659,6 +700,10 @@ SET "PATH=$newPath"
}
function Needs-Elevation() {
if($AssumeElevated) {
return $false
}
$user = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
$elevated = $user.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
return -NOT $elevated
@ -666,69 +711,135 @@ function Needs-Elevation() {
function Requested-Switches() {
$arguments = ""
if ($x86) {$arguments = "$arguments -x86"}
if ($amd64) {$arguments = "$arguments -amd64"}
if ($X86) {$arguments = "$arguments -x86"}
if ($Amd64) {$arguments = "$arguments -amd64"}
#deprecated
if ($x64) {$arguments = "$arguments -x64"}
if ($X64) {$arguments = "$arguments -x64"}
if ($selectedRuntime) {$arguments = "$arguments -runtime $selectedRuntime"}
if ($persistent) {$arguments = "$arguments -persistent"}
if ($force) {$arguments = "$arguments -force"}
if (!$(String-IsEmptyOrWhitespace($alias))) {$arguments = "$arguments -alias '$alias'"}
if ($Persistent) {$arguments = "$arguments -persistent"}
if ($Force) {$arguments = "$arguments -force"}
if (!$(String-IsEmptyOrWhitespace($Alias))) {$arguments = "$arguments -alias '$Alias'"}
return $arguments
}
function Validate-And-Santitise-Switches()
function Validate-And-Santitize-Switches()
{
if ($svr50 -and $runtime) {throw "You cannot select both the -runtime switch and the -svr50 runtimes"}
if ($svrc50 -and $runtime) {throw "You cannot select both the -runtime switch and the -svrc50 runtimes"}
if ($x86 -and $amd64) {throw "You cannot select both x86 and amd64 architectures"}
if ($x86 -and $x64) {throw "You cannot select both x86 and x64 architectures"}
if ($x64 -and $amd64) {throw "You cannot select both x64 and amd64 architectures"}
if ($Svr50 -and $Runtime) {throw "You cannot select both the -runtime switch and the -svr50 runtimes"}
if ($Svrc50 -and $Runtime) {throw "You cannot select both the -runtime switch and the -svrc50 runtimes"}
if ($X86 -and $Amd64) {throw "You cannot select both x86 and amd64 architectures"}
if ($X86 -and $X64) {throw "You cannot select both x86 and x64 architectures"}
if ($X64 -and $Amd64) {throw "You cannot select both x64 and amd64 architectures"}
if ($runtime) {
if ($Runtime) {
$validRuntimes = "CoreCLR", "CLR", "svr50", "svrc50"
$match = $validRuntimes | ? { $_ -like $runtime } | Select -First 1
$match = $validRuntimes | ? { $_ -like $Runtime } | Select -First 1
if (!$match) {throw "'$runtime' is not a valid runtime"}
Set-Variable -Name "selectedRuntime" -Value $match -Scope Script
} elseif ($svr50) {
Write-Host "Warning: -svr50 is deprecated, use -runtime CLR for new packages."
} elseif ($Svr50) {
Console-Write "Warning: -svr50 is deprecated, use -runtime CLR for new packages."
Set-Variable -Name "selectedRuntime" -Value "svr50" -Scope Script
} elseif ($svrc50) {
Write-Host "Warning: -svrc50 is deprecated, use -runtime CoreCLR for new packages."
} elseif ($Svrc50) {
Console-Write "Warning: -svrc50 is deprecated, use -runtime CoreCLR for new packages."
Set-Variable -Name "selectedRuntime" -Value "svrc50" -Scope Script
}
if ($x64) {
Write-Host "Warning: -x64 is deprecated, use -amd64 for new packages."
Set-Variable -Name "selectedArch" -Value "x64" -Scope Script
} elseif ($amd64) {
Set-Variable -Name "selectedArch" -Value "amd64" -Scope Script
} elseif ($x86) {
Set-Variable -Name "selectedArch" -Value "x86" -Scope Script
if($Architecture) {
$validArchitectures = "amd64", "x86"
$match = $validArchitectures | ? { $_ -like $Architecture } | Select -First 1
if(!$match) {throw "'$architecture' is not a valid architecture"}
Set-Variable -Name "selectedArch" -Value $match -Scope Script
}
else {
if ($X64) {
Console-Write "Warning: -x64 is deprecated, use -amd64 for new packages."
Set-Variable -Name "selectedArch" -Value "x64" -Scope Script
} elseif ($Amd64) {
Set-Variable -Name "selectedArch" -Value "amd64" -Scope Script
} elseif ($X86) {
Set-Variable -Name "selectedArch" -Value "x86" -Scope Script
}
}
}
$script:capturedOut = @()
function Console-Write() {
param(
[Parameter(ValueFromPipeline=$true)]
[string] $message
)
if($OutputVariable) {
# Update the capture output
$script:capturedOut += @($message)
}
if(!$Quiet) {
if ($useHostOutputMethods) {
try {
Write-Host $message
}
catch {
$script:useHostOutputMethods = $false
Console-Write $message
}
}
else {
[Console]::WriteLine($message)
}
}
}
function Console-Write-Error() {
param(
[Parameter(ValueFromPipeline=$true)]
[string] $message
)
if ($useHostOutputMethods) {
try {
Write-Error $message
}
catch {
$script:useHostOutputMethods = $false
Console-Write-Error $message
}
}
else {
[Console]::Error.WriteLine($message)
}
}
function Validate-Full-Package-Name-Arguments-Combination() {
param(
[string] $versionOrAlias
)
if ($versionOrAlias -like "KRE-*" -and
($selectedArch -or $selectedRuntime)) {
throw "Runtime or architecture cannot be specified when using the full package name."
}
}
$script:exitCode = 0
try {
Validate-And-Santitise-Switches
if ($global) {
switch -wildcard ($command + " " + $args.Count) {
Validate-And-Santitize-Switches
if ($Global) {
switch -wildcard ($Command + " " + $Args.Count) {
"setup 0" {Kvm-Global-Setup}
"upgrade 0" {Kvm-Global-Upgrade}
"install 1" {Kvm-Install $args[0] $true}
"use 1" {Kvm-Global-Use $args[0]}
"upgrade 0" {Kvm-Upgrade $true}
"install 1" {Kvm-Install $Args[0] $true}
"use 1" {Kvm-Global-Use $Args[0]}
default {throw "Unknown command, or global switch not supported"};
}
} else {
switch -wildcard ($command + " " + $args.Count) {
switch -wildcard ($Command + " " + $Args.Count) {
"setup 0" {Kvm-Global-Setup}
"upgrade 0" {Kvm-Upgrade}
"install 1" {Kvm-Install $args[0] $false}
"upgrade 0" {Kvm-Upgrade $false}
"install 1" {Kvm-Install $Args[0] $false}
"list 0" {Kvm-List}
"use 1" {Kvm-Use $args[0]}
"use 1" {Kvm-Use $Args[0]}
"alias 0" {Kvm-Alias-List}
"alias 1" {Kvm-Alias-Get $args[0]}
"alias 2" {Kvm-Alias-Set $args[0] $args[1]}
"unalias 1" {Kvm-Unalias $args[0]}
"alias 1" {Kvm-Alias-Get $Args[0]}
"alias 2" {Kvm-Alias-Set $Args[0] $Args[1]}
"unalias 1" {Kvm-Unalias $Args[0]}
"help 0" {Kvm-Help}
" 0" {Kvm-Help}
default {throw "Unknown command"};
@ -736,10 +847,18 @@ try {
}
}
catch {
Write-Host $_ -ForegroundColor Red ;
Write-Host "Type 'kvm help' for help on how to use kvm."
Console-Write-Error $_
Console-Write "Type 'kvm help' for help on how to use kvm."
$script:exitCode = -1
}
if ($wait) {
Write-Host "Press any key to continue ..."
if ($Wait) {
Console-Write "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown,AllowCtrlC")
}
# If the user specified an output variable, push the value up to the parent scope
if($OutputVariable) {
Set-Variable $OutputVariable $script:capturedOut -Scope 1
}
exit $script:exitCode

71
kvm.sh
View File

@ -83,13 +83,13 @@ _kvm_download() {
mkdir -p "$kreFolder" > /dev/null 2>&1
local httpResult=$(curl -L -D - -u aspnetreadonly:4d8a2d9c-7b80-4162-9978-47e918c9658c "$url" -o "$kreFile" 2>/dev/null | grep "^HTTP/1.1" | head -n 1 | sed "s/HTTP.1.1 \([0-9]*\).*/\1/")
local httpResult=$(curl -L -D - "$url" -o "$kreFile" 2>/dev/null | grep "^HTTP/1.1" | head -n 1 | sed "s/HTTP.1.1 \([0-9]*\).*/\1/")
[[ $httpResult == "404" ]] && echo "$kreFullName was not found in repository $KRE_FEED" && return 1
[[ $httpResult != "302" && $httpResult != "200" ]] && echo "HTTP Error $httpResult fetching $kreFullName from $KRE_FEED" && return 1
_kvm_unpack $kreFile $kreFolder
return $?
return $?
}
_kvm_unpack() {
@ -118,18 +118,24 @@ _kvm_unpack() {
_kvm_requested_version_or_alias() {
local versionOrAlias="$1"
local kreBin=$(_kvm_locate_kre_bin_from_full_name "$versionOrAlias")
# If the name specified is an existing package, just use it as is
if [ -n "$kreBin" ]; then
echo "$versionOrAlias"
else
if [ -e "$KRE_USER_HOME/alias/$versionOrAlias.alias" ]; then
local kreFullName=$(cat "$KRE_USER_HOME/alias/$versionOrAlias.alias")
local pkgName=$(echo $kreFullName | sed "s/\([^.]*\).*/\1/")
local pkgVersion=$(echo $kreFullName | sed "s/[^.]*.\(.*\)/\1/")
local pkgPlatform=$(echo "$pkgName" | sed "s/KRE-\([^.-]*\).*/\1/")
else
local pkgVersion=$versionOrAlias
local pkgPlatform="Mono"
fi
if [ -e "$KRE_USER_HOME/alias/$versionOrAlias.alias" ]; then
local kreFullName=$(cat "$KRE_USER_HOME/alias/$versionOrAlias.alias")
local pkgName=$(echo $kreFullName | sed "s/\([^.]*\).*/\1/")
local pkgVersion=$(echo $kreFullName | sed "s/[^.]*.\(.*\)/\1/")
local pkgPlatform=$(echo "$pkgName" | sed "s/KRE-\([^.-]*\).*/\1/")
else
local pkgVersion=$versionOrAlias
local pkgPlatform="Mono"
echo "KRE-$pkgPlatform.$pkgVersion"
fi
echo "KRE-$pkgPlatform.$pkgVersion"
}
# This will be more relevant if we support global installs
@ -148,7 +154,7 @@ kvm()
case $1 in
"help" )
echo ""
echo "K Runtime Environment Version Manager - Build 10017"
echo "K Runtime Environment Version Manager - Build 10058"
echo ""
echo "USAGE: kvm <command> [options]"
echo ""
@ -165,10 +171,10 @@ kvm()
echo "-p -persistent set installed version as default"
echo "add KRE bin to path of current command line"
echo ""
echo "kvm use <semver>|<alias>|none [-p -persistent]"
echo "<semver>|<alias> add KRE bin to path of current command line "
echo "none remove KRE bin from path of current command line"
echo "-p -persistent set selected version as default"
echo "kvm use <semver>|<alias>|<package>|none [-p -persistent]"
echo "<semver>|<alias>|<package> add KRE bin to path of current command line "
echo "none remove KRE bin from path of current command line"
echo "-p -persistent set selected version as default"
echo ""
echo "kvm list"
echo "list KRE versions installed "
@ -179,9 +185,9 @@ kvm()
echo "kvm alias <alias>"
echo "display value of the specified alias"
echo ""
echo "kvm alias <alias> <semver>"
echo "<alias> The name of the alias to set"
echo "<semver>|<alias> The KRE version to set the alias to. Alternatively use the version of the specified alias"
echo "kvm alias <alias> <semver>|<alias>|<package>"
echo "<alias> the name of the alias to set"
echo "<semver>|<alias>|<package> the KRE version to set the alias to. Alternatively use the version of the specified alias"
echo ""
echo "kvm unalias <alias>"
echo "remove the specified alias"
@ -328,10 +334,10 @@ kvm()
echo "$action alias '$name' to '$kreFullName'"
echo "$kreFullName" > "$KRE_USER_HOME/alias/$name.alias"
;;
"unalias" )
[[ $# -ne 2 ]] && kvm help && return
local name=$2
local aliasPath="$KRE_USER_HOME/alias/$name.alias"
[[ ! -e "$aliasPath" ]] && echo "Cannot remove alias, '$name' is not a valid alias name" && return 1
@ -350,21 +356,26 @@ kvm()
local searchGlob=$(_kvm_requested_version_or_alias "$versionOrAlias")
fi
echo ""
local arr=()
local i=0
# 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"
for _kvm_file in $(find "$KRE_USER_HOME/alias" -name *.alias); do
arr[$i]="$(basename $_kvm_file | sed 's/.alias//')/$(cat $_kvm_file)"
let i+=1
done
local formatString="%-6s %-20s %-7s %-20s %s\n"
printf "$formatString" "Active" "Version" "Runtime" "Location" "Alias"
printf "$formatString" "------" "-------" "-------" "--------" "-----"
local formattedHome=`(echo $KRE_USER_PACKAGES | sed s=$HOME=~=g)`
for f in $(find $KRE_USER_PACKAGES/* -name "$searchGlob" -type d -prune -exec basename {} \;); do
for f in $(find $KRE_USER_PACKAGES -name "$searchGlob" \( -type d -or -type l \) -prune -exec basename {} \;); do
local active=""
[[ $PATH == *"$KRE_USER_PACKAGES/$f/bin"* ]] && local active=" *"
local pkgName=$(_kvm_package_runtime "$f")
@ -372,7 +383,7 @@ kvm()
local alias=""
local delim=""
for i in "${arr[@]}"; do
for i in "${arr[@]}"; do
temp="KRE-$pkgName.$pkgVersion"
temp2="KRE-$pkgName-x86.$pkgVersion"
if [[ ${i#*/} == $temp || ${i#*/} == $temp2 ]]; then
@ -393,7 +404,7 @@ kvm()
echo "Unknown command $1"
return 1
esac
return 0
}