Skip to content

Instantly share code, notes, and snippets.

@rafaneri
Created September 23, 2016 17:26
Show Gist options
  • Save rafaneri/c7cc4536bd2e0d91b2ea82a578b95aa6 to your computer and use it in GitHub Desktop.
Save rafaneri/c7cc4536bd2e0d91b2ea82a578b95aa6 to your computer and use it in GitHub Desktop.
Shell Script to create ROS messages, services, actions and executables, including the paths in CMakeLists.txt
#!/bin/bash
_file="CMakeLists.txt"
_temp_file="CMakeLists.tmp"
_pattern=
_object=
_object_name=
cmakefile_executable()
{
if [ "${1#*$_pattern}" != "$1" ]; then
echo "add_executable($_object_name src/$_object_name.cpp)" >> $_temp_file
echo "add_dependencies($_object_name \${\${PROJECT_NAME}_EXPORTED_TARGETS} \${catkin_EXPORTED_TARGETS})" >> $_temp_file
echo "target_link_libraries($_object_name \${catkin_LIBRARIES})" >> $_temp_file
fi
echo "$1" >> $_temp_file #copy the line to the temp cmakelist file
}
cmakefile_itens()
{
start=$1
line=$2
if [ "${line#*$_pattern}" != "$line" ]; then
start=1
result="${line/\#/}"
echo "$result" >> $_temp_file #copy the line to the temp cmakelist file
else
if [ "$start" == 1 ]; then
if [ "${line#*FILES}" != "$line" ]; then
result="${line/\#/}"
echo "$result" >> $_temp_file #write the new line to the temp cmakelist file
else
if [ "${line#*)}" != "$line" ]; then
start=0
result="${line/\#/}"
echo "$_object_name.$_object" >> $_temp_file #write the filename to the temp cmakelist file
echo "$result" >> $_temp_file #write the new line to the temp cmakelist file
else
echo "$line" >> $_temp_file #copy the line to the temp cmakelist file
fi
fi
else
echo "$line" >> $_temp_file #copy the line to the temp cmakelist file
fi
fi
}
generate_cmakefile()
{
if [[ -f "$_file" ]]
then
rm -f $_temp_file
echo -n "" > $_temp_file # create a new cmakelist file
start=0
while IFS= read line
do
if [ "$_pattern" != "## Declare a C++ executable" ]; then
cmakefile_itens $start "$line"
else
cmakefile_executable "$line"
fi
done <"$_file"
fi
mv $_temp_file $_file
rm -f $_temp_file
}
create_directory()
{
if [ ! -d "$_object" ]; then
mkdir "$_object" # create the _object directory
fi
echo -n "" > "$_object/$_object_name.$1" # create an object empty file
if [ "$1" == "cpp" ]; then
echo -n "" > "$_object/$_object_name.h" # create an object empty file
#write .h template
echo "#ifndef SRC_${_object_name}_H_" >> "$_object/$_object_name.h"
echo "#define SRC_${_object_name}_H_" >> "$_object/$_object_name.h"
echo "#include \"std_msgs/String.h\"" >> "$_object/$_object_name.h"
echo "class $_object_name {" >> "$_object/$_object_name.h"
echo "public:" >> "$_object/$_object_name.h"
echo " $_object_name();" >> "$_object/$_object_name.h"
echo " virtual ~$_object_name();" >> "$_object/$_object_name.h"
echo "private:" >> "$_object/$_object_name.h"
echo " void init();" >> "$_object/$_object_name.h"
echo "};" >> "$_object/$_object_name.h"
echo "int main(int argc, char **argv) {" >> "$_object/$_object_name.h"
echo " ros::init(argc, argv, \"$_object_name\");" >> "$_object/$_object_name.h"
echo " ros::spin();" >> "$_object/$_object_name.h"
echo " return 0;" >> "$_object/$_object_name.h"
echo "}" >> "$_object/$_object_name.h"
echo "#endif /* SRC_${_object_name}_H_ */" >> "$_object/$_object_name.h"
#write .cpp template
echo "#include \"ros/ros.h\"" >> "$_object/$_object_name.$1"
echo "#include \"$_object_name.h\"" >> "$_object/$_object_name.$1"
echo "$_object_name::$_object_name()" >> "$_object/$_object_name.$1"
echo "{" >> "$_object/$_object_name.$1"
echo " this->init();" >> "$_object/$_object_name.$1"
echo "}" >> "$_object/$_object_name.$1"
echo "$_object_name::~$_object_name() {}" >> "$_object/$_object_name.$1"
echo "void $_object_name::init() {" >> "$_object/$_object_name.$1"
echo "}" >> "$_object/$_object_name.$1"
fi
}
read_object_name()
{
echo "Ok, $_object selected."
echo "Enter the $_object name:"
read _object_name
}
echo_finish_task()
{
echo "The $1 $_object_name.$1 was created!"
echo "Task completed."
}
template_file()
{
read_object_name
generate_cmakefile
create_directory $1
echo_finish_task $1
}
create_msg()
{
# setup default variables
_pattern="add_message_files"
_object="msg"
template_file $_object
}
create_srv()
{
# setup default variables
_pattern="add_service_files"
_object="srv"
template_file $_object
}
create_action()
{
# setup default variables
_pattern="add_action_files"
_object="action"
template_file $_object
}
create_executable()
{
# setup default variables
_pattern="## Declare a C++ executable"
_object="src"
_extension="cpp"
template_file $_extension
}
SOURCE_DIR=$(cd "`dirname "$0"`" > /dev/null && pwd)
cd $SOURCE_DIR
cd ../..
SOURCE_DIR=$(cd "`dirname "$1"`" > /dev/null && pwd)
echo "********************************"
echo " CREATE A NEW OBJECT "
echo "********************************"
echo "Enter the package name:"
read package_name
cd $package_name
echo "****************************************************"
echo " 1:msg 2:srv 3:action 4:C++ executable "
echo "****************************************************"
echo "Enter the object type [1, 2, 3, 4]:"
read object_type
case $object_type in
1) create_msg # call the create_msg() function
;;
2) create_srv # call the create_srv() function
;;
3) create_action # call the create_action() function
;;
4) create_executable # call the create_executable() function
;;
*) exit
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment