Improve script naming linter with suggestions
This commit is contained in:
@@ -2,19 +2,32 @@ import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
pattern = re.compile(r'^[a-z0-9_]+\.gd$')
|
||||
pattern = re.compile(r"^[a-z0-9_]+\.gd$")
|
||||
invalid_files = []
|
||||
|
||||
for root, dirs, files in os.walk('.'):
|
||||
def to_snake_case(filename: str) -> str:
|
||||
base = os.path.splitext(filename)[0]
|
||||
s1 = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", base)
|
||||
s2 = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", s1)
|
||||
s3 = re.sub(r"[-\s]+", "_", s2)
|
||||
return s3.lower() + ".gd"
|
||||
|
||||
for root, _, files in os.walk('.'):
|
||||
for name in files:
|
||||
if name.endswith('.gd'):
|
||||
if not pattern.match(name):
|
||||
invalid_files.append(os.path.join(root, name))
|
||||
suggestion = to_snake_case(name)
|
||||
invalid_files.append(
|
||||
(
|
||||
os.path.relpath(os.path.join(root, name)),
|
||||
os.path.relpath(os.path.join(root, suggestion)),
|
||||
)
|
||||
)
|
||||
|
||||
if invalid_files:
|
||||
print('Invalid script filenames detected:')
|
||||
for f in invalid_files:
|
||||
print(f)
|
||||
for path, suggestion in invalid_files:
|
||||
print(f"{path} -> {suggestion}")
|
||||
sys.exit(1)
|
||||
else:
|
||||
print('All script filenames follow Godot naming convention.')
|
||||
|
||||
Reference in New Issue
Block a user