crosvm/tools/fmt

43 lines
1.1 KiB
Text
Raw Normal View History

#!/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, cwd, run_main, cmd, chdir
cargo = cmd("cargo")
mdformat = cmd("mdformat")
find = cmd("find")
def main(check: bool = False):
chdir(CROSVM_ROOT)
check_arg = "--check" if check else None
print("Format crosvm workspace")
cargo("fmt --all --", check_arg).fg()
for crate in CROSVM_ROOT.glob("common/*/Cargo.toml"):
print("Format", crate.parent.relative_to(CROSVM_ROOT))
with cwd(crate.parent):
cargo("fmt --", check_arg).fg()
print("Format markdown docs")
mdformat("--wrap 100", check_arg, find(".", "-name *.md -not -path ./third_party/*")).fg()
run_main(main)