Compile the upstream Quirrel checker with MSVC and invoke syntax and static analysis for each input file. Treat reported errors and warnings as failures because the checker can return a zero exit code for them. Split the command line by purpose and add GUI pack construction from declared edits and injected scripts. Verify the checker against stock scripts and invalid inputs, and confirm that patch checks still run.
50 lines
1.9 KiB
Python
50 lines
1.9 KiB
Python
"""Commands for syntax checking game scripts.
|
|
|
|
wtotools build-script-checker [--source build/quirrel-source] [--out build/quirrel-check]
|
|
wtotools check-scripts [--checker build/quirrel-check/sq.exe] <file or folder>...
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from ..scriptcheck import CheckerBuildError, build_checker, check_scripts, find_scripts
|
|
|
|
DEFAULT_SOURCE = Path("build/quirrel-source")
|
|
DEFAULT_OUT = Path("build/quirrel-check")
|
|
|
|
|
|
def register(commands: argparse._SubParsersAction) -> None:
|
|
build = commands.add_parser("build-script-checker", help="compile the Quirrel syntax checker with MSVC")
|
|
build.add_argument("--source", type=Path, default=DEFAULT_SOURCE, help="Quirrel source checkout")
|
|
build.add_argument("--out", type=Path, default=DEFAULT_OUT, help="where to put sq.exe")
|
|
build.set_defaults(handler=_build)
|
|
|
|
check = commands.add_parser("check-scripts", help="syntax-check .nut files")
|
|
check.add_argument("paths", type=Path, nargs="+", help=".nut files or folders containing them")
|
|
check.add_argument("--checker", type=Path, default=DEFAULT_OUT / "sq.exe", help="the compiled checker")
|
|
check.set_defaults(handler=_check)
|
|
|
|
|
|
def _build(args: argparse.Namespace) -> int:
|
|
try:
|
|
checker = build_checker(args.source, args.out)
|
|
except CheckerBuildError as error:
|
|
print(error, file=sys.stderr)
|
|
return 1
|
|
print(f"built {checker}")
|
|
return 0
|
|
|
|
|
|
def _check(args: argparse.Namespace) -> int:
|
|
if not args.checker.is_file():
|
|
print(f"checker not found at {args.checker}; run build-script-checker first", file=sys.stderr)
|
|
return 2
|
|
files = find_scripts(args.paths)
|
|
failures = check_scripts(args.checker, files)
|
|
for failure in failures:
|
|
print(f"FAIL {failure.path}\n {failure.message}")
|
|
print(f"{len(files) - len(failures)}/{len(files)} file(s) pass syntax and static checks")
|
|
return 1 if failures else 0
|