|
| 1 | +""" |
| 2 | +Remove duplicate class and enum definitions from the generated files. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import re |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +def find_classes_and_enums_in_file(file_path: Path) -> tuple[list[str], list[str]]: |
| 11 | + """ |
| 12 | + Find all partial class and enum definitions in a given file. |
| 13 | +
|
| 14 | + Args: |
| 15 | + file_path (str): The path to the file. |
| 16 | +
|
| 17 | + Returns: |
| 18 | + tuple: Two lists containing class names and enum names found in the file. |
| 19 | + """ |
| 20 | + class_pattern = re.compile(r"\bpublic\s+partial\s+class\s+(\w+)") |
| 21 | + enum_pattern = re.compile(r"\bpublic\s+enum\s+(\w+)") |
| 22 | + classes: list[str] = [] |
| 23 | + enums: list[str] = [] |
| 24 | + |
| 25 | + with open(file_path, "r", encoding="utf-8") as file: |
| 26 | + content = file.read() |
| 27 | + class_matches = class_pattern.findall(content) |
| 28 | + enum_matches = enum_pattern.findall(content) |
| 29 | + classes.extend(class_matches) |
| 30 | + enums.extend(enum_matches) |
| 31 | + |
| 32 | + return classes, enums |
| 33 | + |
| 34 | + |
| 35 | +def remove_definitions( # pylint: disable=too-many-locals, too-many-branches |
| 36 | + file_path: Path, main_class_name: str, classes_to_remove: list[str], enums_to_remove: list[str] |
| 37 | +) -> None: |
| 38 | + """ |
| 39 | + Remove specified class and enum definitions from a file, keeping the main class intact. |
| 40 | +
|
| 41 | + Args: |
| 42 | + file_path (str): The path to the file. |
| 43 | + main_class_name (str): The main class to keep intact. |
| 44 | + classes_to_remove (list): List of class names to remove. |
| 45 | + enums_to_remove (list): List of enum names to remove. |
| 46 | + """ |
| 47 | + try: |
| 48 | + with open(file_path, "r", encoding="utf-8") as file: |
| 49 | + lines = file.readlines() |
| 50 | + except (PermissionError, OSError) as e: |
| 51 | + print(f"Error reading file {file_path}: {e}") |
| 52 | + return |
| 53 | + |
| 54 | + in_definition = False |
| 55 | + definitions = {} |
| 56 | + current_definition = None |
| 57 | + definition_start_index = 0 |
| 58 | + comment_block_start_index = None |
| 59 | + |
| 60 | + def find_comment_block_start(index: int) -> int: |
| 61 | + """Find the start index of the comment block preceding the definition.""" |
| 62 | + while index > 0 and re.match(r"\s*///", lines[index - 1]): |
| 63 | + index -= 1 |
| 64 | + return index |
| 65 | + |
| 66 | + # Read the file line by line and identify class and enum definitions |
| 67 | + for index, line in enumerate(lines): |
| 68 | + if re.match(r"\s*///", line) and not in_definition: |
| 69 | + if comment_block_start_index is None: |
| 70 | + comment_block_start_index = index |
| 71 | + continue |
| 72 | + if comment_block_start_index is not None and not re.match(r"\s*///", line): |
| 73 | + comment_block_start_index = None |
| 74 | + |
| 75 | + if re.match(r"\s*public\s+partial\s+class\s+\w+", line): |
| 76 | + class_name = re.findall(r"\bpublic\s+partial\s+class\s+(\w+)", line)[0] |
| 77 | + if class_name == main_class_name: |
| 78 | + current_definition = None |
| 79 | + in_definition = False |
| 80 | + continue |
| 81 | + current_definition = class_name |
| 82 | + in_definition = True |
| 83 | + definition_start_index = find_comment_block_start(index) if comment_block_start_index is not None else index |
| 84 | + elif re.match(r"\s*public\s+enum\s+\w+", line): |
| 85 | + enum_name = re.findall(r"\bpublic\s+enum\s+(\w+)", line)[0] |
| 86 | + current_definition = enum_name |
| 87 | + in_definition = True |
| 88 | + definition_start_index = find_comment_block_start(index) if comment_block_start_index is not None else index |
| 89 | + |
| 90 | + if in_definition: |
| 91 | + if re.match(r"\s*\}", line): |
| 92 | + definitions[current_definition] = (definition_start_index, index) |
| 93 | + in_definition = False |
| 94 | + current_definition = None |
| 95 | + |
| 96 | + # Remove classes and enums to remove from the lines |
| 97 | + for name in classes_to_remove + enums_to_remove: |
| 98 | + if name in definitions: |
| 99 | + start, end = definitions[name] |
| 100 | + # Check for leading empty lines and comments |
| 101 | + while start > 0 and re.match(r"^\s*$", lines[start - 1]): |
| 102 | + start -= 1 |
| 103 | + while start > 0 and re.match(r"\s*///", lines[start - 1]): |
| 104 | + start -= 1 |
| 105 | + lines[start : end + 1] = [] |
| 106 | + |
| 107 | + # Write the cleaned content back to the file |
| 108 | + try: |
| 109 | + with open(file_path, "w", encoding="utf-8") as file: |
| 110 | + file.writelines(lines) |
| 111 | + except (PermissionError, OSError) as e: |
| 112 | + print(f"Error writing to file {file_path}: {e}") |
| 113 | + |
| 114 | + |
| 115 | +def process_directory(directory_path: Path) -> None: |
| 116 | + """ |
| 117 | + Process all files in the directory to remove duplicate class and enum definitions. |
| 118 | + Args: |
| 119 | + directory_path (Path): The path to the directory. |
| 120 | + """ |
| 121 | + for root, _, files in os.walk(directory_path): |
| 122 | + for filename in files: |
| 123 | + if filename.endswith(".cs"): |
| 124 | + file_path = Path(root) / filename |
| 125 | + class_name_from_filename = file_path.stem |
| 126 | + classes_in_file, enums_in_file = find_classes_and_enums_in_file(file_path) |
| 127 | + |
| 128 | + # Exclude the main class that matches the filename |
| 129 | + classes_to_remove = [ |
| 130 | + class_name for class_name in classes_in_file if class_name != class_name_from_filename |
| 131 | + ] |
| 132 | + enums_to_remove = list(enums_in_file) |
| 133 | + |
| 134 | + for class_name in classes_to_remove: |
| 135 | + class_file_path_bo = directory_path / "bo" / f"{class_name}.cs" |
| 136 | + class_file_path_com = directory_path / "com" / f"{class_name}.cs" |
| 137 | + |
| 138 | + if class_file_path_bo.exists() or class_file_path_com.exists(): |
| 139 | + print(f"Removing class {class_name} from {file_path}") |
| 140 | + remove_definitions(file_path, class_name_from_filename, [class_name], []) |
| 141 | + |
| 142 | + for enum_name in enums_to_remove: |
| 143 | + enum_file_path = directory_path / "enum" / f"{enum_name}.cs" |
| 144 | + |
| 145 | + if enum_file_path.exists(): |
| 146 | + print(f"Removing enum {enum_name} from {file_path}") |
| 147 | + remove_definitions(file_path, class_name_from_filename, [], [enum_name]) |
| 148 | + |
| 149 | + print(f"Processed {file_path}") |
0 commit comments