2019-02-12 15:20:32 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import os
|
|
|
|
import sys
|
2019-09-11 16:47:42 -04:00
|
|
|
import argparse
|
2019-03-04 23:24:51 -05:00
|
|
|
from third_party import google_env, python_env
|
2019-09-14 10:25:49 -04:00
|
|
|
from util import git_ls_files, third_party_path, root_path, run
|
2019-02-12 15:20:32 -05:00
|
|
|
|
2019-07-18 15:00:50 -04:00
|
|
|
|
|
|
|
def main():
|
|
|
|
os.chdir(root_path)
|
2019-09-14 10:25:49 -04:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("--cc", help="run clang-format", action="store_true")
|
|
|
|
parser.add_argument("--gn", help="run gn format", action="store_true")
|
|
|
|
parser.add_argument("--js", help="run prettier", action="store_true")
|
|
|
|
parser.add_argument("--py", help="run yapf", action="store_true")
|
|
|
|
parser.add_argument("--rs", help="run rustfmt", action="store_true")
|
2019-07-18 15:00:50 -04:00
|
|
|
args = parser.parse_args()
|
2019-09-14 10:25:49 -04:00
|
|
|
|
2019-07-18 15:00:50 -04:00
|
|
|
did_fmt = False
|
|
|
|
if args.cc:
|
|
|
|
clang_format()
|
|
|
|
did_fmt = True
|
|
|
|
if args.gn:
|
|
|
|
gn_format()
|
|
|
|
did_fmt = True
|
2019-09-14 10:25:49 -04:00
|
|
|
if args.js:
|
|
|
|
prettier()
|
|
|
|
did_fmt = True
|
2019-07-18 15:00:50 -04:00
|
|
|
if args.py:
|
|
|
|
yapf()
|
|
|
|
did_fmt = True
|
2019-09-14 10:25:49 -04:00
|
|
|
if args.rs:
|
|
|
|
rustfmt()
|
2019-07-18 15:00:50 -04:00
|
|
|
did_fmt = True
|
2019-09-14 10:25:49 -04:00
|
|
|
|
2019-07-18 15:00:50 -04:00
|
|
|
if not did_fmt:
|
|
|
|
clang_format()
|
|
|
|
gn_format()
|
|
|
|
prettier()
|
2019-09-14 10:25:49 -04:00
|
|
|
yapf()
|
|
|
|
rustfmt()
|
2019-02-12 15:20:32 -05:00
|
|
|
|
|
|
|
|
2019-07-18 15:00:50 -04:00
|
|
|
def clang_format():
|
|
|
|
print "clang_format"
|
2019-09-14 10:25:49 -04:00
|
|
|
exe = os.path.join(third_party_path, "depot_tools", "clang-format")
|
|
|
|
source_files = git_ls_files(root_path, ["*.cc", "*.h"])
|
|
|
|
run([exe, "-i", "-style", "Google", "--"] + source_files,
|
|
|
|
env=google_env(),
|
|
|
|
quiet=True)
|
2019-07-18 15:00:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
def gn_format():
|
|
|
|
print "gn format"
|
2019-09-14 10:25:49 -04:00
|
|
|
exe = os.path.join(third_party_path, "depot_tools", "gn")
|
|
|
|
source_files = git_ls_files(root_path, ["*.gn", "*.gni"])
|
|
|
|
run([exe, "format", "--"] + source_files, env=google_env(), quiet=True)
|
|
|
|
|
|
|
|
|
|
|
|
def prettier():
|
|
|
|
print "prettier"
|
|
|
|
script = os.path.join(third_party_path, "node_modules", "prettier",
|
|
|
|
"bin-prettier.js")
|
|
|
|
source_files = git_ls_files(root_path, ["*.js", "*.json", "*.ts", "*.md"])
|
|
|
|
run(["node", script, "--write", "--loglevel=error", "--"] + source_files,
|
|
|
|
shell=False,
|
|
|
|
quiet=True)
|
2019-07-18 15:00:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
def yapf():
|
|
|
|
print "yapf"
|
2019-09-14 10:25:49 -04:00
|
|
|
script = os.path.join(third_party_path, "python_packages", "bin", "yapf")
|
|
|
|
source_files = git_ls_files(root_path, ["*.py"])
|
|
|
|
run([sys.executable, script, "-i", "--"] + source_files,
|
|
|
|
env=python_env(),
|
|
|
|
shell=False,
|
|
|
|
quiet=True)
|
2019-07-18 15:00:50 -04:00
|
|
|
|
|
|
|
|
2019-09-14 10:25:49 -04:00
|
|
|
def rustfmt():
|
|
|
|
print "rustfmt"
|
|
|
|
config_file = os.path.join(root_path, ".rustfmt.toml")
|
|
|
|
source_files = git_ls_files(root_path, ["*.rs"])
|
|
|
|
run([
|
|
|
|
"rustfmt",
|
|
|
|
"--config-path=" + config_file,
|
|
|
|
"--",
|
|
|
|
] + source_files,
|
|
|
|
shell=False,
|
|
|
|
quiet=True)
|
2019-07-18 15:00:50 -04:00
|
|
|
|
|
|
|
|
2019-09-14 10:25:49 -04:00
|
|
|
if __name__ == "__main__":
|
2019-07-18 15:00:50 -04:00
|
|
|
sys.exit(main())
|