diff --git a/.github/workflows/check-json.yml b/.github/workflows/check-json.yml new file mode 100644 index 00000000..1f2f6d5d --- /dev/null +++ b/.github/workflows/check-json.yml @@ -0,0 +1,26 @@ +name: Validate JSON Files + +on: + pull_request: + branches: [ master ] + workflow_dispatch: + +jobs: + run-script: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v7 + + - name: Set up Python + uses: actions/setup-python@v7 + with: + python-version: '3.14' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r assets/base/scripts/requirements.txt + + - name: Validate JSON files + run: python assets/base/scripts/validate-lang.py assets/base/lang/ --github diff --git a/assets/base/scripts/requirements.txt b/assets/base/scripts/requirements.txt new file mode 100644 index 00000000..a894311c --- /dev/null +++ b/assets/base/scripts/requirements.txt @@ -0,0 +1 @@ +click>=8.4.2 diff --git a/assets/base/scripts/validate-lang.py b/assets/base/scripts/validate-lang.py index 16501fcc..31fbc67c 100644 --- a/assets/base/scripts/validate-lang.py +++ b/assets/base/scripts/validate-lang.py @@ -1,16 +1,17 @@ import json import os +import sys import click def validate_json_from_file(file_path): try: with open(file_path, 'r', encoding='utf-8') as file: json.load(file) - return True, "Success" + return True, None, "Success" except json.JSONDecodeError as e: - return False, str(e) + return False, e.lineno, str(e) except FileNotFoundError: - return False, "File not found" + return False, None, "File not found" def validate_json_in_directory(directory): results = {} @@ -18,25 +19,30 @@ def validate_json_in_directory(directory): for file in files: if file.endswith(".json"): file_path = os.path.join(root, file) - success, reason = validate_json_from_file(file_path) + success, line, reason = validate_json_from_file(file_path) if not success: - results[file_path] = reason + results[file_path] = (line, reason) return results @click.command() @click.argument('directory_path', default='.') -def main(directory_path): - try: - results = validate_json_in_directory(directory_path) - if len(results) == 0: - print("Language files are valid!") - else: - for path, reason in results.items(): - print(path + ": " + reason) - raise ValueError("Found syntax errors.") - except ValueError as ex: - print("Language files are NOT valid!", ex) - exit(1) +@click.option('--github', is_flag=True, default=False, help="Emit GitHub Actions error annotations") +def main(directory_path, github): + results = validate_json_in_directory(directory_path) + if len(results) == 0: + print("Language files are valid!") + else: + for path, (line, reason) in results.items(): + rel_path = os.path.relpath(path) + if github: + if line: + print(f"::error file={rel_path},line={line}::{reason}") + else: + print(f"::error file={rel_path}::{reason}") + else: + print(f"{path}: {reason}") + print("Language files are NOT valid!") + sys.exit(1) if __name__ == "__main__": main()