diff --git a/dnvm.ps1 b/dnvm.ps1 index 7af73589fd..f3fe858ba0 100644 --- a/dnvm.ps1 +++ b/dnvm.ps1 @@ -28,6 +28,16 @@ function _WriteOut { [Parameter(Mandatory=$false)][ConsoleColor]$BackgroundColor, [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(!$msg) { $msg = "" @@ -53,20 +63,11 @@ function _WriteOut { _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 $ProductVersion="1.0.0" -$BuildVersion="beta5-10358" +$BuildVersion="beta5-10359" $Authors="Microsoft Open Technologies, Inc." # If the Version hasn't been replaced... @@ -131,6 +132,7 @@ if(!$ColorScheme) { } Set-Variable -Option Constant "OptionPadding" 20 +Set-Variable -Option Constant "CommandPadding" 15 # Test Control Variables if($__TeeTo) { @@ -216,6 +218,39 @@ $RuntimesDir = Join-Path $UserHome "runtimes" $Aliases = $null ### 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) { if(![String]::IsNullOrWhiteSpace($Architecture)) { $Architecture @@ -695,7 +730,7 @@ function dnvm-help { if($PassThru) { $help } else { - _WriteOut -ForegroundColor $ColorScheme.Help_Header "$CommandName-$Command" + _WriteOut -ForegroundColor $ColorScheme.Help_Header "$CommandName $Command" _WriteOut " $($help.Synopsis.Trim())" _WriteOut _WriteOut -ForegroundColor $ColorScheme.Help_Header "usage:" @@ -776,7 +811,7 @@ function dnvm-help { $name = $_.Name.Substring($CommandPrefix.Length) if($DeprecatedCommands -notcontains $name) { _WriteOut -NoNewLine " " - _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Command $name.PadRight(10) + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Command $name.PadRight($CommandPadding) _WriteOut " $($h.Synopsis.Trim())" } } @@ -1124,7 +1159,7 @@ function dnvm-install { #> function dnvm-use { param( - [Parameter(Mandatory=$false, Position=0)] + [Parameter(Mandatory=$true, Position=0)] [string]$VersionOrAlias, [Alias("arch")] @@ -1141,13 +1176,6 @@ function dnvm-use { [Parameter(Mandatory=$false)] [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") { _WriteOut "Removing all runtimes from process PATH" Set-Path (Change-Path $env:Path "" ($RuntimeDirs)) @@ -1180,63 +1208,61 @@ function dnvm-use { <# .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 - The version or alias of the runtime to place on the PATH -.PARAMETER Architecture - 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) -.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) + The version of alias of the runtime to execute +.PARAMETER DnxArguments + The arguments to pass to dnx.exe #> -function dnvm-name { +function dnvm-run { param( - [Parameter(Mandatory=$false, Position=0)] + [Parameter(Mandatory=$true, Position=0)] [string]$VersionOrAlias, + [Parameter(Mandatory=$false, Position=1, ValueFromRemainingArguments=$true)] + [object[]]$DnxArguments) - [Alias("arch")] - [ValidateSet("x86","x64")] - [Parameter(Mandatory=$false)] - [string]$Architecture = "", - - [Alias("r")] - [ValidateSet("clr","coreclr")] - [Parameter(Mandatory=$false)] - [string]$Runtime = "") - - Get-RuntimeName $VersionOrAlias $Architecture $Runtime + $runtimeFullName = Get-RuntimeName $VersionOrAlias $Architecture $Runtime + $runtimeBin = Get-RuntimePath $runtimeFullName + if ($runtimeBin -eq $null) { + throw "Cannot find $runtimeFullName, do you need to run '$CommandName install $versionOrAlias'?" + } + $dnxExe = Join-Path $runtimeBin "dnx.exe" + if(!(Test-Path $dnxExe)) { + throw "Cannot find a dnx.exe in $runtimeBin, the installation may be corrupt. Try running 'dnvm install $VersionOrAlias -f' to reinstall it" + } + _WriteDebug "> $dnxExe $DnxArguments" + & $dnxExe @DnxArguments } - -# Checks if a specified file exists in the destination folder and if not, copies the file -# to the destination folder. -function Safe-Filecopy { +<# +.SYNOPSIS + Executes the specified command in a sub-shell where the PATH has been augmented to include the specified DNX +.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( - [Parameter(Mandatory=$true, Position=0)] $Filename, - [Parameter(Mandatory=$true, Position=1)] $SourceFolder, - [Parameter(Mandatory=$true, Position=2)] $DestinationFolder) + [Parameter(Mandatory=$true, Position=0)] + [string]$VersionOrAlias, + [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. - if(!(Test-Path $DestinationFolder)) { - _WriteOut "Creating destination folder '$DestinationFolder' ... " - - New-Item -Type Directory $Destination | Out-Null + $runtimeFullName = Get-RuntimeName $VersionOrAlias $Architecture $Runtime + $runtimeBin = Get-RuntimePath $runtimeFullName + if ($runtimeBin -eq $null) { + throw "Cannot find $runtimeFullName, do you need to run '$CommandName install $versionOrAlias'?" } - $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'. " + $oldPath = $env:PATH + try { + $env:PATH = "$runtimeBin;$($env:PATH)" + & $Command @Arguments + } finally { + $env:PATH = $oldPath } } diff --git a/dnvm.sh b/dnvm.sh index 7988084084..7162796921 100644 --- a/dnvm.sh +++ b/dnvm.sh @@ -2,13 +2,14 @@ # Source this file from your .bash-profile or script to use # "Constants" -_DNVM_BUILDNUMBER="beta5-10358" +_DNVM_BUILDNUMBER="beta5-10359" _DNVM_AUTHORS="Microsoft Open Technologies, Inc." _DNVM_RUNTIME_PACKAGE_NAME="dnx" _DNVM_RUNTIME_FRIENDLY_NAME=".NET Execution Environment" _DNVM_RUNTIME_SHORT_NAME="DNX" _DNVM_RUNTIME_FOLDER_NAME=".dnx" _DNVM_COMMAND_NAME="dnvm" +_DNVM_PACKAGE_MANAGER_NAME="dnu" _DNVM_VERSION_MANAGER_NAME=".NET Version Manager" _DNVM_DEFAULT_FEED="https://www.myget.org/F/aspnetvnext/api/v2" _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 " -p|-persistent set selected version as default" echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME run | ${RCol}" + echo " | the version or alias to run" + echo " 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 | ${RCol}" + echo " | the version or alias to execute in" + echo " the command to run" + echo " 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}" echo " list $_DNVM_RUNTIME_SHORT_NAME versions installed " echo "" @@ -347,23 +362,29 @@ dnvm() fi ;; - "use" ) - [ $# -gt 3 ] && __dnvm_help && return - [ $# -lt 2 ] && __dnvm_help && return + "use"|"run"|"exec" ) + [[ $1 == "use" && $# -gt 3 ]] && __dnvm_help && return + [[ $1 == "use" && $# -lt 2 ]] && __dnvm_help && return + local cmd=$1 + local persistent= shift - local persistent= - while [ $# -ne 0 ] - do - if [[ $1 == "-p" || $1 == "-persistent" ]]; then - local persistent="true" - elif [[ -n $1 ]]; then - local versionOrAlias=$1 - fi + if [ $cmd == "use" ]; then + while [ $# -ne 0 ] + do + if [[ $1 == "-p" || $1 == "-persistent" ]]; then + local persistent="true" + elif [[ -n $1 ]]; then + local versionOrAlias=$1 + fi + shift + done + else + local versionOrAlias=$1 shift - done + fi - if [[ $versionOrAlias == "none" ]]; then + if [[ $cmd == "use" && $versionOrAlias == "none" ]]; then echo "Removing $_DNVM_RUNTIME_SHORT_NAME from process PATH" # Strip other version from PATH PATH=$(__dnvm_strip_path "$PATH" "/bin") @@ -383,15 +404,34 @@ dnvm() return 1 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_prepend_path "$PATH" "$runtimeBin") + PATH=$(__dnvm_strip_path "$PATH" "/bin") + PATH=$(__dnvm_prepend_path "$PATH" "$runtimeBin") - if [[ -n $persistent ]]; then - local runtimeVersion=$(__dnvm_package_version "$runtimeFullName") - $_DNVM_COMMAND_NAME alias default "$runtimeVersion" - fi + if [[ -n $persistent ]]; then + local runtimeVersion=$(__dnvm_package_version "$runtimeFullName") + $_DNVM_COMMAND_NAME alias default "$runtimeVersion" + fi + ;; + esac ;; "alias" )