Anvil v1.0.0 -- Arduino build tool with HAL and test scaffolding

Single-binary CLI that scaffolds testable Arduino projects, compiles,
uploads, and monitors serial output. Templates embed a hardware
abstraction layer, Google Mock infrastructure, and CMake-based host
tests so application logic can be verified without hardware.

Commands: new, doctor, setup, devices, build, upload, monitor
39 Rust tests (21 unit, 18 integration)
Cross-platform: Linux and Windows
This commit is contained in:
Eric Ratliff
2026-02-15 11:16:17 -06:00
commit 3298844399
41 changed files with 4866 additions and 0 deletions

345
src/board/mod.rs Normal file
View File

@@ -0,0 +1,345 @@
use anyhow::{Result, bail};
use colored::*;
use serde::Deserialize;
use std::path::{Path, PathBuf};
use std::process::Command;
/// Information about a detected serial port.
#[derive(Debug, Clone)]
pub struct PortInfo {
pub port_name: String,
pub protocol: String,
pub board_name: String,
pub fqbn: String,
}
/// JSON schema for `arduino-cli board list --format json`
#[derive(Debug, Deserialize)]
struct BoardListOutput {
#[serde(default)]
detected_ports: Vec<DetectedPort>,
}
#[derive(Debug, Deserialize)]
struct DetectedPort {
#[serde(default)]
port: Option<PortEntry>,
#[serde(default)]
matching_boards: Option<Vec<MatchingBoard>>,
}
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct PortEntry {
#[serde(default)]
address: String,
#[serde(default)]
protocol: String,
#[serde(default)]
protocol_label: String,
#[serde(default)]
properties: Option<serde_json::Value>,
}
#[derive(Debug, Deserialize)]
struct MatchingBoard {
#[serde(default)]
name: String,
#[serde(default)]
fqbn: String,
}
/// Enumerate serial ports via `arduino-cli board list --format json`.
/// Falls back to OS-level detection if arduino-cli is unavailable.
pub fn list_ports() -> Vec<PortInfo> {
if let Some(cli) = find_arduino_cli() {
if let Ok(ports) = list_ports_via_cli(&cli) {
return ports;
}
}
list_ports_fallback()
}
fn list_ports_via_cli(cli: &Path) -> Result<Vec<PortInfo>> {
let output = Command::new(cli)
.args(["board", "list", "--format", "json"])
.output()?;
if !output.status.success() {
bail!("arduino-cli board list failed");
}
let stdout = String::from_utf8_lossy(&output.stdout);
let parsed: BoardListOutput = serde_json::from_str(&stdout)?;
let mut result = Vec::new();
for dp in parsed.detected_ports {
let port = match dp.port {
Some(p) => p,
None => continue,
};
if port.protocol != "serial" {
continue;
}
let (board_name, fqbn) = match dp.matching_boards {
Some(ref boards) if !boards.is_empty() => {
(boards[0].name.clone(), boards[0].fqbn.clone())
}
_ => ("Unknown".to_string(), String::new()),
};
result.push(PortInfo {
port_name: port.address,
protocol: port.protocol_label,
board_name,
fqbn,
});
}
Ok(result)
}
/// Fallback port detection when arduino-cli is not available.
fn list_ports_fallback() -> Vec<PortInfo> {
let mut result = Vec::new();
#[cfg(unix)]
{
use std::fs;
if let Ok(entries) = fs::read_dir("/dev") {
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if name.starts_with("ttyUSB") || name.starts_with("ttyACM") {
let path = format!("/dev/{}", name);
let board = if name.starts_with("ttyUSB") {
"Likely CH340/FTDI (run 'anvil setup' for full detection)"
} else {
"Likely Arduino (run 'anvil setup' for full detection)"
};
result.push(PortInfo {
port_name: path,
protocol: "serial".to_string(),
board_name: board.to_string(),
fqbn: String::new(),
});
}
}
}
}
#[cfg(windows)]
{
if let Ok(output) = Command::new("powershell")
.args([
"-NoProfile", "-Command",
"Get-CimInstance Win32_SerialPort | Select-Object DeviceID,Caption | ConvertTo-Json",
])
.output()
{
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
if let Some(idx) = line.find("COM") {
let port = line[idx..].split('"').next().unwrap_or("").trim();
if !port.is_empty() {
result.push(PortInfo {
port_name: port.to_string(),
protocol: "serial".to_string(),
board_name: "Detected via WMI".to_string(),
fqbn: String::new(),
});
}
}
}
}
}
}
result
}
/// Auto-detect a single serial port.
pub fn auto_detect_port() -> Result<String> {
let ports = list_ports();
if ports.is_empty() {
bail!(
"No serial ports found. Is the board plugged in?\n\
Run: anvil devices"
);
}
if ports.len() == 1 {
return Ok(ports[0].port_name.clone());
}
eprintln!("{}", "Multiple serial ports detected:".yellow());
for p in &ports {
eprintln!(" {} ({})", p.port_name, p.board_name);
}
// Prefer a port with a recognized board
for p in &ports {
if !p.fqbn.is_empty() {
eprintln!(
"{}",
format!(
"Auto-selected {} ({}). Use -p to override.",
p.port_name, p.board_name
).yellow()
);
return Ok(p.port_name.clone());
}
}
let selected = ports[0].port_name.clone();
eprintln!(
"{}",
format!("Auto-selected {}. Use -p to override.", selected).yellow()
);
Ok(selected)
}
/// Print detailed port information.
pub fn print_port_details(ports: &[PortInfo]) {
if ports.is_empty() {
println!(" {}", "No serial devices found.".yellow());
println!();
println!(" Checklist:");
println!(" 1. Is the board plugged in via USB?");
println!(" 2. Is the USB cable a data cable (not charge-only)?");
#[cfg(target_os = "linux")]
{
println!(" 3. Check kernel log: dmesg | tail -20");
println!(" 4. Check USB bus: lsusb | grep -i -E 'ch34|arduino|1a86|2341'");
}
println!(" 5. Try a different USB port or cable");
return;
}
for port in ports {
println!(" {}", port.port_name.green().bold());
println!(" Board: {}", port.board_name);
if !port.fqbn.is_empty() {
println!(" FQBN: {}", port.fqbn);
}
if !port.protocol.is_empty() {
println!(" Protocol: {}", port.protocol);
}
#[cfg(unix)]
{
use std::fs::OpenOptions;
let path = std::path::Path::new(&port.port_name);
if path.exists() {
match OpenOptions::new().write(true).open(path) {
Ok(_) => {
println!(
" Permissions: {}",
"OK (writable)".green()
);
}
Err(_) => {
println!(
" Permissions: {}",
"NOT writable -- run: sudo usermod -aG dialout $USER"
.red()
);
}
}
}
}
println!();
}
}
/// Find arduino-cli in PATH or in ~/.anvil/bin.
pub fn find_arduino_cli() -> Option<PathBuf> {
if let Ok(path) = which::which("arduino-cli") {
return Some(path);
}
if let Ok(home) = crate::project::config::anvil_home() {
let name = if cfg!(target_os = "windows") {
"arduino-cli.exe"
} else {
"arduino-cli"
};
let bin = home.join("bin").join(name);
if bin.exists() {
return Some(bin);
}
}
None
}
/// Check if the arduino:avr core is installed.
pub fn is_avr_core_installed(cli_path: &Path) -> bool {
let output = Command::new(cli_path)
.args(["core", "list"])
.output();
match output {
Ok(out) => {
let stdout = String::from_utf8_lossy(&out.stdout);
stdout.contains("arduino:avr")
}
Err(_) => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_list_ports_does_not_panic() {
let _ports = list_ports();
}
#[test]
fn test_port_info_clone() {
let info = PortInfo {
port_name: "/dev/ttyUSB0".to_string(),
protocol: "serial".to_string(),
board_name: "Test".to_string(),
fqbn: "arduino:avr:uno".to_string(),
};
let cloned = info.clone();
assert_eq!(cloned.port_name, info.port_name);
}
#[test]
fn test_parse_empty_board_list() {
let json = r#"{"detected_ports": []}"#;
let parsed: BoardListOutput = serde_json::from_str(json).unwrap();
assert!(parsed.detected_ports.is_empty());
}
#[test]
fn test_parse_board_list_with_port() {
let json = r#"{
"detected_ports": [{
"port": {
"address": "/dev/ttyUSB0",
"protocol": "serial",
"protocol_label": "Serial Port (USB)"
},
"matching_boards": [{
"name": "Arduino Uno",
"fqbn": "arduino:avr:uno"
}]
}]
}"#;
let parsed: BoardListOutput = serde_json::from_str(json).unwrap();
assert_eq!(parsed.detected_ports.len(), 1);
let dp = &parsed.detected_ports[0];
assert_eq!(dp.port.as_ref().unwrap().address, "/dev/ttyUSB0");
let boards = dp.matching_boards.as_ref().unwrap();
assert_eq!(boards[0].name, "Arduino Uno");
}
}

