Updating kvm scripts in the home repo master branch

This commit is contained in:
Praburaj 2014-08-12 13:17:16 -07:00
parent ec0fb252f5
commit 2f1854f1e2
2 changed files with 712 additions and 632 deletions

75
kvm.ps1
View File

@ -22,6 +22,10 @@ $globalKrePath = $env:ProgramFiles + "\KRE"
$globalKrePackages = $globalKrePath + "\packages" $globalKrePackages = $globalKrePath + "\packages"
$feed = $env:KRE_NUGET_API_URL $feed = $env:KRE_NUGET_API_URL
function String-IsEmptyOrWhitespace([string]$str) {
return [string]::IsNullOrEmpty($str) -or $str.Trim().length -eq 0
}
if (!$feed) if (!$feed)
{ {
$feed = "https://www.myget.org/F/aspnetvnext/api/v2"; $feed = "https://www.myget.org/F/aspnetvnext/api/v2";
@ -31,7 +35,7 @@ $scriptPath = $myInvocation.MyCommand.Definition
function Kvm-Help { function Kvm-Help {
@" @"
K Runtime Environment Version Manager - Build 10002 K Runtime Environment Version Manager - Build 10010
USAGE: kvm <command> [options] USAGE: kvm <command> [options]
@ -66,13 +70,16 @@ kvm alias
list KRE aliases which have been defined list KRE aliases which have been defined
kvm alias <alias> kvm alias <alias>
display value of named alias display value of the specified alias
kvm alias <alias> <semver>|<alias> [-x86][-x64] [-svr50][-svrc50] kvm alias <alias> <semver>|<alias> [-x86][-x64] [-svr50][-svrc50]
<alias> The name of the alias to set <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 <semver>|<alias> The KRE version to set the alias to. Alternatively use the version of the specified alias
"@ | Write-Host kvm unalias <alias>
remove the specified alias
"@ -replace "`n","`r`n" | Write-Host
} }
function Kvm-Global-Setup { function Kvm-Global-Setup {
@ -218,6 +225,7 @@ param(
md $kreFolder -Force | Out-Null md $kreFolder -Force | Out-Null
Write-Host "Installing to $kreFolder" Write-Host "Installing to $kreFolder"
mv "$kreTempDownload\*" $kreFolder mv "$kreTempDownload\*" $kreFolder
Remove-Item "$kreTempDownload" -Force | Out-Null
} }
function Do-Kvm-Unpack { function Do-Kvm-Unpack {
@ -271,7 +279,7 @@ param(
Do-Kvm-Download $kreFullName $globalKrePackages Do-Kvm-Download $kreFullName $globalKrePackages
Kvm-Use $versionOrAlias Kvm-Use $versionOrAlias
if (![string]::IsNullOrWhiteSpace($alias)) { if (!$(String-IsEmptyOrWhitespace($alias))) {
Kvm-Alias-Set $alias $versionOrAlias Kvm-Alias-Set $alias $versionOrAlias
} }
} }
@ -308,11 +316,12 @@ param(
md $kreFolder -Force | Out-Null md $kreFolder -Force | Out-Null
Write-Host "Installing to $kreFolder" Write-Host "Installing to $kreFolder"
mv "$tempUnpackFolder\*" $kreFolder mv "$tempUnpackFolder\*" $kreFolder
Remove-Item "$tempUnpackFolder" -Force | Out-Null
} }
$packageVersion = Package-Version $kreFullName $packageVersion = Package-Version $kreFullName
Kvm-Use $packageVersion Kvm-Use $packageVersion
if (![string]::IsNullOrWhiteSpace($alias)) { if (!$(String-IsEmptyOrWhitespace($alias))) {
Kvm-Alias-Set $alias $packageVersion Kvm-Alias-Set $alias $packageVersion
} }
} }
@ -325,7 +334,7 @@ param(
Do-Kvm-Download $kreFullName $userKrePackages Do-Kvm-Download $kreFullName $userKrePackages
Kvm-Use $versionOrAlias Kvm-Use $versionOrAlias
if (![string]::IsNullOrWhiteSpace($alias)) { if (!$(String-IsEmptyOrWhitespace($alias))) {
Kvm-Alias-Set $alias $versionOrAlias Kvm-Alias-Set $alias $versionOrAlias
} }
} }
@ -334,19 +343,26 @@ param(
function Kvm-List { function Kvm-List {
$kreHome = $env:KRE_HOME $kreHome = $env:KRE_HOME
if (!$kreHome) { if (!$kreHome) {
$kreHome = $env:ProgramFiles + "\KRE;%USERPROFILE%\.kre" $kreHome = "$globalKrePath;$userKrePath"
} }
md ($userKrePath + "\alias\") -Force | Out-Null
$aliases = Get-ChildItem ($userKrePath + "\alias\") | Select @{label='Alias';expression={$_.BaseName}}, @{label='Name';expression={Get-Content $_.FullName }}
$items = @() $items = @()
foreach($portion in $kreHome.Split(';')) { foreach($portion in $kreHome.Split(';')) {
$path = [System.Environment]::ExpandEnvironmentVariables($portion) $path = [System.Environment]::ExpandEnvironmentVariables($portion)
if (Test-Path("$path\packages")) { if (Test-Path("$path\packages")) {
$items += Get-ChildItem ("$path\packages\KRE-*") | List-Parts $items += Get-ChildItem ("$path\packages\KRE-*") | List-Parts $aliases
} }
} }
$items | Sort-Object Version, Runtime, Architecture | Format-Table -AutoSize -Property @{name="Active";expression={$_.Active};alignment="center"}, "Version", "Runtime", "Architecture", "Location"
$items | Sort-Object Version, Runtime, Architecture, Alias | Format-Table -AutoSize -Property @{name="Active";expression={$_.Active};alignment="center"}, "Version", "Runtime", "Architecture", "Location", "Alias"
} }
filter List-Parts { filter List-Parts {
param($aliases)
$hasBin = Test-Path($_.FullName+"\bin") $hasBin = Test-Path($_.FullName+"\bin")
if (!$hasBin) { if (!$hasBin) {
return return
@ -357,14 +373,26 @@ filter List-Parts {
$active = $true $active = $true
} }
} }
$fullAlias=""
$delim=""
foreach($alias in $aliases){
if($_.Name.Split('\', 2).Contains($alias.Name)){
$fullAlias += $delim + $alias.Alias
$delim = ", "
}
}
$parts1 = $_.Name.Split('.', 2) $parts1 = $_.Name.Split('.', 2)
$parts2 = $parts1[0].Split('-', 3) $parts2 = $parts1[0].Split('-', 3)
return New-Object PSObject -Property @{ return New-Object PSObject -Property @{
Active = if($active){"*"}else{""} Active = if ($active) { "*" } else { "" }
Version = $parts1[1] Version = $parts1[1]
Runtime = $parts2[1] Runtime = $parts2[1]
Architecture = $parts2[2] Architecture = $parts2[2]
Location = $_.Parent.FullName Location = $_.Parent.FullName
Alias = $fullAlias
} }
} }
@ -474,10 +502,24 @@ param(
[string] $value [string] $value
) )
$kreFullName = Requested-VersionOrAlias $value $kreFullName = Requested-VersionOrAlias $value
$aliasFilePath = $userKrePath + "\alias\" + $name + ".txt"
Write-Host "Setting alias '$name' to '$kreFullName'" $action = if (Test-Path $aliasFilePath) { "Updating" } else { "Setting" }
Write-Host "$action alias '$name' to '$kreFullName'"
md ($userKrePath + "\alias\") -Force | Out-Null md ($userKrePath + "\alias\") -Force | Out-Null
$kreFullName | Out-File ($userKrePath + "\alias\" + $name + ".txt") ascii $kreFullName | Out-File ($aliasFilePath) ascii
}
function Kvm-Unalias {
param(
[string] $name
)
$aliasPath=$userKrePath + "\alias\" + $name + ".txt"
if (Test-Path -literalPath "$aliasPath") {
Write-Host "Removing alias $name"
Remove-Item -literalPath $aliasPath
} else {
Write-Host "Cannot remove alias, '$name' is not a valid alias name"
}
} }
function Locate-KreBinFromFullName() { function Locate-KreBinFromFullName() {
@ -486,7 +528,7 @@ param(
) )
$kreHome = $env:KRE_HOME $kreHome = $env:KRE_HOME
if (!$kreHome) { if (!$kreHome) {
$kreHome = $env:ProgramFiles + ";%USERPROFILE%\.kre" $kreHome = "$globalKrePath;$userKrePath"
} }
foreach($portion in $kreHome.Split(';')) { foreach($portion in $kreHome.Split(';')) {
$path = [System.Environment]::ExpandEnvironmentVariables($portion) $path = [System.Environment]::ExpandEnvironmentVariables($portion)
@ -623,8 +665,9 @@ function Requested-Switches() {
"alias 0" {Kvm-Alias-List} "alias 0" {Kvm-Alias-List}
"alias 1" {Kvm-Alias-Get $args[0]} "alias 1" {Kvm-Alias-Get $args[0]}
"alias 2" {Kvm-Alias-Set $args[0] $args[1]} "alias 2" {Kvm-Alias-Set $args[0] $args[1]}
"help 0" {Kvm-Help} "unalias 1" {Kvm-Unalias $args[0]}
" 0" {Kvm-Help} "help 0" {Kvm-Help}
" 0" {Kvm-Help}
default {Write-Host 'Unknown command'; Kvm-Help;} default {Write-Host 'Unknown command'; Kvm-Help;}
} }
} }

55
kvm.sh
View File

@ -169,7 +169,7 @@ kvm()
case $1 in case $1 in
"help" ) "help" )
echo "" echo ""
echo "K Runtime Environment Version Manager - Build 10002" echo "K Runtime Environment Version Manager - Build 10010"
echo "" echo ""
echo "USAGE: kvm <command> [options]" echo "USAGE: kvm <command> [options]"
echo "" echo ""
@ -198,12 +198,14 @@ kvm()
echo "list KRE aliases which have been defined" echo "list KRE aliases which have been defined"
echo "" echo ""
echo "kvm alias <alias>" echo "kvm alias <alias>"
echo "display value of named alias" echo "display value of the specified alias"
echo "" echo ""
echo "kvm alias <alias> <semver>" echo "kvm alias <alias> <semver>"
echo "<alias> The name of the alias to set" 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 "<semver>|<alias> The KRE version to set the alias to. Alternatively use the version of the specified alias"
echo "" echo ""
echo "kvm unalias <alias>"
echo "remove the specified alias"
echo "" echo ""
;; ;;
@ -339,13 +341,24 @@ kvm()
local kreFullName=$(_kvm_requested_version_or_alias "$3") local kreFullName=$(_kvm_requested_version_or_alias "$3")
[[ ! -d "$KRE_USER_PACKAGES/$kreFullName" ]] && echo "$kreFullName is not an installed KRE version." && return 1 [[ ! -d "$KRE_USER_PACKAGES/$kreFullName" ]] && echo "$kreFullName is not an installed KRE version" && return 1
echo "Setting alias '$name' to '$kreFullName'"
local action="Setting"
[[ -e "$KRE_USER_HOME/alias/$name.alias" ]] && action="Updating"
echo "$action alias '$name' to '$kreFullName'"
echo "$kreFullName" > "$KRE_USER_HOME/alias/$name.alias" 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
echo "Removing alias $name"
rm "$aliasPath" >> /dev/null 2>&1
;;
"list" ) "list" )
[[ $# -gt 2 ]] && kvm help && return [[ $# -gt 2 ]] && kvm help && return
@ -357,15 +370,37 @@ kvm()
local searchGlob=$(_kvm_requested_version_or_alias "$versionOrAlias") local searchGlob=$(_kvm_requested_version_or_alias "$versionOrAlias")
fi fi
echo "" echo ""
local formatString="%-6s %-20s %-7s %-12s %s\n"
printf "$formatString" "Active" "Version" "Runtime" "Architecture" "Location" local arr=()
printf "$formatString" "------" "-------" "-------" "------------" "--------" local i=0
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 %-12s %-20s %s\n"
printf "$formatString" "Active" "Version" "Runtime" "Architecture" "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 -prune -exec basename {} \;); do
local active="" local active=""
[[ $PATH == *"$KRE_USER_PACKAGES/$f/bin"* ]] && local active=" *" [[ $PATH == *"$KRE_USER_PACKAGES/$f/bin"* ]] && local active=" *"
local pkgName=$(_kvm_package_runtime "$f") local pkgName=$(_kvm_package_runtime "$f")
local pkgVersion=$(_kvm_package_version "$f") local pkgVersion=$(_kvm_package_version "$f")
printf "$formatString" "$active" "$pkgVersion" "$pkgName" "x86" "$KRE_USER_PACKAGES"
local alias=""
local delim=""
for i in "${arr[@]}"; do
temp="KRE-$pkgName-x86.$pkgVersion"
if [[ ${i#*/} == $temp ]]; then
alias+="$delim${i%/*}"
delim=", "
fi
done
printf "$formatString" "$active" "$pkgVersion" "$pkgName" "x86" "$formattedHome" "$alias"
[[ $# == 2 ]] && echo "" && return 0 [[ $# == 2 ]] && echo "" && return 0
done done
@ -377,6 +412,8 @@ kvm()
echo "Unknown command $1" echo "Unknown command $1"
return 1 return 1
esac esac
return 0
} }
kvm list default >/dev/null && kvm use default >/dev/null || true kvm list default >/dev/null && kvm use default >/dev/null || true