Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cargo-espflash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ categories = [
[dependencies]
miette = "2"
cargo_metadata = "0.14"
cargo_toml = "0.10"
clap = "2.33"
crossterm = "0.21"
espflash = { version = "*", path = "../espflash" }
guess_host_triple = "0.1"
serde = { version = "1.0", features = ["derive"] }
serial = "0.4"
toml = "0.5"
thiserror = "1"
thiserror = "1"
10 changes: 10 additions & 0 deletions cargo-espflash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ or `%APPDATA%/esp/espflash/espflash.toml` on Windows.
serial = "/dev/ttyUSB0"
```

### Package metadata

You can also specify the bootloader or partition table for a project in the package metadata in `Cargo.toml`

```toml
[package.metadata.espflash]
partition_table = "partitions.csv"
bootloader = "bootloader.bin"
```

### Example

```bash
Expand Down
6 changes: 6 additions & 0 deletions cargo-espflash/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ pub enum Error {
help("Please specify which artifact to flash using --bin")
)]
MultipleArtifacts,
#[error("Specified partition table is not a csv file")]
#[diagnostic(code(cargo_espflash::partition_table_path))]
InvalidPartitionTablePath,
#[error("Specified bootloader table is not a bin file")]
#[diagnostic(code(cargo_espflash::bootloader_path))]
InvalidBootloaderPath,
}
18 changes: 13 additions & 5 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use cargo_config::has_build_std;
use cargo_metadata::Message;
use clap::{App, Arg, SubCommand};
use error::Error;
use espflash::{Config, Flasher, PartitionTable};
use miette::{IntoDiagnostic, Result, WrapErr};
use monitor::monitor;
use package_metadata::CargoEspFlashMeta;
use serial::{BaudRate, SerialPort};
use std::{
fs,
Expand All @@ -11,13 +14,11 @@ use std::{
string::ToString,
};

use cargo_config::has_build_std;
use monitor::monitor;

mod cargo_config;
mod error;
mod line_endings;
mod monitor;
mod package_metadata;

fn main() -> Result<()> {
let mut app = App::new(env!("CARGO_PKG_NAME"))
Expand Down Expand Up @@ -100,6 +101,7 @@ fn main() -> Result<()> {
};

let config = Config::load();
let metadata = CargoEspFlashMeta::load("Cargo.toml")?;

// The serial port must be specified, either as a command-line argument or in
// the cargo configuration file. In the case that both have been provided the
Expand Down Expand Up @@ -158,7 +160,10 @@ fn main() -> Result<()> {

// If the '--bootloader' option is provided, load the binary file at the
// specified path.
let bootloader = if let Some(path) = matches.value_of("bootloader") {
let bootloader = if let Some(path) = matches
.value_of("bootloader")
.or_else(|| metadata.bootloader.as_deref())
{
let path = fs::canonicalize(path).into_diagnostic()?;
let data = fs::read(path).into_diagnostic()?;
Some(data)
Expand All @@ -168,7 +173,10 @@ fn main() -> Result<()> {

// If the '--partition-table' option is provided, load the partition table from
// the CSV at the specified path.
let partition_table = if let Some(path) = matches.value_of("partition_table") {
let partition_table = if let Some(path) = matches
.value_of("partition_table")
.or_else(|| metadata.partition_table.as_deref())
{
let path = fs::canonicalize(path).into_diagnostic()?;
let data = fs::read_to_string(path).into_diagnostic()?;
let table = PartitionTable::try_from_str(data)?;
Expand Down
43 changes: 43 additions & 0 deletions cargo-espflash/src/package_metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::error::Error;
use cargo_toml::Manifest;
use miette::{IntoDiagnostic, Result, WrapErr};
use serde::Deserialize;
use std::path::Path;

#[derive(Clone, Debug, Deserialize, Default)]
pub struct CargoEspFlashMeta {
pub partition_table: Option<String>,
pub bootloader: Option<String>,
}

#[derive(Clone, Debug, Default, Deserialize)]
pub struct Meta {
pub espflash: Option<CargoEspFlashMeta>,
}

impl CargoEspFlashMeta {
pub fn load<P: AsRef<Path>>(path: P) -> Result<CargoEspFlashMeta> {
let manifest = Manifest::<Meta>::from_path_with_metadata(path)
.into_diagnostic()
.wrap_err("Failed to parse Cargo.toml")?;
let meta = manifest
.package
.and_then(|pkg| pkg.metadata)
.unwrap_or_default()
.espflash
.unwrap_or_default();
match meta.partition_table {
Some(table) if !table.ends_with(".csv") => {
return Err(Error::InvalidPartitionTablePath.into())
}
_ => {}
}
match meta.bootloader {
Some(table) if !table.ends_with(".bin") => {
return Err(Error::InvalidBootloaderPath.into())
}
_ => {}
}
Ok(meta)
}
}