import argparse
import pathlib
import subprocess
import sys
import typing as T
WORKDIR = pathlib.Path("/tmp")
OLD_SUFFIX = "-old"
NEW_SUFFIX = "-new"
EXTENSIONS = {
"tree-sitter-tsx": ["*.tsx"],
"tree-sitter-typescript": ["*.ts", "*.jsw", "*.jsmw"],
"tree-sitter-java": ["*.java"],
"tree-sitter-rust": ["*.rs"],
"tree-sitter-python": ["*.py"],
"tree-sitter-mozjs": ["*.js", "*.js2", "*.jsm", "*.mjs", "*.jsx"],
"tree-sitter-mozcpp": [
"*.cpp",
"*.cx",
"*.cxx",
"*.cc",
"*.hxx",
"*.hpp",
"*.c",
"*.h",
"*.hh",
"*.inc",
"*.mm",
"*.m",
],
}
def run_subprocess(cmd: str, *args: T.Union[str, pathlib.Path]) -> None:
subprocess.run([cmd, *args])
def run_rca(
repo_dir: pathlib.Path,
output_dir: pathlib.Path,
manifest_path: T.Optional[pathlib.Path],
include_grammars: T.List[str],
) -> None:
run_subprocess(
"cargo",
"run",
"--manifest-path",
manifest_path / "Cargo.toml" if manifest_path else "Cargo.toml",
"--release",
"--package",
"rust-code-analysis-cli",
"--",
"--metrics",
"--output-format=json",
"--pr",
"-I",
*include_grammars,
"-p",
repo_dir,
"-o",
output_dir,
)
def compute_ci_metrics(args: argparse.Namespace) -> None:
if args.grammar != "tree-sitter" and args.grammar not in EXTENSIONS.keys():
print(args.grammar, "is not a valid tree-sitter grammar")
sys.exit(1)
if args.grammar == "tree-sitter":
grammar = "tree-sitter-mozcpp"
else:
grammar = args.grammar
repo_dir = pathlib.Path(args.path)
rca_path = WORKDIR / "rust-code-analysis"
old_dir = WORKDIR / (args.grammar + OLD_SUFFIX)
new_dir = WORKDIR / (args.grammar + NEW_SUFFIX)
old_dir.mkdir(parents=True, exist_ok=True)
new_dir.mkdir(parents=True, exist_ok=True)
print(f"Cloning rust-code-analysis master branch into /tmp")
run_subprocess(
"git",
"clone",
"--depth=1",
"-j8",
"https://github.com/mozilla/rust-code-analysis",
rca_path,
)
print("\nComputing metrics before the update and saving them in", old_dir)
run_rca(repo_dir, old_dir, rca_path, EXTENSIONS[grammar])
print("\nComputing metrics after the update and saving them in", new_dir)
run_rca(repo_dir, new_dir, None, EXTENSIONS[grammar])
def compute_metrics(args: argparse.Namespace) -> None:
if args.grammar not in EXTENSIONS.keys():
print(args.grammar, "is not a valid tree-sitter grammar")
sys.exit(1)
repo_dir = WORKDIR / args.path
old_dir = WORKDIR / (args.grammar + OLD_SUFFIX)
new_dir = WORKDIR / (args.grammar + NEW_SUFFIX)
old_dir.mkdir(parents=True, exist_ok=True)
new_dir.mkdir(parents=True, exist_ok=True)
if not args.only_new:
print(f"Cloning {args.url} into {repo_dir}")
run_subprocess("git", "clone", "--depth=1", args.url, repo_dir)
print("\nComputing metrics before the update and saving them in", old_dir)
run_rca(repo_dir, old_dir, None, EXTENSIONS[args.grammar])
print("\nCreate a new branch called", args.grammar)
run_subprocess("git", "checkout", "-B", args.grammar)
print("\nComputing metrics after the update and saving them in", new_dir)
run_rca(repo_dir, new_dir, None, EXTENSIONS[args.grammar])
def compare_metrics(args: argparse.Namespace) -> None:
old_dir = WORKDIR / (args.grammar + OLD_SUFFIX)
new_dir = WORKDIR / (args.grammar + NEW_SUFFIX)
compare_dir = WORKDIR / (args.grammar + "-compare")
compare_dir.mkdir(parents=True, exist_ok=True)
print("\nSave minimal tests in", compare_dir)
run_subprocess("json-minimal-tests", "-o", compare_dir, old_dir, new_dir)
def main() -> None:
parser = argparse.ArgumentParser(
prog="check-grammar-crate",
description="This tool computes the metrics of a chosen repository "
"before and after a tree-sitter grammar update.",
epilog="The source code of this program can be found on "
"GitHub at https://github.com/mozilla/rust-code-analysis",
)
commands = parser.add_subparsers(help="Sub-command help")
compute_metrics_cmd = commands.add_parser(
"compute-metrics",
help="Computes the metrics of a chosen repository before and after "
"a tree-sitter grammar update.",
)
compute_metrics_cmd.add_argument(
"--only-new",
"-n",
action="store_true",
help="Only compute the metrics after a tree-sitter grammar update",
)
compute_metrics_cmd.add_argument(
"-u",
"--url",
type=str,
required=True,
help="URL of the repository used to compute the metrics",
)
compute_metrics_cmd.add_argument(
"-p",
"--path",
type=str,
required=True,
help="Path where the repository will be saved locally",
)
compute_metrics_cmd.add_argument(
"-g",
"--grammar",
type=str,
required=True,
help="tree-sitter grammar to be updated",
)
compute_metrics_cmd.set_defaults(func=compute_metrics)
compute_ci_metrics_cmd = commands.add_parser(
"compute-ci-metrics",
help="Computes the metrics of a chosen repository before and after "
"a tree-sitter grammar update on a continuous integration system.",
)
compute_ci_metrics_cmd.add_argument(
"-p",
"--path",
type=str,
required=True,
help="Path where the rust-code-analysis repository is saved on the "
"continuous integration system",
)
compute_ci_metrics_cmd.add_argument(
"-g",
"--grammar",
type=str,
required=True,
help="tree-sitter grammar to be updated",
)
compute_ci_metrics_cmd.set_defaults(func=compute_ci_metrics)
compare_metrics_cmd = commands.add_parser(
"compare-metrics",
help="Compares the metrics before and after "
"a tree-sitter grammar update in order to discover whether "
"there are differences.",
)
compare_metrics_cmd.add_argument(
"-g",
"--grammar",
type=str,
required=True,
help="tree-sitter grammar used to compare the metrics",
)
compare_metrics_cmd.set_defaults(func=compare_metrics)
args = parser.parse_args()
args.func(args)
if __name__ == "__main__":
main()