299
src/commands/build.rs Normal file
View File

@@ -0,0 +1,299 @@
use anyhow::{Result, bail, Context};
use colored::*;
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::board;
use crate::project::config::{ProjectConfig, build_cache_dir};
/// Full build: compile + upload (+ optional monitor).
pub fn run_build(
sketch: &str,
verify_only: bool,
do_monitor: bool,
do_clean: bool,
verbose: bool,
port: Option<&str>,
baud: Option<u32>,
fqbn_override: Option<&str>,
) -> Result<()> {
let sketch_path = resolve_sketch(sketch)?;
let sketch_name = sketch_name(&sketch_path)?;
let project_root = ProjectConfig::find_project_root(&sketch_path)
.ok();
// Load project config if available, otherwise use defaults
let config = match &project_root {
Some(root) => ProjectConfig::load(root)?,
None => {
eprintln!(
"{}",
"No .anvil.toml found; using default settings.".yellow()
);
ProjectConfig::default()
}
};
let fqbn = fqbn_override.unwrap_or(&config.build.fqbn);
let monitor_baud = baud.unwrap_or(config.monitor.baud);
println!("Sketch: {}", sketch_name.bright_white().bold());
println!("Board: {}", fqbn.bright_white());
// Locate arduino-cli
let cli = board::find_arduino_cli()
.context("arduino-cli not found. Run: anvil setup")?;
// Verify AVR core
if !board::is_avr_core_installed(&cli) {
bail!("arduino:avr core not installed. Run: anvil setup");
}
// Build cache directory
let cache_dir = build_cache_dir()?.join(&sketch_name);
// Clean if requested
if do_clean && cache_dir.exists() {
println!("{}", "Cleaning build cache...".bright_yellow());
std::fs::remove_dir_all(&cache_dir)?;
println!(" {} Cache cleared.", "ok".green());
}
// Compile
println!("{}", "Compiling...".bright_yellow());
std::fs::create_dir_all(&cache_dir)?;
let mut compile_args: Vec<String> = vec![
"compile".to_string(),
"--fqbn".to_string(),
fqbn.to_string(),
"--build-path".to_string(),
cache_dir.display().to_string(),
"--warnings".to_string(),
config.build.warnings.clone(),
];
if verbose {
compile_args.push("--verbose".to_string());
}
// Inject project-level build flags (include paths, -Werror, etc.)
if let Some(ref root) = project_root {
let extra = config.extra_flags_string(root);
if !extra.is_empty() {
compile_args.push("--build-property".to_string());
compile_args.push(format!("build.extra_flags={}", extra));
}
}
compile_args.push(sketch_path.display().to_string());
let status = Command::new(&cli)
.args(&compile_args)
.status()
.context("Failed to execute arduino-cli compile")?;
if !status.success() {
bail!("Compilation failed.");
}
println!(" {} Compile succeeded.", "ok".green());
// Report binary size
report_binary_size(&cache_dir, &sketch_name);
// Verify-only: stop here
if verify_only {
println!();
println!(" {} Verify-only mode. Done.", "ok".green());
return Ok(());
}
// Upload
let port = match port {
Some(p) => p.to_string(),
None => board::auto_detect_port()?,
};
upload_to_board(&cli, fqbn, &port, &cache_dir, verbose)?;
// Monitor
if do_monitor {
println!();
println!(
"Opening serial monitor on {} at {} baud...",
port.bright_white(),
monitor_baud
);
println!("Press Ctrl+C to exit.");
println!();
let _ = Command::new(&cli)
.args([
"monitor",
"-p", &port,
"-c", &format!("baudrate={}", monitor_baud),
])
.status();
} else {
println!();
println!("To open serial monitor:");
println!(
" anvil monitor -p {} -b {}",
port, monitor_baud
);
}
Ok(())
}
/// Upload cached build artifacts without recompiling.
pub fn run_upload_only(
sketch: &str,
port: Option<&str>,
verbose: bool,
fqbn_override: Option<&str>,
) -> Result<()> {
let sketch_path = resolve_sketch(sketch)?;
let sketch_name = sketch_name(&sketch_path)?;
let project_root = ProjectConfig::find_project_root(&sketch_path)
.ok();
let config = match &project_root {
Some(root) => ProjectConfig::load(root)?,
None => ProjectConfig::default(),
};
let fqbn = fqbn_override.unwrap_or(&config.build.fqbn);
// Verify cached build exists
let cache_dir = build_cache_dir()?.join(&sketch_name);
if !cache_dir.exists() {
bail!(
"No cached build found for '{}'.\n\
Run a compile first: anvil build --verify {}",
sketch_name,
sketch
);
}
let hex_name = format!("{}.ino.hex", sketch_name);
if !cache_dir.join(&hex_name).exists() {
bail!(
"Build cache exists but no .hex file found.\n\
Try a clean rebuild: anvil build --clean {}",
sketch
);
}
println!(" {} Using cached build.", "ok".green());
report_binary_size(&cache_dir, &sketch_name);
let cli = board::find_arduino_cli()
.context("arduino-cli not found. Run: anvil setup")?;
let port = match port {
Some(p) => p.to_string(),
None => board::auto_detect_port()?,
};
upload_to_board(&cli, fqbn, &port, &cache_dir, verbose)?;
Ok(())
}
/// Upload compiled artifacts to the board.
fn upload_to_board(
cli: &Path,
fqbn: &str,
port: &str,
input_dir: &Path,
verbose: bool,
) -> Result<()> {
println!(
"Uploading to {}...",
port.bright_white().bold()
);
let mut upload_args = vec![
"upload".to_string(),
"--fqbn".to_string(),
fqbn.to_string(),
"--port".to_string(),
port.to_string(),
"--input-dir".to_string(),
input_dir.display().to_string(),
];
if verbose {
upload_args.push("--verbose".to_string());
}
let status = Command::new(cli)
.args(&upload_args)
.status()
.context("Failed to execute arduino-cli upload")?;
if !status.success() {
bail!(
"Upload failed. Run with --verbose for details.\n\
Also try: anvil devices"
);
}
println!(" {} Upload complete!", "ok".green());
Ok(())
}
/// Resolve sketch argument to an absolute path.
fn resolve_sketch(sketch: &str) -> Result<PathBuf> {
let path = PathBuf::from(sketch);
let abs = if path.is_absolute() {
path
} else {
std::env::current_dir()?.join(&path)
};
// Canonicalize if it exists
let resolved = if abs.exists() {
abs.canonicalize().unwrap_or(abs)
} else {
abs
};
if !resolved.is_dir() {
bail!("Not a directory: {}", resolved.display());
}
Ok(resolved)
}
/// Extract the sketch name from a path (basename of the directory).
fn sketch_name(sketch_path: &Path) -> Result<String> {
let name = sketch_path
.file_name()
.context("Could not determine sketch name")?
.to_string_lossy()
.to_string();
Ok(name)
}
/// Report binary size using avr-size if available.
fn report_binary_size(cache_dir: &Path, sketch_name: &str) {
let elf_name = format!("{}.ino.elf", sketch_name);
let elf_path = cache_dir.join(&elf_name);
if !elf_path.exists() {
return;
}
if which::which("avr-size").is_err() {
return;
}
println!();
let _ = Command::new("avr-size")
.args(["--mcu=atmega328p", "-C"])
.arg(&elf_path)
.status();
println!();
}

