Skip to content

Commit 5f440f9

Browse files
committed
allow specifying partition table and bootloader in the projects Cargo.toml
1 parent 1a86d2f commit 5f440f9

File tree

5 files changed

+74
-6
lines changed

5 files changed

+74
-6
lines changed

cargo-espflash/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ categories = [
2626
[dependencies]
2727
miette = "2"
2828
cargo_metadata = "0.14"
29+
cargo_toml = "0.10"
2930
clap = "2.33"
3031
crossterm = "0.21"
3132
espflash = { version = "*", path = "../espflash" }
3233
guess_host_triple = "0.1"
3334
serde = { version = "1.0", features = ["derive"] }
3435
serial = "0.4"
3536
toml = "0.5"
36-
thiserror = "1"
37+
thiserror = "1"

cargo-espflash/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ or `%APPDATA%/esp/espflash/espflash.toml` on Windows.
4343
serial = "/dev/ttyUSB0"
4444
```
4545

46+
### Package metadata
47+
48+
You can also specify the bootloader or partition table for a project in the package metadata in `Cargo.toml`
49+
50+
```toml
51+
[package.metadata.espflash]
52+
partition_table = "partitions.csv"
53+
bootloader = "bootloader.bin"
54+
```
55+
4656
### Example
4757

4858
```bash

cargo-espflash/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ pub enum Error {
2626
help("Please specify which artifact to flash using --bin")
2727
)]
2828
MultipleArtifacts,
29+
#[error("Specified partition table is not a csv file")]
30+
#[diagnostic(code(cargo_espflash::partition_table_path))]
31+
InvalidPartitionTablePath,
32+
#[error("Specified bootloader table is not a bin file")]
33+
#[diagnostic(code(cargo_espflash::bootloader_path))]
34+
InvalidBootloaderPath,
2935
}

cargo-espflash/src/main.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use cargo_config::has_build_std;
12
use cargo_metadata::Message;
23
use clap::{App, Arg, SubCommand};
34
use error::Error;
45
use espflash::{Config, Flasher, PartitionTable};
56
use miette::{IntoDiagnostic, Result, WrapErr};
7+
use monitor::monitor;
8+
use package_metadata::CargoEspFlashMeta;
69
use serial::{BaudRate, SerialPort};
710
use std::{
811
fs,
@@ -11,13 +14,11 @@ use std::{
1114
string::ToString,
1215
};
1316

14-
use cargo_config::has_build_std;
15-
use monitor::monitor;
16-
1717
mod cargo_config;
1818
mod error;
1919
mod line_endings;
2020
mod monitor;
21+
mod package_metadata;
2122

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

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

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

159161
// If the '--bootloader' option is provided, load the binary file at the
160162
// specified path.
161-
let bootloader = if let Some(path) = matches.value_of("bootloader") {
163+
let bootloader = if let Some(path) = matches
164+
.value_of("bootloader")
165+
.or_else(|| metadata.bootloader.as_deref())
166+
{
162167
let path = fs::canonicalize(path).into_diagnostic()?;
163168
let data = fs::read(path).into_diagnostic()?;
164169
Some(data)
@@ -168,7 +173,10 @@ fn main() -> Result<()> {
168173

169174
// If the '--partition-table' option is provided, load the partition table from
170175
// the CSV at the specified path.
171-
let partition_table = if let Some(path) = matches.value_of("partition_table") {
176+
let partition_table = if let Some(path) = matches
177+
.value_of("partition_table")
178+
.or_else(|| metadata.partition_table.as_deref())
179+
{
172180
let path = fs::canonicalize(path).into_diagnostic()?;
173181
let data = fs::read_to_string(path).into_diagnostic()?;
174182
let table = PartitionTable::try_from_str(data)?;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::error::Error;
2+
use cargo_toml::Manifest;
3+
use miette::{IntoDiagnostic, Result, WrapErr};
4+
use serde::Deserialize;
5+
use std::path::Path;
6+
7+
#[derive(Clone, Debug, Deserialize, Default)]
8+
pub struct CargoEspFlashMeta {
9+
pub partition_table: Option<String>,
10+
pub bootloader: Option<String>,
11+
}
12+
13+
#[derive(Clone, Debug, Default, Deserialize)]
14+
pub struct Meta {
15+
pub espflash: Option<CargoEspFlashMeta>,
16+
}
17+
18+
impl CargoEspFlashMeta {
19+
pub fn load<P: AsRef<Path>>(path: P) -> Result<CargoEspFlashMeta> {
20+
let manifest = Manifest::<Meta>::from_path_with_metadata(path)
21+
.into_diagnostic()
22+
.wrap_err("Failed to parse Cargo.toml")?;
23+
let meta = manifest
24+
.package
25+
.and_then(|pkg| pkg.metadata)
26+
.unwrap_or_default()
27+
.espflash
28+
.unwrap_or_default();
29+
match meta.partition_table {
30+
Some(table) if !table.ends_with(".csv") => {
31+
return Err(Error::InvalidPartitionTablePath.into())
32+
}
33+
_ => {}
34+
}
35+
match meta.bootloader {
36+
Some(table) if !table.ends_with(".bin") => {
37+
return Err(Error::InvalidBootloaderPath.into())
38+
}
39+
_ => {}
40+
}
41+
Ok(meta)
42+
}
43+
}

0 commit comments

Comments
 (0)