Skip to content

Add fmt command #510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"] };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we just forward the arguments to cargo fmt directly instead of handling this ourselves?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we only want --check and --help, so better handle the available options ourselves imo.


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
Loading