67
src/commands/devices.rs Normal file
View File

@@ -0,0 +1,67 @@
use anyhow::Result;
use colored::*;
use crate::board;
pub fn scan_devices() -> Result<()> {
println!(
"{}",
"=== Connected Serial Devices ===".bold()
);
println!();
let ports = board::list_ports();
board::print_port_details(&ports);
// Also run arduino-cli board list for cross-reference
if let Some(cli_path) = board::find_arduino_cli() {
println!();
println!(
"{}",
"=== arduino-cli Board Detection ===".bold()
);
println!();
let output = std::process::Command::new(&cli_path)
.args(["board", "list"])
.output();
match output {
Ok(out) if out.status.success() => {
let stdout = String::from_utf8_lossy(&out.stdout);
for line in stdout.lines() {
println!(" {}", line);
}
}
_ => {
eprintln!(
" {}",
"arduino-cli board list failed (is the core installed?)"
.yellow()
);
}
}
} else {
println!();
eprintln!(
" {}",
"arduino-cli not found -- run 'anvil setup' first".yellow()
);
}
println!();
if ports.is_empty() {
println!("{}", "Troubleshooting:".bright_yellow().bold());
println!(" - Try a different USB cable (many are charge-only)");
println!(" - Try a different USB port");
#[cfg(target_os = "linux")]
{
println!(" - Check kernel log: dmesg | tail -20");
println!(" - Check USB bus: lsusb | grep -i -E 'ch34|arduino|1a86|2341'");
}
println!();
}
Ok(())
}

237
src/commands/doctor.rs Normal file
View File

