diff --git a/ament_package/template/package_level/local_setup.bat.in b/ament_package/template/package_level/local_setup.bat.in index 9b83676..4bb2015 100644 --- a/ament_package/template/package_level/local_setup.bat.in +++ b/ament_package/template/package_level/local_setup.bat.in @@ -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 @@ -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 diff --git a/ament_package/template/package_level/local_setup.sh.in b/ament_package/template/package_level/local_setup.sh.in index 28e12cf..60a8b16 100644 --- a/ament_package/template/package_level/local_setup.sh.in +++ b/ament_package/template/package_level/local_setup.sh.in @@ -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() {