From 674fae186363dd3c3fb9573aae4b986372352db2 Mon Sep 17 00:00:00 2001 From: Sriharsha Date: Sun, 9 Jun 2024 14:13:50 +0800 Subject: [PATCH 1/2] update: add zsh support --- setup.sh | 68 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/setup.sh b/setup.sh index 929d09f..80af1e5 100644 --- a/setup.sh +++ b/setup.sh @@ -6,25 +6,53 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Adds the following function to the bashrc file: -echo "Adding ros_dev function to bashrc file" - -echo 'ros_dev() { - # Check if the correct number of arguments were provided - if (( $# % 2 != 0 )); then - echo "Usage: ros_dev [ ...]" - return 1 +if [[ $SHELL == "/bin/bash" ]]; then + # Check if the function already exists in the bashrc file + if ! grep -q "ros_dev()" "$HOME/.bashrc"; then + echo 'ros_dev() { + # Check if the correct number of arguments were provided + if (( $# % 2 != 0 )); then + echo "Usage: ros_dev [ ...]" + return 1 + fi + while (( $# >= 2 )); do + # Set environment variables + export ROS_DEV_CONTAINER_NAME=$1 + export ROS_PROJECT_PATH=$2 + shift 2 + # Run docker-compose from the correct directory + cd "$SCRIPT_DIR" && docker-compose up -d --build + done + }' >> "$HOME/.bashrc" + echo "ros_dev function added to bashrc file" + else + echo "ros_dev function already exists in bashrc file" + fi +else if [[ $SHELL == "/bin/zsh" ]]; then + # Check if the function already exists in the zshrc file + if ! grep -q "ros_dev()" "$HOME/.zshrc"; then + echo 'ros_dev() { + # Check if the correct number of arguments were provided + if (( $# % 2 != 0 )); then + echo "Usage: ros_dev [ ...]" + return 1 + fi + while (( $# >= 2 )); do + # Set environment variables + export ROS_DEV_CONTAINER_NAME=$1 + export ROS_PROJECT_PATH=$2 + shift 2 + # Run docker-compose from the correct directory + cd "$SCRIPT_DIR" && docker-compose up -d --build + done + }' >> "$HOME/.zshrc" + echo "ros_dev function added to zshrc file" + else + echo "ros_dev function already exists in zshrc file" + fi + else + echo "Unsupported shell" + exit 1 fi - - while (( $# >= 2 )); do - # Set environment variables - export ROS_DEV_CONTAINER_NAME=$1 - export ROS_PROJECT_PATH=$2 - shift 2 - - # Run docker-compose from the correct directory - cd "$SCRIPT_DIR" && docker-compose up -d --build - done -} -' >> "$HOME/.bashrc" - +fi echo "Done" From 59bbd911ad0ef65cb19ddb04263c8dbe82f938ce Mon Sep 17 00:00:00 2001 From: Sriharsha Ghanta Date: Mon, 10 Jun 2024 22:53:27 +0800 Subject: [PATCH 2/2] SImplify the function --- setup.sh | 82 +++++++++++++++++++++++--------------------------------- 1 file changed, 34 insertions(+), 48 deletions(-) diff --git a/setup.sh b/setup.sh index 80af1e5..0ae4cd8 100644 --- a/setup.sh +++ b/setup.sh @@ -1,58 +1,44 @@ +#!/bin/bash + ## Author: Adeeb Abbas # This script sets up the ros_dev function in the bashrc file. # Get the absolute path to the script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# Adds the following function to the bashrc file: +# Define the function +ros_dev() { + # Check if the correct number of arguments were provided + if (( $# % 2 != 0 )); then + echo "Usage: ros_dev [ ...]" + return 1 + fi + + while (( $# >= 2 )); do + # Set environment variables + export ROS_DEV_CONTAINER_NAME=$1 + export ROS_PROJECT_PATH=$2 + shift 2 + + # Run docker-compose from the correct directory + cd "$SCRIPT_DIR" && docker-compose up -d --build + done +} if [[ $SHELL == "/bin/bash" ]]; then - # Check if the function already exists in the bashrc file - if ! grep -q "ros_dev()" "$HOME/.bashrc"; then - echo 'ros_dev() { - # Check if the correct number of arguments were provided - if (( $# % 2 != 0 )); then - echo "Usage: ros_dev [ ...]" - return 1 - fi - while (( $# >= 2 )); do - # Set environment variables - export ROS_DEV_CONTAINER_NAME=$1 - export ROS_PROJECT_PATH=$2 - shift 2 - # Run docker-compose from the correct directory - cd "$SCRIPT_DIR" && docker-compose up -d --build - done - }' >> "$HOME/.bashrc" - echo "ros_dev function added to bashrc file" - else - echo "ros_dev function already exists in bashrc file" - fi -else if [[ $SHELL == "/bin/zsh" ]]; then - # Check if the function already exists in the zshrc file - if ! grep -q "ros_dev()" "$HOME/.zshrc"; then - echo 'ros_dev() { - # Check if the correct number of arguments were provided - if (( $# % 2 != 0 )); then - echo "Usage: ros_dev [ ...]" - return 1 - fi - while (( $# >= 2 )); do - # Set environment variables - export ROS_DEV_CONTAINER_NAME=$1 - export ROS_PROJECT_PATH=$2 - shift 2 - # Run docker-compose from the correct directory - cd "$SCRIPT_DIR" && docker-compose up -d --build - done - }' >> "$HOME/.zshrc" - echo "ros_dev function added to zshrc file" - else - echo "ros_dev function already exists in zshrc file" - fi - else - echo "Unsupported shell" - exit 1 - fi + shell_config_file="$HOME/.bashrc" +elif [[ $SHELL == "/bin/zsh" ]]; then + shell_config_file="$HOME/.zshrc" +else + echo "Unsupported shell" + exit 1 +fi + +if ! grep -q "ros_dev ()" "$shell_config_file"; then + declare -f ros_dev >> "$shell_config_file" + echo "ros_dev function added to $shell_config_file" +else + echo "ros_dev function already exists in $shell_config_file" fi + echo "Done"