@@ -0,0 +1,237 @@
use anyhow::Result;
use colored::*;
use crate::board;
#[derive(Debug)]
pub struct SystemHealth {
pub arduino_cli_ok: bool,
pub arduino_cli_path: Option<String>,
pub avr_core_ok: bool,
pub avr_size_ok: bool,
pub dialout_ok: bool,
pub cmake_ok: bool,
pub cpp_compiler_ok: bool,
pub git_ok: bool,
pub ports_found: usize,
}
impl SystemHealth {
pub fn is_healthy(&self) -> bool {
self.arduino_cli_ok && self.avr_core_ok
}
}
pub fn run_diagnostics() -> Result<()> {
println!(
"{}",
"Checking system health...".bright_yellow().bold()
);
println!();
let health = check_system_health();
print_diagnostics(&health);
println!();
if health.is_healthy() {
println!(
"{}",
"System is ready for Arduino development."
.bright_green()
.bold()
);
} else {
println!(
"{}",
"Issues found. Run 'anvil setup' to fix."
.bright_yellow()
.bold()
);
}
println!();
Ok(())
}
pub fn check_system_health() -> SystemHealth {
// arduino-cli
let (arduino_cli_ok, arduino_cli_path) = match board::find_arduino_cli() {
Some(path) => (true, Some(path.display().to_string())),
None => (false, None),
};
// AVR core
let avr_core_ok = if let Some(ref path_str) = arduino_cli_path {
let path = std::path::Path::new(path_str);
board::is_avr_core_installed(path)
} else {
false
};
// avr-size (optional)
let avr_size_ok = which::which("avr-size").is_ok();
// dialout group (Linux only)
let dialout_ok = check_dialout();
// cmake (optional -- for host tests)
let cmake_ok = which::which("cmake").is_ok();
// C++ compiler (optional -- for host tests)
let cpp_compiler_ok = which::which("g++").is_ok() || which::which("clang++").is_ok();
// git
let git_ok = which::which("git").is_ok();
// Serial ports
let ports_found = board::list_ports().len();
SystemHealth {
arduino_cli_ok,
arduino_cli_path,
avr_core_ok,
avr_size_ok,
dialout_ok,
cmake_ok,
cpp_compiler_ok,
git_ok,
ports_found,
}
}
fn print_diagnostics(health: &SystemHealth) {
println!("{}", "Required:".bright_yellow().bold());
println!();
// arduino-cli
if health.arduino_cli_ok {
println!(
" {} arduino-cli {}",
"ok".green(),
health
.arduino_cli_path
.as_ref()
.unwrap_or(&String::new())
.bright_black()
);
} else {
println!(" {} arduino-cli {}", "MISSING".red(), "not found in PATH".red());
}
// AVR core
if health.avr_core_ok {
println!(" {} arduino:avr core installed", "ok".green());
} else if health.arduino_cli_ok {
println!(
" {} arduino:avr core {}",
"MISSING".red(),
"run: anvil setup".red()
);
} else {
println!(
" {} arduino:avr core {}",
"MISSING".red(),
"(needs arduino-cli first)".bright_black()
);
}
println!();
println!("{}", "Optional:".bright_yellow().bold());
println!();
// avr-size
if health.avr_size_ok {
println!(" {} avr-size (binary size reporting)", "ok".green());
} else {
println!(
" {} avr-size {}",
"--".bright_black(),
"install: sudo apt install gcc-avr".bright_black()
);
}
// dialout
#[cfg(unix)]
{
if health.dialout_ok {
println!(" {} user in dialout group", "ok".green());
} else {
println!(
" {} dialout group {}",
"WARN".yellow(),
"run: sudo usermod -aG dialout $USER".yellow()
);
}
}
// cmake
if health.cmake_ok {
println!(" {} cmake (for host-side tests)", "ok".green());
} else {
println!(
" {} cmake {}",
"--".bright_black(),
"install: sudo apt install cmake".bright_black()
);
}
// C++ compiler
if health.cpp_compiler_ok {
println!(" {} C++ compiler (g++/clang++)", "ok".green());
} else {
println!(
" {} C++ compiler {}",
"--".bright_black(),
"install: sudo apt install g++".bright_black()
);
}
// git
if health.git_ok {
println!(" {} git", "ok".green());
} else {
println!(
" {} git {}",
"--".bright_black(),
"install: sudo apt install git".bright_black()
);
}
println!();
println!("{}", "Hardware:".bright_yellow().bold());
println!();
if health.ports_found > 0 {
println!(
" {} {} serial port(s) detected",
"ok".green(),
health.ports_found
);
} else {
println!(
" {} no serial ports {}",
"--".bright_black(),
"(plug in a board to detect)".bright_black()
);
}
}
fn check_dialout() -> bool {
#[cfg(unix)]
{
let output = std::process::Command::new("groups")
.output();
match output {
Ok(out) => {
let groups = String::from_utf8_lossy(&out.stdout);
groups.contains("dialout")
}
Err(_) => false,
}
}
#[cfg(not(unix))]
{
true // Not applicable on Windows
}
}

6
src/commands/mod.rs Normal file
View File

@@ -0,0 +1,6 @@
pub mod new;
pub mod doctor;
pub mod setup;
pub mod devices;
pub mod build;
pub mod monitor;

167
src/commands/monitor.rs Normal file
View File

@@ -0,0 +1,167 @@
use anyhow::{Result, Context};
use colored::*;
use std::process::Command;
use std::time::Duration;
use std::thread;
use crate::board;
const DEFAULT_BAUD: u32 = 115200;
pub fn run_monitor(
port: Option<&str>,
baud: Option<u32>,
watch: bool,
) -> Result<()> {
let cli = board::find_arduino_cli()
.context("arduino-cli not found. Run: anvil setup")?;
let baud = baud.unwrap_or(DEFAULT_BAUD);
if watch {
run_watch(&cli, port, baud)
} else {
run_single(&cli, port, baud)
}
}
/// Open serial monitor once.
fn run_single(
cli: &std::path::Path,
port: Option<&str>,
baud: u32,
) -> Result<()> {
let port = match port {
Some(p) => p.to_string(),
None => board::auto_detect_port()?,
};
println!(
"Opening serial monitor on {} at {} baud...",
port.bright_white().bold(),
baud
);
println!("Press Ctrl+C to exit.");
println!();
let status = Command::new(cli)
.args([
"monitor",
"-p", &port,
"-c", &format!("baudrate={}", baud),
])
.status()
.context("Failed to start serial monitor")?;
if !status.success() {
anyhow::bail!("Serial monitor exited with error.");
}
Ok(())
}
/// Persistent watch mode: reconnect after upload/reset/replug.
fn run_watch(
cli: &std::path::Path,
port_hint: Option<&str>,
baud: u32,
) -> Result<()> {
let port = match port_hint {
Some(p) => p.to_string(),
None => {
match board::auto_detect_port() {
Ok(p) => p,
Err(_) => {
let default = default_port();
println!(
"No port detected yet. Waiting for {}...",
default
);
default
}
}
}
};
println!(
"Persistent monitor on {} at {} baud",
port.bright_white().bold(),
baud
);
println!("Reconnects automatically after upload / reset / replug.");
println!("Press Ctrl+C to exit.");
println!();
let running = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
let r = running.clone();
let _ = ctrlc::set_handler(move || {
r.store(false, std::sync::atomic::Ordering::Relaxed);
});
while running.load(std::sync::atomic::Ordering::Relaxed) {
if !port_exists(&port) {
println!(
"{}",
format!("--- Waiting for {} ...", port).bright_black()
);
while !port_exists(&port)
&& running.load(std::sync::atomic::Ordering::Relaxed)
{
thread::sleep(Duration::from_millis(500));
}
if !running.load(std::sync::atomic::Ordering::Relaxed) {
break;
}
// Settle time
thread::sleep(Duration::from_secs(1));
println!("{}", format!("--- {} connected ---", port).green());
}
let _ = Command::new(cli.as_os_str())
.args([
"monitor",
"-p", &port,
"-c", &format!("baudrate={}", baud),
])
.status();
if !running.load(std::sync::atomic::Ordering::Relaxed) {
break;
}
println!(
"{}",
format!("--- {} disconnected ---", port).yellow()
);
thread::sleep(Duration::from_millis(500));
}
println!();
println!("Monitor stopped.");
Ok(())
}
fn port_exists(port: &str) -> bool {
#[cfg(unix)]
{
std::path::Path::new(port).exists()
}
#[cfg(windows)]
{
// On Windows, check if the port appears in current device list
board::list_ports()
.iter()
.any(|p| p.port_name == port)
}
}
fn default_port() -> String {
if cfg!(target_os = "windows") {
"COM3".to_string()
} else {
"/dev/ttyUSB0".to_string()
}
}

