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:
408
src/board/mod.rs
408
src/board/mod.rs
@@ -11,6 +11,40 @@ pub struct PortInfo {
|
||||
pub protocol: String,
|
||||
pub board_name: String,
|
||||
pub fqbn: String,
|
||||
pub vid: String,
|
||||
pub pid: String,
|
||||
pub serial_number: String,
|
||||
}
|
||||
|
||||
impl PortInfo {
|
||||
/// Returns true if this looks like a USB serial port rather than a
|
||||
/// legacy motherboard COM port.
|
||||
pub fn is_usb(&self) -> bool {
|
||||
// Has USB identifiers
|
||||
if !self.vid.is_empty() && !self.pid.is_empty() {
|
||||
return true;
|
||||
}
|
||||
// arduino-cli labels USB ports "Serial Port (USB)"
|
||||
if self.protocol.contains("USB") {
|
||||
return true;
|
||||
}
|
||||
// Unix: ttyUSB* and ttyACM* are always USB
|
||||
if self.port_name.contains("ttyUSB") || self.port_name.contains("ttyACM") {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Returns VID:PID string like "2341:0043", or empty if unknown.
|
||||
pub fn vid_pid(&self) -> String {
|
||||
if self.vid.is_empty() || self.pid.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
// Normalize: strip 0x prefix, lowercase, 4-digit padded
|
||||
let vid = self.vid.trim_start_matches("0x").trim_start_matches("0X");
|
||||
let pid = self.pid.trim_start_matches("0x").trim_start_matches("0X");
|
||||
format!("{}:{}", vid.to_lowercase(), pid.to_lowercase())
|
||||
}
|
||||
}
|
||||
|
||||
/// JSON schema for `arduino-cli board list --format json`
|
||||
@@ -90,11 +124,34 @@ fn list_ports_via_cli(cli: &Path) -> Result<Vec<PortInfo>> {
|
||||
_ => ("Unknown".to_string(), String::new()),
|
||||
};
|
||||
|
||||
// Extract VID, PID, serial number from properties
|
||||
let (vid, pid, serial_number) = match &port.properties {
|
||||
Some(props) => {
|
||||
let v = props.get("vid")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let p = props.get("pid")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let sn = props.get("serialNumber")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
(v, p, sn)
|
||||
}
|
||||
None => (String::new(), String::new(), String::new()),
|
||||
};
|
||||
|
||||
result.push(PortInfo {
|
||||
port_name: port.address,
|
||||
protocol: port.protocol_label,
|
||||
board_name,
|
||||
fqbn,
|
||||
vid,
|
||||
pid,
|
||||
serial_number,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -123,6 +180,9 @@ fn list_ports_fallback() -> Vec<PortInfo> {
|
||||
protocol: "serial".to_string(),
|
||||
board_name: board.to_string(),
|
||||
fqbn: String::new(),
|
||||
vid: String::new(),
|
||||
pid: String::new(),
|
||||
serial_number: String::new(),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -149,6 +209,9 @@ fn list_ports_fallback() -> Vec<PortInfo> {
|
||||
protocol: "serial".to_string(),
|
||||
board_name: "Detected via WMI".to_string(),
|
||||
fqbn: String::new(),
|
||||
vid: String::new(),
|
||||
pid: String::new(),
|
||||
serial_number: String::new(),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -160,6 +223,50 @@ fn list_ports_fallback() -> Vec<PortInfo> {
|
||||
result
|
||||
}
|
||||
|
||||
/// Find a port by VID:PID. Returns the port name if found.
|
||||
pub fn resolve_vid_pid(vid_pid: &str) -> Option<String> {
|
||||
let ports = list_ports();
|
||||
let needle = vid_pid.to_lowercase();
|
||||
|
||||
for p in &ports {
|
||||
if p.vid_pid() == needle {
|
||||
return Some(p.port_name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Pick the best default port from a list. Returns the index into the
|
||||
/// slice, or None if the list is empty.
|
||||
///
|
||||
/// Priority:
|
||||
/// 1. A port with a recognized board (non-empty FQBN)
|
||||
/// 2. A USB serial port (skip legacy motherboard COM ports)
|
||||
/// 3. First port in the list
|
||||
pub fn pick_default_port(ports: &[PortInfo]) -> Option<usize> {
|
||||
if ports.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Prefer a port with a recognized FQBN
|
||||
for (i, p) in ports.iter().enumerate() {
|
||||
if !p.fqbn.is_empty() {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Prefer a USB port over a legacy serial port
|
||||
for (i, p) in ports.iter().enumerate() {
|
||||
if p.is_usb() {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to the first port
|
||||
Some(0)
|
||||
}
|
||||
|
||||
/// Auto-detect a single serial port.
|
||||
pub fn auto_detect_port() -> Result<String> {
|
||||
let ports = list_ports();
|
||||
@@ -171,35 +278,23 @@ pub fn auto_detect_port() -> Result<String> {
|
||||
);
|
||||
}
|
||||
|
||||
if ports.len() == 1 {
|
||||
return Ok(ports[0].port_name.clone());
|
||||
}
|
||||
let idx = pick_default_port(&ports).unwrap_or(0);
|
||||
|
||||
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());
|
||||
if ports.len() > 1 {
|
||||
eprintln!("{}", "Multiple serial ports detected:".yellow());
|
||||
for p in &ports {
|
||||
eprintln!(" {} ({})", p.port_name, p.board_name);
|
||||
}
|
||||
eprintln!(
|
||||
"{}",
|
||||
format!(
|
||||
"Auto-selected {}. Use -p to override.",
|
||||
ports[idx].port_name
|
||||
).yellow()
|
||||
);
|
||||
}
|
||||
|
||||
let selected = ports[0].port_name.clone();
|
||||
eprintln!(
|
||||
"{}",
|
||||
format!("Auto-selected {}. Use -p to override.", selected).yellow()
|
||||
);
|
||||
Ok(selected)
|
||||
Ok(ports[idx].port_name.clone())
|
||||
}
|
||||
|
||||
/// Print detailed port information.
|
||||
@@ -219,8 +314,18 @@ pub fn print_port_details(ports: &[PortInfo]) {
|
||||
return;
|
||||
}
|
||||
|
||||
for port in ports {
|
||||
println!(" {}", port.port_name.green().bold());
|
||||
let default_idx = pick_default_port(ports);
|
||||
|
||||
for (i, port) in ports.iter().enumerate() {
|
||||
let is_default = default_idx == Some(i);
|
||||
|
||||
if is_default {
|
||||
print!(" {}", port.port_name.green().bold());
|
||||
println!(" {}", "<-- default".bright_green());
|
||||
} else {
|
||||
println!(" {}", port.port_name.green().bold());
|
||||
}
|
||||
|
||||
println!(" Board: {}", port.board_name);
|
||||
if !port.fqbn.is_empty() {
|
||||
println!(" FQBN: {}", port.fqbn);
|
||||
@@ -229,6 +334,14 @@ pub fn print_port_details(ports: &[PortInfo]) {
|
||||
println!(" Protocol: {}", port.protocol);
|
||||
}
|
||||
|
||||
let vp = port.vid_pid();
|
||||
if !vp.is_empty() {
|
||||
println!(" VID:PID: {}", vp.bright_cyan());
|
||||
}
|
||||
if !port.serial_number.is_empty() {
|
||||
println!(" Serial: {}", port.serial_number.bright_black());
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::fs::OpenOptions;
|
||||
@@ -254,6 +367,85 @@ pub fn print_port_details(ports: &[PortInfo]) {
|
||||
|
||||
println!();
|
||||
}
|
||||
|
||||
// Show help section
|
||||
if let Some(idx) = default_idx {
|
||||
if ports.len() > 1 {
|
||||
println!(
|
||||
" Anvil will use {} when no port is specified.",
|
||||
ports[idx].port_name.bright_white()
|
||||
);
|
||||
println!();
|
||||
}
|
||||
|
||||
println!(
|
||||
" {}",
|
||||
"Save a default port for your project:".bright_white()
|
||||
);
|
||||
println!();
|
||||
println!(
|
||||
" {}",
|
||||
format!("anvil devices --set {}", ports[idx].port_name).bright_cyan(),
|
||||
);
|
||||
println!(
|
||||
" {}",
|
||||
"anvil devices --set".bright_cyan(),
|
||||
);
|
||||
println!();
|
||||
println!(
|
||||
" {}",
|
||||
"Both forms save the port AND VID:PID to .anvil.local automatically.".bright_black()
|
||||
);
|
||||
println!(
|
||||
" {}",
|
||||
"Use --get to see what's saved, --set to change it.".bright_black()
|
||||
);
|
||||
println!();
|
||||
|
||||
// Show VID:PID explanation if any USB devices are present
|
||||
let has_usb = ports.iter().any(|p| !p.vid_pid().is_empty());
|
||||
if has_usb {
|
||||
println!(
|
||||
" {}",
|
||||
"VID:PID identifies the USB device, not the port number.".bright_white()
|
||||
);
|
||||
println!(
|
||||
" {}",
|
||||
"If the device moves to a different port after replug,".bright_black()
|
||||
);
|
||||
println!(
|
||||
" {}",
|
||||
"Anvil will find it automatically by VID:PID.".bright_black()
|
||||
);
|
||||
println!();
|
||||
}
|
||||
|
||||
println!(
|
||||
" {}",
|
||||
"Port resolution priority:".bright_white()
|
||||
);
|
||||
println!(
|
||||
" {} {}",
|
||||
"1.".bright_white(),
|
||||
"-p flag (upload.bat -p COM3)".bright_black()
|
||||
);
|
||||
println!(
|
||||
" {} {}",
|
||||
"2.".bright_white(),
|
||||
"VID:PID from .anvil.local (tracks device across port changes)".bright_black()
|
||||
);
|
||||
println!(
|
||||
" {} {}",
|
||||
"3.".bright_white(),
|
||||
"Saved port from .anvil.local".bright_black()
|
||||
);
|
||||
println!(
|
||||
" {} {}",
|
||||
"4.".bright_white(),
|
||||
"Auto-detect (prefers USB over legacy COM)".bright_black()
|
||||
);
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
/// Find arduino-cli in PATH or in ~/.anvil/bin.
|
||||
@@ -308,11 +500,70 @@ mod tests {
|
||||
protocol: "serial".to_string(),
|
||||
board_name: "Test".to_string(),
|
||||
fqbn: "arduino:avr:uno".to_string(),
|
||||
vid: "0x2341".to_string(),
|
||||
pid: "0x0043".to_string(),
|
||||
serial_number: String::new(),
|
||||
};
|
||||
let cloned = info.clone();
|
||||
assert_eq!(cloned.port_name, info.port_name);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vid_pid_formatting() {
|
||||
let info = PortInfo {
|
||||
port_name: "COM3".to_string(),
|
||||
protocol: "Serial Port (USB)".to_string(),
|
||||
board_name: "Arduino Uno".to_string(),
|
||||
fqbn: "arduino:avr:uno".to_string(),
|
||||
vid: "0x2341".to_string(),
|
||||
pid: "0x0043".to_string(),
|
||||
serial_number: String::new(),
|
||||
};
|
||||
assert_eq!(info.vid_pid(), "2341:0043");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vid_pid_empty() {
|
||||
let info = PortInfo {
|
||||
port_name: "COM1".to_string(),
|
||||
protocol: "Serial Port".to_string(),
|
||||
board_name: "Unknown".to_string(),
|
||||
fqbn: String::new(),
|
||||
vid: String::new(),
|
||||
pid: String::new(),
|
||||
serial_number: String::new(),
|
||||
};
|
||||
assert_eq!(info.vid_pid(), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vid_pid_no_prefix() {
|
||||
let info = PortInfo {
|
||||
port_name: "COM3".to_string(),
|
||||
protocol: "Serial Port (USB)".to_string(),
|
||||
board_name: "Unknown".to_string(),
|
||||
fqbn: String::new(),
|
||||
vid: "2341".to_string(),
|
||||
pid: "0043".to_string(),
|
||||
serial_number: String::new(),
|
||||
};
|
||||
assert_eq!(info.vid_pid(), "2341:0043");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_usb_from_vid_pid() {
|
||||
let info = PortInfo {
|
||||
port_name: "COM5".to_string(),
|
||||
protocol: "Serial Port".to_string(),
|
||||
board_name: "Unknown".to_string(),
|
||||
fqbn: String::new(),
|
||||
vid: "1a86".to_string(),
|
||||
pid: "7523".to_string(),
|
||||
serial_number: String::new(),
|
||||
};
|
||||
assert!(info.is_usb());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_empty_board_list() {
|
||||
let json = r#"{"detected_ports": []}"#;
|
||||
@@ -327,7 +578,12 @@ mod tests {
|
||||
"port": {
|
||||
"address": "/dev/ttyUSB0",
|
||||
"protocol": "serial",
|
||||
"protocol_label": "Serial Port (USB)"
|
||||
"protocol_label": "Serial Port (USB)",
|
||||
"properties": {
|
||||
"vid": "0x2341",
|
||||
"pid": "0x0043",
|
||||
"serialNumber": "ABC123"
|
||||
}
|
||||
},
|
||||
"matching_boards": [{
|
||||
"name": "Arduino Uno",
|
||||
@@ -338,8 +594,96 @@ mod tests {
|
||||
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");
|
||||
let port = dp.port.as_ref().unwrap();
|
||||
assert_eq!(port.address, "/dev/ttyUSB0");
|
||||
let props = port.properties.as_ref().unwrap();
|
||||
assert_eq!(props["vid"].as_str().unwrap(), "0x2341");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_usb_from_protocol_label() {
|
||||
let usb = PortInfo {
|
||||
port_name: "COM3".to_string(),
|
||||
protocol: "Serial Port (USB)".to_string(),
|
||||
board_name: "Unknown".to_string(),
|
||||
fqbn: String::new(),
|
||||
vid: String::new(),
|
||||
pid: String::new(),
|
||||
serial_number: String::new(),
|
||||
};
|
||||
assert!(usb.is_usb());
|
||||
|
||||
let legacy = PortInfo {
|
||||
port_name: "COM1".to_string(),
|
||||
protocol: "Serial Port".to_string(),
|
||||
board_name: "Unknown".to_string(),
|
||||
fqbn: String::new(),
|
||||
vid: String::new(),
|
||||
pid: String::new(),
|
||||
serial_number: String::new(),
|
||||
};
|
||||
assert!(!legacy.is_usb());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pick_default_prefers_fqbn() {
|
||||
let ports = vec![
|
||||
PortInfo {
|
||||
port_name: "COM1".to_string(),
|
||||
protocol: "Serial Port".to_string(),
|
||||
board_name: "Unknown".to_string(),
|
||||
fqbn: String::new(),
|
||||
vid: String::new(), pid: String::new(), serial_number: String::new(),
|
||||
},
|
||||
PortInfo {
|
||||
port_name: "COM3".to_string(),
|
||||
protocol: "Serial Port (USB)".to_string(),
|
||||
board_name: "Arduino Uno".to_string(),
|
||||
fqbn: "arduino:avr:uno".to_string(),
|
||||
vid: "0x2341".to_string(), pid: "0x0043".to_string(), serial_number: String::new(),
|
||||
},
|
||||
];
|
||||
assert_eq!(pick_default_port(&ports), Some(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pick_default_prefers_usb_over_legacy() {
|
||||
let ports = vec![
|
||||
PortInfo {
|
||||
port_name: "COM1".to_string(),
|
||||
protocol: "Serial Port".to_string(),
|
||||
board_name: "Unknown".to_string(),
|
||||
fqbn: String::new(),
|
||||
vid: String::new(), pid: String::new(), serial_number: String::new(),
|
||||
},
|
||||
PortInfo {
|
||||
port_name: "COM3".to_string(),
|
||||
protocol: "Serial Port (USB)".to_string(),
|
||||
board_name: "Unknown".to_string(),
|
||||
fqbn: String::new(),
|
||||
vid: "1a86".to_string(), pid: "7523".to_string(), serial_number: String::new(),
|
||||
},
|
||||
];
|
||||
assert_eq!(pick_default_port(&ports), Some(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pick_default_empty() {
|
||||
let ports: Vec<PortInfo> = vec![];
|
||||
assert_eq!(pick_default_port(&ports), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pick_default_single() {
|
||||
let ports = vec![
|
||||
PortInfo {
|
||||
port_name: "COM1".to_string(),
|
||||
protocol: "Serial Port".to_string(),
|
||||
board_name: "Unknown".to_string(),
|
||||
fqbn: String::new(),
|
||||
vid: String::new(), pid: String::new(), serial_number: String::new(),
|
||||
},
|
||||
];
|
||||
assert_eq!(pick_default_port(&ports), Some(0));
|
||||
}
|
||||
}
|
||||
@@ -1,299 +0,0 @@
|
||||
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!();
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
use anyhow::Result;
|
||||
use anyhow::{Result, Context};
|
||||
use colored::*;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::fs;
|
||||
|
||||
use crate::board;
|
||||
|
||||
@@ -60,8 +62,261 @@ pub fn scan_devices() -> Result<()> {
|
||||
println!(" - Check kernel log: dmesg | tail -20");
|
||||
println!(" - Check USB bus: lsusb | grep -i -E 'ch34|arduino|1a86|2341'");
|
||||
}
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
println!(" - Open Device Manager and check Ports (COM & LPT)");
|
||||
println!(" - Install CH340 driver if needed: https://www.wch-ic.com/downloads/CH341SER_EXE.html");
|
||||
println!(" - Check if the board appears under \"Other devices\" with a warning icon");
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Read and display the saved port from .anvil.local.
|
||||
pub fn get_port(project_dir: Option<&str>) -> Result<()> {
|
||||
let project_path = resolve_project_dir(project_dir)?;
|
||||
require_anvil_project(&project_path)?;
|
||||
|
||||
let local_file = project_path.join(".anvil.local");
|
||||
if !local_file.exists() {
|
||||
println!(
|
||||
"{} No saved port (no .anvil.local file).",
|
||||
"--".bright_black()
|
||||
);
|
||||
println!();
|
||||
println!(" To save a default port for this machine, run:");
|
||||
println!();
|
||||
println!(" {} {}", "anvil devices --set".bright_cyan(),
|
||||
"auto-detect and save".bright_black());
|
||||
println!(" {} {}",
|
||||
"anvil devices --set COM3".bright_cyan(),
|
||||
"save a specific port".bright_black());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let (saved_port, saved_vid_pid) = read_anvil_local(&local_file)?;
|
||||
|
||||
if saved_port.is_empty() && saved_vid_pid.is_empty() {
|
||||
println!(
|
||||
"{} .anvil.local exists but no port is set.",
|
||||
"--".bright_black()
|
||||
);
|
||||
println!();
|
||||
println!(" To save a default port, run:");
|
||||
println!();
|
||||
println!(" {}", "anvil devices --set COM3".bright_cyan());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Try to resolve VID:PID to current port
|
||||
if !saved_vid_pid.is_empty() {
|
||||
match board::resolve_vid_pid(&saved_vid_pid) {
|
||||
Some(current_port) => {
|
||||
println!(
|
||||
"{} Device {} is on {}",
|
||||
"ok".green(),
|
||||
saved_vid_pid.bright_cyan(),
|
||||
current_port.bright_white().bold()
|
||||
);
|
||||
if !saved_port.is_empty() && saved_port != current_port {
|
||||
println!(
|
||||
" {}",
|
||||
format!(
|
||||
"Note: saved port was {}, device has moved",
|
||||
saved_port
|
||||
).bright_yellow()
|
||||
);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
println!(
|
||||
"{} Device {} is not connected",
|
||||
"!!".bright_red(),
|
||||
saved_vid_pid.bright_cyan()
|
||||
);
|
||||
if !saved_port.is_empty() {
|
||||
println!(
|
||||
" Last known port: {}",
|
||||
saved_port.bright_black()
|
||||
);
|
||||
}
|
||||
println!();
|
||||
println!(" Is the board plugged in?");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!(
|
||||
"{} Saved port: {}",
|
||||
"ok".green(),
|
||||
saved_port.bright_white().bold()
|
||||
);
|
||||
println!(
|
||||
" {}",
|
||||
"No VID:PID saved -- port won't track if reassigned.".bright_black()
|
||||
);
|
||||
println!(
|
||||
" {}",
|
||||
"Re-run 'anvil devices --set' to save the device identity.".bright_black()
|
||||
);
|
||||
}
|
||||
|
||||
println!();
|
||||
println!(
|
||||
" {}",
|
||||
format!("Source: {}", local_file.display()).bright_black()
|
||||
);
|
||||
println!(" To change: {}", "anvil devices --set <PORT>".bright_cyan());
|
||||
println!(" To remove: {}", "delete .anvil.local".bright_cyan());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write a port to .anvil.local in the given project directory.
|
||||
pub fn set_port(port: Option<&str>, project_dir: Option<&str>) -> Result<()> {
|
||||
let project_path = resolve_project_dir(project_dir)?;
|
||||
require_anvil_project(&project_path)?;
|
||||
|
||||
// Resolve the port and find its VID:PID
|
||||
let ports = board::list_ports();
|
||||
|
||||
let (resolved_port, vid_pid) = match port {
|
||||
Some(p) => {
|
||||
// User specified a port -- find it in the list to get VID:PID
|
||||
let port_name = if cfg!(target_os = "windows") {
|
||||
p.to_uppercase()
|
||||
} else {
|
||||
p.to_string()
|
||||
};
|
||||
|
||||
let vp = ports.iter()
|
||||
.find(|pi| pi.port_name.eq_ignore_ascii_case(&port_name))
|
||||
.map(|pi| pi.vid_pid())
|
||||
.unwrap_or_default();
|
||||
|
||||
(port_name, vp)
|
||||
}
|
||||
None => {
|
||||
// Auto-detect the best port
|
||||
println!("Detecting best port...");
|
||||
println!();
|
||||
|
||||
if ports.is_empty() {
|
||||
anyhow::bail!(
|
||||
"No serial ports detected. Is the board plugged in?\n \
|
||||
Specify a port explicitly: anvil devices --set COM3"
|
||||
);
|
||||
}
|
||||
|
||||
let idx = board::pick_default_port(&ports).unwrap_or(0);
|
||||
let selected = &ports[idx];
|
||||
|
||||
println!(
|
||||
" Found {} port(s), best match: {}",
|
||||
ports.len(),
|
||||
selected.port_name.bright_white().bold()
|
||||
);
|
||||
if !selected.board_name.is_empty() && selected.board_name != "Unknown" {
|
||||
println!(" Board: {}", selected.board_name);
|
||||
}
|
||||
if selected.is_usb() {
|
||||
println!(" Type: USB serial");
|
||||
}
|
||||
let vp = selected.vid_pid();
|
||||
if !vp.is_empty() {
|
||||
println!(" ID: {}", vp.bright_cyan());
|
||||
}
|
||||
println!();
|
||||
|
||||
(selected.port_name.clone(), vp)
|
||||
}
|
||||
};
|
||||
|
||||
// Write .anvil.local
|
||||
let local_file = project_path.join(".anvil.local");
|
||||
let mut content = String::new();
|
||||
content.push_str("# Machine-specific Anvil config (not tracked by git)\n");
|
||||
content.push_str("# Created by: anvil devices --set\n");
|
||||
content.push_str("# To change: anvil devices --set <PORT>\n");
|
||||
content.push_str("# To remove: delete this file\n");
|
||||
content.push_str(&format!("port = \"{}\"\n", resolved_port));
|
||||
|
||||
if !vid_pid.is_empty() {
|
||||
content.push_str(&format!("vid_pid = \"{}\"\n", vid_pid));
|
||||
}
|
||||
|
||||
fs::write(&local_file, &content)
|
||||
.context(format!("Failed to write {}", local_file.display()))?;
|
||||
|
||||
println!(
|
||||
"{} Saved port {} to {}",
|
||||
"ok".green(),
|
||||
resolved_port.bright_white().bold(),
|
||||
".anvil.local".bright_cyan()
|
||||
);
|
||||
|
||||
if !vid_pid.is_empty() {
|
||||
println!(
|
||||
" Device ID: {} -- port will be tracked even if COM number changes.",
|
||||
vid_pid.bright_cyan()
|
||||
);
|
||||
}
|
||||
|
||||
println!(
|
||||
" {}",
|
||||
"This file is gitignored -- each machine keeps its own."
|
||||
.bright_black()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// -- Helpers --------------------------------------------------------------
|
||||
|
||||
fn resolve_project_dir(project_dir: Option<&str>) -> Result<PathBuf> {
|
||||
match project_dir {
|
||||
Some(dir) => Ok(PathBuf::from(dir)),
|
||||
None => std::env::current_dir()
|
||||
.context("Could not determine current directory"),
|
||||
}
|
||||
}
|
||||
|
||||
fn require_anvil_project(path: &Path) -> Result<()> {
|
||||
let config_file = path.join(".anvil.toml");
|
||||
if !config_file.exists() {
|
||||
anyhow::bail!(
|
||||
"No .anvil.toml found in {}\n \
|
||||
Run this from inside an Anvil project, or use -d <DIR>",
|
||||
path.display()
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Read port and vid_pid from .anvil.local
|
||||
fn read_anvil_local(path: &Path) -> Result<(String, String)> {
|
||||
let content = fs::read_to_string(path)
|
||||
.context("Failed to read .anvil.local")?;
|
||||
|
||||
let mut port = String::new();
|
||||
let mut vid_pid = String::new();
|
||||
|
||||
for line in content.lines() {
|
||||
let trimmed = line.trim();
|
||||
if trimmed.starts_with('#') || !trimmed.contains('=') {
|
||||
continue;
|
||||
}
|
||||
if let Some((key, val)) = trimmed.split_once('=') {
|
||||
let k = key.trim();
|
||||
let v = val.trim().trim_matches('"');
|
||||
if k == "port" {
|
||||
port = v.to_string();
|
||||
} else if k == "vid_pid" {
|
||||
vid_pid = v.to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok((port, vid_pid))
|
||||
}
|
||||
@@ -43,10 +43,12 @@ pub fn run_diagnostics() -> Result<()> {
|
||||
} else {
|
||||
println!(
|
||||
"{}",
|
||||
"Issues found. Run 'anvil setup' to fix."
|
||||
"Issues found. See instructions below."
|
||||
.bright_yellow()
|
||||
.bold()
|
||||
);
|
||||
println!();
|
||||
print_fix_instructions(&health);
|
||||
}
|
||||
println!();
|
||||
|
||||
@@ -78,7 +80,7 @@ pub fn check_system_health() -> SystemHealth {
|
||||
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();
|
||||
let cpp_compiler_ok = has_cpp_compiler();
|
||||
|
||||
// git
|
||||
let git_ok = which::which("git").is_ok();
|
||||
@@ -99,6 +101,20 @@ pub fn check_system_health() -> SystemHealth {
|
||||
}
|
||||
}
|
||||
|
||||
/// Check for a C++ compiler on any platform.
|
||||
fn has_cpp_compiler() -> bool {
|
||||
if which::which("g++").is_ok() || which::which("clang++").is_ok() {
|
||||
return true;
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
if which::which("cl").is_ok() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn print_diagnostics(health: &SystemHealth) {
|
||||
println!("{}", "Required:".bright_yellow().bold());
|
||||
println!();
|
||||
@@ -139,14 +155,23 @@ fn print_diagnostics(health: &SystemHealth) {
|
||||
println!("{}", "Optional:".bright_yellow().bold());
|
||||
println!();
|
||||
|
||||
// avr-size
|
||||
// avr-size -- installed as part of the avr core, not a separate step
|
||||
if health.avr_size_ok {
|
||||
println!(" {} avr-size (binary size reporting)", "ok".green());
|
||||
} else {
|
||||
} else if !health.avr_core_ok {
|
||||
println!(
|
||||
" {} avr-size {}",
|
||||
"--".bright_black(),
|
||||
"install: sudo apt install gcc-avr".bright_black()
|
||||
"included with arduino:avr core (no separate install)".bright_black()
|
||||
);
|
||||
} else {
|
||||
// Core is installed but avr-size is not on PATH --
|
||||
// this can happen on Windows where the tool is buried
|
||||
// inside the Arduino15 packages directory.
|
||||
println!(
|
||||
" {} avr-size {}",
|
||||
"--".bright_black(),
|
||||
hint_avr_size_not_on_path().bright_black()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -171,18 +196,18 @@ fn print_diagnostics(health: &SystemHealth) {
|
||||
println!(
|
||||
" {} cmake {}",
|
||||
"--".bright_black(),
|
||||
"install: sudo apt install cmake".bright_black()
|
||||
hint_cmake().bright_black()
|
||||
);
|
||||
}
|
||||
|
||||
// C++ compiler
|
||||
if health.cpp_compiler_ok {
|
||||
println!(" {} C++ compiler (g++/clang++)", "ok".green());
|
||||
println!(" {} C++ compiler", "ok".green());
|
||||
} else {
|
||||
println!(
|
||||
" {} C++ compiler {}",
|
||||
"--".bright_black(),
|
||||
"install: sudo apt install g++".bright_black()
|
||||
hint_cpp_compiler().bright_black()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -193,7 +218,7 @@ fn print_diagnostics(health: &SystemHealth) {
|
||||
println!(
|
||||
" {} git {}",
|
||||
"--".bright_black(),
|
||||
"install: sudo apt install git".bright_black()
|
||||
hint_git().bright_black()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -216,6 +241,160 @@ fn print_diagnostics(health: &SystemHealth) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Print step-by-step fix instructions when required items are missing.
|
||||
fn print_fix_instructions(health: &SystemHealth) {
|
||||
println!("{}", "How to fix:".bright_cyan().bold());
|
||||
println!();
|
||||
|
||||
let mut step = 1u32;
|
||||
|
||||
if !health.arduino_cli_ok {
|
||||
println!(
|
||||
" {}. {}",
|
||||
step,
|
||||
"Install arduino-cli:".bright_white().bold()
|
||||
);
|
||||
if cfg!(target_os = "windows") {
|
||||
println!();
|
||||
println!(" Option A -- WinGet (recommended):");
|
||||
println!(" {}", "winget install ArduinoSA.CLI".bright_cyan());
|
||||
println!();
|
||||
println!(" Option B -- Chocolatey:");
|
||||
println!(" {}", "choco install arduino-cli".bright_cyan());
|
||||
println!();
|
||||
println!(" Option C -- Direct download:");
|
||||
println!(
|
||||
" {}",
|
||||
"https://arduino.github.io/arduino-cli/installation/"
|
||||
.bright_cyan()
|
||||
);
|
||||
} else if cfg!(target_os = "macos") {
|
||||
println!(" {}", "brew install arduino-cli".bright_cyan());
|
||||
} else {
|
||||
println!();
|
||||
println!(" Option A -- Install script:");
|
||||
println!(
|
||||
" {}",
|
||||
"curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh"
|
||||
.bright_cyan()
|
||||
);
|
||||
println!(
|
||||
" {}",
|
||||
"sudo mv bin/arduino-cli /usr/local/bin/".bright_cyan()
|
||||
);
|
||||
println!();
|
||||
println!(" Option B -- Package manager:");
|
||||
println!(
|
||||
" {} {}",
|
||||
"sudo apt install arduino-cli".bright_cyan(),
|
||||
"(Debian/Ubuntu)".bright_black()
|
||||
);
|
||||
println!(
|
||||
" {} {}",
|
||||
"yay -S arduino-cli".bright_cyan(),
|
||||
"(Arch)".bright_black()
|
||||
);
|
||||
}
|
||||
println!();
|
||||
step += 1;
|
||||
}
|
||||
|
||||
if !health.arduino_cli_ok {
|
||||
// They need to open a new terminal after installing arduino-cli
|
||||
println!(
|
||||
" {}. {}",
|
||||
step,
|
||||
"Close and reopen your terminal".bright_white().bold()
|
||||
);
|
||||
println!(
|
||||
" {}",
|
||||
"(so the new PATH takes effect)".bright_black()
|
||||
);
|
||||
println!();
|
||||
step += 1;
|
||||
}
|
||||
|
||||
if !health.avr_core_ok {
|
||||
println!(
|
||||
" {}. {}",
|
||||
step,
|
||||
"Install the AVR core and verify everything:"
|
||||
.bright_white()
|
||||
.bold()
|
||||
);
|
||||
println!(" {}", "anvil setup".bright_cyan());
|
||||
println!();
|
||||
// step += 1;
|
||||
}
|
||||
|
||||
if !health.git_ok {
|
||||
println!(
|
||||
" {}",
|
||||
"Tip: git is optional but recommended for version control."
|
||||
.bright_black()
|
||||
);
|
||||
if cfg!(target_os = "windows") {
|
||||
println!(
|
||||
" {}",
|
||||
"winget install Git.Git".bright_black()
|
||||
);
|
||||
} else if cfg!(target_os = "macos") {
|
||||
println!(
|
||||
" {}",
|
||||
"xcode-select --install".bright_black()
|
||||
);
|
||||
} else {
|
||||
println!(
|
||||
" {}",
|
||||
"sudo apt install git".bright_black()
|
||||
);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Platform-aware install hints (one-liners for the diagnostics table)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn hint_avr_size_not_on_path() -> &'static str {
|
||||
if cfg!(target_os = "windows") {
|
||||
"installed but not on PATH (binary size reports will be skipped)"
|
||||
} else {
|
||||
"installed but not on PATH"
|
||||
}
|
||||
}
|
||||
|
||||
fn hint_cmake() -> &'static str {
|
||||
if cfg!(target_os = "windows") {
|
||||
"install: winget install Kitware.CMake (or choco install cmake)"
|
||||
} else if cfg!(target_os = "macos") {
|
||||
"install: brew install cmake"
|
||||
} else {
|
||||
"install: sudo apt install cmake"
|
||||
}
|
||||
}
|
||||
|
||||
fn hint_cpp_compiler() -> &'static str {
|
||||
if cfg!(target_os = "windows") {
|
||||
"install: winget install Microsoft.VisualStudio.2022.BuildTools (or MinGW g++)"
|
||||
} else if cfg!(target_os = "macos") {
|
||||
"install: xcode-select --install"
|
||||
} else {
|
||||
"install: sudo apt install g++"
|
||||
}
|
||||
}
|
||||
|
||||
fn hint_git() -> &'static str {
|
||||
if cfg!(target_os = "windows") {
|
||||
"install: winget install Git.Git (or https://git-scm.com)"
|
||||
} else if cfg!(target_os = "macos") {
|
||||
"install: xcode-select --install (or brew install git)"
|
||||
} else {
|
||||
"install: sudo apt install git"
|
||||
}
|
||||
}
|
||||
|
||||
fn check_dialout() -> bool {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
@@ -234,4 +413,4 @@ fn check_dialout() -> bool {
|
||||
{
|
||||
true // Not applicable on Windows
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,5 +2,4 @@ pub mod new;
|
||||
pub mod doctor;
|
||||
pub mod setup;
|
||||
pub mod devices;
|
||||
pub mod build;
|
||||
pub mod monitor;
|
||||
pub mod refresh;
|
||||
@@ -1,167 +0,0 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -177,7 +177,12 @@ fn init_git(project_dir: &PathBuf, template_name: &str) {
|
||||
fn make_executable(project_dir: &PathBuf) {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let scripts = ["test/run_tests.sh"];
|
||||
let scripts = [
|
||||
"build.sh",
|
||||
"upload.sh",
|
||||
"monitor.sh",
|
||||
"test/run_tests.sh",
|
||||
];
|
||||
for script in &scripts {
|
||||
let path = project_dir.join(script);
|
||||
if path.exists() {
|
||||
@@ -196,23 +201,67 @@ fn print_next_steps(project_name: &str) {
|
||||
" 1. {}",
|
||||
format!("cd {}", project_name).bright_cyan()
|
||||
);
|
||||
println!(" 2. Check your system: {}", "anvil doctor".bright_cyan());
|
||||
|
||||
if cfg!(target_os = "windows") {
|
||||
println!(
|
||||
" 2. Compile: {}",
|
||||
"build.bat".bright_cyan()
|
||||
);
|
||||
println!(
|
||||
" 3. Upload to board: {}",
|
||||
"upload.bat".bright_cyan()
|
||||
);
|
||||
println!(
|
||||
" 4. Upload + monitor: {}",
|
||||
"upload.bat --monitor".bright_cyan()
|
||||
);
|
||||
println!(
|
||||
" 5. Serial monitor: {}",
|
||||
"monitor.bat".bright_cyan()
|
||||
);
|
||||
println!(
|
||||
" 6. Run host tests: {}",
|
||||
"test\\run_tests.bat".bright_cyan()
|
||||
);
|
||||
println!();
|
||||
println!(
|
||||
" {}",
|
||||
"On Linux/macOS: ./build.sh, ./upload.sh, ./monitor.sh"
|
||||
.bright_black()
|
||||
);
|
||||
} else {
|
||||
println!(
|
||||
" 2. Compile: {}",
|
||||
"./build.sh".bright_cyan()
|
||||
);
|
||||
println!(
|
||||
" 3. Upload to board: {}",
|
||||
"./upload.sh".bright_cyan()
|
||||
);
|
||||
println!(
|
||||
" 4. Upload + monitor: {}",
|
||||
"./upload.sh --monitor".bright_cyan()
|
||||
);
|
||||
println!(
|
||||
" 5. Serial monitor: {}",
|
||||
"./monitor.sh".bright_cyan()
|
||||
);
|
||||
println!(
|
||||
" 6. Run host tests: {}",
|
||||
"./test/run_tests.sh".bright_cyan()
|
||||
);
|
||||
println!();
|
||||
println!(
|
||||
" {}",
|
||||
"On Windows: build.bat, upload.bat, monitor.bat, test\\run_tests.bat"
|
||||
.bright_black()
|
||||
);
|
||||
}
|
||||
|
||||
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()
|
||||
" {}",
|
||||
"System check: anvil doctor | Port scan: anvil devices"
|
||||
.bright_black()
|
||||
);
|
||||
println!();
|
||||
}
|
||||
@@ -249,4 +298,4 @@ mod tests {
|
||||
let long_name = "a".repeat(51);
|
||||
assert!(validate_project_name(&long_name).is_err());
|
||||
}
|
||||
}
|
||||
}
|
||||
191
src/commands/refresh.rs
Normal file
191
src/commands/refresh.rs
Normal file
@@ -0,0 +1,191 @@
|
||||
use anyhow::{Result, Context};
|
||||
use colored::*;
|
||||
use std::path::PathBuf;
|
||||
use std::fs;
|
||||
|
||||
use crate::project::config::ProjectConfig;
|
||||
use crate::templates::{TemplateManager, TemplateContext};
|
||||
use crate::version::ANVIL_VERSION;
|
||||
|
||||
/// Files that anvil owns and can safely refresh.
|
||||
/// These are build/deploy infrastructure -- not user source code.
|
||||
const REFRESHABLE_FILES: &[&str] = &[
|
||||
"build.sh",
|
||||
"build.bat",
|
||||
"upload.sh",
|
||||
"upload.bat",
|
||||
"monitor.sh",
|
||||
"monitor.bat",
|
||||
"_detect_port.ps1",
|
||||
"test/run_tests.sh",
|
||||
"test/run_tests.bat",
|
||||
];
|
||||
|
||||
pub fn run_refresh(project_dir: Option<&str>, force: bool) -> Result<()> {
|
||||
// Resolve project directory
|
||||
let project_path = match project_dir {
|
||||
Some(dir) => PathBuf::from(dir),
|
||||
None => std::env::current_dir()
|
||||
.context("Could not determine current directory")?,
|
||||
};
|
||||
|
||||
let project_root = ProjectConfig::find_project_root(&project_path)?;
|
||||
let config = ProjectConfig::load(&project_root)?;
|
||||
|
||||
println!(
|
||||
"Refreshing project: {}",
|
||||
config.project.name.bright_white().bold()
|
||||
);
|
||||
println!(
|
||||
"Project directory: {}",
|
||||
project_root.display().to_string().bright_black()
|
||||
);
|
||||
println!();
|
||||
|
||||
// Generate fresh copies of all refreshable files from the template
|
||||
let template_name = "basic";
|
||||
let context = TemplateContext {
|
||||
project_name: config.project.name.clone(),
|
||||
anvil_version: ANVIL_VERSION.to_string(),
|
||||
};
|
||||
|
||||
// Extract template into a temp directory so we can compare
|
||||
let temp_dir = tempfile::tempdir()
|
||||
.context("Failed to create temp directory")?;
|
||||
TemplateManager::extract(template_name, temp_dir.path(), &context)?;
|
||||
|
||||
// Compare each refreshable file
|
||||
let mut up_to_date = Vec::new();
|
||||
let mut will_create = Vec::new();
|
||||
let mut has_changes = Vec::new();
|
||||
|
||||
for &filename in REFRESHABLE_FILES {
|
||||
let existing = project_root.join(filename);
|
||||
let fresh = temp_dir.path().join(filename);
|
||||
|
||||
if !fresh.exists() {
|
||||
// Template doesn't produce this file (shouldn't happen)
|
||||
continue;
|
||||
}
|
||||
|
||||
let fresh_content = fs::read(&fresh)
|
||||
.context(format!("Failed to read template file: {}", filename))?;
|
||||
|
||||
if !existing.exists() {
|
||||
will_create.push(filename);
|
||||
continue;
|
||||
}
|
||||
|
||||
let existing_content = fs::read(&existing)
|
||||
.context(format!("Failed to read project file: {}", filename))?;
|
||||
|
||||
if existing_content == fresh_content {
|
||||
up_to_date.push(filename);
|
||||
} else {
|
||||
has_changes.push(filename);
|
||||
}
|
||||
}
|
||||
|
||||
// Report status
|
||||
if !up_to_date.is_empty() {
|
||||
println!(
|
||||
"{} {} file(s) already up to date",
|
||||
"ok".green(),
|
||||
up_to_date.len()
|
||||
);
|
||||
}
|
||||
|
||||
if !will_create.is_empty() {
|
||||
for f in &will_create {
|
||||
println!(" {} {} (new)", "+".bright_green(), f.bright_white());
|
||||
}
|
||||
}
|
||||
|
||||
if !has_changes.is_empty() {
|
||||
for f in &has_changes {
|
||||
println!(
|
||||
" {} {} (differs from latest)",
|
||||
"~".bright_yellow(),
|
||||
f.bright_white()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Decide what to do
|
||||
if has_changes.is_empty() && will_create.is_empty() {
|
||||
println!();
|
||||
println!(
|
||||
"{}",
|
||||
"All scripts are up to date. Nothing to do."
|
||||
.bright_green()
|
||||
.bold()
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !has_changes.is_empty() && !force {
|
||||
println!();
|
||||
println!(
|
||||
"{} {} script(s) differ from the latest Anvil templates.",
|
||||
"!".bright_yellow(),
|
||||
has_changes.len()
|
||||
);
|
||||
println!(
|
||||
"This is normal after upgrading Anvil. To update them, run:"
|
||||
);
|
||||
println!();
|
||||
println!(" {}", "anvil refresh --force".bright_cyan());
|
||||
println!();
|
||||
println!(
|
||||
" {}",
|
||||
"Only build scripts are replaced. Your .anvil.toml and source code are never touched."
|
||||
.bright_black()
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Apply updates
|
||||
let files_to_write: Vec<&str> = if force {
|
||||
will_create.iter().chain(has_changes.iter()).copied().collect()
|
||||
} else {
|
||||
will_create.to_vec()
|
||||
};
|
||||
|
||||
for filename in &files_to_write {
|
||||
let fresh = temp_dir.path().join(filename);
|
||||
let dest = project_root.join(filename);
|
||||
|
||||
// Ensure parent directory exists
|
||||
if let Some(parent) = dest.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
fs::copy(&fresh, &dest)
|
||||
.context(format!("Failed to write: {}", filename))?;
|
||||
}
|
||||
|
||||
// Make shell scripts executable on Unix
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
for filename in &files_to_write {
|
||||
if filename.ends_with(".sh") {
|
||||
let path = project_root.join(filename);
|
||||
if let Ok(meta) = fs::metadata(&path) {
|
||||
let mut perms = meta.permissions();
|
||||
perms.set_mode(0o755);
|
||||
let _ = fs::set_permissions(&path, perms);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!();
|
||||
println!(
|
||||
"{} Updated {} file(s).",
|
||||
"ok".green(),
|
||||
files_to_write.len()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -78,11 +78,22 @@ pub fn run_setup() -> Result<()> {
|
||||
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");
|
||||
print_optional_hint("avr-size", hint_avr_size());
|
||||
}
|
||||
|
||||
if which::which("cmake").is_ok() {
|
||||
println!(" {} cmake (for host-side tests)", "ok".green());
|
||||
} else {
|
||||
print_optional_hint("cmake", hint_cmake());
|
||||
}
|
||||
|
||||
if which::which("g++").is_ok()
|
||||
|| which::which("clang++").is_ok()
|
||||
|| cfg!(windows) && which::which("cl").is_ok()
|
||||
{
|
||||
println!(" {} C++ compiler", "ok".green());
|
||||
} else {
|
||||
print_optional_hint("C++ compiler", hint_cpp_compiler());
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
@@ -128,15 +139,31 @@ pub fn run_setup() -> Result<()> {
|
||||
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()
|
||||
);
|
||||
if cfg!(target_os = "windows") {
|
||||
println!(
|
||||
" 4. {}",
|
||||
"cd blink && build.bat".bright_cyan()
|
||||
);
|
||||
} else {
|
||||
println!(
|
||||
" 4. {}",
|
||||
"cd blink && ./build.sh".bright_cyan()
|
||||
);
|
||||
}
|
||||
println!();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_optional_hint(name: &str, hint: &str) {
|
||||
println!(
|
||||
" {} {} not found. Install for full functionality:",
|
||||
"info".bright_black(),
|
||||
name
|
||||
);
|
||||
println!(" {}", hint);
|
||||
}
|
||||
|
||||
fn print_install_instructions() {
|
||||
println!("{}", "Install arduino-cli:".bright_yellow().bold());
|
||||
println!();
|
||||
@@ -161,3 +188,37 @@ fn print_install_instructions() {
|
||||
println!();
|
||||
println!(" Then re-run: anvil setup");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Platform-aware install hints
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn hint_avr_size() -> &'static str {
|
||||
if cfg!(target_os = "windows") {
|
||||
"bundled with arduino:avr core (avr-size.exe in Arduino15 packages)"
|
||||
} else if cfg!(target_os = "macos") {
|
||||
"brew install avr-gcc"
|
||||
} else {
|
||||
"sudo apt install gcc-avr"
|
||||
}
|
||||
}
|
||||
|
||||
fn hint_cmake() -> &'static str {
|
||||
if cfg!(target_os = "windows") {
|
||||
"winget install Kitware.CMake (or choco install cmake)"
|
||||
} else if cfg!(target_os = "macos") {
|
||||
"brew install cmake"
|
||||
} else {
|
||||
"sudo apt install cmake"
|
||||
}
|
||||
}
|
||||
|
||||
fn hint_cpp_compiler() -> &'static str {
|
||||
if cfg!(target_os = "windows") {
|
||||
"winget install Microsoft.VisualStudio.2022.BuildTools (or MinGW g++)"
|
||||
} else if cfg!(target_os = "macos") {
|
||||
"xcode-select --install"
|
||||
} else {
|
||||
"sudo apt install g++"
|
||||
}
|
||||
}
|
||||
123
src/main.rs
123
src/main.rs
@@ -43,73 +43,31 @@ enum Commands {
|
||||
Setup,
|
||||
|
||||
/// List connected boards and serial ports
|
||||
Devices,
|
||||
Devices {
|
||||
/// Save a port to .anvil.local for this project
|
||||
#[arg(long, conflicts_with = "get")]
|
||||
set: bool,
|
||||
|
||||
/// Compile a sketch (and optionally upload)
|
||||
Build {
|
||||
/// Path to sketch directory
|
||||
sketch: String,
|
||||
/// Show the saved port for this project
|
||||
#[arg(long, conflicts_with = "set")]
|
||||
get: bool,
|
||||
|
||||
/// Compile only -- do not upload
|
||||
#[arg(long)]
|
||||
verify: bool,
|
||||
/// Port name (e.g. COM3, /dev/ttyUSB0). Auto-detects if omitted with --set.
|
||||
port_or_dir: Option<String>,
|
||||
|
||||
/// 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>,
|
||||
/// Path to project directory (defaults to current directory)
|
||||
#[arg(long, short = 'd', value_name = "DIR")]
|
||||
dir: Option<String>,
|
||||
},
|
||||
|
||||
/// Upload cached build artifacts (no recompile)
|
||||
Upload {
|
||||
/// Path to sketch directory
|
||||
sketch: String,
|
||||
/// Update project scripts to the latest version
|
||||
Refresh {
|
||||
/// Path to project directory (defaults to current directory)
|
||||
dir: Option<String>,
|
||||
|
||||
/// Serial port (auto-detected if omitted)
|
||||
#[arg(short, long)]
|
||||
port: Option<String>,
|
||||
|
||||
/// Show full avrdude output
|
||||
/// Overwrite scripts even if they have been modified
|
||||
#[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,
|
||||
force: bool,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -133,7 +91,7 @@ fn main() -> Result<()> {
|
||||
} else {
|
||||
anyhow::bail!(
|
||||
"Project name required.\n\
|
||||
Usage: anvil new <name>\n\
|
||||
Usage: anvil new <n>\n\
|
||||
List templates: anvil new --list-templates"
|
||||
);
|
||||
}
|
||||
@@ -144,31 +102,24 @@ fn main() -> Result<()> {
|
||||
Commands::Setup => {
|
||||
commands::setup::run_setup()
|
||||
}
|
||||
Commands::Devices => {
|
||||
commands::devices::scan_devices()
|
||||
Commands::Devices { set, get, port_or_dir, dir } => {
|
||||
if set {
|
||||
commands::devices::set_port(
|
||||
port_or_dir.as_deref(),
|
||||
dir.as_deref(),
|
||||
)
|
||||
} else if get {
|
||||
commands::devices::get_port(
|
||||
dir.as_deref().or(port_or_dir.as_deref()),
|
||||
)
|
||||
} else {
|
||||
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,
|
||||
Commands::Refresh { dir, force } => {
|
||||
commands::refresh::run_refresh(
|
||||
dir.as_deref(),
|
||||
force,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -193,4 +144,4 @@ fn print_banner() {
|
||||
.bright_cyan()
|
||||
);
|
||||
println!();
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,8 @@ pub struct BuildConfig {
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct MonitorConfig {
|
||||
pub baud: u32,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub port: Option<String>,
|
||||
}
|
||||
|
||||
impl ProjectConfig {
|
||||
@@ -49,6 +51,7 @@ impl ProjectConfig {
|
||||
},
|
||||
monitor: MonitorConfig {
|
||||
baud: 115200,
|
||||
port: None,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -143,13 +146,6 @@ pub fn anvil_home() -> Result<PathBuf> {
|
||||
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::*;
|
||||
@@ -223,4 +219,4 @@ mod tests {
|
||||
assert!(flags.contains("-Werror"));
|
||||
assert!(flags.contains("-I"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user