Skip to content

Commit

Permalink
dedup *FLAGS env vars, bash unset empty env vars
Browse files Browse the repository at this point in the history
/close #224

To prevent C from being empty, `unset -v` is now used.

To prevent FLAGS from being misconfigured, deduplication is now done.

Both are still premptively exported to ensure that they are exported if their value is kept.
  • Loading branch information
balupton committed Apr 25, 2024
1 parent 2da8d15 commit 8cc572e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
11 changes: 6 additions & 5 deletions commands/setup-environment-commands
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ source "$DOROTHY/sources/env.bash"
# =====================================
# Prepare

# reset editor
export LANG LC_CTYPE EDITOR
# ensure editor vars are exported
export LANG LC_ALL EDITOR

# Essentials, required for these environments
# crontab
Expand Down Expand Up @@ -103,9 +103,10 @@ mkdir -p "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" "$XDG_BIN_HOME" "$XDG_DATA_HOME" "
export APPIMAGE_HOME="${APPIMAGE_HOME:-"$HOME/Applications"}"

# reset paths and flags
export PATH="$DOROTHY/commands:$XDG_BIN_HOME:/usr/sbin:/usr/bin:/sbin:/bin:$PATH" # $PATH last to make sure we inherit operating-system specific paths, such as WSL paths, and let the deduplicator later trim out duplicates in preferred order
export MANPATH="${MANPATH-}" INFOPATH="${INFOPATH-}" CLASSPATH="${CLASSPATH-}" PKG_CONFIG_PATH="${PKG_CONFIG_PATH-}" # don't wipe, as we want to inherit
export LDFLAGS='' CPPFLAGS='' CC='' CXX='' # do wipe as don't have duplicate prevention
# $PATH last to make sure we inherit operating-system specific paths, such as WSL paths, and let the deduplicator later trim out duplicates in preferred order
export PATH="$DOROTHY/commands:$XDG_BIN_HOME:/usr/sbin:/usr/bin:/sbin:/bin:$PATH"

export MANPATH="${MANPATH-}" INFOPATH="${INFOPATH-}" CLASSPATH="${CLASSPATH-}" PKG_CONFIG_PATH="${PKG_CONFIG_PATH-}" LDFLAGS="${LDFLAGS-}" CPPFLAGS="${CPPFLAGS-}" CC CXX

# local vars
p=''
Expand Down
18 changes: 13 additions & 5 deletions sources/env.bash
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function on_env_finish {
fi

# success condition, echo var actions
local name value i items_array items_string item item_last_index item_index item_existing
local name value i items_array items_string item item_last_index item_index item_existing split_char is_path
while read -r line; do
# parse line
name='' value=''
Expand All @@ -51,15 +51,23 @@ function on_env_finish {
fi

# adjust
split_char=''
is_path='no'
if [[ $name == *'PATH' ]] || [[ $name == *'DIRS' ]]; then
split_char=':'
is_path='yes'
elif [[ $name == *'FLAGS' ]]; then
split_char=' '
fi
if test -n "$split_char"; then
# cycle through each item in the path, removing duplicates and empties
items_array=()
items_string=''
item_last_index=0
# the <= and -o, is to ensure that the last item is processed, as it does not have a trailing :
for ((item_index = 0; item_index <= ${#value}; item_index++)); do
# || is used instead of -o, because of [test '(' = ':' -o 375 -eq 7258] producing [test: `)' expected, found :]
if test "${value:item_index:1}" = ':' || test "$item_index" -eq "${#value}"; then
if test "${value:item_index:1}" = "$split_char" || test "$item_index" -eq "${#value}"; then
item="${value:item_last_index:item_index-item_last_index}"
item_last_index="$((item_index + 1))"
# check if empty
Expand All @@ -80,7 +88,7 @@ function on_env_finish {
if test -z "$items_string"; then
items_string="$item"
else
items_string="$items_string:$item"
items_string="$items_string$split_char$item"
fi
fi
done
Expand Down Expand Up @@ -113,9 +121,9 @@ function on_env_finish {
# https://elv.sh/ref/builtin.html#unset-env
echo "unset-env $name"
else
echo "export $name='';"
echo "unset -v $name;"
fi
elif [[ $name == *'PATH' ]] || [[ $name == *'DIRS' ]]; then
elif test "$is_path" = 'yes'; then
# echo var action: set path
if test "$shell" = 'fish'; then
echo "set --export --path $name '$value';"
Expand Down

0 comments on commit 8cc572e

Please sign in to comment.