From 52b0a5d031c0b45f2248d0a319d66b07281a515d Mon Sep 17 00:00:00 2001
From: fherrero <fherrero@iri.upc.edu>
Date: Wed, 13 May 2020 17:38:41 +0200
Subject: [PATCH] Add README filling with dependencies and ROS interfaces when
 adding topics/serviers/actions

---
 add_action_server_client.sh       |   2 +
 add_service_server_client.sh      |   3 +
 add_topic_publisher_subscriber.sh |   2 +
 common_templates/README.md        |  19 ++++++
 libraries/scripts_library.sh      | 108 +++++++++++++++++++++++++++++-
 5 files changed, 133 insertions(+), 1 deletion(-)

diff --git a/add_action_server_client.sh b/add_action_server_client.sh
index ed0cf7b..2cc0a3c 100755
--- a/add_action_server_client.sh
+++ b/add_action_server_client.sh
@@ -126,3 +126,5 @@ else
   create_action_client ${ros_pkg} ${action_name} ${action_file%.action} ${file_pkg} ${node_h} ${node_c} ${driver_alg}
 fi
 
+type="action"
+fill_readme_ros_interface ${type} ${server_client} ${ros_pkg} ${action_name} ${file_pkg} ${action_file}.action
\ No newline at end of file
diff --git a/add_service_server_client.sh b/add_service_server_client.sh
index ee94f42..bae8fc5 100755
--- a/add_service_server_client.sh
+++ b/add_service_server_client.sh
@@ -124,3 +124,6 @@ then
 else
   create_client ${ros_pkg} ${service_name} ${srv_file%.srv} ${file_pkg} ${node_h} ${node_c} ${driver_alg}
 fi
+
+type="service"
+fill_readme_ros_interface ${type} ${server_client} ${ros_pkg} ${service_name} ${file_pkg} ${srv_file}
\ No newline at end of file
diff --git a/add_topic_publisher_subscriber.sh b/add_topic_publisher_subscriber.sh
index 5a06140..2a27b5a 100755
--- a/add_topic_publisher_subscriber.sh
+++ b/add_topic_publisher_subscriber.sh
@@ -148,3 +148,5 @@ else
   create_subscriber ${ros_pkg} ${topic_name} ${msg_file%.msg} ${file_pkg} ${buffer} ${node_h} ${node_c} ${driver_alg}
 fi
 
+type="topic"
+fill_readme_ros_interface ${type} ${pub_subs} ${ros_pkg} ${topic_name} ${file_pkg} ${msg_file}
\ No newline at end of file
diff --git a/common_templates/README.md b/common_templates/README.md
index 61d155e..c769329 100644
--- a/common_templates/README.md
+++ b/common_templates/README.md
@@ -7,6 +7,25 @@ The template project description
 Developed at Institut de Robòtica i Informàtica Industrial (IRI, CSIC-UPC) (www.iri.upc.edu)
 Contact: labrobotica@iri.upc.edu
 
+# ROS Interface
+    
+### Topic subscribers
+
+### Topic publishers
+
+### Service servers
+
+### Service clients
+
+### Action servers
+
+### Action clients
+
+### Parameters
+- ~rate (Double; default: 10.0; min: 0.1; max: 1000) The main node thread loop rate in Hz. 
+
+## Dependencies
+
 ## Installation
 
 Clone the package inside a ROS workspace, although it will normally come within a rosinstall file.
diff --git a/libraries/scripts_library.sh b/libraries/scripts_library.sh
index 6ca85e7..17dba63 100755
--- a/libraries/scripts_library.sh
+++ b/libraries/scripts_library.sh
@@ -282,8 +282,113 @@ function add_pkg_to_packagexml
     pub="<build_depend>${file_pkg}<build_depend/>"
     sed -i -e "/${comment}/a\\  ${pub}" "package.xml"
   else
-    echo "Package \"${file_pkg}\" already added as dependency, skipping"
+    echo "Package \"${file_pkg}\" already added as dependency in package.xml, skipping"
   fi
+  
+  add_pkg_dependency_to_readme $ros_pkg $file_pkg
+}
+
+# add_pkg_to_readme
+# Adds $2 as dependency to README.md file.
+# - $1: current ros package
+# - $2: message ros package
+function add_pkg_dependency_to_readme
+{
+  if [[ -z $1 ]] || [[ -z $2 ]]
+  then
+    kill_exit "add_pkg_to_readme: missing input parameters"
+  fi
+
+  local ros_pkg=$1
+  local file_pkg=$2
+
+  #go to package folder
+  roscd "${ros_pkg}"
+  
+  file=README.md
+
+#   #look for package in package.xml and add dependency if necessary
+#   local pub="- ${file_pkg}"
+#   local pub1=$(grep "${pub}" ${file})
+#   if [[ -z "${pub1}" ]]
+#   then
+#     echo "Adding package \"${file_pkg}\" as dependency..."
+#     local comment="##Dependencies"
+#     pub="- ${file_pkg}"
+#     sed -i -e "/${comment}/a\\  ${pub}" ${file}
+#   else
+#     echo "Package \"${file_pkg}\" already added as dependency in README, skipping"
+#   fi
+  
+  line="\- ${file_pkg}"
+  comment="## Dependencies"
+  add_line_to_file "${line}" "${comment}" "${file}"
+}
+
+# fill_readme_ros_interface
+# Adds ROS interface type $1/$2 to README.md from $2 package, as a $3 name and $4/$5 type
+# - $1: interface type: topic, service, action
+# - $2: interface subtype: publisher/subscriber, client/server, client/server
+# - $3: current ros package
+# - $4: interface name
+# - $5: interface package
+# - $6: interface message
+function fill_readme_ros_interface # ${type} ${pub_subs} ${ros_pkg} ${topic_name} ${file_pkg} ${msg_file}
+{
+  if [[ -z $1 ]] || [[ -z $2 ]] || [[ -z $3 ]] || [[ -z $4 ]] || [[ -z $5 ]] || [[ -z $6 ]]
+  then
+    kill_exit "fill_readme_ros_interface missing input parameters"
+  fi
+  
+  #echo "fill_readme_ros_interface $1 $2 $3 $4 $5 $6"
+  local if_type=$1
+  local if_subtype=$2
+  local ros_pkg=$3
+  local if_name=$4
+  local if_pkg=$5
+  local if_msg=$6
+  
+  roscd "${ros_pkg}"
+  
+  file=README.md
+  line="  - ~$if_name ($if_pkg/$if_msg)"
+  comment=""
+  
+  case "$if_type" in
+  "topic")
+
+      case "$if_subtype" in
+      "publisher")
+        comment="### Topic publishers"
+        ;;
+      "subscriber")
+        comment="### Topic subscribers"
+        ;;
+      esac
+      ;;
+  "service")
+      case "$if_subtype" in
+      "client")
+        comment="### Service clients"
+        ;;
+      "server")
+        comment="### Service servers"
+        ;;
+      esac
+      ;;
+  "action")
+      case "$if_subtype" in
+      "client")
+        comment="### Action clients"
+        ;;
+      "server")
+        comment="### Action servers"
+        ;;
+      esac
+      ;;
+  esac
+
+  add_line_to_file "${line}" "${comment}" "${file}"
 }
 
 # find_comment_in_file
@@ -344,6 +449,7 @@ function text_found_in_file
 # - $3: text file name
 function add_line_to_file
 {
+  #echo "add_line_to_file $1 $2 $3"
   if [[ -z $1 ]] || [[ -z $2 ]] || [[ -z $3 ]]
   then
     kill_exit "add_line_to_file: missing input parameters"
-- 
GitLab