Add format subcommand to xtask (#1551)

This commit is contained in:
Kirill Mikhailov 2024-05-14 15:49:26 +02:00 committed by GitHub
parent 58f40e95e5
commit 68628fc008
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 5 deletions

View File

@ -5,6 +5,7 @@ To help us review it efficiently, please ensure you've gone through the followin
### Submission Checklist 📝
- [ ] I have updated existing examples or added new ones (if applicable).
- [ ] I have used `cargo xtask fmt-packages` command to ensure that all changed code is formatted correctly.
- [ ] My changes were added to the [`CHANGELOG.md`](https://github.com/esp-rs/esp-hal/blob/main/esp-hal/CHANGELOG.md) in the **_proper_** section.
#### Extra:
- [ ] I have read the [CONTRIBUTING.md guide](https://github.com/esp-rs/esp-hal/blob/main/CONTRIBUTING.md) and followed its instructions.

View File

@ -69,15 +69,14 @@ rustup component add rustfmt
rustup component add clippy
```
We _strongly_ recommend that you use the supplied `pre-commit` Git hook, which will ensure that all source code has been formatted correctly prior to committing. See the [Git documentation] for more information on hooks.
The `pre-commit` hook can be installed by running the following command in a terminal, from the root of the repository:
We _strongly_ recommend that you format your code before committing to ensure consistency throughout the project.
To format all packages in the workspace, run the following command in a terminal from the root of the repository:
```shell
cp pre-commit .git/hooks/pre-commit
cargo xtask fmt-workspace
```
[Git documentation]: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
This will use `rustfmt` to ensure that all source code is formatted correctly prior to committing.
### Pull Request

View File

@ -12,6 +12,7 @@ Commands:
build-examples Build all examples for the specified chip
build-package Build the specified package with the given options
bump-version Bump the version of the specified package(s)
fmt-packages Format all packages in the workspace with rustfmt
generate-efuse-fields Generate the eFuse fields source file from a CSV
run-example Run the given example for the specified chip
run-tests Run all applicable tests or the specified test for a specified chip

View File

@ -24,6 +24,8 @@ enum Cli {
BuildTests(TestArgs),
/// Bump the version of the specified package(s).
BumpVersion(BumpVersionArgs),
/// Format all packages in the workspace with rustfmt
FmtPackages,
/// Generate the eFuse fields source file from a CSV.
GenerateEfuseFields(GenerateEfuseFieldsArgs),
/// Run the given example for the specified chip.
@ -133,6 +135,7 @@ fn main() -> Result<()> {
Cli::BuildPackage(args) => build_package(&workspace, args),
Cli::BuildTests(args) => tests(&workspace, args, CargoAction::Build),
Cli::BumpVersion(args) => bump_version(&workspace, args),
Cli::FmtPackages => fmt_packages(&workspace),
Cli::GenerateEfuseFields(args) => generate_efuse_src(&workspace, args),
Cli::RunExample(args) => examples(&workspace, args, CargoAction::Run),
Cli::RunTests(args) => tests(&workspace, args, CargoAction::Run),
@ -427,6 +430,37 @@ fn run_elfs(args: RunElfArgs) -> Result<()> {
Ok(())
}
fn fmt_packages(workspace: &Path) -> Result<()> {
// Ensure that `rustfmt` is installed
if Command::new("rustfmt").arg("--version").output().is_err() {
bail!("The 'rustfmt' command is not installed, exiting");
}
// Iterate over all Cargo.toml files in the workspace and format the projects
for entry in fs::read_dir(workspace)? {
let entry = entry?;
if entry.file_type()?.is_dir() {
let manifest_path = entry.path().join("Cargo.toml");
if manifest_path.exists() {
// Run `cargo fmt` on the project
let status = Command::new("cargo")
.arg("+nightly")
.arg("fmt")
.arg("--all")
.arg("--manifest-path")
.arg(manifest_path.to_str().unwrap())
.status()?;
if !status.success() {
bail!("Formatting failed for {}", manifest_path.display());
}
}
}
}
Ok(())
}
// ----------------------------------------------------------------------------
// Helper Functions