Skip to content

Commit 31c7588

Browse files
ASP.NET Push Botunknown
authored andcommitted
⬆️ dnvm.ps1, dnvm.cmd, dnvm.sh
Source: aspnet/dnvm@133607e
1 parent e652ec7 commit 31c7588

File tree

2 files changed

+155
-89
lines changed

2 files changed

+155
-89
lines changed

dnvm.ps1

Lines changed: 92 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ function _WriteOut {
2828
[Parameter(Mandatory=$false)][ConsoleColor]$BackgroundColor,
2929
[Parameter(Mandatory=$false)][switch]$NoNewLine)
3030

31+
if($__TestWriteTo) {
32+
$cur = Get-Variable -Name $__TestWriteTo -ValueOnly -Scope Global -ErrorAction SilentlyContinue
33+
$val = $cur + "$msg"
34+
if(!$NoNewLine) {
35+
$val += [Environment]::NewLine
36+
}
37+
Set-Variable -Name $__TestWriteTo -Value $val -Scope Global -Force
38+
return
39+
}
40+
3141
if(!$Script:UseWriteHost) {
3242
if(!$msg) {
3343
$msg = ""
@@ -53,20 +63,11 @@ function _WriteOut {
5363
_WriteOut $msg
5464
}
5565
}
56-
57-
if($__TeeTo) {
58-
$cur = Get-Variable -Name $__TeeTo -ValueOnly -Scope Global -ErrorAction SilentlyContinue
59-
$val = $cur + "$msg"
60-
if(!$NoNewLine) {
61-
$val += [Environment]::NewLine
62-
}
63-
Set-Variable -Name $__TeeTo -Value $val -Scope Global -Force
64-
}
6566
}
6667

6768
### Constants
6869
$ProductVersion="1.0.0"
69-
$BuildVersion="beta5-10358"
70+
$BuildVersion="beta5-10359"
7071
$Authors="Microsoft Open Technologies, Inc."
7172

7273
# If the Version hasn't been replaced...
@@ -131,6 +132,7 @@ if(!$ColorScheme) {
131132
}
132133

133134
Set-Variable -Option Constant "OptionPadding" 20
135+
Set-Variable -Option Constant "CommandPadding" 15
134136