252
src/commands/new.rs Normal file
View File

@@ -0,0 +1,252 @@
use anyhow::{Result, bail};
use colored::*;
use std::path::PathBuf;
use crate::templates::{TemplateManager, TemplateContext};
use crate::version::ANVIL_VERSION;
pub fn list_templates() -> Result<()> {
println!("{}", "Available templates:".bright_cyan().bold());
println!();
for info in TemplateManager::list_templates() {
let marker = if info.is_default { " (default)" } else { "" };
println!(
" {}{}",
info.name.bright_white().bold(),
marker.bright_cyan()
);
println!(" {}", info.description);
println!();
}
println!("{}", "Usage:".bright_yellow().bold());
println!(" anvil new <project-name>");
println!(" anvil new <project-name> --template basic");
println!();
Ok(())
}
pub fn create_project(name: &str, template: Option<&str>) -> Result<()> {
// Validate project name
validate_project_name(name)?;
let project_path = PathBuf::from(name);
if project_path.exists() {
bail!(
"Directory already exists: {}\n\
Choose a different name or remove the existing directory.",
project_path.display()
);
}
let template_name = template.unwrap_or("basic");
if !TemplateManager::template_exists(template_name) {
println!(
"{}",
format!("Template '{}' not found.", template_name).red().bold()
);
println!();
list_templates()?;
bail!("Invalid template");
}
println!(
"{}",
format!("Creating Arduino project: {}", name)
.bright_green()
.bold()
);
println!("{}", format!("Template: {}", template_name).bright_cyan());
println!();
// Create project directory
std::fs::create_dir_all(&project_path)?;
// Extract template
println!("{}", "Extracting template files...".bright_yellow());
let context = TemplateContext {
project_name: name.to_string(),
anvil_version: ANVIL_VERSION.to_string(),
};
let file_count = TemplateManager::extract(template_name, &project_path, &context)?;
println!("{} Extracted {} files", "ok".green(), file_count);
// Make shell scripts executable on Unix
#[cfg(unix)]
{
make_executable(&project_path);
}
// Initialize git repository
init_git(&project_path, template_name);
// Print success
println!();
println!(
"{}",
"================================================================"
.bright_green()
);
println!(
"{}",
format!(" Project created: {}", name)
.bright_green()
.bold()
);
println!(
"{}",
"================================================================"
.bright_green()
);
println!();
print_next_steps(name);
Ok(())
}
fn validate_project_name(name: &str) -> Result<()> {
if name.is_empty() {
bail!("Project name cannot be empty");
}
if name.len() > 50 {
bail!("Project name must be 50 characters or less");
}
if !name.chars().next().unwrap().is_alphabetic() {
bail!("Project name must start with a letter");
}
let valid = name
.chars()
.all(|c| c.is_alphanumeric() || c == '-' || c == '_');
if !valid {
bail!(
"Invalid project name '{}'\n\
Names may contain: letters, numbers, hyphens, underscores\n\
Must start with a letter.\n\n\
Valid examples:\n\
\x20 blink\n\
\x20 my-sensor\n\
\x20 team1234_robot",
name
);
}
Ok(())
}
fn init_git(project_dir: &PathBuf, template_name: &str) {
println!("{}", "Initializing git repository...".bright_yellow());
// Check if git is available
if which::which("git").is_err() {
eprintln!(
"{} git not found, skipping repository initialization.",
"warn".yellow()
);
return;
}
let run = |args: &[&str]| -> bool {
std::process::Command::new("git")
.args(args)
.current_dir(project_dir)
.output()
.map(|o| o.status.success())
.unwrap_or(false)
};
if !run(&["init"]) {
eprintln!("{} Failed to initialize git.", "warn".yellow());
return;
}
run(&["add", "."]);
let msg = format!(
"Initial commit from anvil new --template {}",
template_name
);
run(&["commit", "-m", &msg]);
println!("{} Git repository initialized", "ok".green());
}
#[cfg(unix)]
fn make_executable(project_dir: &PathBuf) {
use std::os::unix::fs::PermissionsExt;
let scripts = ["test/run_tests.sh"];
for script in &scripts {
let path = project_dir.join(script);
if path.exists() {
if let Ok(meta) = std::fs::metadata(&path) {
let mut perms = meta.permissions();
perms.set_mode(0o755);
let _ = std::fs::set_permissions(&path, perms);
}
}
}
}
fn print_next_steps(project_name: &str) {
println!("{}", "Next steps:".bright_yellow().bold());
println!(
" 1. {}",
format!("cd {}", project_name).bright_cyan()
);
println!(" 2. Check your system: {}", "anvil doctor".bright_cyan());
println!(
" 3. Find your board: {}",
"anvil devices".bright_cyan()
);
println!(
" 4. Build and upload: {}",
format!("anvil build {}", project_name).bright_cyan()
);
println!(
" 5. Build + monitor: {}",
format!("anvil build --monitor {}", project_name).bright_cyan()
);
println!();
println!(
" Run host tests: {}",
"cd test && ./run_tests.sh".bright_cyan()
);
println!();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validate_name_valid() {
assert!(validate_project_name("blink").is_ok());
assert!(validate_project_name("my-sensor").is_ok());
assert!(validate_project_name("team1234_robot").is_ok());
}
#[test]
fn test_validate_name_empty() {
assert!(validate_project_name("").is_err());
}
#[test]
fn test_validate_name_starts_with_number() {
assert!(validate_project_name("123abc").is_err());
}
#[test]
fn test_validate_name_special_chars() {
assert!(validate_project_name("my project").is_err());
assert!(validate_project_name("my.project").is_err());
}
#[test]
fn test_validate_name_too_long() {
let long_name = "a".repeat(51);
assert!(validate_project_name(&long_name).is_err());
}
}

163
src/commands/setup.rs Normal file
View File

