2019-02-12 15:20:32 -05:00
|
|
|
#!/usr/bin/env python
|
2020-01-02 18:41:59 -05:00
|
|
|
# Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-02-12 15:20:32 -05:00
|
|
|
import os
|
|
|
|
import sys
|
2019-09-11 16:47:42 -04:00
|
|
|
import argparse
|
2020-01-05 09:19:29 -05:00
|
|
|
from third_party import python_env
|
2020-05-09 06:22:27 -04:00
|
|
|
from util import git_ls_files, git_staged, third_party_path, root_path
|
|
|
|
from util import print_command, run
|
2019-02-12 15:20:32 -05:00
|
|
|
|
2020-05-09 06:22:27 -04:00
|
|
|
cmd_args = None
|
2019-07-18 15:00:50 -04:00
|
|
|
|
2020-05-09 06:22:27 -04:00
|
|
|
|
|
|
|
def get_cmd_args():
|
|
|
|
global cmd_args
|
|
|
|
|
|
|
|
if cmd_args:
|
|
|
|
return cmd_args
|
2019-09-14 10:25:49 -04:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
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")
|
2020-05-09 06:22:27 -04:00
|
|
|
parser.add_argument(
|
|
|
|
"--staged", help="run only on staged files", action="store_true")
|
|
|
|
cmd_args = parser.parse_args()
|
|
|
|
return cmd_args
|
|
|
|
|
|
|
|
|
|
|
|
def get_sources(*args):
|
|
|
|
getter = git_staged if get_cmd_args().staged else git_ls_files
|
|
|
|
return getter(*args)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
os.chdir(root_path)
|
|
|
|
|
|
|
|
args = get_cmd_args()
|
2019-09-14 10:25:49 -04:00
|
|
|
|
2019-07-18 15:00:50 -04:00
|
|
|
did_fmt = False
|
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:
|
|
|
|
prettier()
|
2019-09-14 10:25:49 -04:00
|
|
|
yapf()
|
|
|
|
rustfmt()
|
2019-02-12 15:20:32 -05:00
|
|
|
|
|
|
|
|
2019-09-14 10:25:49 -04:00
|
|
|
def prettier():
|
|
|
|
script = os.path.join(third_party_path, "node_modules", "prettier",
|
|
|
|
"bin-prettier.js")
|
2020-05-09 06:22:27 -04:00
|
|
|
source_files = get_sources(root_path, ["*.js", "*.json", "*.ts", "*.md"])
|
|
|
|
if source_files:
|
2020-06-06 07:20:29 -04:00
|
|
|
max_command_length = 24000
|
|
|
|
while len(source_files) > 0:
|
|
|
|
command = ["node", script, "--write", "--loglevel=error", "--"]
|
|
|
|
while len(source_files) > 0:
|
|
|
|
command.append(source_files.pop())
|
|
|
|
if len(" ".join(command)) > max_command_length:
|
|
|
|
break
|
2020-06-09 11:50:41 -04:00
|
|
|
run(command, shell=False, quiet=True)
|
2019-07-18 15:00:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
def yapf():
|
2019-09-14 10:25:49 -04:00
|
|
|
script = os.path.join(third_party_path, "python_packages", "bin", "yapf")
|
2020-05-09 06:22:27 -04:00
|
|
|
source_files = get_sources(root_path, ["*.py"])
|
|
|
|
if source_files:
|
|
|
|
print_command("yapf", source_files)
|
|
|
|
run([sys.executable, script, "-i", "--style=pep8", "--"] +
|
|
|
|
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():
|
|
|
|
config_file = os.path.join(root_path, ".rustfmt.toml")
|
2020-05-09 06:22:27 -04:00
|
|
|
source_files = get_sources(root_path, ["*.rs"])
|
|
|
|
if source_files:
|
|
|
|
print_command("rustfmt", source_files)
|
|
|
|
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())
|