Skip to content

Commit 0a3c709

Browse files
phip1611nicholasbishop
authored andcommitted
xtask: add "cargo xtask fmt [--check]" for rust, nix, and yml
1 parent b215a92 commit 0a3c709

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

shell.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pkgs.mkShell {
1818

1919
# Rust toolchain
2020
rustup
21+
22+
# Other
23+
yamlfmt
24+
which # used by "cargo xtask fmt"
2125
];
2226

2327
# Set ENV vars.

xtask/src/main.rs

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ mod qemu;
1111
mod tpm;
1212
mod util;
1313

14-
use crate::opt::TestOpt;
14+
use crate::opt::{FmtOpt, TestOpt};
1515
use anyhow::Result;
1616
use arch::UefiArch;
1717
use cargo::{Cargo, CargoAction, Feature, Package, TargetTypes};
1818
use clap::Parser;
1919
use itertools::Itertools;
2020
use opt::{Action, BuildOpt, ClippyOpt, DocOpt, Opt, QemuOpt, TpmVersion};
21+
use std::process::Command;
2122
use util::run_cmd;
2223

2324
fn build_feature_permutations(opt: &BuildOpt) -> Result<()> {
@@ -206,6 +207,98 @@ fn run_host_tests(test_opt: &TestOpt) -> Result<()> {
206207
run_cmd(cargo.command()?)
207208
}
208209

210+
/// Formats the project: nix, rust, and yml.
211+
fn run_fmt_project(fmt_opt: &FmtOpt) -> Result<()> {
212+
// fmt rust
213+
{
214+
eprintln!("Formatting: rust");
215+
let mut command = Command::new("cargo");
216+
command.arg("fmt");
217+
if fmt_opt.check {
218+
command.arg("--check");
219+
}
220+
command
221+
.arg("--all")
222+
.arg("--")
223+
.arg("--config")
224+
.arg("imports_granularity=Module");
225+
226+
match run_cmd(command) {
227+
Ok(_) => {
228+
eprintln!("✅ rust files format")
229+
}
230+
Err(e) => {
231+
if fmt_opt.check {
232+
eprintln!("❌ rust files to not pass check");
233+
} else {
234+
eprintln!("❌ rust formatter failed: {e:#?}");
235+
}
236+
}
237+
}
238+
}
239+
240+
// fmt yml
241+
if has_cmd("yamlfmt") {
242+
eprintln!("Formatting: yml");
243+
let mut command = Command::new("yamlfmt");
244+
if fmt_opt.check {
245+
command.arg("-lint");
246+
}
247+
command.arg(".");
248+
249+
match run_cmd(command) {
250+
Ok(_) => {
251+
eprintln!("✅ yml files format")
252+
}
253+
Err(e) => {
254+
if fmt_opt.check {
255+
eprintln!("❌ yml files to not pass check");
256+
} else {
257+
eprintln!("❌ yml formatter failed: {e:#?}");
258+
}
259+
}
260+
}
261+
} else {
262+
eprintln!("Formatting: yml - SKIPPED");
263+
}
264+
265+
// fmt nix
266+
if has_cmd("nixpkgs-fmt") {
267+
eprintln!("Formatting: nix");
268+
let mut command = Command::new("nixpkgs-fmt");
269+
if fmt_opt.check {
270+
command.arg("--check");
271+
}
272+
command.arg(".");
273+
274+
match run_cmd(command) {
275+
Ok(_) => {
276+
eprintln!("✅ nix files format")
277+
}
278+
Err(e) => {
279+
if fmt_opt.check {
280+
eprintln!("❌ nix files to not pass check");
281+
} else {
282+
eprintln!("❌ nix formatter failed: {e:#?}");
283+
}
284+
}
285+
}
286+
} else {
287+
eprintln!("Formatting: nix - SKIPPED");
288+
}
289+
290+
Ok(())
291+
}
292+
293+
fn has_cmd(target_cmd: &str) -> bool {
294+
#[cfg(target_os = "windows")]
295+
let mut cmd = Command::new("where");
296+
#[cfg(target_family = "unix")]
297+
let mut cmd = Command::new("which");
298+
cmd.arg(target_cmd);
299+
run_cmd(cmd).is_ok()
300+
}
301+
209302
fn main() -> Result<()> {
210303
let opt = Opt::parse();
211304

@@ -218,5 +311,6 @@ fn main() -> Result<()> {
218311
Action::Miri(_) => run_miri(),
219312
Action::Run(qemu_opt) => run_vm_tests(qemu_opt),
220313
Action::Test(test_opt) => run_host_tests(test_opt),
314+
Action::Fmt(fmt_opt) => run_fmt_project(fmt_opt),
221315
}
222316
}

xtask/src/opt.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub enum Action {
7373
Miri(MiriOpt),
7474
Run(QemuOpt),
7575
Test(TestOpt),
76+
Fmt(FmtOpt),
7677
}
7778

7879
/// Build all the uefi packages.
@@ -193,3 +194,11 @@ pub struct TestOpt {
193194
#[clap(long, action)]
194195
pub skip_macro_tests: bool,
195196
}
197+
198+
/// Run formatting on the repo.
199+
#[derive(Debug, Parser)]
200+
pub struct FmtOpt {
201+
/// Just check but do not write files.
202+
#[clap(long, action)]
203+
pub check: bool,
204+
}

0 commit comments

Comments
 (0)