picoco/scripts/scaffold_project.bash
2025-02-19 11:22:12 +01:00

83 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
#######################################################################
#
# Generate a new project from templates.
# It scaffold a project, ready to use, in the current directory with:
# * CMakeLists.txt
# * main.c
#
# ./install_sdk.bash <project_name> <sdk_version>
# ex: ./install_sdk.bash <project_name> <sdk_version>
#
# The SDK must be installed before using the script.
# If the SDK is located in a different directory than the project
# set the `INSTALL_PATH` env var to point to the SDK directory.
#
#######################################################################
PROJECT_NAME=$1
PICO_SDK_VERSION=$2
REGEX_VERSION="^[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}$"
if [[ -z ${INSTALL_PATH} ]]
then
INSTALL_PATH=${PWD}
fi
usage() {
echo "usage:"
echo "./scaffold_project.bash <project_name> <sdk_version> (ex: ./scaffold_project.bash test 2.1.0)"
}
if [[ -z ${PROJECT_NAME} ]]
then
echo "error: project name must not be empty"
usage
exit 1
fi
PROJECT_DIR="${PROJECT_NAME}"
if [[ -d ${PROJECT_DIR} ]]
then
echo "error: a project with same name: ${PROJECT_NAME} already exists"
exit 1
fi
if [[ ! "${PICO_SDK_VERSION}" =~ ${REGEX_VERSION} ]]
then
echo "error: unable to parse the SDK version: '${PICO_SDK_VERSION}' (ex: ./install_sdk 2.1.0)"
exit 1
fi
if [[ ! -d ${INSTALL_PATH}/sdk/${PICO_SDK_VERSION} ]]
then
echo "error: no SDK v${PICO_SDK_VERSION} exists, please make an install first"
exit 1
fi
PICO_SDK_PATH=${INSTALL_PATH}/sdk/${PICO_SDK_VERSION}
mkdir -p ${PROJECT_DIR}
echo "generating templates..."
# build from the cmake list template
cp templates/CMakeLists.txt.tpl templates/CMakeLists.txt.tmp
sed -i "s/<project_name>/${PROJECT_NAME}/g" templates/CMakeLists.txt.tmp
mv templates/CMakeLists.txt.tmp ${PROJECT_DIR}/CMakeLists.txt
# move the c src file into the project folder
cp templates/main.c.tpl templates/main.c.tmp
sed -i "s/<prog_name>/${PROJECT_NAME}/g" templates/main.c.tmp
mv templates/main.c.tmp ${PROJECT_DIR}/main.c
echo "building the project: ${PROJECT_NAME}..."
cd ${PROJECT_DIR}
mkdir -p build
cd build
cmake ..
make -j$(nproc)
echo "project ${PROJECT_NAME} successfully generated"