bin: Add script to run rustfmt against all workspaces

Add a script to run `cargo fmt` on all Rust code contained in crosvm.
This is different from `cargo fmt --all` which formats multiple crates
but a single workspace only. Crosvm consists of multiple workspaces.

Usage:

    $ bin/fmt

To print a diff and exit 1 if code is not formatted, but without
changing any files, use:

    $ bin/fmt --check

TEST=those commands
TEST=local kokoro

Change-Id: I4194509ad3a1bbc829c4b1069d54d940b927113b
Reviewed-on: https://chromium-review.googlesource.com/1477498
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
This commit is contained in:
David Tolnay 2019-02-19 17:08:53 -08:00 committed by chrome-bot
parent fc7427eb2b
commit 18ce5713e6
3 changed files with 46 additions and 3 deletions

View file

@ -138,8 +138,11 @@ for each architecture. See `build_test -h` for more information.
#### `rustfmt`
All code should be formatted with `rustfmt`. Run `cargo fmt --all` to autoformat
your code before checking in a change.
All code should be formatted with `rustfmt`. We have a script that applies
rustfmt to all Rust code in the crosvm repo: please run `bin/fmt` before
checking in a change. This is different from `cargo fmt --all` which formats
multiple crates but a single workspace only; crosvm consists of multiple
workspaces.
#### Dependencies

40
bin/fmt Executable file
View file

@ -0,0 +1,40 @@
#!/bin/bash
# Run `cargo fmt` on all Rust code contained in crosvm. This is different from
# `cargo fmt --all` which formats multiple crates but a single workspace only.
# Crosvm consists of multiple workspaces.
#
# Usage:
#
# $ bin/fmt
#
# To print a diff and exit 1 if code is not formatted, but without changing any
# files, use:
#
# $ bin/fmt --check
#
set -euo pipefail
# Change into directory of script, which is crosvm/bin.
cd "$(dirname "${BASH_SOURCE[0]}")"
# Jump up to root directory of crosvm repo.
cd ..
# Keep track of whether any cargo fmt invocation exited with error.
EXIT=0
FIND_CARGO_TOMLS="$(find "$PWD" -name Cargo.toml)"
while read path_to_cargo_toml; do
cd "$(dirname "$path_to_cargo_toml")"
if grep --quiet '\[workspace\]' Cargo.toml; then
if ! cargo fmt --all -- "$@"; then
EXIT=1
fi
fi
done <<< "$FIND_CARGO_TOMLS"
exit $EXIT

View file

@ -131,4 +131,4 @@ CMD rustup default "$(cat rust-toolchain)" && \
cargo test --no-fail-fast --all-features --all --exclude aarch64 $TEST_FLAGS -- \
--test-threads=1 $TEST_RUNNER_FLAGS && \
echo "Running cargo fmt" && \
cargo fmt --all -- --check
bin/fmt --check