135137
# Test Control Variables
136138
if($__TeeTo) {
@@ -216,6 +218,39 @@ $RuntimesDir = Join-Path $UserHome "runtimes"
216218
$Aliases = $null
217219

218220
### Helper Functions
221+
# Checks if a specified file exists in the destination folder and if not, copies the file
222+
# to the destination folder.
223+
function Safe-Filecopy {
224+
param(
225+
[Parameter(Mandatory=$true, Position=0)] $Filename,
226+
[Parameter(Mandatory=$true, Position=1)] $SourceFolder,
227+
[Parameter(Mandatory=$true, Position=2)] $DestinationFolder)
228+
229+
# Make sure the destination folder is created if it doesn't already exist.
230+
if(!(Test-Path $DestinationFolder)) {
231+
_WriteOut "Creating destination folder '$DestinationFolder' ... "
232+
233+
New-Item -Type Directory $Destination | Out-Null
234+
}
235+
236+
$sourceFilePath = Join-Path $SourceFolder $Filename
237+
$destFilePath = Join-Path $DestinationFolder $Filename
238+
239+
if(Test-Path $sourceFilePath) {
240+
_WriteOut "Installing '$Filename' to '$DestinationFolder' ... "
241+
242+
if (Test-Path $destFilePath) {
243+
_WriteOut " Skipping: file already exists" -ForegroundColor Yellow
244+
}
245+
else {
246+
Copy-Item $sourceFilePath $destFilePath -Force
247+
}
248+
}
249+
else {
250+
_WriteOut "WARNING: Unable to install: Could not find '$Filename' in '$SourceFolder'. "
251+
}
252+
}
253+
219254
function GetArch($Architecture, $FallBackArch = $DefaultArchitecture) {
220255
if(![String]::IsNullOrWhiteSpace($Architecture)) {
221256
$Architecture
@@ -695,7 +730,7 @@ function dnvm-help {
695730
if($PassThru) {
696731
$help
697732
} else {
698-
_WriteOut -ForegroundColor $ColorScheme.Help_Header "$CommandName-$Command"
733+
_WriteOut -ForegroundColor $ColorScheme.Help_Header "$CommandName $Command"
699734
_WriteOut " $($help.Synopsis.Trim())"
700735
_WriteOut
701736
_WriteOut -ForegroundColor $ColorScheme.Help_Header "usage:"
@@ -776,7 +811,7 @@ function dnvm-help {
776811
$name = $_.Name.Substring($CommandPrefix.Length)
777812
if($DeprecatedCommands -notcontains $name) {
778813
_WriteOut -NoNewLine " "
779-
_WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Command $name.PadRight(10)
814+
_WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Command $name.PadRight($CommandPadding)
780815
_WriteOut " $($h.Synopsis.Trim())"
781816
}
782817
}
@@ -1124,7 +1159,7 @@ function dnvm-install {
11241159
#>
11251160
function dnvm-use {
11261161
param(
1127-
[Parameter(Mandatory=$false, Position=0)]
1162+
[Parameter(Mandatory=$true, Position=0)]
11281163
[string]$VersionOrAlias,
11291164

11301165
[Alias("arch")]
@@ -1141,13 +1176,6 @@ function dnvm-use {
11411176
[Parameter(Mandatory=$false)]
11421177
[switch]$Persistent)
11431178

1144-
if([String]::IsNullOrWhiteSpace($VersionOrAlias)) {
1145-
_WriteOut "Missing version or alias to add to path"
1146-
dnvm-help use
1147-
$Script:ExitCode = $ExitCodes.InvalidArguments
1148-
return
1149-
}
1150-
11511179
if ($versionOrAlias -eq "none") {
11521180
_WriteOut "Removing all runtimes from process PATH"
11531181
Set-Path (Change-Path $env:Path "" ($RuntimeDirs))
@@ -1180,63 +1208,61 @@ function dnvm-use {
11801208

11811209
<#
11821210
.SYNOPSIS
1183-
Gets the full name of a runtime
1211+
Locates the dnx.exe for the specified version or alias and executes it, providing the remaining arguments to dnx.exe
11841212
.PARAMETER VersionOrAlias
1185-
The version or alias of the runtime to place on the PATH
1186-
.PARAMETER Architecture
1187-
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)
1188-
.PARAMETER Runtime
1189-
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)
1213+
The version of alias of the runtime to execute
1214+
.PARAMETER DnxArguments
1215+
The arguments to pass to dnx.exe
11901216
#>
1191-
function dnvm-name {
1217+
function dnvm-run {
11921218
param(
1193-
[Parameter(Mandatory=$false, Position=0)]
1219+
[Parameter(Mandatory=$true, Position=0)]
11941220
[string]$VersionOrAlias,
1221+
[Parameter(Mandatory=$false, Position=1, ValueFromRemainingArguments=$true)]
1222+
[object[]]$DnxArguments)
11951223

1196-
[Alias("arch")]
1197-
[ValidateSet("x86","x64")]
1198-
[Parameter(Mandatory=$false)]
1199-
[string]$Architecture = "",
1200-
1201-
[Alias("r")]
1202-
[ValidateSet("clr","coreclr")]
1203-
[Parameter(Mandatory=$false)]
1204-
[string]$Runtime = "")
1205-
1206-
Get-RuntimeName $VersionOrAlias $Architecture $Runtime
1224+
$runtimeFullName = Get-RuntimeName $VersionOrAlias $Architecture $Runtime
1225+
$runtimeBin = Get-RuntimePath $runtimeFullName
1226+
if ($runtimeBin -eq $null) {
1227+
throw "Cannot find $runtimeFullName, do you need to run '$CommandName install $versionOrAlias'?"
1228+
}
1229+
$dnxExe = Join-Path $runtimeBin "dnx.exe"
1230+
if(!(Test-Path $dnxExe)) {
1231+
throw "Cannot find a dnx.exe in $runtimeBin, the installation may be corrupt. Try running 'dnvm install $VersionOrAlias -f' to reinstall it"
1232+
}
1233+
_WriteDebug "> $dnxExe $DnxArguments"
1234+
& $dnxExe @DnxArguments
12071235
}
12081236

1209-
1210-
# Checks if a specified file exists in the destination folder and if not, copies the file
1211-
# to the destination folder.
1212-
function Safe-Filecopy {
1237+
<#
1238+
.SYNOPSIS
1239+
Executes the specified command in a sub-shell where the PATH has been augmented to include the specified DNX
1240+
.PARAMETER VersionOrAlias
1241+
The version of alias of the runtime to make active in the sub-shell
1242+
.PARAMETER Command
1243+
The command to execute in the sub-shell
1244+
#>
1245+
function dnvm-exec {
12131246
param(
1214-
[Parameter(Mandatory=$true, Position=0)] $Filename,
1215-
[Parameter(Mandatory=$true, Position=1)] $SourceFolder,
1216-
[Parameter(Mandatory=$true, Position=2)] $DestinationFolder)
1247+
[Parameter(Mandatory=$true, Position=0)]
1248+
[string]$VersionOrAlias,
1249+
[Parameter(Mandatory=$false, Position=1)]
1250+
[string]$Command,
1251+
[Parameter(Mandatory=$false, Position=2, ValueFromRemainingArguments=$true)]
1252+
[object[]]$Arguments)
12171253

1218-
# Make sure the destination folder is created if it doesn't already exist.
1219-
if(!(Test-Path $DestinationFolder)) {
1220-
_WriteOut "Creating destination folder '$DestinationFolder' ... "
1221-
1222-
New-Item -Type Directory $Destination | Out-Null
1254+
$runtimeFullName = Get-RuntimeName $VersionOrAlias $Architecture $Runtime
1255+
$runtimeBin = Get-RuntimePath $runtimeFullName
1256+
if ($runtimeBin -eq $null) {
1257+
throw "Cannot find $runtimeFullName, do you need to run '$CommandName install $versionOrAlias'?"
12231258
}
12241259

1225-
$sourceFilePath = Join-Path $SourceFolder $Filename
1226-
$destFilePath = Join-Path $DestinationFolder $Filename
1227-
1228-
if(Test-Path $sourceFilePath) {
1229-
_WriteOut "Installing '$Filename' to '$DestinationFolder' ... "
1230-
1231-
if (Test-Path $destFilePath) {
1232-
_WriteOut " Skipping: file already exists" -ForegroundColor Yellow
1233-
}
1234-
else {
1235-
Copy-Item $sourceFilePath $destFilePath -Force
1236-
}
1237-
}
1238-
else {
1239-
_WriteOut "WARNING: Unable to install: Could not find '$Filename' in '$SourceFolder'. "
1260+
$oldPath = $env:PATH
1261+
try {
1262+
$env:PATH = "$runtimeBin;$($env:PATH)"
1263+
& $Command @Arguments
1264+
} finally {
1265+
$env:PATH = $oldPath
12401266
}
12411267
}
12421268

dnvm.sh

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
# Source this file from your .bash-profile or script to use
33

44
# "Constants"
5-
_DNVM_BUILDNUMBER="beta5-10358"
5+
_DNVM_BUILDNUMBER="beta5-10359"
66
_DNVM_AUTHORS="Microsoft Open Technologies, Inc."
77
_DNVM_RUNTIME_PACKAGE_NAME="dnx"
88
_DNVM_RUNTIME_FRIENDLY_NAME=".NET Execution Environment"
99
_DNVM_RUNTIME_SHORT_NAME="DNX"
1010
_DNVM_RUNTIME_FOLDER_NAME=".dnx"
1111
_DNVM_COMMAND_NAME="dnvm"
12+
_DNVM_PACKAGE_MANAGER_NAME="dnu"
1213
_DNVM_VERSION_MANAGER_NAME=".NET Version Manager"
1314
_DNVM_DEFAULT_FEED="https://www.myget.org/F/aspnetvnext/api/v2"
1415
_DNVM_UPDATE_LOCATION="https://raw.githubusercontent.com/aspnet/Home/dev/dnvm.sh"
@@ -243,6 +244,20 @@ __dnvm_help() {
243244
echo " none remove $_DNVM_RUNTIME_SHORT_NAME bin from path of current command line"
244245
echo " -p|-persistent set selected version as default"
245246
echo ""
247+
printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME run <semver>|<alias> <args...> ${RCol}"
248+
echo " <semver>|<alias> the version or alias to run"
249+
echo " <args...> arguments to be passed to $_DNVM_RUNTIME_SHORT_NAME"
250+
echo ""
251+
echo " runs the $_DNVM_RUNTIME_SHORT_NAME command from the specified version of the runtime without affecting the current PATH"
252+
echo ""
253+
printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME exec <semver>|<alias> <command> <args...> ${RCol}"
254+
echo " <semver>|<alias> the version or alias to execute in"
255+
echo " <command> the command to run"
256+
echo " <args...> arguments to be passed to the command"
257+
echo ""
258+
echo " runs the specified command in the context of the specified version of the runtime without affecting the current PATH"
259+
echo " example: $_DNVM_COMMAND_NAME exec 1.0.0-beta4 $_DNVM_PACKAGE_MANAGER_NAME build"
260+
echo ""
246261
printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME list ${RCol}"
247262
echo " list $_DNVM_RUNTIME_SHORT_NAME versions installed "
248263
echo ""
@@ -347,23 +362,29 @@ dnvm()
347362
fi
348363
;;
349364

350-
"use" )
351-
[ $# -gt 3 ] && __dnvm_help && return
352-
[ $# -lt 2 ] && __dnvm_help && return
365+
"use"|"run"|"exec" )
366+
[[ $1 == "use" && $# -gt 3 ]] && __dnvm_help && return
367+
[[ $1 == "use" && $# -lt 2 ]] && __dnvm_help && return
368+
local cmd=$1
369+
local persistent=
353370

354371
shift
355-
local persistent=
356-
while [ $# -ne 0 ]
357-
do
358-
if [[ $1 == "-p" || $1 == "-persistent" ]]; then
359-
local persistent="true"
360-
elif [[ -n $1 ]]; then
361-
local versionOrAlias=$1
362-
fi
372+
if [ $cmd == "use" ]; then
373+
while [ $# -ne 0 ]
374+
do
375+
if [[ $1 == "-p" || $1 == "-persistent" ]]; then
376+
local persistent="true"
377+
elif [[ -n $1 ]]; then
378+
local versionOrAlias=$1
379+
fi
380+
shift
381+
done
382+
else
383+
local versionOrAlias=$1
363384
shift
364-
done
385+
fi
365386

366-
if [[ $versionOrAlias == "none" ]]; then
387+
if [[ $cmd == "use" && $versionOrAlias == "none" ]]; then
367388
echo "Removing $_DNVM_RUNTIME_SHORT_NAME from process PATH"
368389
# Strip other version from PATH
369390
PATH=$(__dnvm_strip_path "$PATH" "/bin")
@@ -383,15 +404,34 @@ dnvm()
383404
return 1
384405
fi
385406

386-
echo "Adding" $runtimeBin "to process PATH"
387-
388-
PATH=$(__dnvm_strip_path "$PATH" "/bin")
389-
PATH=$(__dnvm_prepend_path "$PATH" "$runtimeBin")
390-
391-
if [[ -n $persistent ]]; then
392-
local runtimeVersion=$(__dnvm_package_version "$runtimeFullName")
393-
$_DNVM_COMMAND_NAME alias default "$runtimeVersion"
394-
fi
407+
case $cmd in
408+
"run")
409+
local hostpath="$runtimeBin/dnx"
410+
if [[ -e $hostpath ]]; then
411+
$hostpath $@
412+
else
413+
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"
414+
fi
415+
;;
416+
"exec")
417+
(
418+
PATH=$(__dnvm_strip_path "$PATH" "/bin")
419+
PATH=$(__dnvm_prepend_path "$PATH" "$runtimeBin")
420+
$@
421+
)
422+
;;
423+
"use")
424+
echo "Adding" $runtimeBin "to process PATH"
425+
426+
PATH=$(__dnvm_strip_path "$PATH" "/bin")
427+
PATH=$(__dnvm_prepend_path "$PATH" "$runtimeBin")
428+
429+
if [[ -n $persistent ]]; then
430+
local runtimeVersion=$(__dnvm_package_version "$runtimeFullName")
431+
$_DNVM_COMMAND_NAME alias default "$runtimeVersion"
432+
fi
433+
;;
434+
esac
395435
;;
396436

397437
"alias" )

0 commit comments

Comments
 (0)