⬆️ dnvm.ps1, dnvm.cmd, dnvm.sh

Source: AspNet/kvm@133607eb6b
This commit is contained in:
ASP.NET Push Bot 2015-04-04 22:13:56 -07:00 committed by unknown
parent e652ec7fbf
commit 31c7588c13
2 changed files with 153 additions and 87 deletions

158
dnvm.ps1
View File

@ -28,6 +28,16 @@ function _WriteOut {
[Parameter(Mandatory=$false)][ConsoleColor]$BackgroundColor, [Parameter(Mandatory=$false)][ConsoleColor]$BackgroundColor,
[Parameter(Mandatory=$false)][switch]$NoNewLine) [Parameter(Mandatory=$false)][switch]$NoNewLine)
if($__TestWriteTo) {
$cur = Get-Variable -Name $__TestWriteTo -ValueOnly -Scope Global -ErrorAction SilentlyContinue
$val = $cur + "$msg"
if(!$NoNewLine) {
$val += [Environment]::NewLine
}
Set-Variable -Name $__TestWriteTo -Value $val -Scope Global -Force
return
}
if(!$Script:UseWriteHost) { if(!$Script:UseWriteHost) {
if(!$msg) { if(!$msg) {
$msg = "" $msg = ""
@ -53,20 +63,11 @@ function _WriteOut {
_WriteOut $msg _WriteOut $msg
} }
} }
if($__TeeTo) {
$cur = Get-Variable -Name $__TeeTo -ValueOnly -Scope Global -ErrorAction SilentlyContinue
$val = $cur + "$msg"
if(!$NoNewLine) {
$val += [Environment]::NewLine
}
Set-Variable -Name $__TeeTo -Value $val -Scope Global -Force
}
} }
### Constants ### Constants
$ProductVersion="1.0.0" $ProductVersion="1.0.0"
$BuildVersion="beta5-10358" $BuildVersion="beta5-10359"
$Authors="Microsoft Open Technologies, Inc." $Authors="Microsoft Open Technologies, Inc."
# If the Version hasn't been replaced... # If the Version hasn't been replaced...
@ -131,6 +132,7 @@ if(!$ColorScheme) {
} }
Set-Variable -Option Constant "OptionPadding" 20 Set-Variable -Option Constant "OptionPadding" 20
Set-Variable -Option Constant "CommandPadding" 15
# Test Control Variables # Test Control Variables
if($__TeeTo) { if($__TeeTo) {
@ -216,6 +218,39 @@ $RuntimesDir = Join-Path $UserHome "runtimes"
$Aliases = $null $Aliases = $null
### Helper Functions ### Helper Functions
# Checks if a specified file exists in the destination folder and if not, copies the file
# to the destination folder.
function Safe-Filecopy {
param(
[Parameter(Mandatory=$true, Position=0)] $Filename,
[Parameter(Mandatory=$true, Position=1)] $SourceFolder,
[Parameter(Mandatory=$true, Position=2)] $DestinationFolder)
# Make sure the destination folder is created if it doesn't already exist.
if(!(Test-Path $DestinationFolder)) {
_WriteOut "Creating destination folder '$DestinationFolder' ... "
New-Item -Type Directory $Destination | Out-Null
}
$sourceFilePath = Join-Path $SourceFolder $Filename
$destFilePath = Join-Path $DestinationFolder $Filename
if(Test-Path $sourceFilePath) {
_WriteOut "Installing '$Filename' to '$DestinationFolder' ... "
if (Test-Path $destFilePath) {
_WriteOut " Skipping: file already exists" -ForegroundColor Yellow
}
else {
Copy-Item $sourceFilePath $destFilePath -Force
}
}
else {
_WriteOut "WARNING: Unable to install: Could not find '$Filename' in '$SourceFolder'. "
}
}
function GetArch($Architecture, $FallBackArch = $DefaultArchitecture) { function GetArch($Architecture, $FallBackArch = $DefaultArchitecture) {
if(![String]::IsNullOrWhiteSpace($Architecture)) { if(![String]::IsNullOrWhiteSpace($Architecture)) {
$Architecture $Architecture
@ -695,7 +730,7 @@ function dnvm-help {
if($PassThru) { if($PassThru) {
$help $help
} else { } else {
_WriteOut -ForegroundColor $ColorScheme.Help_Header "$CommandName-$Command" _WriteOut -ForegroundColor $ColorScheme.Help_Header "$CommandName $Command"
_WriteOut " $($help.Synopsis.Trim())" _WriteOut " $($help.Synopsis.Trim())"
_WriteOut _WriteOut
_WriteOut -ForegroundColor $ColorScheme.Help_Header "usage:" _WriteOut -ForegroundColor $ColorScheme.Help_Header "usage:"
@ -776,7 +811,7 @@ function dnvm-help {
$name = $_.Name.Substring($CommandPrefix.Length) $name = $_.Name.Substring($CommandPrefix.Length)
if($DeprecatedCommands -notcontains $name) { if($DeprecatedCommands -notcontains $name) {
_WriteOut -NoNewLine " " _WriteOut -NoNewLine " "
_WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Command $name.PadRight(10) _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Command $name.PadRight($CommandPadding)
_WriteOut " $($h.Synopsis.Trim())" _WriteOut " $($h.Synopsis.Trim())"
} }
} }
@ -1124,7 +1159,7 @@ function dnvm-install {
#> #>
function dnvm-use { function dnvm-use {
param( param(
[Parameter(Mandatory=$false, Position=0)] [Parameter(Mandatory=$true, Position=0)]
[string]$VersionOrAlias, [string]$VersionOrAlias,
[Alias("arch")] [Alias("arch")]
@ -1141,13 +1176,6 @@ function dnvm-use {
[Parameter(Mandatory=$false)] [Parameter(Mandatory=$false)]
[switch]$Persistent) [switch]$Persistent)
if([String]::IsNullOrWhiteSpace($VersionOrAlias)) {
_WriteOut "Missing version or alias to add to path"
dnvm-help use
$Script:ExitCode = $ExitCodes.InvalidArguments
return
}
if ($versionOrAlias -eq "none") { if ($versionOrAlias -eq "none") {
_WriteOut "Removing all runtimes from process PATH" _WriteOut "Removing all runtimes from process PATH"
Set-Path (Change-Path $env:Path "" ($RuntimeDirs)) Set-Path (Change-Path $env:Path "" ($RuntimeDirs))
@ -1180,63 +1208,61 @@ function dnvm-use {
<# <#
.SYNOPSIS .SYNOPSIS
Gets the full name of a runtime Locates the dnx.exe for the specified version or alias and executes it, providing the remaining arguments to dnx.exe
.PARAMETER VersionOrAlias .PARAMETER VersionOrAlias
The version or alias of the runtime to place on the PATH The version of alias of the runtime to execute
.PARAMETER Architecture .PARAMETER DnxArguments
The processor architecture of the runtime to place on the PATH (default: x86, or whatever the alias specifies in the case of use-ing an alias) The arguments to pass to dnx.exe
.PARAMETER Runtime
The runtime flavor of the runtime to place on the PATH (default: clr, or whatever the alias specifies in the case of use-ing an alias)
#> #>
function dnvm-name { function dnvm-run {
param( param(
[Parameter(Mandatory=$false, Position=0)] [Parameter(Mandatory=$true, Position=0)]
[string]$VersionOrAlias, [string]$VersionOrAlias,
[Parameter(Mandatory=$false, Position=1, ValueFromRemainingArguments=$true)]
[object[]]$DnxArguments)
[Alias("arch")] $runtimeFullName = Get-RuntimeName $VersionOrAlias $Architecture $Runtime
[ValidateSet("x86","x64")] $runtimeBin = Get-RuntimePath $runtimeFullName
[Parameter(Mandatory=$false)] if ($runtimeBin -eq $null) {
[string]$Architecture = "", throw "Cannot find $runtimeFullName, do you need to run '$CommandName install $versionOrAlias'?"
}
[Alias("r")] $dnxExe = Join-Path $runtimeBin "dnx.exe"
[ValidateSet("clr","coreclr")] if(!(Test-Path $dnxExe)) {
[Parameter(Mandatory=$false)] throw "Cannot find a dnx.exe in $runtimeBin, the installation may be corrupt. Try running 'dnvm install $VersionOrAlias -f' to reinstall it"
[string]$Runtime = "") }
_WriteDebug "> $dnxExe $DnxArguments"
Get-RuntimeName $VersionOrAlias $Architecture $Runtime & $dnxExe @DnxArguments
} }
<#
# Checks if a specified file exists in the destination folder and if not, copies the file .SYNOPSIS
# to the destination folder. Executes the specified command in a sub-shell where the PATH has been augmented to include the specified DNX
function Safe-Filecopy { .PARAMETER VersionOrAlias
The version of alias of the runtime to make active in the sub-shell
.PARAMETER Command
The command to execute in the sub-shell
#>
function dnvm-exec {
param( param(
[Parameter(Mandatory=$true, Position=0)] $Filename, [Parameter(Mandatory=$true, Position=0)]
[Parameter(Mandatory=$true, Position=1)] $SourceFolder, [string]$VersionOrAlias,
[Parameter(Mandatory=$true, Position=2)] $DestinationFolder) [Parameter(Mandatory=$false, Position=1)]
[string]$Command,
[Parameter(Mandatory=$false, Position=2, ValueFromRemainingArguments=$true)]
[object[]]$Arguments)
# Make sure the destination folder is created if it doesn't already exist. $runtimeFullName = Get-RuntimeName $VersionOrAlias $Architecture $Runtime
if(!(Test-Path $DestinationFolder)) { $runtimeBin = Get-RuntimePath $runtimeFullName
_WriteOut "Creating destination folder '$DestinationFolder' ... " if ($runtimeBin -eq $null) {
throw "Cannot find $runtimeFullName, do you need to run '$CommandName install $versionOrAlias'?"
New-Item -Type Directory $Destination | Out-Null
} }
$sourceFilePath = Join-Path $SourceFolder $Filename $oldPath = $env:PATH
$destFilePath = Join-Path $DestinationFolder $Filename try {
$env:PATH = "$runtimeBin;$($env:PATH)"
if(Test-Path $sourceFilePath) { & $Command @Arguments
_WriteOut "Installing '$Filename' to '$DestinationFolder' ... " } finally {
$env:PATH = $oldPath
if (Test-Path $destFilePath) {
_WriteOut " Skipping: file already exists" -ForegroundColor Yellow
}
else {
Copy-Item $sourceFilePath $destFilePath -Force
}
}
else {
_WriteOut "WARNING: Unable to install: Could not find '$Filename' in '$SourceFolder'. "
} }
} }

82
dnvm.sh
View File

@ -2,13 +2,14 @@
# Source this file from your .bash-profile or script to use # Source this file from your .bash-profile or script to use
# "Constants" # "Constants"
_DNVM_BUILDNUMBER="beta5-10358" _DNVM_BUILDNUMBER="beta5-10359"
_DNVM_AUTHORS="Microsoft Open Technologies, Inc." _DNVM_AUTHORS="Microsoft Open Technologies, Inc."
_DNVM_RUNTIME_PACKAGE_NAME="dnx" _DNVM_RUNTIME_PACKAGE_NAME="dnx"
_DNVM_RUNTIME_FRIENDLY_NAME=".NET Execution Environment" _DNVM_RUNTIME_FRIENDLY_NAME=".NET Execution Environment"
_DNVM_RUNTIME_SHORT_NAME="DNX" _DNVM_RUNTIME_SHORT_NAME="DNX"
_DNVM_RUNTIME_FOLDER_NAME=".dnx" _DNVM_RUNTIME_FOLDER_NAME=".dnx"
_DNVM_COMMAND_NAME="dnvm" _DNVM_COMMAND_NAME="dnvm"
_DNVM_PACKAGE_MANAGER_NAME="dnu"
_DNVM_VERSION_MANAGER_NAME=".NET Version Manager" _DNVM_VERSION_MANAGER_NAME=".NET Version Manager"
_DNVM_DEFAULT_FEED="https://www.myget.org/F/aspnetvnext/api/v2" _DNVM_DEFAULT_FEED="https://www.myget.org/F/aspnetvnext/api/v2"
_DNVM_UPDATE_LOCATION="https://raw.githubusercontent.com/aspnet/Home/dev/dnvm.sh" _DNVM_UPDATE_LOCATION="https://raw.githubusercontent.com/aspnet/Home/dev/dnvm.sh"
@ -243,6 +244,20 @@ __dnvm_help() {
echo " none remove $_DNVM_RUNTIME_SHORT_NAME bin from path of current command line" echo " none remove $_DNVM_RUNTIME_SHORT_NAME bin from path of current command line"
echo " -p|-persistent set selected version as default" echo " -p|-persistent set selected version as default"
echo "" echo ""
printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME run <semver>|<alias> <args...> ${RCol}"
echo " <semver>|<alias> the version or alias to run"
echo " <args...> arguments to be passed to $_DNVM_RUNTIME_SHORT_NAME"
echo ""
echo " runs the $_DNVM_RUNTIME_SHORT_NAME command from the specified version of the runtime without affecting the current PATH"
echo ""
printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME exec <semver>|<alias> <command> <args...> ${RCol}"
echo " <semver>|<alias> the version or alias to execute in"
echo " <command> the command to run"
echo " <args...> arguments to be passed to the command"
echo ""
echo " runs the specified command in the context of the specified version of the runtime without affecting the current PATH"
echo " example: $_DNVM_COMMAND_NAME exec 1.0.0-beta4 $_DNVM_PACKAGE_MANAGER_NAME build"
echo ""
printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME list ${RCol}" printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME list ${RCol}"
echo " list $_DNVM_RUNTIME_SHORT_NAME versions installed " echo " list $_DNVM_RUNTIME_SHORT_NAME versions installed "
echo "" echo ""
@ -347,23 +362,29 @@ dnvm()
fi fi
;; ;;
"use" ) "use"|"run"|"exec" )
[ $# -gt 3 ] && __dnvm_help && return [[ $1 == "use" && $# -gt 3 ]] && __dnvm_help && return
[ $# -lt 2 ] && __dnvm_help && return [[ $1 == "use" && $# -lt 2 ]] && __dnvm_help && return
local cmd=$1
local persistent=
shift shift
local persistent= if [ $cmd == "use" ]; then
while [ $# -ne 0 ] while [ $# -ne 0 ]
do do
if [[ $1 == "-p" || $1 == "-persistent" ]]; then if [[ $1 == "-p" || $1 == "-persistent" ]]; then
local persistent="true" local persistent="true"
elif [[ -n $1 ]]; then elif [[ -n $1 ]]; then
local versionOrAlias=$1 local versionOrAlias=$1
fi fi
shift
done
else
local versionOrAlias=$1
shift shift
done fi
if [[ $versionOrAlias == "none" ]]; then if [[ $cmd == "use" && $versionOrAlias == "none" ]]; then
echo "Removing $_DNVM_RUNTIME_SHORT_NAME from process PATH" echo "Removing $_DNVM_RUNTIME_SHORT_NAME from process PATH"
# Strip other version from PATH # Strip other version from PATH
PATH=$(__dnvm_strip_path "$PATH" "/bin") PATH=$(__dnvm_strip_path "$PATH" "/bin")
@ -383,15 +404,34 @@ dnvm()
return 1 return 1
fi fi
echo "Adding" $runtimeBin "to process PATH" case $cmd in
"run")
local hostpath="$runtimeBin/dnx"
if [[ -e $hostpath ]]; then
$hostpath $@
else
echo "Cannot find $_DNVM_RUNTIME_SHORT_NAME in $runtimeBin. It may have been corrupted. Use '$_DNVM_COMMAND_NAME install $versionOrAlias -f' to attempt to reinstall it"
fi
;;
"exec")
(
PATH=$(__dnvm_strip_path "$PATH" "/bin")
PATH=$(__dnvm_prepend_path "$PATH" "$runtimeBin")
$@
)
;;
"use")
echo "Adding" $runtimeBin "to process PATH"
PATH=$(__dnvm_strip_path "$PATH" "/bin") PATH=$(__dnvm_strip_path "$PATH" "/bin")
PATH=$(__dnvm_prepend_path "$PATH" "$runtimeBin") PATH=$(__dnvm_prepend_path "$PATH" "$runtimeBin")
if [[ -n $persistent ]]; then if [[ -n $persistent ]]; then
local runtimeVersion=$(__dnvm_package_version "$runtimeFullName") local runtimeVersion=$(__dnvm_package_version "$runtimeFullName")
$_DNVM_COMMAND_NAME alias default "$runtimeVersion" $_DNVM_COMMAND_NAME alias default "$runtimeVersion"
fi fi
;;
esac
;; ;;
"alias" ) "alias" )