Refactor CLI, add refresh command, fix port detection, add device tracking

- Remove build/upload/monitor subcommands (projects are self-contained)
- Remove ctrlc dependency (only used by removed monitor watch mode)
- Update next-steps messaging to reference project scripts directly

- Add 'anvil refresh [DIR] [--force]' to update project scripts
  to latest templates without touching user code

- Fix Windows port detection: replace fragile findstr/batch TOML
  parsing with proper comment-skipping logic; add _detect_port.ps1
  helper for reliable JSON-based port detection via PowerShell

- Add .anvil.local for machine-specific config (gitignored)
  - 'anvil devices --set [PORT] [-d DIR]' saves port + VID:PID
  - 'anvil devices --get [-d DIR]' shows saved port status
  - VID:PID tracks USB devices across COM port reassignment
  - Port resolution: -p flag > VID:PID > saved port > auto-detect
  - Uppercase normalization for Windows COM port names

- Update all .bat/.sh templates to read from .anvil.local
- Remove port entries from .anvil.toml (no machine-specific config in git)
- Add .anvil.local to .gitignore template
- Expand 'anvil devices' output with VID:PID, serial number, and
  usage instructions
This commit is contained in:
Eric Ratliff
2026-02-16 08:29:33 -06:00
parent 3298844399
commit 8fe1ef0e27
25 changed files with 2551 additions and 731 deletions

145
templates/basic/build.sh Normal file
View File

@@ -0,0 +1,145 @@
#!/usr/bin/env bash
#
# build.sh -- Compile the sketch using arduino-cli
#
# Reads all settings from .anvil.toml. No Anvil binary required.
#
# Usage:
# ./build.sh Compile (verify only)
# ./build.sh --clean Delete build cache first
# ./build.sh --verbose Show full compiler output
#
# Prerequisites: arduino-cli in PATH, arduino:avr core installed
# Install: anvil setup (or manually: arduino-cli core install arduino:avr)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
CONFIG="$SCRIPT_DIR/.anvil.toml"
# -- Colors ----------------------------------------------------------------
if [[ -t 1 ]]; then
RED=$'\033[0;31m'; GRN=$'\033[0;32m'; YLW=$'\033[0;33m'
CYN=$'\033[0;36m'; BLD=$'\033[1m'; RST=$'\033[0m'
else
RED=''; GRN=''; YLW=''; CYN=''; BLD=''; RST=''
fi
ok() { echo "${GRN}ok${RST} $*"; }
warn() { echo "${YLW}warn${RST} $*"; }
die() { echo "${RED}FAIL${RST} $*" >&2; exit 1; }
# -- Parse .anvil.toml -----------------------------------------------------
[[ -f "$CONFIG" ]] || die "No .anvil.toml found in $SCRIPT_DIR"
# Extract a simple string value: toml_get "key"
# Searches the whole file; for sectioned keys, grep is specific enough
# given our small, flat schema.
toml_get() {
(grep "^$1 " "$CONFIG" 2>/dev/null || true) | head -1 | sed 's/.*= *"\{0,1\}\([^"]*\)"\{0,1\}/\1/' | tr -d ' '
}
# Extract a TOML array as space-separated values: toml_array "key"
toml_array() {
(grep "^$1 " "$CONFIG" 2>/dev/null || true) | head -1 \
| sed 's/.*\[//; s/\].*//; s/"//g; s/,/ /g' | tr -s ' '
}
SKETCH_NAME="$(toml_get 'name')"
FQBN="$(toml_get 'fqbn')"
WARNINGS="$(toml_get 'warnings')"
INCLUDE_DIRS="$(toml_array 'include_dirs')"
EXTRA_FLAGS="$(toml_array 'extra_flags')"
[[ -n "$SKETCH_NAME" ]] || die "Could not read project name from .anvil.toml"
[[ -n "$FQBN" ]] || die "Could not read fqbn from .anvil.toml"
SKETCH_DIR="$SCRIPT_DIR/$SKETCH_NAME"
BUILD_DIR="$SCRIPT_DIR/.build"
# -- Parse arguments -------------------------------------------------------
DO_CLEAN=0
VERBOSE=""
for arg in "$@"; do
case "$arg" in
--clean) DO_CLEAN=1 ;;
--verbose) VERBOSE="--verbose" ;;
-h|--help)
echo "Usage: ./build.sh [--clean] [--verbose]"
echo " Compiles the sketch. Settings from .anvil.toml."
exit 0
;;
*) die "Unknown option: $arg" ;;
esac
done
# -- Preflight -------------------------------------------------------------
command -v arduino-cli &>/dev/null \
|| die "arduino-cli not found in PATH. Install it first."
[[ -d "$SKETCH_DIR" ]] \
|| die "Sketch directory not found: $SKETCH_DIR"
[[ -f "$SKETCH_DIR/$SKETCH_NAME.ino" ]] \
|| die "Sketch file not found: $SKETCH_DIR/$SKETCH_NAME.ino"
# -- Clean -----------------------------------------------------------------
if [[ $DO_CLEAN -eq 1 ]] && [[ -d "$BUILD_DIR" ]]; then
echo "${YLW}Cleaning build cache...${RST}"
rm -rf "$BUILD_DIR"
ok "Cache cleared."
fi
# -- Build include flags ---------------------------------------------------
BUILD_FLAGS=""
for dir in $INCLUDE_DIRS; do
abs="$SCRIPT_DIR/$dir"
if [[ -d "$abs" ]]; then
BUILD_FLAGS="$BUILD_FLAGS -I$abs"
else
warn "Include directory not found: $dir"
fi
done
for flag in $EXTRA_FLAGS; do
BUILD_FLAGS="$BUILD_FLAGS $flag"
done
# -- Compile ---------------------------------------------------------------
echo "${CYN}${BLD}Compiling ${SKETCH_NAME}...${RST}"
echo " Board: $FQBN"
echo " Sketch: $SKETCH_DIR"
echo ""
mkdir -p "$BUILD_DIR"
COMPILE_ARGS=(
compile
--fqbn "$FQBN"
--build-path "$BUILD_DIR"
--warnings "$WARNINGS"
)
if [[ -n "$BUILD_FLAGS" ]]; then
COMPILE_ARGS+=(--build-property "build.extra_flags=$BUILD_FLAGS")
fi
if [[ -n "$VERBOSE" ]]; then
COMPILE_ARGS+=("$VERBOSE")
fi
COMPILE_ARGS+=("$SKETCH_DIR")
arduino-cli "${COMPILE_ARGS[@]}" || die "Compilation failed."
echo ""
ok "Compile succeeded."
# -- Binary size -----------------------------------------------------------
ELF="$BUILD_DIR/$SKETCH_NAME.ino.elf"
if [[ -f "$ELF" ]] && command -v avr-size &>/dev/null; then
echo ""
avr-size --mcu=atmega328p -C "$ELF"
fi
echo ""