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:
299
src/commands/build.rs
Normal file
299
src/commands/build.rs
Normal 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
67
src/commands/devices.rs
Normal 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
237
src/commands/doctor.rs
Normal 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
6
src/commands/mod.rs
Normal 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
167
src/commands/monitor.rs
Normal 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
252
src/commands/new.rs
Normal 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
163
src/commands/setup.rs
Normal 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");
|
||||
}
|
||||
Reference in New Issue
Block a user