about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--build_system/src/fmt.rs35
-rw-r--r--build_system/src/main.rs7
2 files changed, 41 insertions, 1 deletions
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);