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

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

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

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

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

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

View File

@@ -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!();
}

View File

@@ -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))
}

View File

@@ -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
}
}
}

View File

@@ -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;

View File

@@ -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()
}
}

View File

@@ -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
View 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(())
}

View File

@@ -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++"
}
}