aboutsummaryrefslogtreecommitdiffstats
path: root/sortImports.py
blob: 6f3fb647751392971b0efde6ef29921d561b9c29 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os

def sort_imports(java_file_path):
    with open(java_file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()

    # Separate license block
    license_block = []
    package_line = None
    static_imports = []
    normal_imports = []
    other_lines = []

    i = 0

    # Process license block
    if lines and lines[0].startswith('/**'):
        for i, line in enumerate(lines):
            license_block.append(line)
            if line.strip().startswith('*/'):
                i += 1  # Move past the license block
                break
    else:
        license_block = []

    # Process remaining lines
    while i < len(lines):
        line = lines[i]
        if line.startswith('package '):
            package_line = line
        elif line.startswith('import static '):
            static_imports.append(line)
        elif line.startswith('import '):
            normal_imports.append(line)
        else:
            other_lines.append(line)
        i += 1

    # Sort imports
    static_imports.sort()
    normal_imports.sort()

    # Build the new file content
    new_content = []

    # Add license block (always at the top)
    if license_block:
        new_content.extend(license_block)
        if not license_block[-1].endswith('\n'):
            new_content.append('\n')

    # Add package line
    if package_line:
        new_content.append(package_line)
        if not package_line.endswith('\n'):
            new_content.append('\n')

    # Add imports
    if static_imports:
        new_content.append('\n')  # Separate static imports
        new_content.extend(static_imports)
    if normal_imports:
        new_content.append('\n')  # Separate normal imports
        new_content.extend(normal_imports)

    # Add remaining lines (code, comments, etc.)
    if other_lines:
        if not new_content[-1].endswith('\n'):
            new_content.append('\n')
        new_content.extend(other_lines)

    # Write back to file
    with open(java_file_path, 'w', encoding='utf-8') as file:
        file.writelines(new_content)

def process_java_files(root_dir):
    for subdir, _, files in os.walk(root_dir):
        for file in files:
            if file.endswith('.java'):
                java_file_path = os.path.join(subdir, file)
                print(f"Processing: {java_file_path}")
                sort_imports(java_file_path)

if __name__ == "__main__":
    project_root = input("Enter the root directory of the Java project: ")
    process_java_files(project_root)