From 3eeb6cfd6e58b430573aad932b9809603cadaee2 Mon Sep 17 00:00:00 2001 From: Brennan Date: Tue, 11 Aug 2015 13:59:00 -0700 Subject: [PATCH] Global Install --- src/dnvm.ps1 | 79 ++++++++-- src/dnvm.sh | 142 ++++++++++++++---- test/ps1/_Execute-Tests.ps1 | 16 +- test/ps1/tests/Alias.Tests.ps1 | 18 +++ test/ps1/tests/EntryPoint.Tests.ps1 | 4 + test/ps1/tests/Install.Tests.ps1 | 25 ++- test/ps1/tests/List.Tests.ps1 | 14 ++ test/ps1/tests/RunAndExec.Tests.ps1 | 4 +- test/sh/run-tests.sh | 6 + ...ocal already installed does not install.sh | 14 ++ .../install/Installing runtime globally.sh | 12 ++ ...ith os specified gives correct defaults.sh | 4 +- ...friendly message when no runtimes found.sh | 6 +- ...displays global and local installed dnx.sh | 22 +++ 14 files changed, 314 insertions(+), 52 deletions(-) create mode 100755 test/sh/tests/install/Installing global with local already installed does not install.sh create mode 100755 test/sh/tests/install/Installing runtime globally.sh mode change 100644 => 100755 test/sh/tests/install/Installing with os specified gives correct defaults.sh create mode 100755 test/sh/tests/list/Listing displays global and local installed dnx.sh diff --git a/src/dnvm.ps1 b/src/dnvm.ps1 index 9c72184..18373cf 100644 --- a/src/dnvm.ps1 +++ b/src/dnvm.ps1 @@ -82,6 +82,7 @@ $FullVersion="$ProductVersion-$BuildVersion" Set-Variable -Option Constant "CommandName" ([IO.Path]::GetFileNameWithoutExtension($ScriptPath)) Set-Variable -Option Constant "CommandFriendlyName" ".NET Version Manager" Set-Variable -Option Constant "DefaultUserDirectoryName" ".dnx" +Set-Variable -Option Constant "DefaultGlobalDirectoryName" "Microsoft DNX" Set-Variable -Option Constant "OldUserDirectoryNames" @(".kre", ".k") Set-Variable -Option Constant "RuntimePackageName" "dnx" Set-Variable -Option Constant "DefaultFeed" "https://www.nuget.org/api/v2" @@ -155,6 +156,7 @@ $DeprecatedCommands = @("unalias") # Load Environment variables $RuntimeHomes = $env:DNX_HOME $UserHome = $env:DNX_USER_HOME +$GlobalHome = $env:DNX_GLOBAL_HOME $ActiveFeed = $env:DNX_FEED $ActiveUnstableFeed = $env:DNX_UNSTABLE_FEED @@ -182,11 +184,24 @@ if($CmdPathFile) { # Determine where runtimes can exist (RuntimeHomes) if(!$RuntimeHomes) { # Set up a default value for the runtime home - $UnencodedHomes = "%USERPROFILE%\$DefaultUserDirectoryName" + $UnencodedHomes = "$env:USERPROFILE\$DefaultUserDirectoryName;$GlobalHome\$DefaultGlobalDirectoryName" } else { $UnencodedHomes = $RuntimeHomes } +# Determine the default global installation directory (GlobalHome) +if(!$GlobalHome) { + if($env:ProgramData) { + $GlobalHome = "$env:ProgramData\$DefaultGlobalDirectoryName" + } else { + $GlobalHome = "$env:AllUsersProfile\$DefaultGlobalDirectoryName" + } + + $env:DNX_GLOBAL_HOME="$GlobalHome" + $env:DNX_HOME="$env:DNX_HOME;$env:DNX_GLOBAL_HOME" + $UnencodedHomes = "$UnencodedHomes;$GlobalHome" +} + $UnencodedHomes = $UnencodedHomes.Split(";") $RuntimeHomes = $UnencodedHomes | ForEach-Object { [Environment]::ExpandEnvironmentVariables($_) } $RuntimeDirs = $RuntimeHomes | ForEach-Object { Join-Path $_ "runtimes" } @@ -221,6 +236,7 @@ _WriteDebug "Runtime Homes: $RuntimeHomes" _WriteDebug "User Home: $UserHome" $AliasesDir = Join-Path $UserHome "alias" $RuntimesDir = Join-Path $UserHome "runtimes" +$GlobalRuntimesDir = Join-Path $GlobalHome "runtimes" $Aliases = $null ### Helper Functions @@ -394,7 +410,7 @@ function Get-RuntimeAliasOrRuntimeInfo( } filter List-Parts { - param($aliases) + param($aliases, $items) $location = "" @@ -422,6 +438,20 @@ filter List-Parts { $parts2 += "x86/x64" } + $aliasUsed = "" + if($items) { + $aliasUsed = $items | ForEach-Object { + if($_.Architecture -eq $parts2[3] -and $_.Runtime -eq $parts2[1] -and $_.OperatingSystem -eq $parts2[2] -and $_.Version -eq $parts1[1]) { + return $true; + } + return $false; + } + } + + if($aliasUsed -eq $true) { + $fullAlias = "" + } + return New-Object PSObject -Property @{ Active = $active Version = $parts1[1] @@ -693,7 +723,7 @@ function Get-RuntimePath($runtimeFullName) { foreach($RuntimeHome in $RuntimeHomes) { $runtimeBin = "$RuntimeHome\runtimes\$runtimeFullName\bin" _WriteDebug " Candidate $runtimeBin" - if (Test-Path "$runtimeBin") { + if (Test-Path $runtimeBin) { _WriteDebug " Found in $runtimeBin" return $runtimeBin } @@ -976,7 +1006,7 @@ function dnvm-list { $RuntimeHomes | ForEach-Object { _WriteDebug "Scanning $_ for runtimes..." if (Test-Path "$_\runtimes") { - $items += Get-ChildItem "$_\runtimes\$RuntimePackageName-*" | List-Parts $aliases + $items += Get-ChildItem "$_\runtimes\$RuntimePackageName-*" | List-Parts $aliases $items } } @@ -1105,6 +1135,8 @@ function dnvm-unalias { For CLR flavor only. Generate native images for runtime libraries on Desktop CLR to improve startup time. This option requires elevated privilege and will be automatically turned on if the script is running in administrative mode. To opt-out in administrative mode, use -NoNative switch. .PARAMETER Unstable Upgrade from the unstable dev feed. This will give you the latest development version of the runtime. +.PARAMETER Global + Installs to configured global dnx file location (default: C:\ProgramData) #> function dnvm-upgrade { param( @@ -1140,7 +1172,10 @@ function dnvm-upgrade { [switch]$Ngen, [Parameter(Mandatory=$false)] - [switch]$Unstable) + [switch]$Unstable, + + [Parameter(Mandatory=$false)] + [switch]$Global) if($OS -ne "win" -and ![String]::IsNullOrEmpty($OS)) { #We could remove OS as an option from upgrade, but I want to take this opporunty to educate users about the difference between install and upgrade @@ -1150,7 +1185,7 @@ function dnvm-upgrade { return } - dnvm-install "latest" -Alias:$Alias -Architecture:$Architecture -Runtime:$Runtime -OS:$OS -Force:$Force -Proxy:$Proxy -NoNative:$NoNative -Ngen:$Ngen -Unstable:$Unstable -Persistent:$true + dnvm-install "latest" -Alias:$Alias -Architecture:$Architecture -Runtime:$Runtime -OS:$OS -Force:$Force -Proxy:$Proxy -NoNative:$NoNative -Ngen:$Ngen -Unstable:$Unstable -Persistent:$true -Global:$Global } <# @@ -1180,6 +1215,8 @@ function dnvm-upgrade { Make the installed runtime useable across all processes run by the current user .PARAMETER Unstable Upgrade from the unstable dev feed. This will give you the latest development version of the runtime. +.PARAMETER Global + Installs to configured global dnx file location (default: C:\ProgramData) .DESCRIPTION A proxy can also be specified by using the 'http_proxy' environment variable #> @@ -1223,7 +1260,10 @@ function dnvm-install { [switch]$Persistent, [Parameter(Mandatory=$false)] - [switch]$Unstable) + [switch]$Unstable, + + [Parameter(Mandatory=$false)] + [switch]$Global) $selectedFeed = "" @@ -1279,7 +1319,7 @@ function dnvm-install { $Version = Get-PackageVersion $BaseName } - if([String]::IsNullOrEmpty($Architecture)) { + if([String]::IsNullOrEmpty($OS)) { $OS = Get-PackageOS $BaseName } } else { @@ -1313,7 +1353,15 @@ function dnvm-install { _WriteDebug "Version: $($runtimeInfo.Version)" _WriteDebug "OS: $($runtimeInfo.OS)" - $RuntimeFolder = Join-Path $RuntimesDir $($runtimeInfo.RuntimeName) + $installDir = $RuntimesDir + if (!$Global) { + $RuntimeFolder = Join-Path $RuntimesDir $($runtimeInfo.RuntimeName) + } + else { + $installDir = $GlobalRuntimesDir + $RuntimeFolder = Join-Path $GlobalRuntimesDir $($runtimeInfo.RuntimeName) + } + _WriteDebug "Destination: $RuntimeFolder" if((Test-Path $RuntimeFolder) -and $Force) { @@ -1321,8 +1369,15 @@ function dnvm-install { Remove-Item $RuntimeFolder -Recurse -Force } - if(Test-Path $RuntimeFolder) { - _WriteOut "'$($runtimeInfo.RuntimeName)' is already installed." + $installed="" + if(Test-Path (Join-Path $RuntimesDir $($runtimeInfo.RuntimeName))) { + $installed = Join-Path $RuntimesDir $($runtimeInfo.RuntimeName) + } + if(Test-Path (Join-Path $GlobalRuntimesDir $($runtimeInfo.RuntimeName))) { + $installed = Join-Path $GlobalRuntimesDir $($runtimeInfo.RuntimeName) + } + if($installed -ne "") { + _WriteOut "'$($runtimeInfo.RuntimeName)' is already installed in $installed." if($runtimeInfo.OS -eq "win") { dnvm-use $runtimeInfo.Version -Architecture:$runtimeInfo.Architecture -Runtime:$runtimeInfo.Runtime -Persistent:$Persistent -OS:$runtimeInfo.OS } @@ -1333,7 +1388,7 @@ function dnvm-install { $Runtime = $runtimeInfo.Runtime $OS = $runtimeInfo.OS - $TempFolder = Join-Path $RuntimesDir "temp" + $TempFolder = Join-Path $installDir "temp" $UnpackFolder = Join-Path $TempFolder $runtimeFullName $DownloadFile = Join-Path $UnpackFolder "$runtimeFullName.nupkg" diff --git a/src/dnvm.sh b/src/dnvm.sh index 758244e..82758ac 100755 --- a/src/dnvm.sh +++ b/src/dnvm.sh @@ -46,12 +46,19 @@ if [ -z "$DNX_USER_HOME" ]; then eval DNX_USER_HOME="~/$_DNVM_RUNTIME_FOLDER_NAME" fi +if [ -z "$DNX_GLOBAL_HOME" ]; then + eval DNX_GLOBAL_HOME="/usr/local/lib/dnx" +fi + if [ -z "$DNX_HOME" ]; then # Set to the user home value - DNX_HOME="$DNX_USER_HOME" + eval DNX_HOME="$DNX_USER_HOME:$DNX_GLOBAL_HOME" +elif [[ $DNX_HOME != *"$DNX_GLOBAL_HOME"* ]]; then + eval DNX_HOME="$DNX_HOME:$DNX_GLOBAL_HOME" fi _DNVM_USER_PACKAGES="$DNX_USER_HOME/runtimes" +_DNVM_GLOBAL_PACKAGES="$DNX_GLOBAL_HOME/runtimes" _DNVM_ALIAS_DIR="$DNX_USER_HOME/alias" _DNVM_DNVM_DIR="$DNX_USER_HOME/dnvm" @@ -146,7 +153,7 @@ __dnvm_find_package() { } __dnvm_strip_path() { - echo "$1" | sed -e "s#$_DNVM_USER_PACKAGES/[^/]*$2[^:]*:##g" -e "s#:$_DNVM_USER_PACKAGES/[^/]*$2[^:]*##g" -e "s#$_DNVM_USER_PACKAGES/[^/]*$2[^:]*##g" + echo "$1" | sed -e "s#$_DNVM_USER_PACKAGES/[^/]*$2[^:]*:##g" -e "s#:$_DNVM_USER_PACKAGES/[^/]*$2[^:]*##g" -e "s#$_DNVM_USER_PACKAGES/[^/]*$2[^:]*##g" | sed -e "s#$_DNVM_GLOBAL_PACKAGES/[^/]*$2[^:]*:##g" -e "s#:$_DNVM_GLOBAL_PACKAGES/[^/]*$2[^:]*##g" -e "s#$_DNVM_GLOBAL_PACKAGES/[^/]*$2[^:]*##g" } __dnvm_prepend_path() { @@ -183,7 +190,7 @@ __dnvm_package_arch() { __dnvm_package_os() { local runtimeFullName="$1" if [[ "$runtimeFullName" =~ "mono" ]]; then - echo "linux/darwin" + echo "linux/osx" else echo "$runtimeFullName" | sed "s/$_DNVM_RUNTIME_PACKAGE_NAME-[^-.]*-\([^.-]*\).*/\1/" fi @@ -206,11 +213,29 @@ __dnvm_update_self() { source "$dnvmFileLocation" } +__dnvm_promptSudo() { + local acceptSudo="$1" + + local answer= + if [ "$acceptSudo" == "0" ]; then + echo "In order to install dnx globally, dnvm will have to temporarily run as root." + read -p "You may be prompted for your password via 'sudo' during this process. Is this Ok? (y/N) " answer + else + answer="y" + fi + if echo $answer | grep -iq "^y" ; then + return 1 + else + return 0 + fi +} + __dnvm_download() { local runtimeFullName="$1" local downloadUrl="$2" local runtimeFolder="$3" local force="$4" + local acceptSudo="$5" local pkgName=$(__dnvm_package_name "$runtimeFullName") local pkgVersion=$(__dnvm_package_version "$runtimeFullName") @@ -231,12 +256,20 @@ __dnvm_download() { return 1 fi + local useSudo= mkdir -p "$runtimeFolder" > /dev/null 2>&1 - + if [ ! -d $runtimeFolder ]; then + if ! __dnvm_promptSudo $acceptSudo ; then + useSudo=sudo + sudo mkdir -p "$runtimeFolder" > /dev/null 2>&1 || return 1 + else + return 1 + fi + fi echo "Downloading $runtimeFullName from $DNX_ACTIVE_FEED" echo "Download: $downloadUrl" - local httpResult=$(curl -L -D - "$downloadUrl" -o "$runtimeFile" -# | grep "^HTTP/1.1" | head -n 1 | sed "s/HTTP.1.1 \([0-9]*\).*/\1/") + local httpResult=$($useSudo curl -L -D - "$downloadUrl" -o "$runtimeFile" -# | grep "^HTTP/1.1" | head -n 1 | sed "s/HTTP.1.1 \([0-9]*\).*/\1/") if [[ $httpResult == "404" ]]; then printf "%b\n" "${Red}$runtimeFullName was not found in repository $DNX_ACTIVE_FEED ${RCol}" @@ -245,13 +278,14 @@ __dnvm_download() { fi [[ $httpResult != "302" && $httpResult != "200" ]] && echo "${Red}HTTP Error $httpResult fetching $runtimeFullName from $DNX_ACTIVE_FEED ${RCol}" && return 1 - __dnvm_unpack $runtimeFile $runtimeFolder + __dnvm_unpack $runtimeFile $runtimeFolder $useSudo return $? } __dnvm_unpack() { local runtimeFile="$1" local runtimeFolder="$2" + local useSudo=$3 echo "Installing to $runtimeFolder" @@ -260,24 +294,24 @@ __dnvm_unpack() { return 1 fi - unzip $runtimeFile -d $runtimeFolder > /dev/null 2>&1 + $useSudo unzip $runtimeFile -d $runtimeFolder > /dev/null 2>&1 - [ -e "$runtimeFolder/[Content_Types].xml" ] && rm "$runtimeFolder/[Content_Types].xml" + [ -e "$runtimeFolder/[Content_Types].xml" ] && $useSudo rm "$runtimeFolder/[Content_Types].xml" - [ -e "$runtimeFolder/_rels/" ] && rm -rf "$runtimeFolder/_rels/" + [ -e "$runtimeFolder/_rels/" ] && $useSudo rm -rf "$runtimeFolder/_rels/" - [ -e "$runtimeFolder/package/" ] && rm -rf "$runtimeFolder/_package/" + [ -e "$runtimeFolder/package/" ] && $useSudo rm -rf "$runtimeFolder/_package/" - [ -e "$runtimeFile" ] && rm -f "$runtimeFile" + [ -e "$runtimeFile" ] && $useSudo rm -f "$runtimeFile" #Set dnx to be executable if [[ -s "$runtimeFolder/bin/dnx" ]]; then - chmod 775 "$runtimeFolder/bin/dnx" + $useSudo chmod 775 "$runtimeFolder/bin/dnx" fi #Set dnu to be executable if [[ -s "$runtimeFolder/bin/dnu" ]]; then - chmod 775 "$runtimeFolder/bin/dnu" + $useSudo chmod 775 "$runtimeFolder/bin/dnu" fi } @@ -306,6 +340,9 @@ __dnvm_requested_version_or_alias() { if [ "$arch" != "" ]; then local pkgArchitecture="$arch" fi + if [ "$os" == "" ]; then + local pkgSystem=$(__dnvm_current_os) + fi echo "$_DNVM_RUNTIME_PACKAGE_NAME-$runtime-$pkgSystem-$pkgArchitecture.$pkgVersion" fi @@ -316,7 +353,11 @@ __dnvm_requested_version_or_alias() { # This will be more relevant if we support global installs __dnvm_locate_runtime_bin_from_full_name() { local runtimeFullName=$1 - [ -e "$_DNVM_USER_PACKAGES/$runtimeFullName/bin" ] && echo "$_DNVM_USER_PACKAGES/$runtimeFullName/bin" && return + for v in `echo $DNX_HOME | tr ":" "\n"`; do + if [ -e "$v/runtimes/$runtimeFullName/bin" ]; then + echo "$v/runtimes/$runtimeFullName/bin" && return + fi + done } __echo_art() { @@ -358,15 +399,17 @@ __dnvm_help() { __dnvm_description printf "%b\n" "${Cya}USAGE:${Yel} $_DNVM_COMMAND_NAME [options] ${RCol}" echo "" - printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME upgrade [-f|-force] [-u|-unstable] ${RCol}" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME upgrade [-f|-force] [-u|-unstable] [-g|-global] [-y]${RCol}" echo " install latest $_DNVM_RUNTIME_SHORT_NAME from feed" echo " adds $_DNVM_RUNTIME_SHORT_NAME bin to path of current command line" echo " set installed version as default" echo " -f|forces force upgrade. Overwrite existing version of $_DNVM_RUNTIME_SHORT_NAME if already installed" echo " -u|unstable use unstable feed. Installs the $_DNVM_RUNTIME_SHORT_NAME from the unstable feed" echo " -r|runtime The runtime flavor to install [clr or coreclr] (default: clr)" + echo " -g|global Installs the latest $_DNVM_RUNTIME_SHORT_NAME in the configured global $_DNVM_RUNTIME_SHORT_NAME file location (default: /usr/local/lib/dnx current: $DNX_GLOBAL_HOME)" + echo " -y Assume Yes to all queries and do not prompt" echo "" - printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME install |||latest [-r ] [-OS ] [-a|-alias ] [-p|-persistent] [-f|-force] [-u|-unstable] ${RCol}" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME install |||latest [-r ] [-OS ] [-a|-alias ] [-p|-persistent] [-f|-force] [-u|-unstable] [-g|-global] [-y]${RCol}" echo " | install requested $_DNVM_RUNTIME_SHORT_NAME from feed" echo " install requested $_DNVM_RUNTIME_SHORT_NAME from local package on filesystem" echo " latest install latest version of $_DNVM_RUNTIME_SHORT_NAME from feed" @@ -376,6 +419,8 @@ __dnvm_help() { echo " -f|force force install. Overwrite existing version of $_DNVM_RUNTIME_SHORT_NAME if already installed" echo " -u|unstable use unstable feed. Installs the $_DNVM_RUNTIME_SHORT_NAME from the unstable feed" echo " -r|runtime The runtime flavor to install [mono or coreclr] (default: mono)" + echo " -g|global Installs to the configured global $_DNVM_RUNTIME_SHORT_NAME file location (default: /usr/local/lib/dnx current: $DNX_GLOBAL_HOME)" + echo " -y Assume Yes to all queries and do not prompt" echo "" echo " adds $_DNVM_RUNTIME_SHORT_NAME bin to path of current command line" echo "" @@ -460,6 +505,8 @@ dnvm() local os= local runtime= local arch= + local global=0 + local acceptSudo=0 while [ $# -ne 0 ] do if [[ $1 == "-p" || $1 == "-persistent" ]]; then @@ -477,6 +524,8 @@ dnvm() elif [[ $1 == "-OS" ]]; then local os=$2 shift + elif [[ $1 == "-y" ]]; then + local acceptSudo=1 elif [[ $1 == "-arch" ]]; then local arch=$2 shift @@ -485,7 +534,8 @@ dnvm() printf "%b\n" "${Red}Architecture must be x86 or x64.${RCol}" return 1 fi - + elif [[ $1 == "-g" || $1 == "-global" ]]; then + local global=1 elif [[ -n $1 ]]; then [[ -n $versionOrAlias ]] && echo "Invalid option $1" && __dnvm_help && return 1 local versionOrAlias=$1 @@ -533,6 +583,11 @@ dnvm() printf "%b\n" "${Yel}It appears you don't have Mono available. Remember to get Mono before trying to run $DNVM_RUNTIME_SHORT_NAME application. ${RCol}" >&2; fi + local runtimeDir=$_DNVM_USER_PACKAGES + if [ $global == 1 ]; then + runtimeDir=$_DNVM_GLOBAL_PACKAGES + fi + if [[ "$versionOrAlias" != *.nupkg ]]; then if [[ "$versionOrAlias" == "latest" ]]; then echo "Determining latest version" @@ -542,12 +597,24 @@ dnvm() else local runtimeFullName=$(__dnvm_requested_version_or_alias "$versionOrAlias" "$runtime" "$arch" "$os") local runtimeVersion=$(__dnvm_package_version "$runtimeFullName") + read versionOrAlias downloadUrl < <(__dnvm_find_package "$runtime" "$arch" "$os" "$runtimeVersion") [[ $? == 1 ]] && echo "Error: Could not find version $runtimeVersion in feed $DNX_ACTIVE_FEED" && return 1 fi local runtimeFullName=$(__dnvm_requested_version_or_alias "$versionOrAlias" "$runtime" "$arch" "$os") - local runtimeFolder="$_DNVM_USER_PACKAGES/$runtimeFullName" - __dnvm_download "$runtimeFullName" "$downloadUrl" "$runtimeFolder" "$force" + local runtimeFolder="$runtimeDir/$runtimeFullName" + + local exist=0 + for folder in `echo $DNX_HOME | tr ":" "\n"`; do + if [ -e "$folder/runtimes/$runtimeFullName" ]; then + echo "$runtimeFullName already installed in $folder" + exist=1 + fi + done + + if [[ $exist != 1 ]]; then + __dnvm_download "$runtimeFullName" "$downloadUrl" "$runtimeFolder" "$force" "$acceptSudo" + fi [[ $? == 1 ]] && return 1 if [[ "$os" == $(__dnvm_current_os) ]]; then $_DNVM_COMMAND_NAME use "$versionOrAlias" "$persistent" "-runtime" "$runtime" "-arch" "$arch" @@ -556,7 +623,7 @@ dnvm() else local runtimeFullName=$(basename $versionOrAlias | sed "s/\(.*\)\.nupkg/\1/") local runtimeVersion=$(__dnvm_package_version "$runtimeFullName") - local runtimeFolder="$_DNVM_USER_PACKAGES/$runtimeFullName" + local runtimeFolder="$runtimeDir/$runtimeFullName" local runtimeFile="$runtimeFolder/$runtimeFullName.nupkg" local runtimeClr=$(__dnvm_package_runtime "$runtimeFullName") @@ -568,9 +635,18 @@ dnvm() if [ -e "$runtimeFolder" ]; then echo "$runtimeFullName already installed" else + local useSudo= mkdir -p "$runtimeFolder" > /dev/null 2>&1 + if [ ! -d $runtimeFolder ]; then + if ! __dnvm_promptSudo $acceptSudo ; then + useSudo=sudo + sudo mkdir -p "$runtimeFolder" > /dev/null 2>&1 || return 1 + else + return 1 + fi + fi cp -a "$versionOrAlias" "$runtimeFile" - __dnvm_unpack "$runtimeFile" "$runtimeFolder" + __dnvm_unpack "$runtimeFile" "$runtimeFolder" $useSudo [[ $? == 1 ]] && return 1 fi $_DNVM_COMMAND_NAME use "$runtimeVersion" "$persistent" -r "$runtimeClr" @@ -712,7 +788,7 @@ dnvm() local runtimeFullName=$(__dnvm_requested_version_or_alias "$versionOrAlias" "$runtime" "$arch" "$os") - [[ ! -d "$_DNVM_USER_PACKAGES/$runtimeFullName" ]] && echo "$runtimeFullName is not an installed $_DNVM_RUNTIME_SHORT_NAME version" && return 1 + ([[ ! -d "$_DNVM_USER_PACKAGES/$runtimeFullName" ]] && [[ ! -d "$_DNVM_GLOBAL_PACKAGES/$runtimeFullName" ]]) && echo "$runtimeFullName is not an installed $_DNVM_RUNTIME_SHORT_NAME version" && return 1 local action="Setting" [[ -e "$_DNVM_ALIAS_DIR/$name.alias" ]] && action="Updating" @@ -736,7 +812,15 @@ dnvm() [[ ! -d $_DNVM_USER_PACKAGES ]] && echo "$_DNVM_RUNTIME_FRIENDLY_NAME is not installed." && return 1 local searchGlob="$_DNVM_RUNTIME_PACKAGE_NAME-*" - local runtimes="$(find $_DNVM_USER_PACKAGES -name "$searchGlob" \( -type d -or -type l \) -prune -exec basename {} \;)" + + local runtimes="" + for location in `echo $DNX_HOME | tr ":" "\n"`; do + location+="/runtimes" + local oruntimes="$(find $location -name "$searchGlob" \( -type d -or -type l \) -prune -exec basename {} \;)" + for v in `echo $oruntimes | tr "\n" " "`; do + runtimes+="$v:$location"$'\n' + done + done [[ -z $runtimes ]] && echo 'No runtimes installed. You can run `dnvm install latest` or `dnvm upgrade` to install a runtime.' && return @@ -752,7 +836,7 @@ dnvm() local format="%-20s %s\n" if [ -d "$_DNVM_ALIAS_DIR" ]; then for __dnvm_file in $(find "$_DNVM_ALIAS_DIR" -name *.alias); do - if [ ! -d "$_DNVM_USER_PACKAGES/$(cat $__dnvm_file)" ]; then + if [ ! -d "$_DNVM_USER_PACKAGES/$(cat $__dnvm_file)" ] && [ ! -d "$_DNVM_GLOBAL_PACKAGES/$(cat $__dnvm_file)" ]; then arr[$i]="$(basename $__dnvm_file | sed 's/\.alias//')/missing/$(cat $__dnvm_file)" runtimes="$runtimes $(cat $__dnvm_file)" else @@ -792,10 +876,12 @@ dnvm() printf "$formatString" "------" "-------" "-------" "------------" "---------------" "-----" fi - for f in `echo $runtimes | sort -t. -k2 -k3 -k4 -k1`; do - local formattedHome=`(echo $_DNVM_USER_PACKAGES | sed s=$HOME=~=g)` + for f in `echo -e "$runtimes" | sort -t. -k2 -k3 -k4 -k1`; do + local location=`echo $f | sed 's/.*\([:]\)//'` + f=`echo $f | sed 's/\([:]\).*//'` + local formattedHome=`(echo $location | sed s=$HOME=~=g)` local active="" - [[ $PATH == *"$_DNVM_USER_PACKAGES/$f/bin"* ]] && local active=" *" + [[ $PATH == *"$location/$f/bin"* ]] && local active=" *" local pkgRuntime=$(__dnvm_package_runtime "$f") local pkgName=$(__dnvm_package_name "$f") local pkgVersion=$(__dnvm_package_version "$f") @@ -834,7 +920,7 @@ dnvm() } # Add the home location's bin directory to the path if it doesn't exist -[[ ":$PATH:" != *":$DNX_HOME/bin:"* ]] && export PATH="$DNX_HOME/bin:$PATH" +[[ ":$PATH:" != *":$DNX_USER_HOME/bin:"* ]] && export PATH="$DNX_USER_HOME/bin:$PATH" # Generate the command function using the constant defined above. $_DNVM_COMMAND_NAME list default >/dev/null && $_DNVM_COMMAND_NAME use default >/dev/null || true diff --git a/test/ps1/_Execute-Tests.ps1 b/test/ps1/_Execute-Tests.ps1 index 760585c..f2147b4 100644 --- a/test/ps1/_Execute-Tests.ps1 +++ b/test/ps1/_Execute-Tests.ps1 @@ -39,6 +39,7 @@ if(!$PesterPath) { $PesterPath = Join-Path $scriptDir ".pester" } if(!$TestsPath) { $TestsPath = Join-Path $scriptDir "tests" } if(!$TargetPath) { $TargetPath = Convert-Path (Join-Path $scriptDir "../../src/dnvm.ps1") } if(!$TestWorkingDir) { $TestWorkingDir = Join-Path $scriptDir "testwork" } +if(!$TestWorkingGlobalDir) { $TestWorkingGlobalDir = Join-Path $scriptDir "testworkGlobal" } if(!$TestAppsDir) { $TestAppsDir = Convert-Path (Join-Path $scriptDir "../apps") } @@ -48,9 +49,9 @@ if(!$TestAppsDir) { $TestAppsDir = Convert-Path (Join-Path $scriptDir "../apps") #$env:DNX_FEED = "https://www.myget.org/F/aspnetrelease/api/v2" #NOTE: This should be set back to release once we have non windows DNX on that feed. $env:DNX_FEED = "https://www.myget.org/F/aspnetvnext/api/v2" -$TestRuntimeVersion = "1.0.0-beta6-12208" +$TestRuntimeVersion = "1.0.0-beta8-15530" $specificNupkgUrl = "$($env:DNX_FEED)/package/dnx-coreclr-win-x64/$TestRuntimeVersion" -$specificNupkgHash = "75aOIb/kOT3UYsTKcE1vmD0WeJUmChHk54mm4JItxjM=" +$specificNupkgHash = "PJpvX+iILTIUA0HxKMPRSU6oO4vkEKe1J/wsors0rIw=" $specificNupkgName = "dnx-coreclr-win-x64.$TestRuntimeVersion.nupkg" $specificNuPkgFxName = "Asp.Net,Version=v5.0" @@ -62,10 +63,17 @@ if(Test-Path "$TestWorkingDir\$RuntimeFolderName") { Write-Banner "Wiping old test working area" del -rec -for "$TestWorkingDir\$RuntimeFolderName" } +if(Test-Path "$TestWorkingGlobalDir\$RuntimeFolderName") { + Write-Banner "Wiping old global test working area" + del -rec -for "$TestWorkingGlobalDir\$RuntimeFolderName" +} if(!(Test-Path $TestWorkingDir)) { mkdir $TestWorkingDir | Out-Null } +if(!(Test-Path $TestWorkingGlobalDir)) { + mkdir $TestWorkingGlobalDir | Out-Null +} # Import the module and set up test environment Import-Module "$PesterPath\Pester.psm1" @@ -88,8 +96,10 @@ Remove-EnvVar PATH # Set up the user/global install directories to be inside the test work area $UserPath = "$TestWorkingDir\$RuntimeFolderName" -$env:DNX_HOME=$UserPath +$GlobalPath = "$TestWorkingGlobalDir\$RuntimeFolderName" +$env:DNX_HOME="$($UserPath);$($GlobalPath)" $env:DNX_USER_HOME=$UserPath +$env:DNX_GLOBAL_HOME=$GlobalPath mkdir $UserPath | Out-Null # Helper function to run dnvm and capture stuff. diff --git a/test/ps1/tests/Alias.Tests.ps1 b/test/ps1/tests/Alias.Tests.ps1 index c939296..b563ebe 100644 --- a/test/ps1/tests/Alias.Tests.ps1 +++ b/test/ps1/tests/Alias.Tests.ps1 @@ -99,4 +99,22 @@ Describe "alias" -Tag "alias" { "$UserPath\alias\$testAlias.txt" | Should Not Exist } } + + Context "When aliasing global dnx" { + $runtimeName = GetRuntimeName "CLR" "x86" + if(Test-Path $UserPath\runtimes\$runtimeName) { del -rec -for $UserPath\runtimes\$runtimeName } + __dnvmtest_run install $TestRuntimeVersion -arch x86 -r "CLR" -g | Out-Null + __dnvmtest_run alias globalAlias $TestRuntimeVersion | Out-Null + + It "keeps alias local" { + "$UserPath\alias\globalAlias.txt" | Should Exist + } + + It "references global installed dnx" { + __dnvmtest_run use globalAlias | Out-Null + $__dnvmtest_out.Trim() | Should Be "Adding $GlobalPath\runtimes\$runtimeName\bin to process PATH" + } + + if(Test-Path $GlobalPath\runtimes\$runtimeName) { del -rec -for $GlobalPath\runtimes\$runtimeName } + } } diff --git a/test/ps1/tests/EntryPoint.Tests.ps1 b/test/ps1/tests/EntryPoint.Tests.ps1 index 841c66a..064009c 100644 --- a/test/ps1/tests/EntryPoint.Tests.ps1 +++ b/test/ps1/tests/EntryPoint.Tests.ps1 @@ -1,4 +1,6 @@ Describe "entrypoint" -Tag "entrypoint" { + __dnvmtest_run install $TestRuntimeVersion | Out-Null + Context "When an invalid command is specified" { It "returns exit code and displays help" { __dnvmtest_run sdfjklsdfljkasdfjklasdfjkl | Out-Null @@ -13,4 +15,6 @@ Describe "entrypoint" -Tag "entrypoint" { $__dnvmtest_out | Should Match "usage:" } } + + del -rec -for $UserPath\runtimes\ } diff --git a/test/ps1/tests/Install.Tests.ps1 b/test/ps1/tests/Install.Tests.ps1 index 6709b2f..0d597d1 100644 --- a/test/ps1/tests/Install.Tests.ps1 +++ b/test/ps1/tests/Install.Tests.ps1 @@ -42,7 +42,7 @@ function DefineInstallTests($clr, $arch, $os) { try { "$runtimeRoot\bin\$RuntimeHostName" | Should Exist - $output = & "$runtimeRoot\bin\$RuntimeHostName" . run + $output = & "$runtimeRoot\bin\$RuntimeHostName" run $LASTEXITCODE | Should Be 0 $fullOutput = [String]::Join("`r`n", $output) $output | ForEach-Object { Write-Verbose $_ } @@ -87,7 +87,7 @@ Describe "install" -Tag "install" { $runtimeName = GetRuntimeName -clr CLR -arch x86 It "uses x86" { - ($__dnvmtest_out.Trim() -like "*'$runtimeName' is already installed.`r`nAdding $UserPath\runtimes\$runtimeName\bin to process PATH*") | Should Be $true + ($__dnvmtest_out.Trim() -like "*'$runtimeName' is already installed in $UserPath\runtimes\$runtimeName.`r`nAdding $UserPath\runtimes\$runtimeName\bin to process PATH*") | Should Be $true } } @@ -96,7 +96,7 @@ Describe "install" -Tag "install" { $runtimeName = GetRuntimeName -clr CLR -arch x86 It "uses Desktop CLR" { - ($__dnvmtest_out.Trim() -like "*'$runtimeName' is already installed.`r`nAdding $UserPath\runtimes\$runtimeName\bin to process PATH*") | Should Be $true + ($__dnvmtest_out.Trim() -like "*'$runtimeName' is already installed in $UserPath\runtimes\$runtimeName.`r`nAdding $UserPath\runtimes\$runtimeName\bin to process PATH*") | Should Be $true } } @@ -105,7 +105,7 @@ Describe "install" -Tag "install" { $runtimeName = GetRuntimeName -clr CLR -arch x86 It "uses x86/Desktop" { - ($__dnvmtest_out.Trim() -like "*'$runtimeName' is already installed.`r`nAdding $UserPath\runtimes\$runtimeName\bin to process PATH*") | Should Be $true + ($__dnvmtest_out.Trim() -like "*'$runtimeName' is already installed in $UserPath\runtimes\$runtimeName.`r`nAdding $UserPath\runtimes\$runtimeName\bin to process PATH*") | Should Be $true } } @@ -171,7 +171,7 @@ Describe "install" -Tag "install" { $runtimePath = "$UserPath\runtimes\$runtimeName" It "ensures the runtime is installed" { __dnvmtest_run install $TestRuntimeVersion -arch x86 -r "CLR" | Out-Null - ($__dnvmtest_out.Trim() -like "*'$runtimeName' is already installed.`r`nAdding $UserPath\runtimes\$runtimeName\bin to process PATH*") | Should Be $true + ($__dnvmtest_out.Trim() -like "*'$runtimeName' is already installed in $UserPath\runtimes\$runtimeName.`r`nAdding $UserPath\runtimes\$runtimeName\bin to process PATH*") | Should Be $true $runtimePath | Should Exist } } @@ -188,4 +188,19 @@ Describe "install" -Tag "install" { $runtimeRoot | Should Exist } } + + Context "When installing global" { + $runtimeName = GetRuntimeName "CLR" "x86" + if(Test-Path $UserPath\runtimes\$runtimeName) { del -rec -for $UserPath\runtimes\$runtimeName } + if(Test-Path $GlobalPath\runtimes\$runtimeName) { del -rec -for $GlobalPath\runtimes\$runtimeName } + + It "installs runtime to global install location" { + __dnvmtest_run install $TestRuntimeVersion -arch x86 -r "CLR" -g | Out-Null + + ($__dnvmtest_out.Trim() -like "*Installing to $GlobalPath\runtimes\$runtimeName`r`nAdding $GlobalPath\runtimes\$runtimeName\bin to process PATH*") | Should Be $true + "$GlobalPath\runtimes\$runtimeName" | Should Exist + } + + del -rec -for $GlobalPath\runtimes\$runtimeName + } } diff --git a/test/ps1/tests/List.Tests.ps1 b/test/ps1/tests/List.Tests.ps1 index 27c04a7..65aecd6 100644 --- a/test/ps1/tests/List.Tests.ps1 +++ b/test/ps1/tests/List.Tests.ps1 @@ -37,4 +37,18 @@ Describe "list" -Tag "list" { $default.Location | Should Not BeNullOrEmpty } } + + Context "When list contains a global dnx" { + $runtimeName = GetRuntimeName "CLR" "x86" + if(Test-Path $UserPath\runtimes\$runtimeName) { del -rec -for $UserPath\runtimes\$runtimeName } + if(Test-Path $GlobalPath\runtimes\$runtimeName) { del -rec -for $GlobalPath\runtimes\$runtimeName } + __dnvmtest_run install $TestRuntimeVersion -arch x86 -r "CLR" -g | Out-Null + + $runtimes = (__dnvmtest_run list -PassThru -Detailed) + It "shows location correctly" { + $runtimes | Where { $_.Location -like "$GlobalPath\runtimes" } | Should Not BeNullOrEmpty + } + + if(Test-Path $GlobalPath\runtimes\$runtimeName) { del -rec -for $GlobalPath\runtimes\$runtimeName } + } } \ No newline at end of file diff --git a/test/ps1/tests/RunAndExec.Tests.ps1 b/test/ps1/tests/RunAndExec.Tests.ps1 index 5dadc8b..ed83ddc 100644 --- a/test/ps1/tests/RunAndExec.Tests.ps1 +++ b/test/ps1/tests/RunAndExec.Tests.ps1 @@ -5,7 +5,7 @@ Describe "run" -Tag "run" { It "executes dnx with provides args" { __dnvmtest_run use none | Out-Null { Get-Command dnx -ErrorAction Stop } | Should Throw - __dnvmtest_run run $TestRuntimeVersion . | Should Be "Please specify the command to run" + (__dnvmtest_run run $TestRuntimeVersion | Select-String -SimpleMatch "Microsoft .NET Execution environment CLR-x86-$TestRuntimeVersion") | Should Be $true $__dnvmtest_exit | Should Be 0 } } @@ -15,7 +15,7 @@ Describe "run" -Tag "run" { __dnvmtest_run alias "test_alias_run" $TestRuntimeVersion | Out-Null __dnvmtest_run use none | Out-Null { Get-Command dnx -ErrorAction Stop } | Should Throw - __dnvmtest_run run "test_alias_run" . | Should Be "Please specify the command to run" + (__dnvmtest_run run "test_alias_run" | Select-String -SimpleMatch "Microsoft .NET Execution environment CLR-x86-$TestRuntimeVersion") | Should Be $true $__dnvmtest_exit | Should Be 0 } } diff --git a/test/sh/run-tests.sh b/test/sh/run-tests.sh index dd2eaff..f92adda 100755 --- a/test/sh/run-tests.sh +++ b/test/sh/run-tests.sh @@ -95,10 +95,16 @@ for shell in $TEST_SHELLS; do fi fi mkdir "$TEST_WORK_DIR/$shell" + mkdir "$TEST_WORK_DIR/$shell/runtimes" + mkdir "$TEST_WORK_DIR/${shell}_global" + mkdir "$TEST_WORK_DIR/${shell}_global/runtimes" export DNX_USER_HOME="$TEST_WORK_DIR/$shell" [ -d $DNX_USER_HOME ] || mkdir $DNX_USER_HOME + export DNX_GLOBAL_HOME="$TEST_WORK_DIR/${shell}_global" + [ -d $DNX_GLOBAL_HOME ] || mkdir $DNX_GLOBAL_HOME + pushd "$SCRIPT_DIR/tests" >/dev/null 2>&1 $CHESTER $@ -s $shell -n $shell "*" err_code="$?" diff --git a/test/sh/tests/install/Installing global with local already installed does not install.sh b/test/sh/tests/install/Installing global with local already installed does not install.sh new file mode 100755 index 0000000..07e14b5 --- /dev/null +++ b/test/sh/tests/install/Installing global with local already installed does not install.sh @@ -0,0 +1,14 @@ +source $COMMON_HELPERS +source $_DNVM_PATH + +mv $DNX_USER_HOME/runtimes $DNX_USER_HOME/runtimes_backup +mv $DNX_GLOBAL_HOME/runtimes $DNX_GLOBAL_HOME/runtimes_backup + +$_DNVM_COMMAND_NAME install $_TEST_VERSION + +OUTPUT=$($_DNVM_COMMAND_NAME install $_TEST_VERSION -g) +echo $OUTPUT | grep -E "\salready installed in $DNX_USER_HOME" || die "expected message was not reported" + +rm -Rf $DNX_USER_HOME/runtimes +mv $DNX_USER_HOME/runtimes_backup $DNX_USER_HOME/runtimes +mv $DNX_GLOBAL_HOME/runtimes_backup $DNX_GLOBAL_HOME/runtimes diff --git a/test/sh/tests/install/Installing runtime globally.sh b/test/sh/tests/install/Installing runtime globally.sh new file mode 100755 index 0000000..539bc43 --- /dev/null +++ b/test/sh/tests/install/Installing runtime globally.sh @@ -0,0 +1,12 @@ +source $COMMON_HELPERS +source $_DNVM_PATH + +mv $DNX_USER_HOME/runtimes $DNX_USER_HOME/runtimes_backup +mv $DNX_GLOBAL_HOME/runtimes $DNX_GLOBAL_HOME/runtimes_backup + +OUTPUT=$($_DNVM_COMMAND_NAME install $_TEST_VERSION -g -y) +echo $OUTPUT | grep -E "Installing to $DNX_GLOBAL_HOME/runtimes/dnx-mono.$_TEST_VERSION" || die "expected message was not reported" + +rm -rf $DNX_GLOBAL_HOME/runtimes +mv $DNX_GLOBAL_HOME/runtimes_backup $DNX_GLOBAL_HOME/runtimes +mv $DNX_USER_HOME/runtimes_backup $DNX_USER_HOME/runtimes diff --git a/test/sh/tests/install/Installing with os specified gives correct defaults.sh b/test/sh/tests/install/Installing with os specified gives correct defaults.sh old mode 100644 new mode 100755 index 6e33a6d..07b3510 --- a/test/sh/tests/install/Installing with os specified gives correct defaults.sh +++ b/test/sh/tests/install/Installing with os specified gives correct defaults.sh @@ -23,4 +23,6 @@ $_DNVM_COMMAND_NAME install $_TEST_VERSION -OS linux RUNTIME_PATH="$DNX_USER_HOME/runtimes/$_DNVM_RUNTIME_PACKAGE_NAME-mono.$_TEST_VERSION" [ -f "$RUNTIME_PATH/bin/$_DNVM_RUNTIME_HOST_NAME" ] || die "$_DNVM_COMMAND_NAME did not include '$_DNVM_RUNTIME_HOST_NAME' command!" -[ -f "$RUNTIME_PATH/bin/$_DNVM_PACKAGE_MANAGER_NAME" ] || die "$_DNVM_COMMAND_NAME did not include '$_DNVM_PACKAGE_MANAGER_NAME' command!" \ No newline at end of file +[ -f "$RUNTIME_PATH/bin/$_DNVM_PACKAGE_MANAGER_NAME" ] || die "$_DNVM_COMMAND_NAME did not include '$_DNVM_PACKAGE_MANAGER_NAME' command!" + +rm -Rf $DNX_USER_HOME/runtimes diff --git a/test/sh/tests/list/Listing displays friendly message when no runtimes found.sh b/test/sh/tests/list/Listing displays friendly message when no runtimes found.sh index a6b0a77..021d60c 100755 --- a/test/sh/tests/list/Listing displays friendly message when no runtimes found.sh +++ b/test/sh/tests/list/Listing displays friendly message when no runtimes found.sh @@ -3,7 +3,9 @@ source $_DNVM_PATH # Clean all runtimes (temporarily) mv $DNX_USER_HOME/runtimes $DNX_USER_HOME/runtimes_backup +mv $DNX_GLOBAL_HOME/runtimes $DNX_GLOBAL_HOME/runtimes_backup mkdir -p $DNX_USER_HOME/runtimes +mkdir -p $DNX_GLOBAL_HOME/runtimes # List runtimes OUTPUT=$($_DNVM_COMMAND_NAME list || die "failed at listing runtimes") @@ -11,4 +13,6 @@ echo $OUTPUT | grep "^No runtimes installed" || die "expected message was not re # Restore runtimes rm -Rf $DNX_USER_HOME/runtimes -mv $DNX_USER_HOME/runtimes_backup $DNX_USER_HOME/runtimes \ No newline at end of file +rm -Rf $DNX_GLOBAL_HOME/runtimes +mv $DNX_USER_HOME/runtimes_backup $DNX_USER_HOME/runtimes +mv $DNX_GLOBAL_HOME/runtimes_backup $DNX_GLOBAL_HOME/runtimes diff --git a/test/sh/tests/list/Listing displays global and local installed dnx.sh b/test/sh/tests/list/Listing displays global and local installed dnx.sh new file mode 100755 index 0000000..39fd7bf --- /dev/null +++ b/test/sh/tests/list/Listing displays global and local installed dnx.sh @@ -0,0 +1,22 @@ +source $COMMON_HELPERS +source $_DNVM_PATH + +# Clean all runtime (temporarily) +mv $DNX_USER_HOME/runtimes $DNX_USER_HOME/runtimes_backup +mv $DNX_GLOBAL_HOME/runtimes $DNX_GLOBAL_HOME/runtimes_backup + +# Install runtimes +$_DNVM_COMMAND_NAME install "$_TEST_VERSION" +$_DNVM_COMMAND_NAME install "$_TEST_VERSION" -r coreclr -g +echo $($_DNVM_COMMAND_NAME list -detailed) +# List runtimes +OUTPUT=$($_DNVM_COMMAND_NAME list -detailed || die "failed at listing runtimes") +OS="$(__dnvm_current_os)" + +echo $OUTPUT | grep -E "\s+$_TEST_VERSION\s+mono\s+linux/osx\s+$(echo $DNX_USER_HOME | sed s=$HOME=~=g)/runtimes" || die "expected message was not reported" +echo $OUTPUT | grep -E "\s+$_TEST_VERSION\s+coreclr\s+x64\s+$OS\s+$(echo $DNX_GLOBAL_HOME | sed s=$HOME=~=g)/runtimes" || die "expected message was not reported" + +rm -rf $DNX_GLOBAL_HOME/runtimes +rm -rf $DNX_USER_HOME/runtimes +mv $DNX_USER_HOME/runtimes_backup $DNX_USER_HOME/runtimes +mv $DNX_GLOBAL_HOME/runtimes_backup $DNX_GLOBAL_HOME/runtimes