Skip to content

Commit

Permalink
Merge pull request rust-lang#510 from GuillaumeGomez/fmt-cmd
Browse files Browse the repository at this point in the history
Add `fmt` command
  • Loading branch information
antoyo authored May 3, 2024
2 parents aee803b + 7e369b3 commit d7c8e0f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ jobs:
./y.sh test --release --clean --build-sysroot ${{ matrix.commands }}
- name: Check formatting
run: |
cargo fmt -- --check
cd build_system
cargo fmt -- --check
run: ./y.sh fmt --check

- name: clippy
run: |
Expand Down
35 changes: 35 additions & 0 deletions build_system/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::utils::run_command_with_output;
use std::ffi::OsStr;
use std::path::Path;

fn show_usage() {
println!(
r#"
`fmt` command help:
--check : Pass `--check` argument to `cargo fmt` commands
--help : Show this help"#
);
}

pub fn run() -> Result<(), String> {
let mut check = false;
// We skip binary name and the `info` command.
let mut args = std::env::args().skip(2);
while let Some(arg) = args.next() {
match arg.as_str() {
"--help" => {
show_usage();
return Ok(());
}
"--check" => check = true,
_ => return Err(format!("Unknown option {}", arg)),
}
}

let cmd: &[&dyn AsRef<OsStr>] =
if check { &[&"cargo", &"fmt", &"--check"] } else { &[&"cargo", &"fmt"] };

run_command_with_output(cmd, Some(&Path::new(".")))?;
run_command_with_output(cmd, Some(&Path::new("build_system")))
}
7 changes: 6 additions & 1 deletion build_system/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod build;
mod clean;
mod clone_gcc;
mod config;
mod fmt;
mod info;
mod prepare;
mod rust_tools;
Expand Down Expand Up @@ -41,7 +42,8 @@ Commands:
build : Compiles the project.
test : Runs tests for the project.
info : Displays information about the build environment and project configuration.
clone-gcc : Clones the GCC compiler from a specified source."
clone-gcc : Clones the GCC compiler from a specified source.
fmt : Runs rustfmt"
);
}

Expand All @@ -54,6 +56,7 @@ pub enum Command {
Rustc,
Test,
Info,
Fmt,
}

fn main() {
Expand All @@ -70,6 +73,7 @@ fn main() {
Some("test") => Command::Test,
Some("info") => Command::Info,
Some("clone-gcc") => Command::CloneGcc,
Some("fmt") => Command::Fmt,
Some("--help") => {
usage();
process::exit(0);
Expand All @@ -91,6 +95,7 @@ fn main() {
Command::Test => test::run(),
Command::Info => info::run(),
Command::CloneGcc => clone_gcc::run(),
Command::Fmt => fmt::run(),
} {
eprintln!("Command failed to run: {e}");
process::exit(1);
Expand Down

0 comments on commit d7c8e0f

Please sign in to comment.