@@ -0,0 +1,163 @@
use anyhow::Result;
use colored::*;
use std::process::Command;
use crate::board;
pub fn run_setup() -> Result<()> {
println!(
"{}",
"First-time setup".bright_yellow().bold()
);
println!();
// 1. Check for arduino-cli
println!("{}", "Checking for arduino-cli...".bright_yellow());
let cli_path = match board::find_arduino_cli() {
Some(path) => {
println!(" {} Found: {}", "ok".green(), path.display());
path
}
None => {
println!(" {} arduino-cli not found.", "MISSING".red());
println!();
print_install_instructions();
anyhow::bail!(
"Install arduino-cli first, then re-run: anvil setup"
);
}
};
println!();
// 2. Update board index
println!("{}", "Updating board index...".bright_yellow());
let status = Command::new(&cli_path)
.args(["core", "update-index"])
.status();
match status {
Ok(s) if s.success() => {
println!(" {} Board index updated.", "ok".green());
}
_ => {
eprintln!(
" {} Failed to update board index.",
"warn".yellow()
);
}
}
println!();
// 3. Install arduino:avr core
println!("{}", "Checking arduino:avr core...".bright_yellow());
if board::is_avr_core_installed(&cli_path) {
println!(" {} arduino:avr core already installed.", "ok".green());
} else {
println!(" Installing arduino:avr core (this may take a minute)...");
let status = Command::new(&cli_path)
.args(["core", "install", "arduino:avr"])
.status();
match status {
Ok(s) if s.success() => {
println!(" {} arduino:avr core installed.", "ok".green());
}
_ => {
eprintln!(
" {} Failed to install arduino:avr core.",
"FAIL".red()
);
}
}
}
println!();
// 4. Check optional tools
println!("{}", "Checking optional tools...".bright_yellow());
if which::which("avr-size").is_ok() {
println!(" {} avr-size (binary size reporting)", "ok".green());
} else {
println!(
" {} avr-size not found. Install for binary size details:",
"info".bright_black()
);
println!(" sudo apt install gcc-avr");
}
#[cfg(unix)]
{
let in_dialout = std::process::Command::new("groups")
.output()
.map(|o| String::from_utf8_lossy(&o.stdout).contains("dialout"))
.unwrap_or(false);
if in_dialout {
println!(" {} User in dialout group", "ok".green());
} else {
println!(
" {} Not in dialout group. Fix with:",
"warn".yellow()
);
println!(" sudo usermod -aG dialout $USER");
println!(" Then log out and back in.");
}
}
println!();
// 5. Scan for devices
println!("{}", "Scanning for boards...".bright_yellow());
let ports = board::list_ports();
board::print_port_details(&ports);
println!();
// Summary
println!(
"{}",
"================================================================"
.bright_green()
);
println!("{}", " Setup complete!".bright_green().bold());
println!(
"{}",
"================================================================"
.bright_green()
);
println!();
println!("{}", "Next steps:".bright_yellow().bold());
println!(" 1. Plug in your RedBoard");
println!(" 2. {}", "anvil devices".bright_cyan());
println!(" 3. {}", "anvil new blink".bright_cyan());
println!(
" 4. {}",
"cd blink && anvil build blink".bright_cyan()
);
println!();
Ok(())
}
fn print_install_instructions() {
println!("{}", "Install arduino-cli:".bright_yellow().bold());
println!();
if cfg!(target_os = "linux") {
println!(" curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh");
println!(" sudo mv bin/arduino-cli /usr/local/bin/");
println!();
println!(" Or via package manager:");
println!(" sudo apt install arduino-cli (Debian/Ubuntu)");
println!(" yay -S arduino-cli (Arch)");
} else if cfg!(target_os = "macos") {
println!(" brew install arduino-cli");
} else if cfg!(target_os = "windows") {
println!(" Download from: https://arduino.github.io/arduino-cli/installation/");
println!(" Or via Chocolatey:");
println!(" choco install arduino-cli");
println!(" Or via WinGet:");
println!(" winget install ArduinoSA.CLI");
}
println!();
println!(" Then re-run: anvil setup");
}

5
src/lib.rs Normal file
View File

@@ -0,0 +1,5 @@
pub mod version;
pub mod commands;
pub mod project;
pub mod board;
pub mod templates;

196
src/main.rs Normal file
View File

@@ -0,0 +1,196 @@
use clap::{Parser, Subcommand};
use colored::*;
use anyhow::Result;
use anvil::version::ANVIL_VERSION;
mod commands {
pub use anvil::commands::*;
}
#[derive(Parser)]
#[command(name = "anvil")]
#[command(author = "Eric Ratliff <eric@nxlearn.net>")]
#[command(version = ANVIL_VERSION)]
#[command(
about = "Arduino project generator and build tool - forges clean embedded projects",
long_about = None
)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Create a new Arduino project
New {
/// Project name
name: Option<String>,
/// Template to use (basic)
#[arg(long, short = 't', value_name = "TEMPLATE")]
template: Option<String>,
/// List available templates
#[arg(long, conflicts_with = "name")]
list_templates: bool,
},
/// Check system health and diagnose issues
Doctor,
/// Install arduino-cli and required cores
Setup,
/// List connected boards and serial ports
Devices,
/// Compile a sketch (and optionally upload)
Build {
/// Path to sketch directory
sketch: String,
/// Compile only -- do not upload
#[arg(long)]
verify: bool,
/// Open serial monitor after upload
#[arg(long)]
monitor: bool,
/// Delete cached build artifacts first
#[arg(long)]
clean: bool,
/// Show full compiler output
#[arg(long)]
verbose: bool,
/// Serial port (auto-detected if omitted)
#[arg(short, long)]
port: Option<String>,
/// Serial monitor baud rate
#[arg(short, long)]
baud: Option<u32>,
/// Override Fully Qualified Board Name
#[arg(long)]
fqbn: Option<String>,
},
/// Upload cached build artifacts (no recompile)
Upload {
/// Path to sketch directory
sketch: String,
/// Serial port (auto-detected if omitted)
#[arg(short, long)]
port: Option<String>,
/// Show full avrdude output
#[arg(long)]
verbose: bool,
/// Override Fully Qualified Board Name
#[arg(long)]
fqbn: Option<String>,
},
/// Open serial monitor
Monitor {
/// Serial port (auto-detected if omitted)
#[arg(short, long)]
port: Option<String>,
/// Baud rate (default: from project config or 115200)
#[arg(short, long)]
baud: Option<u32>,
/// Persistent mode: reconnect after upload/reset/replug
#[arg(long)]
watch: bool,
},
}
fn main() -> Result<()> {
#[cfg(windows)]
colored::control::set_virtual_terminal(true).ok();
let cli = Cli::parse();
print_banner();
match cli.command {
Commands::New { name, template, list_templates } => {
if list_templates {
commands::new::list_templates()
} else if let Some(project_name) = name {
commands::new::create_project(
&project_name,
template.as_deref(),
)
} else {
anyhow::bail!(
"Project name required.\n\
Usage: anvil new <name>\n\
List templates: anvil new --list-templates"
);
}
}
Commands::Doctor => {
commands::doctor::run_diagnostics()
}
Commands::Setup => {
commands::setup::run_setup()
}
Commands::Devices => {
commands::devices::scan_devices()
}
Commands::Build {
sketch, verify, monitor, clean, verbose,
port, baud, fqbn,
} => {
commands::build::run_build(
&sketch, verify, monitor, clean, verbose,
port.as_deref(), baud, fqbn.as_deref(),
)
}
Commands::Upload { sketch, port, verbose, fqbn } => {
commands::build::run_upload_only(
&sketch,
port.as_deref(),
verbose,
fqbn.as_deref(),
)
}
Commands::Monitor { port, baud, watch } => {
commands::monitor::run_monitor(
port.as_deref(),
baud,
watch,
)
}
}
}
fn print_banner() {
println!(
"{}",
"================================================================"
.bright_cyan()
);
println!(
"{}",
format!(" Anvil - Arduino Build Tool v{}", ANVIL_VERSION)
.bright_cyan()
.bold()
);
println!("{}", " Nexus Workshops LLC".bright_cyan());
println!(
"{}",
"================================================================"
.bright_cyan()
);
println!();
}

