Skip to content

Commit

Permalink
Add ament_prepend_unique_value to package setups
Browse files Browse the repository at this point in the history
This function can then be called in packages' environment hooks.

Signed-off-by: Scott K Logan <logans@cottsay.net>
  • Loading branch information
cottsay committed Mar 26, 2021
1 parent fa007c0 commit 6c87d40
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
33 changes: 32 additions & 1 deletion ament_package/template/package_level/local_setup.bat.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ set "AMENT_CURRENT_PREFIX="
goto:eof


:: Append non-duplicate values to environment variables
:: Append values to environment variables
:: using semicolons as separators and avoiding leading separators.
:: first argument: the name of the result variable
:: second argument: the value
Expand All @@ -45,6 +45,37 @@ goto:eof
)
goto:eof

:: Append non-duplicate values to environment variables
:: using semicolons as separators and avoiding trailing separators.
:: first argument: the name of the result variable
:: second argument: the value
:ament_append_unique_value
setlocal enabledelayedexpansion
:: arguments
set "listname=%~1"
set "value=%~2"
:: expand the list variable
set "list=!%listname%!"
:: check if the list contains the value
set "is_duplicate="
if "%list%" NEQ "" (
for %%v in ("%list:;=";"%") do (
if "%%~v" == "%value%" set "is_duplicate=1"
)
)
:: if it is not a duplicate append it
if "%is_duplicate%" == "" (
:: if not empty, append a semi-colon
if "!list!" NEQ "" set "list=!list!;"
:: append the value
set "list=!list!%value%"
)
endlocal & (
:: set result variable in parent scope
set "%~1=%list%"
)
goto:eof

:: Call the specified batch file and output the name when tracing is requested.
:: first argument: the batch file
:call_file
Expand Down
51 changes: 51 additions & 0 deletions ament_package/template/package_level/local_setup.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,57 @@ ament_append_value() {
unset _listname
}

# function to append non-duplicate values to environment variables
# using colons as separators and avoiding leading separators
ament_append_unique_value() {
# arguments
_listname=$1
_value=$2
#echo "listname $_listname"
#eval echo "list value \$$_listname"
#echo "value $_value"

# check if the list contains the value
eval _values=\$$_listname
_duplicate=
_ament_append_unique_value_IFS=$IFS
IFS=":"
if [ "$AMENT_SHELL" = "zsh" ]; then
ament_zsh_to_array _values
fi
for _item in $_values; do
# ignore empty strings
if [ -z "$_item" ]; then
continue
fi
if [ $_item = $_value ]; then
_duplicate=1
fi
done
unset _item

# append only non-duplicates
if [ -z "$_duplicate" ]; then
# avoid leading separator
if [ -z "$_values" ]; then
eval $_listname=\"$_value\"
#eval echo "set list \$$_listname"
else
# field separator must not be a colon
unset IFS
eval $_listname=\"\$$_listname:$_value\"
#eval echo "append list \$$_listname"
fi
fi
IFS=$_ament_append_unique_value_IFS
unset _ament_append_unique_value_IFS
unset _duplicate
unset _values

unset _value
unset _listname
}

# function to prepend non-duplicate values to environment variables
# using colons as separators and avoiding trailing separators
ament_prepend_unique_value() {
Expand Down

0 comments on commit 6c87d40

Please sign in to comment.