diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 86fda08..af658bb 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -6,6 +6,7 @@ types: [text] exclude: | (?x)( - ^.pre-commit-config.yaml + ^.vscode/.* | + ^project/addons/.* ) stages: [pre-commit, pre-merge-commit, manual] diff --git a/fc_hooks/__init__.py b/fc_hooks/__init__.py index 7124271..adc5bec 100644 --- a/fc_hooks/__init__.py +++ b/fc_hooks/__init__.py @@ -4,14 +4,40 @@ from pathlib import Path from click import argument, command, echo -def check_path(path: Path) -> bool: - str_path = str(path) +valid_file_name = set([ + ".editorconfig", + ".gitattributes", + ".gitignore", + ".gitkeep", + ".pre-commit-config.yaml", + "__init__.py", + "README.md", +]) - if path.suffix == ".cs": - if re.search(r"^[a-zA-Z\\\/\._]*$", str_path) is None: +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: - if re.search(r"^[a-z\\\/\._]*$", str_path) is None: + # Check that file name use snake_case. + if re.search(r"[a-z]+(?:_[a-z0-9]+)*$", path.stem) is None: return False return True