From 5f67ade5bcc65ad0c2f825e2f20ef968630db1a3 Mon Sep 17 00:00:00 2001 From: "J.P. Hutchins" Date: Sun, 9 Apr 2023 15:47:19 -0700 Subject: [PATCH] feat: 0.5; expand variables for ADD_TO_PATH; test --- envr.ps1 | 60 ++++++++++++++++---------------- tests/fixtures/expansion | 6 +++- tests/sh/test_expansion.sh | 4 ++- tests/windows/test_expansion.ps1 | 3 ++ 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/envr.ps1 b/envr.ps1 index c7869d4..7b7e166 100644 --- a/envr.ps1 +++ b/envr.ps1 @@ -1,4 +1,4 @@ -# envr v0.4.0 +# envr v0.5.0 # https://www.github.com/JPHutchins/envr # https://www.crumpledpaper.tech @@ -6,23 +6,7 @@ # Copyright (c) 2022 J.P. Hutchins # License text at the bottom of this source file -# Use with "source" from *bash* or *Windows PowerShell* -# Usage: -# bash $> . envr.ps1 -# zsh $> . ./envr.ps1 -# WinPS $> . ./envr.ps1 -# You cannot use it directly; it will not set your environment variables. - -# Installation (optional) -# - Windows PowerShell -# - Add the installation location to your system PATH -# - Usage: -# WinPS $> . envr -# - BASH -# - Alias or link envr.ps1 as envr -# - Add it to your system PATH (or add a link to a folder that is in PATH) -# - Usage: -# bash $> . envr +# Usage: . ./envr.ps1 # The following line is for PowerShell/bash cross compatability. # - The bash section shall begin with the delimiter "<#'" @@ -402,17 +386,27 @@ _envr_main () { local key=$(echo ${path_dir/%=*/}) local value=$(echo ${path_dir#${key}=}) - # make sure that the directory exists - if [[ ! -d "$value" ]] ; then - echo -e "\033[0;31mERROR\033[0m - ${KEY}, line $config_file_line_number of ${config_file}: $value is not a directory." - unsource - return 1 + local dir="" + # expand the variables + if [[ -n "${BASH:-}" ]] ; then + if [[ $(printf %.1s $BASH_VERSION) -ge 5 ]] ; then + dir="${value@P}" + else # bash < 4.4 doesn't have @P + dir="$(eval echo "$value")" + fi + elif [[ -n "${ZSH_VERSION:-}" ]] ; then + dir="${(e)value}" + fi + + # warn if the directory does not exist + if [[ ! -d "$dir" ]] ; then + echo -e "\033[0;33mWARNING\033[0m - ${key}=$dir is not a directory." fi # don't add duplicate directories to PATH - if [[ ":${_ENVR_NEW_PATH}:" == *":${value}:"* ]]; then + if [[ ":${_ENVR_NEW_PATH}:" == *":${dir}:"* ]]; then continue fi - _ENVR_NEW_PATH="${value}:${_ENVR_NEW_PATH}" + _ENVR_NEW_PATH="${dir}:${_ENVR_NEW_PATH}" done @@ -694,12 +688,18 @@ $global:_ENVR_NEW_ALIASES.GetEnumerator().ForEach({ }) # Apply the additions to the system PATH -foreach ($val in $global:_ENVR_PATH_ADDITIONS.Values) { +$global:_ENVR_PATH_ADDITIONS.GetEnumerator().ForEach({ + $key = $($_.Key) + $val = $($_.Value) + + # expand the variables + if ($null -ne $val) { + $val = $ExecutionContext.InvokeCommand.ExpandString($val.Replace('$', '$env:')) + } + if (Test-Path -Path "$val") { } else { - Write-Host "$val is not a directory." -ForegroundColor Red - unsource - return + Write-Host "WARNING - $key=$val is not a directory." -ForegroundColor Yellow } foreach ($folder in $(Get-Item env:path).value.split($([System.IO.Path]::PathSeparator))) { if ($folder -eq $val) { @@ -711,7 +711,7 @@ foreach ($val in $global:_ENVR_PATH_ADDITIONS.Values) { } $Env:PATH = "$val$([System.IO.Path]::PathSeparator)$Env:PATH" -} +}) # Activate the python venv if specified if (-not $global:_ENVR_PYTHON_VENV -eq "") { diff --git a/tests/fixtures/expansion b/tests/fixtures/expansion index 38b8fe5..af7e63e 100644 --- a/tests/fixtures/expansion +++ b/tests/fixtures/expansion @@ -3,4 +3,8 @@ ANSWER=42 ABS_PATH_EXAMPLE=$ENVR_ROOT/path/to/resource COMBINED=$FOO$ANSWER - COMBINED_PATH=$ABS_PATH_EXAMPLE/$COMBINED \ No newline at end of file + COMBINED_PATH=$ABS_PATH_EXAMPLE/$COMBINED + TESTS=tests +[ADD_TO_PATH] + USE_VARS=$ENVR_ROOT/$TESTS/fixtures + COMBINED_PATH=$COMBINED_PATH diff --git a/tests/sh/test_expansion.sh b/tests/sh/test_expansion.sh index 9b75bc8..d24deaf 100644 --- a/tests/sh/test_expansion.sh +++ b/tests/sh/test_expansion.sh @@ -13,7 +13,6 @@ assertContains "$OLD_ENV" "USER_VAR=original user value" . ./envr.ps1 -assertEqual "$OLD_PATH" "$PATH" assertEqual "$OLD_ALS" "$(alias)" assertEqual "$(pwd)" "$ENVR_ROOT" @@ -21,6 +20,9 @@ assertEqual "$(pwd)/path/to/resource" $ABS_PATH_EXAMPLE assertEqual bar42 $COMBINED assertEqual "$(pwd)/path/to/resource/bar42" $COMBINED_PATH +assertContains "$PATH" "$(pwd)/tests/fixtures" +assertContains "$PATH" "$(pwd)/path/to/resource/bar42" + unsource assertEqual $OLD_PATH "$PATH" diff --git a/tests/windows/test_expansion.ps1 b/tests/windows/test_expansion.ps1 index d17c674..d976858 100644 --- a/tests/windows/test_expansion.ps1 +++ b/tests/windows/test_expansion.ps1 @@ -15,6 +15,9 @@ function global:expansion () { $TEST_RES += assertEqual bar42 $env:COMBINED $TEST_RES += assertEqual "$(Get-Location)/path/to/resource/bar42" $env:COMBINED_PATH + $TEST_RES += assertContains $env:path "$(Get-Location)/tests/fixtures$([System.IO.Path]::PathSeparator)" + $TEST_RES += assertContains $env:path "$(Get-Location)/path/to/resource/bar42$([System.IO.Path]::PathSeparator)" + unsource $TEST_RES += assertNotInEnv ENVR_ROOT