about summary refs log tree commit diff
diff options
context:
space:
mode:
authorantoyo <antoyo@users.noreply.github.com>2024-05-03 08:07:47 -0400
committerGitHub <noreply@github.com>2024-05-03 08:07:47 -0400
commitd7c8e0fb434144b441e48f79eb2d728b1eab298a (patch)
tree9437e67fba7d36c7122edff46ae8bbd0e779edaf
parentaee803b5427583e06694caba78cb8bcbd222183f (diff)
parent7e369b314bf7f08640febc341731ebd8b8a34236 (diff)
downloadrust-d7c8e0fb434144b441e48f79eb2d728b1eab298a.tar.gz
rust-d7c8e0fb434144b441e48f79eb2d728b1eab298a.zip
Merge pull request #510 from GuillaumeGomez/fmt-cmd
Add `fmt` command
-rw-r--r--.github/workflows/ci.yml5
-rw-r--r--build_system/src/fmt.rs35
-rw-r--r--build_system/src/main.rs7
3 files changed, 42 insertions, 5 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 835e055e8c5..cd366dbae16 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -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: |
diff --git a/build_system/src/fmt.rs b/build_system/src/fmt.rs
new file mode 100644
index 00000000000..43644ba19b3
--- /dev/null
+++ b/build_system/src/fmt.rs
@@ -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")))
+}
diff --git a/build_system/src/main.rs b/build_system/src/main.rs
index 9a30490f621..d678fd75344 100644
--- a/build_system/src/main.rs
+++ b/build_system/src/main.rs
@@ -5,6 +5,7 @@ mod build;
 mod clean;
 mod clone_gcc;
 mod config;
+mod fmt;
 mod info;
 mod prepare;
 mod rust_tools;
@@ -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"
     );
 }
 
@@ -54,6 +56,7 @@ pub enum Command {
     Rustc,
     Test,
     Info,
+    Fmt,
 }
 
 fn main() {
@@ -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);
@@ -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);