Skip to content

Commit

Permalink
Finished script
Browse files Browse the repository at this point in the history
  • Loading branch information
PilnyTomas authored and me-no-dev committed Oct 5, 2023
1 parent b547648 commit e6234ed
Showing 1 changed file with 34 additions and 38 deletions.
72 changes: 34 additions & 38 deletions tools/add_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
HELP="This script help to add library when using arduino-esp32 as an ESP-IDF component
The script accepts up to three arguments:
-n NEW: URL address to new library on GIThub (cannot be combined with -e)
-l LOCAL: Path to the project where the library should be placed locally
-l LOCAL: Path to the project where the library should be placed locally (must be paired with -e or -n)
-e EXISTING: path to existing libary- this will simply skip the download (cannot be combined with -n)
Examples:
Expand Down Expand Up @@ -55,48 +55,55 @@ while getopts "he:l:n:" opt; do
esac
done

# Debug Print the parameters
echo "e_param: $e_param"
echo "l_param: $l_param"
echo "n_param: $n_param"

# No parameter check
if [[ -z "$e_param" ]] && [[ -z "$e_param" ]] && [[ -z "$e_param" ]]; then
if [[ -z "$e_param" ]] && [[ -z "$l_param" ]] && [[ -z "$n_param" ]]; then
echo "Error: No parameters" >&2
echo "$HELP"
exit 1
fi

# Only local path check (not permitted)
if [[ -z "$e_param" ]] && [[ ! -z "$l_param" ]] && [[ -z "$n_param" ]]; then
echo "Error: -l parameter must be paired with -e or -n" >&2
echo "$HELP"
exit 1
fi

# Invalid combination check
if [[ ! -z $e_param ]] && [[ ! -z $n_param ]]; then
echo "ERROR: Cannot combine -n with -e" >&2
echo "$HELP"
exit 1
fi
fi

# Check existing lib
if [[ ! -z "$e_param" ]]; then
echo "DEBUG: existing lib parameter supplied"
if [[ ! -d "${e_param/#~/$HOME}" ]]; then # this works!
echo "Error: existing library parameter - path does not exist" >&2
exit 1
fi
fi

exit # Script is not yet finished and tested - use at your own risk!

LIBRARY=""

# Only existing library was supplied
if [[ ! -z $e_param ]] && [[ -z $l_param ]] && [[ -z $n_param ]]; then
LIBRARY=$e_param
fi

# Install new lib
if [ ! -z $n_param ]; then
INSTALL_TARGET=""
if [ -z $l_param ]; then
# If local path for project is not supplied - use as INSTALL_TARGET Arduino libraries path
INSTALL_TARGET=$($ARDUINO_LIBS_PATH/$(basename "$n_param"))
INSTALL_TARGET=$ARDUINO_LIBS_PATH/$(basename "$n_param")
else
INSTALL_TARGET=$l_param/components/$(basename "$n_param")
if [ ! -d "$l_param/components" ]; then
echo "Folder components does not exist yet: mkdir -p "$l_param/components""
mkdir -p "$l_param/components"
fi
fi

# clone the new lib
echo "Cloning: git clone --recursive $n_param $INSTALL_TARGET"
git clone --recursive $n_param $INSTALL_TARGET
Expand All @@ -105,43 +112,32 @@ fi

# Copy existing lib to local project
if [[ ! -z $e_param ]] && [[ ! -z $l_param ]]; then
if [ ! -d "$l_param/components" ]; then
echo "Folder components does not exist yet: mkdir -p "$l_param/components""
mkdir -p "$l_param/components"
fi
echo "Copy from $e_param to $l_param"
echo "cp -r $e_param $l_param/components/$(basename "$e_param")"
cp -r $e_param $l_param/components/$(basename "$e_param")
LIBRARY=$l_param/components/$(basename "$e_param")
fi

# If Local target was supplied use that path, otherwise use Arduino path
#if [ ! -z $l_param ]; then
# echo "DEBUG: Local lib path"
# if [ ! -z $e_param ]; then
# echo "DEBUG: from existing"
# LIBRARY=$l_param/components/$(basename "$e_param")
# elif [ ! -z $n_param ]; then
# echo "DEBUG: from new"
#LIBRARY=$INSTALL_TARGET
# fi
#else
# echo "DEBUG: Arduino lib path"
# LIBRARY=$($ARDUINO_LIBS_PATH/$(basename "$n_param"))
#fi

echo "DEBUG: LIBRARY: $LIBRARY"


if [ -z LIBRARY ]; then
echo "ERROR: No library -e" >&2

if [ -z "$LIBRARY" ]; then
echo "ERROR: No library path" >&2
exit 1
fi

# Generate CMakeLists.txt

# 1. get the source list:
FILES=$(find $LIBRARY -name '*.c' -o -name '*.cpp' | xargs -I{} basename {})
echo "DEBUG: source files:"
echo "$FILES"

rm $LIBRARY/CMakeLists.txt # Fresh start
# Fresh start
if [ -f $LIBRARY/CMakeLists.txt ]; then
rm $LIBRARY/CMakeLists.txt
touch $LIBRARY/CMakeLists.txt
fi

# Generate CMakeLists.txt
echo "idf_component_register(SRCS $(echo $FILES | sed -e 's/ /" "/g' | sed -e 's/^/"/' -e 's/$/"/')" >> $LIBRARY/CMakeLists.txt
echo " INCLUDE_DIRS \".\"" >> $LIBRARY/CMakeLists.txt
echo " REQUIRES \"arduino-esp32\"" >> $LIBRARY/CMakeLists.txt
Expand Down

0 comments on commit e6234ed

Please sign in to comment.