|
| 1 | +#!/bin/bash |
| 2 | +HELP="This script help to add library when using arduino-esp32 as an ESP-IDF component |
| 3 | +The script accepts up to three arguments: |
| 4 | +-n NEW: URL address to new library on GIThub (cannot be combined with -e) |
| 5 | +-l LOCAL: Path to the project where the library should be placed locally (must be paired with -e or -n) |
| 6 | +-e EXISTING: path to existing libary- this will simply skip the download (cannot be combined with -n) |
| 7 | +
|
| 8 | +Examples: |
| 9 | +./add_lib.sh -n https://github.com/me-no-dev/ESPAsyncWebServer |
| 10 | +./add_lib.sh -l ~/esp/esp-idf/examples/your_project |
| 11 | +./add_lib.sh -e ~/Arduino/libraries/existing_library |
| 12 | +
|
| 13 | +./add_lib.sh -n https://github.com/me-no-dev/ESPAsyncWebServer -l ~/esp/esp-idf/examples/your_project |
| 14 | +./add_lib.sh -e ~/Arduino/libraries/existing_library -l ~/esp/esp-idf/examples/your_project" |
| 15 | + |
| 16 | +# Get the directory name where this script is located |
| 17 | +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" |
| 18 | + |
| 19 | +# Construct the absolute path to libraries folder |
| 20 | +ARDUINO_LIBS_PATH="$SCRIPT_DIR/../libraries" |
| 21 | + |
| 22 | +# Define the default values for the parameters |
| 23 | +e_param="" |
| 24 | +l_param="" |
| 25 | +n_param="" |
| 26 | + |
| 27 | +# Parse the command-line arguments using getopts |
| 28 | +while getopts "he:l:n:" opt; do |
| 29 | + case $opt in |
| 30 | + h) |
| 31 | + echo "$HELP" |
| 32 | + exit 0 |
| 33 | + ;; |
| 34 | + e) |
| 35 | + #e_param="$OPTARG" |
| 36 | + e_param="${OPTARG/#~/$HOME}" |
| 37 | + ;; |
| 38 | + l) |
| 39 | + #l_param="$OPTARG" |
| 40 | + l_param="${OPTARG/#~/$HOME}" |
| 41 | + ;; |
| 42 | + n) |
| 43 | + n_param=$OPTARG |
| 44 | + ;; |
| 45 | + \?) |
| 46 | + echo "Invalid option: -$OPTARG" >&2 |
| 47 | + echo $HELP |
| 48 | + exit 1 |
| 49 | + ;; |
| 50 | + :) |
| 51 | + echo "Option -$OPTARG requires an argument." >&2 |
| 52 | + echo $HELP |
| 53 | + exit 1 |
| 54 | + ;; |
| 55 | + esac |
| 56 | +done |
| 57 | + |
| 58 | +# No parameter check |
| 59 | +if [[ -z "$e_param" ]] && [[ -z "$l_param" ]] && [[ -z "$n_param" ]]; then |
| 60 | + echo "Error: No parameters" >&2 |
| 61 | + echo "$HELP" |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | + |
| 65 | +# Only local path check (not permitted) |
| 66 | +if [[ -z "$e_param" ]] && [[ ! -z "$l_param" ]] && [[ -z "$n_param" ]]; then |
| 67 | + echo "Error: -l parameter must be paired with -e or -n" >&2 |
| 68 | + echo "$HELP" |
| 69 | + exit 1 |
| 70 | +fi |
| 71 | + |
| 72 | +# Invalid combination check |
| 73 | +if [[ ! -z $e_param ]] && [[ ! -z $n_param ]]; then |
| 74 | + echo "ERROR: Cannot combine -n with -e" >&2 |
| 75 | + echo "$HELP" |
| 76 | + exit 1 |
| 77 | +fi |
| 78 | + |
| 79 | +# Check existing lib |
| 80 | +if [[ ! -z "$e_param" ]]; then |
| 81 | + if [[ ! -d "${e_param/#~/$HOME}" ]]; then # this works! |
| 82 | + echo "Error: existing library parameter - path does not exist" >&2 |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | +fi |
| 86 | + |
| 87 | +LIBRARY="" |
| 88 | + |
| 89 | +# Only existing library was supplied |
| 90 | +if [[ ! -z $e_param ]] && [[ -z $l_param ]] && [[ -z $n_param ]]; then |
| 91 | + LIBRARY=$e_param |
| 92 | +fi |
| 93 | + |
| 94 | +# Install new lib |
| 95 | +if [ ! -z $n_param ]; then |
| 96 | + INSTALL_TARGET="" |
| 97 | + if [ -z $l_param ]; then |
| 98 | + # If local path for project is not supplied - use as INSTALL_TARGET Arduino libraries path |
| 99 | + INSTALL_TARGET=$ARDUINO_LIBS_PATH/$(basename "$n_param") |
| 100 | + else |
| 101 | + INSTALL_TARGET=$l_param/components/$(basename "$n_param") |
| 102 | + if [ ! -d "$l_param/components" ]; then |
| 103 | + echo "Folder components does not exist yet: mkdir -p "$l_param/components"" |
| 104 | + mkdir -p "$l_param/components" |
| 105 | + fi |
| 106 | + fi |
| 107 | + # clone the new lib |
| 108 | + echo "Cloning: git clone --recursive $n_param $INSTALL_TARGET" |
| 109 | + git clone --recursive $n_param $INSTALL_TARGET |
| 110 | + LIBRARY=$INSTALL_TARGET |
| 111 | +fi |
| 112 | + |
| 113 | +# Copy existing lib to local project |
| 114 | +if [[ ! -z $e_param ]] && [[ ! -z $l_param ]]; then |
| 115 | + if [ ! -d "$l_param/components" ]; then |
| 116 | + echo "Folder components does not exist yet: mkdir -p "$l_param/components"" |
| 117 | + mkdir -p "$l_param/components" |
| 118 | + fi |
| 119 | + echo "Copy from $e_param to $l_param" |
| 120 | + echo "cp -r $e_param $l_param/components/$(basename "$e_param")" |
| 121 | + cp -r $e_param $l_param/components/$(basename "$e_param") |
| 122 | + LIBRARY=$l_param/components/$(basename "$e_param") |
| 123 | +fi |
| 124 | + |
| 125 | + |
| 126 | +if [ -z "$LIBRARY" ]; then |
| 127 | + echo "ERROR: No library path" >&2 |
| 128 | + exit 1 |
| 129 | +fi |
| 130 | + |
| 131 | +# 1. get the source list: |
| 132 | +FILES=$(find $LIBRARY -name '*.c' -o -name '*.cpp' | xargs -I{} basename {}) |
| 133 | + |
| 134 | +# Fresh start |
| 135 | +if [ -f $LIBRARY/CMakeLists.txt ]; then |
| 136 | + rm $LIBRARY/CMakeLists.txt |
| 137 | + touch $LIBRARY/CMakeLists.txt |
| 138 | +fi |
| 139 | + |
| 140 | +# Generate CMakeLists.txt |
| 141 | +echo "idf_component_register(SRCS $(echo $FILES | sed -e 's/ /" "/g' | sed -e 's/^/"/' -e 's/$/"/')" >> $LIBRARY/CMakeLists.txt |
| 142 | +echo " INCLUDE_DIRS \".\"" >> $LIBRARY/CMakeLists.txt |
| 143 | +echo " REQUIRES \"arduino-esp32\"" >> $LIBRARY/CMakeLists.txt |
| 144 | +echo " )" >> $LIBRARY/CMakeLists.txt |
0 commit comments