Skip to content

Commit 4dd748e

Browse files
authored
Fix building as a library, add CI check to avoid breaking again in the future (#282)
* Refactor to allow building as a library, without the `cli` feature * Build `espflash` as a library in CI * Make more dependencies optional
1 parent f9b7dae commit 4dd748e

File tree

5 files changed

+54
-24
lines changed

5 files changed

+54
-24
lines changed

.github/workflows/raspberry_rust.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ jobs:
2727
toolchain: stable
2828
cargo-command: check
2929
args: --features=raspberry
30+
- name: Check (lib)
31+
toolchain: stable
32+
cargo-command: check
33+
args: --lib --no-default-features --features=raspberry
3034
- name: Check MSRV
3135
toolchain: "1.62"
3236
cargo-command: check

.github/workflows/rust.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ jobs:
3131
with:
3232
command: check
3333

34+
check-lib:
35+
name: Check (lib)
36+
runs-on: ubuntu-latest
37+
steps:
38+
- name: Change apt mirror and install dependencies
39+
run: |
40+
sudo sed -i 's/azure.archive.ubuntu.com/archive.ubuntu.com/' /etc/apt/sources.list
41+
sudo apt-get update
42+
sudo apt-get install musl-tools libudev-dev
43+
- uses: actions/checkout@v2
44+
- uses: actions-rs/toolchain@v1
45+
with:
46+
profile: minimal
47+
toolchain: stable
48+
override: true
49+
- uses: Swatinem/rust-cache@v1
50+
- uses: actions-rs/cargo@v1
51+
with:
52+
command: check
53+
args: --lib --no-default-features
54+
3455
msrv:
3556
name: Check MSRV
3657
runs-on: ubuntu-latest

espflash/Cargo.toml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,27 @@ path = "./src/bin/espflash.rs"
3535
required-features = ["cli"]
3636

3737
[dependencies]
38-
addr2line = "0.18.0"
38+
addr2line = { version = "0.18.0", optional = true }
3939
base64 = "0.13.1"
4040
binread = "2.2.0"
4141
bytemuck = { version = "1.12.1", features = ["derive"] }
4242
clap = { version = "4.0.18", features = ["derive"], optional = true }
43-
comfy-table = "6.1.2"
43+
comfy-table = { version = "6.1.2", optional = true }
4444
crossterm = { version = "0.25.0", optional = true }
4545
dialoguer = { version = "0.10.2", optional = true }
46-
directories-next = "2.0.0"
46+
directories-next = { version = "2.0.0", optional = true }
4747
esp-idf-part = "0.1.1"
4848
env_logger = { version = "0.9.1", optional = true }
4949
flate2 = "1.0.24"
5050
indicatif = "0.17.1"
51-
lazy_static = "1.4.0"
51+
lazy_static = { version = "1.4.0", optional = true }
5252
log = "0.4.17"
5353
miette = { version = "5.3.0", features = ["fancy"] }
54-
parse_int = "0.6.0"
55-
regex = "1.6.0"
54+
parse_int = { version = "0.6.0", optional = true }
55+
regex = { version = "1.6.0", optional = true }
5656
rppal = { version = "0.13.1", optional = true }
5757
serde = { version = "1.0.147", features = ["derive"] }
58-
serde-hex = "0.1.0"
58+
serde-hex = { version = "0.1.0", optional = true }
5959
serialport = "4.2.0"
6060
sha2 = "0.10.6"
6161
slip-codec = "0.3.3"
@@ -67,5 +67,9 @@ xmas-elf = "0.8.0"
6767

6868
[features]
6969
default = ["cli"]
70-
cli = ["dep:clap", "dep:crossterm", "dep:dialoguer", "dep:env_logger", "dep:update-informer"]
70+
cli = [
71+
"dep:addr2line", "dep:clap", "dep:comfy-table", "dep:crossterm", "dep:dialoguer",
72+
"dep:directories-next", "dep:env_logger", "dep:lazy_static", "dep:parse_int",
73+
"dep:regex", "dep:serde-hex", "dep:update-informer"
74+
]
7175
raspberry = ["dep:rppal"]

espflash/src/cli/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,15 @@ pub fn connect(args: &ConnectArgs, config: &Config) -> Result<Flasher> {
176176
info!("Serial port: '{}'", port_info.port_name);
177177
info!("Connecting...");
178178

179-
let interface = Interface::new(&port_info, args, config)
179+
#[cfg(feature = "raspberry")]
180+
let (dtr, rts) = (
181+
args.dtr.or(config.connection.dtr),
182+
args.rts.or(config.connection.rts),
183+
);
184+
#[cfg(not(feature = "raspberry"))]
185+
let (dtr, rts) = (None, None);
186+
187+
let interface = Interface::new(&port_info, dtr, rts)
180188
.wrap_err_with(|| format!("Failed to open serial port {}", port_info.port_name))?;
181189

182190
// NOTE: since `get_serial_port_info` filters out all PCI Port and Bluetooth

espflash/src/interface.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ use miette::{Context, Result};
55
use rppal::gpio::{Gpio, OutputPin};
66
use serialport::{FlowControl, SerialPort, SerialPortInfo};
77

8-
use crate::{
9-
cli::{config::Config, ConnectArgs},
10-
error::Error,
11-
};
8+
use crate::error::Error;
129

1310
#[derive(thiserror::Error, Debug)]
1411
pub enum SerialConfigError {
1512
#[cfg(feature = "raspberry")]
1613
#[error("You need to specify both DTR and RTS pins when using an internal UART peripheral")]
1714
MissingDtrRtsForInternalUart,
18-
1915
#[cfg(feature = "raspberry")]
2016
#[error("GPIO {0} is not available")]
2117
GpioUnavailable(u8),
@@ -55,22 +51,19 @@ impl Interface {
5551
#[cfg(feature = "raspberry")]
5652
pub(crate) fn new(
5753
port_info: &SerialPortInfo,
58-
args: &ConnectArgs,
59-
config: &Config,
54+
dtr: Option<u8>,
55+
rts: Option<u8>,
6056
) -> Result<Self> {
61-
let rts_gpio = args.rts.or(config.connection.rts);
62-
let dtr_gpio = args.dtr.or(config.connection.dtr);
63-
6457
if port_info.port_type == serialport::SerialPortType::Unknown
65-
&& (dtr_gpio.is_none() || rts_gpio.is_none())
58+
&& (dtr.is_none() || rts.is_none())
6659
{
6760
// Assume internal UART, which has no DTR pin and usually no RTS either.
6861
return Err(Error::from(SerialConfigError::MissingDtrRtsForInternalUart).into());
6962
}
7063

7164
let gpios = Gpio::new().unwrap();
7265

73-
let rts = if let Some(gpio) = rts_gpio {
66+
let rts = if let Some(gpio) = rts {
7467
match gpios.get(gpio) {
7568
Ok(pin) => Some(pin.into_output()),
7669
Err(_) => return Err(Error::from(SerialConfigError::GpioUnavailable(gpio)).into()),
@@ -79,7 +72,7 @@ impl Interface {
7972
None
8073
};
8174

82-
let dtr = if let Some(gpio) = dtr_gpio {
75+
let dtr = if let Some(gpio) = dtr {
8376
match gpios.get(gpio) {
8477
Ok(pin) => Some(pin.into_output()),
8578
Err(_) => return Err(Error::from(SerialConfigError::GpioUnavailable(gpio)).into()),
@@ -98,8 +91,8 @@ impl Interface {
9891
#[cfg(not(feature = "raspberry"))]
9992
pub(crate) fn new(
10093
port_info: &SerialPortInfo,
101-
_args: &ConnectArgs,
102-
_config: &Config,
94+
_dtr: Option<u8>,
95+
_rts: Option<u8>,
10396
) -> Result<Self> {
10497
Ok(Self {
10598
serial_port: open_port(port_info)?,

0 commit comments

Comments
 (0)