Skip to content

Commit

Permalink
feat: allow specifying arguments for the formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
threadexio committed Jan 2, 2025
1 parent 394f6c9 commit b1b1578
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cbundl.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ struct QuoteSection {
struct FormatterSection {
enable: Option<bool>,
path: Option<PathBuf>,
args: Option<Vec<String>>,
}

impl File {
Expand Down Expand Up @@ -176,6 +177,7 @@ pub struct Config {

pub no_format: bool,
pub formatter: PathBuf,
pub formatter_args: Vec<String>,

pub entry: PathBuf,
}
Expand Down Expand Up @@ -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::<PathBuf>("entry").unwrap().clone();

Ok(Self {
Expand All @@ -292,6 +300,7 @@ impl Config {

no_format,
formatter,
formatter_args,

entry,
})
Expand Down
9 changes: 7 additions & 2 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

impl Stage for Formatter {
Expand All @@ -18,15 +20,18 @@ impl Stage for Formatter {

fn process(&mut self, code: String) -> Result<String> {
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()?;

Expand Down

0 comments on commit b1b1578

Please sign in to comment.