chore: Add resource validation workflow and script

This commit is contained in:
2024-06-19 03:28:53 +07:00
parent 1f3464d532
commit 05111486d5
2 changed files with 60 additions and 1 deletions

View File

@@ -11,12 +11,21 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Validate
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Validate JSON and YAML
id: json-yaml-validate
uses: GrantBirki/json-yaml-validate@v3
with:
use_gitignore: false
- name: Validate Resource
id: resource-validate
run: python ./scripts/resource_validator.py ./local-files/resource
dispatch:
runs-on: ubuntu-latest

View File

@@ -0,0 +1,50 @@
import re
import sys
import os
def check_syntax(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
errors = []
for line_number, line in enumerate(lines, 1):
if not re.match(r'^\[.*\]$', line.strip()) and line.strip():
errors.append(f"Error: ❌ Syntax error in {file_path} on line {line_number}")
return errors
def check_directory(directory):
all_errors = []
valid_files_count = 0
invalid_files_count = 0
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(".txt"):
file_path = os.path.join(root, file)
errors = check_syntax(file_path)
if errors:
all_errors.extend(errors)
invalid_files_count += 1
else:
valid_files_count += 1
return all_errors, valid_files_count, invalid_files_count
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python resource_validator.py <directory_path>")
sys.exit(1)
directory_path = sys.argv[1]
errors, valid_files_count, invalid_files_count = check_directory(directory_path)
if errors:
for error in errors:
print(error)
print(f"\nError: ❌ {invalid_files_count} files have syntax errors.")
else:
print("✅ No syntax errors found.")
print(f"{valid_files_count} files are valid.")
sys.exit(1 if errors else 0)