#!/usr/bin/env -S bash -e # # Copyright 2021 Nathan L. Conrad # # This program is free software: you can redistribute it and/or modify it under # the terms of version 3 of the GNU General Public License as published by the # Free Software Foundation. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # Establish usage IFS= read -rd '' usage << '__eof__' || true Usage: ino [-cdghu] [sketch] Streamlines the compilation and development of an Arduino sketch Optional arguments: -c, --clean Delete build output and if -d/--compilation-db, -g/--debug, and -u/--upload are not set, exit -d, --compilation-db Construct the compilation command database and exit -g, --debug Optimize compilation for debugging -h, --help Show this help message and exit -u, --upload Upload after successful build Positional arguments: sketch The Arduino sketch directory. Defaults to the current working directory. __eof__ # Usage: argerr msg # # Write an argument usage error message to standard error and fail # # Positional arguments: # msg The core error message function argerr { echo "$1. Use -h for help." >&2 exit 1 } # Parse optional arguments clean=0 compilation_db=0 debug=0 upload=0 while (( $# )) do # If the argument is a single hyphen followed by multiple single-character # options, separate the first option from the argument and leave the rest # for the next iteration (( ${#1} > 2 )) && [[ $1 =~ ^-[^-]*$ ]] && \ set -- "${1::2}" "-${1:2}" "${@:2}" # Handle the argument case $1 in -c|--clean) clean=1 ;; -d|--compilation_db) (( upload )) && argerr "$1 is incompatible with -u/--upload" compilation_db=1 ;; -g|--debug) debug=1 ;; -h|--help) echo -n "$usage" exit 0 ;; -u|--upload) (( compilation_db )) && \ argerr "$1 is incompatible with -d/--compilation-db" upload=1 ;; --) shift break ;; -*|--*) argerr "Invalid argument '$1'" ;; *) break esac # Advance to the next argument shift done # Change into the sketch directory if (( $# )) then (( $# < 2 )) || argerr 'Too many positional arguments' cd "$1" fi # Clean if (( clean )) then rm -fr .compilation-db bin rm -f compile_commands.json if (( !(compilation_db || debug || upload) )) then exit 0 fi fi # Construct Arduino CLI compile options if [[ -f .ino ]] then IFS=$'\n' compile_opts=($(grep -v '^#' .ino | xargs -n 1)) else compile_opts=() fi if (( debug )) then compile_opts+=(--optimize-for-debug) fi # Construct the compilation database rm -fr .compilation-db arduino-cli compile "${compile_opts[@]}" --only-compilation-database \ --build-path .compilation-db cp .compilation-db/compile_commands.json ./ # Compile arduino-cli compile "${compile_opts[@]}" --output-dir bin # Upload if (( upload )) then arduino-cli upload --verify --input-dir bin fi