From b1b1578f32133d66b73606d74049b947c89036e2 Mon Sep 17 00:00:00 2001 From: threadexio Date: Fri, 3 Jan 2025 01:41:37 +0200 Subject: [PATCH] feat: allow specifying arguments for the formatter --- cbundl.toml | 5 +++-- src/cli.rs | 1 + src/config.rs | 9 +++++++++ src/formatter.rs | 9 +++++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cbundl.toml b/cbundl.toml index 1f4c5fb..fc0b7d0 100644 --- a/cbundl.toml +++ b/cbundl.toml @@ -11,8 +11,9 @@ enable = true pick = "custom" [formatter] -# enable = true -# path = "clang-format" +enable = true +path = "clang-format" +args = ["--verbose", "--sort-includes"] [[quote]] text = """ diff --git a/src/cli.rs b/src/cli.rs index 930a533..1924d6e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -36,6 +36,7 @@ pub fn run() -> Result<()> { let formatter = (!config.no_format).then_some(Formatter { exe: config.formatter, + args: config.formatter_args, }); let mut pipeline = Pipeline { diff --git a/src/config.rs b/src/config.rs index 6fb1681..f7b21d3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -103,6 +103,7 @@ struct QuoteSection { struct FormatterSection { enable: Option, path: Option, + args: Option>, } impl File { @@ -176,6 +177,7 @@ pub struct Config { pub no_format: bool, pub formatter: PathBuf, + pub formatter_args: Vec, pub entry: PathBuf, } @@ -278,6 +280,12 @@ impl Config { }) .unwrap_or_else(|| PathBuf::from(DEFAULT_FORMATTER)); + let formatter_args = file + .as_ref() + .and_then(|x| x.formatter.as_ref()) + .and_then(|x| x.args.clone()) + .unwrap_or_default(); + let entry = args.value::("entry").unwrap().clone(); Ok(Self { @@ -292,6 +300,7 @@ impl Config { no_format, formatter, + formatter_args, entry, }) diff --git a/src/formatter.rs b/src/formatter.rs index 86b885a..14b5d1a 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -4,11 +4,13 @@ use std::process::{Command, Stdio}; use eyre::{bail, Context, Result}; +use crate::display::display_path; use crate::pipeline::Stage; #[derive(Debug, Clone)] pub struct Formatter { pub exe: PathBuf, + pub args: Vec, } impl Stage for Formatter { @@ -18,15 +20,18 @@ impl Stage for Formatter { fn process(&mut self, code: String) -> Result { let mut p = Command::new(&self.exe) + .args(&self.args) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::inherit()) - .spawn()?; + .spawn() + .with_context(|| format!("failed to run formatter `{}`", display_path(&self.exe)))?; p.stdin .as_mut() .expect("stdin was captured but was also None") - .write_all(code.as_bytes())?; + .write_all(code.as_bytes()) + .context("failed to write bundle to formatter's stdin")?; let p = p.wait_with_output()?;