Skip to content

Commit

Permalink
feat: 0.5; expand variables for ADD_TO_PATH; test
Browse files Browse the repository at this point in the history
  • Loading branch information
JPHutchins committed Apr 9, 2023
1 parent 3468a3b commit 5f67ade
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
60 changes: 30 additions & 30 deletions envr.ps1
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
# envr v0.4.0
# envr v0.5.0
# https://www.github.com/JPHutchins/envr
# https://www.crumpledpaper.tech

# MIT License
# 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 "<#'"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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 "") {
Expand Down
6 changes: 5 additions & 1 deletion tests/fixtures/expansion
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
ANSWER=42
ABS_PATH_EXAMPLE=$ENVR_ROOT/path/to/resource
COMBINED=$FOO$ANSWER
COMBINED_PATH=$ABS_PATH_EXAMPLE/$COMBINED
COMBINED_PATH=$ABS_PATH_EXAMPLE/$COMBINED
TESTS=tests
[ADD_TO_PATH]
USE_VARS=$ENVR_ROOT/$TESTS/fixtures
COMBINED_PATH=$COMBINED_PATH
4 changes: 3 additions & 1 deletion tests/sh/test_expansion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ assertContains "$OLD_ENV" "USER_VAR=original user value"

. ./envr.ps1

assertEqual "$OLD_PATH" "$PATH"
assertEqual "$OLD_ALS" "$(alias)"

assertEqual "$(pwd)" "$ENVR_ROOT"
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"
Expand Down
3 changes: 3 additions & 0 deletions tests/windows/test_expansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5f67ade

Please sign in to comment.