summaryrefslogtreecommitdiff
path: root/tools/add_class_comments.py
blob: df85e38ca53a2460c48d1f3ee60425f7d06bdf04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import click
import os
import re
import sys

@click.command()
@click.option('--path', required=True, help='Path to the header file or directory.')
def modify_headers(path):
    if os.path.isfile(path):
        process_file(path)
    elif os.path.isdir(path):
        for root, _, files in os.walk(path):
            for file in files:
                if file.endswith('.h'):
                    file_path = os.path.join(root, file)
                    process_file(file_path)
    else:
        sys.exit(f'Error: Path {path} is neither a file nor a directory.')

def process_file(file_path):
    try:
        with open(file_path, 'r') as file:
            lines = file.readlines()

        class_decl_pattern = re.compile(r'class\s+(\w+)\s*:')
        doxygen_comment_pattern = re.compile(r'/\*\*\s+@ingroup actors-npcs\s+@class\s+\w+\s+@brief', re.MULTILINE)
        
        for i, line in enumerate(lines):
            match = class_decl_pattern.search(line)
            if match:
                class_name = match.group(1)
                # Check if Doxygen comment is missing
                if not doxygen_comment_pattern.search(''.join(lines[max(0, i-3):i])):
                    doxygen_comment = f"""/**\n * @ingroup actors-unsorted\n * @class {class_name}\n * @brief\n *\n * @details\n *\n */\n"""
                    lines.insert(i, doxygen_comment)
                
                # Stop after processing the first class declaration
                break

        with open(file_path, 'w') as file:
            file.writelines(lines)
        
        print(f'Processed {file_path} successfully.')

    except FileNotFoundError:
        sys.exit(f'Error: File {file_path} not found.')
    except Exception as e:
        sys.exit(f'Error processing file {file_path}: {e}')

if __name__ == '__main__':
    modify_headers()