Skip to content

Commit

Permalink
make legacy configuration deprecated & make ravedude respect CARGO_MA…
Browse files Browse the repository at this point in the history
…NIFEST_DIR
  • Loading branch information
Creative0708 authored and Rahix committed Sep 14, 2024
1 parent 60a2b40 commit 5e7b08f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 77 deletions.
13 changes: 3 additions & 10 deletions ravedude/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ impl RavedudeConfig {
},
port: args.port.clone(),
reset_delay: args.reset_delay,
board: match args.board.clone() {
Some(board_osstring) => Some(
board_osstring
.into_string()
.map_err(|_| anyhow::anyhow!("board is not valid utf-8"))?,
),
None => None,
},
board: args.legacy_board_name().clone(),
},
board_config: Default::default(),
})
Expand All @@ -73,8 +66,8 @@ pub struct RavedudeGeneralConfig {
}

impl RavedudeGeneralConfig {
pub fn apply_overrides(&mut self, args: &crate::Args) -> anyhow::Result<()> {
// command line args take priority over Ravedude.toml
/// Apply command-line overrides to this configuration. Command-line arguments take priority over Ravedude.toml
pub fn apply_overrides_from(&mut self, args: &crate::Args) -> anyhow::Result<()> {
if args.open_console {
self.open_console = true;
}
Expand Down
128 changes: 61 additions & 67 deletions ravedude/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use anyhow::Context as _;
use colored::Colorize as _;
use structopt::clap::AppSettings;

use std::ffi::OsString;
use std::path::Path;
use std::thread;
use std::time::Duration;
Expand Down Expand Up @@ -59,35 +58,30 @@ struct Args {
#[structopt(long = "debug-avrdude")]
debug_avrdude: bool,

/// Which board to interact with.
///
/// Must be one of the known board identifiers:
///
/// * uno
/// * nano
/// * nano-new
/// * leonardo
/// * micro
/// * mega2560
/// * mega1280
/// * diecimila
/// * promicro
/// * promini-3v3
/// * promini-5v
/// * trinket-pro
/// * trinket
/// * nano168
/// * duemilanove
#[structopt(name = "BOARD", verbatim_doc_comment)]
// When Ravedude.toml exists, the binary is placed where the board should be. This is an OsString to not lose
// informaton when we have to take the board as the binary.
board: Option<OsString>,

#[structopt(name = "BINARY", parse(from_os_str))]
/// The binary to be flashed.
///
/// If no binary is given, flashing will be skipped.
#[structopt(name = "BINARY", parse(from_os_str))]
// (Note: this is where the board is stored in legacy configurations.)
bin: Option<std::path::PathBuf>,

/// Deprecated binary for old configurations of ravedude without `Ravedude.toml`.
/// Should not be used in newer configurations.
#[structopt(name = "LEGACY BINARY", parse(from_os_str))]
bin_legacy: Option<std::path::PathBuf>,
}
impl Args {
/// Get the board name for legacy configurations.
/// `None` if the configuration isn't a legacy configuration or the board name doesn't exist.
fn legacy_board_name(&self) -> Option<String> {
if self.bin_legacy.is_none() {
None
} else {
self.bin
.as_deref()
.and_then(|board| board.to_str().map(String::from))
}
}
}

fn main() {
Expand All @@ -100,56 +94,56 @@ fn main() {
}
}

fn ravedude() -> anyhow::Result<()> {
let mut args: Args = structopt::StructOpt::from_args();
/// Finds the location of a `Ravedude.toml` or `None` if not found.
fn find_manifest() -> anyhow::Result<Option<std::path::PathBuf>> {
if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
let path = Path::new(&manifest_dir).join("Ravedude.toml");
return Ok(path.exists().then_some(path));
}

let manifest_path = 'manifest_path: {
// By default Cargo scans the current dir and all of its parents for Cargo.toml,
// so we mirror its behavior here.
let current_dir = std::env::current_dir()?;
// If `CARGO_MANIFEST_DIR` isn't set, Cargo scans the current dir and all of its parents for Cargo.toml
// so we mirror its behavior here.
let current_dir = std::env::current_dir()?;

for dir_to_test in current_dir.ancestors() {
let path_to_test = dir_to_test.join(Path::new("Ravedude.toml"));
if path_to_test.exists() {
break 'manifest_path Some(path_to_test);
}
for dir_to_test in current_dir.ancestors() {
let path_to_test = dir_to_test.join("Ravedude.toml");
if path_to_test.exists() {
return Ok(Some(path_to_test));
}
}

None
};
Ok(None)
}

if manifest_path.is_some() {
if let Some(board) = args.board.take() {
if args.bin.is_none() {
// The board arg is taken before the binary, so rearrange the args when Ravedude.toml exists
args.bin = Some(std::path::PathBuf::from(board));
} else {
anyhow::bail!("can't pass board as command-line argument when Ravedude.toml is present; set `board = {:?}` under [general] in Ravedude.toml", board)
}
fn ravedude() -> anyhow::Result<()> {
let args: Args = structopt::StructOpt::from_args();

let manifest_path = find_manifest()?;

let mut ravedude_config = match (manifest_path.as_deref(), args.legacy_board_name()) {
(Some(_), Some(board_name)) => {
anyhow::bail!("can't pass board as command-line argument when Ravedude.toml is present; set `board = {:?}` under [general] in Ravedude.toml", board_name);
}
} else if args.board.is_some() && !args.dump_config {
warning!(
"Passing the board as command-line argument is deprecated, use Ravedude.toml instead:"
);
eprintln!(
"\n# Ravedude.toml\n{}",
toml::to_string(&config::RavedudeConfig::from_args(&args)?)?
);
}
(Some(path), None) => board::get_board_from_manifest(path)?,
(None, Some(board_name)) => {
warning!(
"Passing the board as command-line argument is deprecated; create a Ravedude.toml in the project root instead:"
);
eprintln!(
"\n# Ravedude.toml\n{}",
toml::to_string(&config::RavedudeConfig::from_args(&args)?)?
);

let mut ravedude_config = match manifest_path.as_deref() {
Some(path) => board::get_board_from_manifest(path)?,
None => board::get_board_from_name(
args
.board
.as_deref()
.ok_or_else(|| anyhow::anyhow!("no board given and couldn't find Ravedude.toml in project, either pass a board as an argument or make a Ravedude.toml."))?
.to_str()
.ok_or_else(|| anyhow::anyhow!("board name isn't valid UTF-8"))?
)?
board::get_board_from_name(&board_name)?
}
(None, None) => {
anyhow::bail!("couldn't find Ravedude.toml in project");
}
};

ravedude_config.general_options.apply_overrides(&mut args)?;
ravedude_config
.general_options
.apply_overrides_from(&args)?;

if args.dump_config {
println!("{}", toml::to_string(&ravedude_config)?);
Expand Down

0 comments on commit 5e7b08f

Please sign in to comment.