import re from pathlib import Path from click import argument, command, echo valid_file_name = set([ ".editorconfig", ".gitattributes", ".gitignore", ".gitkeep", ".pre-commit-config.yaml", "__init__.py", "README.md", ]) pascal_case_extensions = set([ ".cs", ".csproj", ".sln", ]) def check_path(path: Path) -> bool: # folder names must use snake_case. for parent in path.parents: if parent.stem != "" and re.search(r"^[a-z]+(?:_[a-z0-9]+)*$", parent.stem) is None: return False # File name exceptions. if valid_file_name.__contains__(path.name): return True if pascal_case_extensions.__contains__(path.suffix): # Check that file name use PascalCase. if re.search(r"[A-Z][a-z0-9]*(?:[A-Z][a-z0-9]*)*(?:[A-Z]?)$", path.stem) is None: return False else: # Check that file name use snake_case. if re.search(r"[a-z]+(?:_[a-z0-9]+)*(?:\.[a-z0-9]+)?$", path.stem) is None: return False return True @command @argument( "paths", nargs=-1, ) def main(paths: list[str]) -> None: has_error = False for path in paths: if not check_path(Path(path)): echo(f"{path} doesn't follows convention") has_error = True if has_error: exit(1)