Skip to content

Commit

Permalink
Cleanup environment before sourcing (#24)
Browse files Browse the repository at this point in the history
When sourcing an environment, the shell env now gets cleaned of any modifications done to the variables
that robot_folders manipulates. This way, users can source an environment in any shell.

If users want to explicitly source multiple environments, they should do that through underlays.

Co-authored-by: David Oberacker <[email protected]>
  • Loading branch information
fmauch and Oberacda authored Jun 27, 2024
1 parent dc6208d commit fa741e5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
12 changes: 10 additions & 2 deletions bin/rob_folders_source.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ if [ -z "$ROB_FOLDERS_ACTIVE_ENV" ]; then
export ROB_FOLDERS_EMPTY_LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
export ROB_FOLDERS_EMPTY_QML_IMPORT_PATH=${QML_IMPORT_PATH}
export ROB_FOLDERS_EMPTY_PYTHONPATH=${PYTHONPATH}
export ROB_FOLDERS_EMPTY_AMENT_PREFIX_PATH=${AMENT_PREFIX_PATH}
export ROB_FOLDERS_EMPTY_COLCON_PREFIX_PATH=${COLCON_PREFIX_PATH}
export ROB_FOLDERS_EMPTY_PS1=${PS1}

if [ ! -z "${ROB_FOLDERS_EMPTY_CMAKE_PATH}" ] && [ -z $ROB_FOLDERS_IGNORE_CMAKE_PREFIX_PATH ]
then
Expand Down Expand Up @@ -127,6 +130,8 @@ reset_environment()
export LD_LIBRARY_PATH=${ROB_FOLDERS_EMPTY_LD_LIBRARY_PATH}
export QML_IMPORT_PATH=${ROB_FOLDERS_EMPTY_QML_IMPORT_PATH}
export PYTHONPATH=${ROB_FOLDERS_EMPTY_PYTHONPATH}
export AMENT_PREFIX_PATH=${ROB_FOLDERS_EMPTY_AMENT_PREFIX_PATH}
export COLCON_PREFIX_PATH=${ROB_FOLDERS_EMPTY_COLCON_PREFIX_PATH}
}


Expand Down Expand Up @@ -159,16 +164,19 @@ fzirob()

if [ $? -eq 0 ]; then
if [ $1 = "change_environment" ] && [ "$2" != "--help" ]; then
reset_environment
checkout_dir=$(rob_folders get_checkout_base_dir)

if [ -f ${checkout_dir}/.cur_env ]; then
# Since the python command writes the .cur_env file there is a race condition when
# running change_environment commands in parallel. Thus it can happen that reading the
# file returns an empty value. This race condition only occurs on a very high io load
# and usually this while look should only be entered once.
ROB_FOLDERS_ACTIVE_ENV=$(cat "${checkout_dir}"/.cur_env) || true
while [ -z "$ROB_FOLDERS_ACTIVE_ENV" ]; do
export ROB_FOLDERS_ACTIVE_ENV=$(cat ${checkout_dir}/.cur_env)
ROB_FOLDERS_ACTIVE_ENV=$(cat "${checkout_dir}"/.cur_env) || true
done
export ROB_FOLDERS_ACTIVE_ENV
environment_dir="${checkout_dir}/${ROB_FOLDERS_ACTIVE_ENV}"
if [ -f ${environment_dir}/setup.sh ]; then
source ${environment_dir}/setup.sh
Expand All @@ -186,7 +194,7 @@ fzirob()
if [ -z "${ROB_FOLDERS_DISABLE_PROMPT_MODIFICATION:-}" ] ; then
env_prompt="[${ROB_FOLDERS_ACTIVE_ENV}]"
if [ -n "${PS1##*"$env_prompt"*}" ]; then
PS1="${env_prompt} ${PS1:-}"
PS1="${env_prompt} ${ROB_FOLDERS_EMPTY_PS1:-}"
export PS1
fi
fi
Expand Down
4 changes: 3 additions & 1 deletion bin/source_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fi

if [ -z ${rob_folders_overlay+x} ]; then
echo "Sourcing environment '$environment_dir'"
ROB_FOLDERS_ROOT_ENV=$environment_dir
export ROB_FOLDERS_ROOT_ENV=$environment_dir
fi

# This is the environment's name, which we will print out later
Expand Down Expand Up @@ -130,6 +130,8 @@ if [ -d $environment_dir ]; then
export LD_LIBRARY_PATH=${ROB_FOLDERS_EMPTY_LD_LIBRARY_PATH}
export QML_IMPORT_PATH=${ROB_FOLDERS_EMPTY_QML_IMPORT_PATH}
export PYTHONPATH=${ROB_FOLDERS_EMPTY_PYTHONPATH}
export AMENT_PREFIX_PATH=${ROB_FOLDERS_EMPTY_AMENT_PREFIX_PATH}
export COLCON_PREFIX_PATH=${ROB_FOLDERS_EMPTY_COLCON_PREFIX_PATH}
fi
fi

Expand Down
26 changes: 26 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,39 @@ To activate or source an environment, use the ``fzirob change_environment
ENV_NAME`` command. This command sources the appropriate setup scripts of the
environment depending on its contents.


You can use tab completion on the environments so typing ``fzirob
change_environment`` and then pressing :kbd:`Tab` should list all environments
present.

Executing ``fzirob change_environment`` without any environment specified will
source the most recently sourced environment.

.. note::

Using ``fzirob change_environment`` will reset your shell environment before sourcing a
``robot_folders`` environment. Specifically, the following variables will be reset to the state
they have been at the point when robot_folders was sourced (the ``rob_folders_source.sh`` file,
usually sourced in the ``~/.bashrc`` file).

* ``CMAKE_PATH``
* ``PATH``
* ``LD_LIBRARY_PATH``
* ``QML_IMPORT_PATH``
* ``PYTHONPATH``
* ``AMENT_PREFIX_PATH``
* ``COLCON_PREFIX_PATH``
* ``PS1``


Adding custom source commands
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In addition to the environment's workspace setup files, each environment contains a
``setup_local.sh`` file that will get sourced during the ``change_environment`` call after the
workspaces in an environment have been sourced.


Using underlay environments
---------------------------

Expand Down

0 comments on commit fa741e5

Please sign in to comment.