226
src/project/config.rs Normal file
View File

@@ -0,0 +1,226 @@
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::fs;
use anyhow::{Result, Context, bail};
use crate::version::ANVIL_VERSION;
pub const CONFIG_FILENAME: &str = ".anvil.toml";
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ProjectConfig {
pub project: ProjectMeta,
pub build: BuildConfig,
pub monitor: MonitorConfig,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ProjectMeta {
pub name: String,
pub anvil_version: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BuildConfig {
pub fqbn: String,
pub warnings: String,
pub include_dirs: Vec<String>,
pub extra_flags: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MonitorConfig {
pub baud: u32,
}
impl ProjectConfig {
/// Create a new project config with sensible defaults.
pub fn new(name: &str) -> Self {
Self {
project: ProjectMeta {
name: name.to_string(),
anvil_version: ANVIL_VERSION.to_string(),
},
build: BuildConfig {
fqbn: "arduino:avr:uno".to_string(),
warnings: "more".to_string(),
include_dirs: vec!["lib/hal".to_string(), "lib/app".to_string()],
extra_flags: vec!["-Werror".to_string()],
},
monitor: MonitorConfig {
baud: 115200,
},
}
}
/// Load config from a project directory.
pub fn load(project_root: &Path) -> Result<Self> {
let config_path = project_root.join(CONFIG_FILENAME);
if !config_path.exists() {
bail!(
"Not an Anvil project (missing {}).\n\
Create one with: anvil new <name>",
CONFIG_FILENAME
);
}
let contents = fs::read_to_string(&config_path)
.context(format!("Failed to read {}", config_path.display()))?;
let config: ProjectConfig = toml::from_str(&contents)
.context(format!("Failed to parse {}", config_path.display()))?;
Ok(config)
}
/// Save config to a project directory.
pub fn save(&self, project_root: &Path) -> Result<()> {
let config_path = project_root.join(CONFIG_FILENAME);
let contents = toml::to_string_pretty(self)
.context("Failed to serialize config")?;
fs::write(&config_path, contents)
.context(format!("Failed to write {}", config_path.display()))?;
Ok(())
}
/// Walk up from a directory to find the project root containing .anvil.toml.
pub fn find_project_root(start: &Path) -> Result<PathBuf> {
let mut dir = if start.is_absolute() {
start.to_path_buf()
} else {
std::env::current_dir()?.join(start)
};
for _ in 0..10 {
if dir.join(CONFIG_FILENAME).exists() {
return Ok(dir);
}
match dir.parent() {
Some(parent) => dir = parent.to_path_buf(),
None => break,
}
}
bail!(
"No {} found in {} or any parent directory.\n\
Create a project with: anvil new <name>",
CONFIG_FILENAME,
start.display()
);
}
/// Resolve include directories to absolute paths relative to project root.
pub fn resolve_include_flags(&self, project_root: &Path) -> Vec<String> {
let mut flags = Vec::new();
for dir in &self.build.include_dirs {
let abs = project_root.join(dir);
if abs.is_dir() {
flags.push(format!("-I{}", abs.display()));
}
}
flags
}
/// Build the full extra_flags string for arduino-cli.
pub fn extra_flags_string(&self, project_root: &Path) -> String {
let mut parts = self.resolve_include_flags(project_root);
for flag in &self.build.extra_flags {
parts.push(flag.clone());
}
parts.join(" ")
}
}
impl Default for ProjectConfig {
fn default() -> Self {
Self::new("untitled")
}
}
/// Return the Anvil home directory (~/.anvil).
pub fn anvil_home() -> Result<PathBuf> {
let home = dirs::home_dir()
.context("Could not determine home directory")?;
let anvil_dir = home.join(".anvil");
fs::create_dir_all(&anvil_dir)?;
Ok(anvil_dir)
}
/// Return the build cache directory (~/.anvil/builds).
pub fn build_cache_dir() -> Result<PathBuf> {
let dir = anvil_home()?.join("builds");
fs::create_dir_all(&dir)?;
Ok(dir)
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_new_config_defaults() {
let config = ProjectConfig::new("test_project");
assert_eq!(config.project.name, "test_project");
assert_eq!(config.build.fqbn, "arduino:avr:uno");
assert_eq!(config.monitor.baud, 115200);
assert!(config.build.include_dirs.contains(&"lib/hal".to_string()));
}
#[test]
fn test_save_and_load() {
let tmp = TempDir::new().unwrap();
let config = ProjectConfig::new("roundtrip");
config.save(tmp.path()).unwrap();
let loaded = ProjectConfig::load(tmp.path()).unwrap();
assert_eq!(loaded.project.name, "roundtrip");
assert_eq!(loaded.build.fqbn, config.build.fqbn);
assert_eq!(loaded.monitor.baud, config.monitor.baud);
}
#[test]
fn test_find_project_root() {
let tmp = TempDir::new().unwrap();
let config = ProjectConfig::new("finder");
config.save(tmp.path()).unwrap();
// Create a subdirectory and search from there
let sub = tmp.path().join("sketch").join("deep");
fs::create_dir_all(&sub).unwrap();
let found = ProjectConfig::find_project_root(&sub).unwrap();
assert_eq!(found, tmp.path());
}
#[test]
fn test_find_project_root_not_found() {
let tmp = TempDir::new().unwrap();
let result = ProjectConfig::find_project_root(tmp.path());
assert!(result.is_err());
}
#[test]
fn test_resolve_include_flags() {
let tmp = TempDir::new().unwrap();
fs::create_dir_all(tmp.path().join("lib/hal")).unwrap();
fs::create_dir_all(tmp.path().join("lib/app")).unwrap();
let config = ProjectConfig::new("includes");
let flags = config.resolve_include_flags(tmp.path());
assert_eq!(flags.len(), 2);
assert!(flags[0].starts_with("-I"));
assert!(flags[0].contains("lib"));
}
#[test]
fn test_extra_flags_string() {
let tmp = TempDir::new().unwrap();
fs::create_dir_all(tmp.path().join("lib/hal")).unwrap();
fs::create_dir_all(tmp.path().join("lib/app")).unwrap();
let config = ProjectConfig::new("flags");
let flags = config.extra_flags_string(tmp.path());
assert!(flags.contains("-Werror"));
assert!(flags.contains("-I"));
}
}

3
src/project/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod config;
pub use config::ProjectConfig;

234
src/templates/mod.rs Normal file
View File

@@ -0,0 +1,234 @@
use include_dir::{include_dir, Dir};
use std::path::Path;
use std::fs;
use anyhow::{Result, bail, Context};
use crate::version::ANVIL_VERSION;
static BASIC_TEMPLATE: Dir = include_dir!("$CARGO_MANIFEST_DIR/templates/basic");
pub struct TemplateContext {
pub project_name: String,
pub anvil_version: String,
}
pub struct TemplateManager;
impl TemplateManager {
pub fn template_exists(name: &str) -> bool {
matches!(name, "basic")
}
pub fn list_templates() -> Vec<TemplateInfo> {
vec![
TemplateInfo {
name: "basic".to_string(),
description: "Arduino project with HAL abstraction, mocks, and test infrastructure".to_string(),
is_default: true,
},
]
}
/// Extract a template into the output directory, applying variable
/// substitution and filename transformations.
pub fn extract(
template_name: &str,
output_dir: &Path,
context: &TemplateContext,
) -> Result<usize> {
let template_dir = match template_name {
"basic" => &BASIC_TEMPLATE,
_ => bail!("Unknown template: {}", template_name),
};
let count = extract_dir(template_dir, output_dir, "", context)?;
Ok(count)
}
}
pub struct TemplateInfo {
pub name: String,
pub description: String,
pub is_default: bool,
}
/// Recursively extract a directory from the embedded template.
fn extract_dir(
source: &Dir<'_>,
output_base: &Path,
relative_prefix: &str,
context: &TemplateContext,
) -> Result<usize> {
let mut count = 0;
for file in source.files() {
let file_path = file.path();
let file_name = file_path.to_string_lossy().to_string();
// Build the output path with transformations
let output_rel = transform_path(&file_name, &context.project_name);
let output_path = output_base.join(&output_rel);
// Create parent directories
if let Some(parent) = output_path.parent() {
fs::create_dir_all(parent)
.context(format!("Failed to create directory: {}", parent.display()))?;
}
// Read file contents
let contents = file.contents();
// Check if this is a template file (.tmpl suffix)
if output_rel.ends_with(".tmpl") {
// Variable substitution
let text = std::str::from_utf8(contents)
.context("Template file must be UTF-8")?;
let processed = substitute_variables(text, context);
// Remove .tmpl extension
let final_path_str = output_rel.trim_end_matches(".tmpl");
let final_path = output_base.join(final_path_str);
if let Some(parent) = final_path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&final_path, processed)?;
count += 1;
} else {
fs::write(&output_path, contents)?;
count += 1;
}
}
// Recurse into subdirectories
for dir in source.dirs() {
count += extract_dir(dir, output_base, relative_prefix, context)?;
}
Ok(count)
}
/// Transform template file paths:
/// - `_dot_` prefix -> `.` prefix (hidden files)
/// - `__name__` -> project name
fn transform_path(path: &str, project_name: &str) -> String {
let mut result = path.to_string();
// Replace __name__ with project name in all path components
result = result.replace("__name__", project_name);
// Handle _dot_ prefix for hidden files.
// Split into components and transform each.
let parts: Vec<&str> = result.split('/').collect();
let transformed: Vec<String> = parts
.iter()
.map(|part| {
if let Some(rest) = part.strip_prefix("_dot_") {
format!(".{}", rest)
} else {
part.to_string()
}
})
.collect();
transformed.join(std::path::MAIN_SEPARATOR_STR)
}
/// Simple variable substitution: replace {{VAR}} with values.
fn substitute_variables(text: &str, context: &TemplateContext) -> String {
text.replace("{{PROJECT_NAME}}", &context.project_name)
.replace("{{ANVIL_VERSION}}", &context.anvil_version)
.replace("{{ANVIL_VERSION_CURRENT}}", ANVIL_VERSION)
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_transform_path_dot_prefix() {
assert_eq!(
transform_path("_dot_gitignore", "blink"),
".gitignore"
);
assert_eq!(
transform_path("_dot_vscode/settings.json", "blink"),
format!(".vscode{}settings.json", std::path::MAIN_SEPARATOR)
);
}
#[test]
fn test_transform_path_name_substitution() {
assert_eq!(
transform_path("__name__/__name__.ino.tmpl", "blink"),
format!("blink{}blink.ino.tmpl", std::path::MAIN_SEPARATOR)
);
}
#[test]
fn test_substitute_variables() {
let ctx = TemplateContext {
project_name: "my_project".to_string(),
anvil_version: "1.0.0".to_string(),
};
let input = "Name: {{PROJECT_NAME}}, Version: {{ANVIL_VERSION}}";
let output = substitute_variables(input, &ctx);
assert_eq!(output, "Name: my_project, Version: 1.0.0");
}
#[test]
fn test_template_exists() {
assert!(TemplateManager::template_exists("basic"));
assert!(!TemplateManager::template_exists("nonexistent"));
}
#[test]
fn test_extract_basic_template() {
let tmp = TempDir::new().unwrap();
let ctx = TemplateContext {
project_name: "test_proj".to_string(),
anvil_version: "1.0.0".to_string(),
};
let count = TemplateManager::extract("basic", tmp.path(), &ctx).unwrap();
assert!(count > 0, "Should extract at least one file");
// Verify key files exist
assert!(
tmp.path().join(".anvil.toml").exists(),
".anvil.toml should be created"
);
assert!(
tmp.path().join("test_proj").join("test_proj.ino").exists(),
"Sketch .ino should be created"
);
assert!(
tmp.path().join("lib").join("hal").join("hal.h").exists(),
"HAL header should be created"
);
assert!(
tmp.path().join(".gitignore").exists(),
".gitignore should be created"
);
}
#[test]
fn test_extract_template_variable_substitution() {
let tmp = TempDir::new().unwrap();
let ctx = TemplateContext {
project_name: "my_sensor".to_string(),
anvil_version: "1.0.0".to_string(),
};
TemplateManager::extract("basic", tmp.path(), &ctx).unwrap();
// Read the generated .anvil.toml and check for project name
let config_content = fs::read_to_string(tmp.path().join(".anvil.toml")).unwrap();
assert!(
config_content.contains("my_sensor"),
".anvil.toml should contain project name"
);
}
}

1
src/version.rs Normal file
View File

@@ -0,0 +1 @@
pub const ANVIL_VERSION: &str = env!("CARGO_PKG_VERSION");