Skip to content

Commit d7c8e0f

Browse files
authored
Merge pull request #510 from GuillaumeGomez/fmt-cmd
Add `fmt` command
2 parents aee803b + 7e369b3 commit d7c8e0f

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

Diff for: .github/workflows/ci.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ jobs:
103103
./y.sh test --release --clean --build-sysroot ${{ matrix.commands }}
104104
105105
- name: Check formatting
106-
run: |
107-
cargo fmt -- --check
108-
cd build_system
109-
cargo fmt -- --check
106+
run: ./y.sh fmt --check
110107

111108
- name: clippy
112109
run: |

Diff for: build_system/src/fmt.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::utils::run_command_with_output;
2+
use std::ffi::OsStr;
3+
use std::path::Path;
4+
5+
fn show_usage() {
6+
println!(
7+
r#"
8+
`fmt` command help:
9+
10+
--check : Pass `--check` argument to `cargo fmt` commands
11+
--help : Show this help"#
12+
);
13+
}
14+
15+
pub fn run() -> Result<(), String> {
16+
let mut check = false;
17+
// We skip binary name and the `info` command.
18+
let mut args = std::env::args().skip(2);
19+
while let Some(arg) = args.next() {
20+
match arg.as_str() {
21+
"--help" => {
22+
show_usage();
23+
return Ok(());
24+
}
25+
"--check" => check = true,
26+
_ => return Err(format!("Unknown option {}", arg)),
27+
}
28+
}
29+
30+
let cmd: &[&dyn AsRef<OsStr>] =
31+
if check { &[&"cargo", &"fmt", &"--check"] } else { &[&"cargo", &"fmt"] };
32+
33+
run_command_with_output(cmd, Some(&Path::new(".")))?;
34+
run_command_with_output(cmd, Some(&Path::new("build_system")))
35+
}

Diff for: build_system/src/main.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod build;
55
mod clean;
66
mod clone_gcc;
77
mod config;
8+
mod fmt;
89
mod info;
910
mod prepare;
1011
mod rust_tools;
@@ -41,7 +42,8 @@ Commands:
4142
build : Compiles the project.
4243
test : Runs tests for the project.
4344
info : Displays information about the build environment and project configuration.
44-
clone-gcc : Clones the GCC compiler from a specified source."
45+
clone-gcc : Clones the GCC compiler from a specified source.
46+
fmt : Runs rustfmt"
4547
);
4648
}
4749

@@ -54,6 +56,7 @@ pub enum Command {
5456
Rustc,
5557
Test,
5658
Info,
59+
Fmt,
5760
}
5861

5962
fn main() {
@@ -70,6 +73,7 @@ fn main() {
7073
Some("test") => Command::Test,
7174
Some("info") => Command::Info,
7275
Some("clone-gcc") => Command::CloneGcc,
76+
Some("fmt") => Command::Fmt,
7377
Some("--help") => {
7478
usage();
7579
process::exit(0);
@@ -91,6 +95,7 @@ fn main() {
9195
Command::Test => test::run(),
9296
Command::Info => info::run(),
9397
Command::CloneGcc => clone_gcc::run(),
98+
Command::Fmt => fmt::run(),
9499
} {
95100
eprintln!("Command failed to run: {e}");
96101
process::exit(1);

0 commit comments

Comments
 (0)