summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@chromium.org>2019-02-19 17:08:53 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-02-21 06:29:42 -0800
commit18ce5713e6cb99c40aafec52b67c28ba12a44f31 (patch)
tree12e5e21344a2b942edf9de4cc7a234a6efc84b0a /bin
parentfc7427eb2b7c262722bbc0c2e9d062c48203d7a2 (diff)
downloadcrosvm-18ce5713e6cb99c40aafec52b67c28ba12a44f31.tar
crosvm-18ce5713e6cb99c40aafec52b67c28ba12a44f31.tar.gz
crosvm-18ce5713e6cb99c40aafec52b67c28ba12a44f31.tar.bz2
crosvm-18ce5713e6cb99c40aafec52b67c28ba12a44f31.tar.lz
crosvm-18ce5713e6cb99c40aafec52b67c28ba12a44f31.tar.xz
crosvm-18ce5713e6cb99c40aafec52b67c28ba12a44f31.tar.zst
crosvm-18ce5713e6cb99c40aafec52b67c28ba12a44f31.zip
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>
Diffstat (limited to 'bin')
-rwxr-xr-xbin/fmt40
1 files changed, 40 insertions, 0 deletions
diff --git a/bin/fmt b/bin/fmt
new file mode 100755
index 0000000..37e6070
--- /dev/null
+++ b/bin/fmt
@@ -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