Last active
May 14, 2018 15:53
-
-
Save naoki-mizuno/8b5e44a2b2d61537ee89a08bf3defad8 to your computer and use it in GitHub Desktop.
Utility for sourcing ROS workspaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export BASH_PRE_CD_HOOK=( | |
) | |
export BASH_POST_CD_HOOK=( | |
'__auto_source_ros_ws' | |
'__update_last_ros_ws' | |
) | |
# Automatically source ROS workspace you were last in | |
# Empty to disable | |
export ROS_UTIL_LAST_WS_FILE="$HOME/.cache/ros/last_ws.txt" | |
export ROS_UTIL_AUTO_ROS_WS="true" | |
export ROS_UTIL_VERBOSE="true" | |
export ROS_UTIL_CLEAR_CMAKE_PREFIX="true" | |
export CATKIN_SETUP_UTIL_ARGS="--extend" | |
__is_ros_ws() { | |
local cwd="${1:-$PWD}" | |
[[ -d $cwd/src ]] \ | |
&& [[ -f $cwd/devel/.catkin ]] \ | |
&& [[ -f $cwd/devel/setup.bash ]] | |
} | |
__auto_source_ros_ws() { | |
if [[ -n $ROS_UTIL_AUTO_ROS_WS ]] && $ROS_UTIL_AUTO_ROS_WS && __is_ros_ws; then | |
if [[ -n $ROS_UTIL_VERBOSE ]] && $ROS_UTIL_VERBOSE; then | |
echo "Auto-sourcing workspace $( basename $PWD )" | |
fi | |
if [[ -n $ROS_UTIL_CLEAR_CMAKE_PREFIX ]] && | |
$ROS_UTIL_CLEAR_CMAKE_PREFIX; then | |
sed -i -e "s!CMAKE_PREFIX_PATH = '[^']*'!CMAKE_PREFIX_PATH = '/opt/ros/$ROS_DISTRO'!" \ | |
devel/_setup_util.py | |
fi | |
unset CMAKE_PREFIX_PATH | |
source devel/setup.bash | |
source /opt/ros/$ROS_DISTRO/setup.bash | |
fi | |
} | |
__update_last_ros_ws() { | |
if [[ -z $ROS_UTIL_LAST_WS_FILE ]]; then | |
return | |
fi | |
if ! [[ -d $( dirname $ROS_UTIL_LAST_WS_FILE ) ]]; then | |
mkdir -p "$( dirname $ROS_UTIL_LAST_WS_FILE )" | |
fi | |
if __is_ros_ws; then | |
echo $PWD > $ROS_UTIL_LAST_WS_FILE | |
fi | |
} | |
source_last_ros_ws() { | |
if [[ -f $ROS_UTIL_LAST_WS_FILE ]]; then | |
if [[ -n $ROS_UTIL_VERBOSE ]] && $ROS_UTIL_VERBOSE; then | |
echo "Sourcing last workspace: $( basename $( <$ROS_UTIL_LAST_WS_FILE ) )" | |
fi | |
builtin cd $( <$ROS_UTIL_LAST_WS_FILE ) && \ | |
unset CMAKE_PREFIX_PATH && \ | |
source devel/setup.bash && \ | |
source /opt/ros/$ROS_DISTRO/setup.bash && \ | |
builtin cd $OLDPWD | |
fi | |
} | |
cd() { | |
for cmd in "${BASH_PRE_CD_HOOK[@]}"; do | |
eval "$cmd" | |
done | |
builtin cd $@ | |
for cmd in "${BASH_POST_CD_HOOK[@]}"; do | |
eval "$cmd" | |
done | |
} | |
source_last_ros_ws |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment