crosvm/tools/fmt
Dennis Kempin f2b8cb84bd Add --nightly option to rustfmt
This allows you to use the nightly version of rustfmt
to reformat imports as well.

BUG=None
TEST=./tools/fmt --nightly

Change-Id: I07c2fd1ad59ddfed3d97b636f991ae50076fd5d3
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3792430
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Auto-Submit: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: Dennis Kempin <denniskempin@google.com>
2022-07-28 23:06:51 +00:00

74 lines
1.9 KiB
Python
Executable file

#!/usr/bin/env python3
# Copyright 2022 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Run `rustfmt` on all Rust code contained in the crosvm workspace, including
# all commmon/* crates as well.
#
# Usage:
#
# $ bin/fmt
#
# To print a diff and exit 1 if code is not formatted, but without changing any
# files, use:
#
# $ bin/fmt --check
#
from impl.common import (
CROSVM_ROOT,
find_scripts,
parallel,
run_main,
cmd,
chdir,
find_source_files,
)
from pathlib import Path
mdformat = cmd("mdformat")
black = cmd("black", "--line-length 100")
# How many files to check at once in each thread.
BATCH_SIZE = 8
# Files not under our control or auto-generated.
IGNORE = [
"docs/book/src/appendix/memory_layout.md", # mdformat messes up the tables.
"infra/README.recipes.md",
"infra/recipes.py",
]
def main(check: bool = False, nightly: bool = False):
chdir(CROSVM_ROOT)
check_arg = "--check" if check else None
if nightly:
rustfmt = cmd(
cmd("rustup +nightly which rustfmt"),
"--config imports_granularity=item,group_imports=StdExternalCrate",
)
else:
rustfmt = cmd(cmd("rustup which rustfmt"))
print(f"{'Checking format' if check else 'Formatting'}: Rust, Markdown")
parallel(
*rustfmt(check_arg).foreach(find_source_files("rs"), batch_size=BATCH_SIZE),
*mdformat("--wrap 100", check_arg).foreach(
find_source_files("md", IGNORE),
batch_size=BATCH_SIZE,
),
*black(check_arg).foreach(
(
*find_source_files("py", IGNORE),
*find_scripts(Path("tools"), "/usr/bin/env python3"),
),
batch_size=BATCH_SIZE,
),
).fg()
if __name__ == "__main__":
run_main(main)