|
1 | 1 | #!/usr/bin/python3 |
2 | 2 |
|
3 | 3 | """ |
4 | | -Add a new file to CMakeList.txt and SerialPrograms.pro |
| 4 | +Add a new file to SourceFiles.cmake, the CMake file that sets all code paths |
5 | 5 |
|
6 | 6 | Usage: python3 add_new_file.py <path to new file relative to Arduino-Source/ |
7 | 7 |
|
8 | 8 | The script will try to find the root code folder "Arduino-Source/" from the path of the current directory |
9 | 9 | where this script is executed. |
10 | 10 |
|
11 | | -Then it will add the new file path (as the input parameter of this program) to proper places in CMakeList.txt |
12 | | -and SerialPrograms.pro. |
| 11 | +Then it will add the new file path (as the input parameter of this program) to proper places in SourceFiles.cmake. |
13 | 12 |
|
14 | 13 | Example usage: |
15 | 14 | python3 add_new_file.py Source/PokemonSV/Inference/Map/PokemonSV_MapFlyMenuDetectorDetector.h |
@@ -75,92 +74,33 @@ def get_code_file_range(file_lines: List[str], starting_line: str, ending_line: |
75 | 74 |
|
76 | 75 |
|
77 | 76 |
|
78 | | -cmakelists_path = os.path.join(code_root_path, "SerialPrograms", "CMakeLists.txt") |
79 | | -pro_path = os.path.join(code_root_path, "SerialPrograms", "SerialPrograms.pro") |
80 | | -print(f"CMakeLists path: {cmakelists_path}") |
81 | | -print(f"QT Pro project file path: {pro_path}") |
| 77 | +cmake_file_path = os.path.join(code_root_path, "SerialPrograms", "SourceFiles.cmake") |
| 78 | +print(f"SourceFiles.cmake path: {cmake_file_path}") |
82 | 79 |
|
83 | | -file_lines = read_lines(cmakelists_path) |
| 80 | +file_lines = read_lines(cmake_file_path) |
84 | 81 | old_file_lines = file_lines |
85 | 82 |
|
86 | 83 | code_file_start, code_file_end = get_code_file_range(file_lines, "file(GLOB MAIN_SOURCES", ")") |
87 | 84 | code_file_lines = set(file_lines[code_file_start:code_file_end]) |
88 | 85 | old_num_code_files = len(code_file_lines) |
89 | | -print(f"{old_num_code_files} lines from CMakeLists are for code file paths") |
| 86 | +print(f"{old_num_code_files} lines from SourceFiles.cmake are for code file paths") |
90 | 87 |
|
91 | 88 | # Add new file |
92 | 89 | if len(target_path) > 0: |
93 | 90 | code_file_lines.add(" " + target_path) |
94 | 91 |
|
95 | 92 | code_file_lines = list(code_file_lines) |
96 | 93 | code_file_lines.sort() |
97 | | -if len(code_file_lines) == old_num_code_files: |
| 94 | +if len(target_path) > 0 and len(code_file_lines) == old_num_code_files: |
98 | 95 | print("Warning: you are adding an existing file! The added file is ignored") |
99 | 96 |
|
100 | 97 | # print(f"\"{code_file_lines[0]}\"") |
101 | 98 |
|
102 | 99 | file_lines = file_lines[0:code_file_start] + code_file_lines + file_lines[code_file_end:] |
103 | 100 | file_lines = [line + "\r\n" for line in file_lines] |
104 | | -with open(cmakelists_path, "w") as f: |
| 101 | +with open(cmake_file_path, "w") as f: |
105 | 102 | f.writelines(file_lines) |
106 | | -print(f"Writed changes back to {cmakelists_path}") |
107 | | - |
108 | | -if not os.path.exists(pro_path): |
109 | | - print(f"Pro file not found. End.") |
110 | | - exit(0) |
111 | | - |
112 | | -file_lines = read_lines(pro_path) |
113 | | - |
114 | | -# print("\n".join(file_lines[0:70])) |
115 | | - |
116 | | -# lines = [line for line in file_lines if line.startswith("SOURCES")] |
117 | | -# print(lines) |
118 | | - |
119 | | - |
120 | | -def process_pro_lines(file_lines: List[str], starting_line: str, new_path: str) -> List[str]: |
121 | | - |
122 | | - code_file_start, code_file_end = get_code_file_range(file_lines, starting_line, "") |
123 | | - # print(code_file_start, code_file_end) |
124 | | - # print(f"Starting line {file_lines[code_file_start]}") |
125 | | - |
126 | | - code_file_lines = file_lines[code_file_start:code_file_end] |
127 | | - |
128 | | - code_file_lines = [ line[0:-1].rstrip() if line.endswith("\\") else line for line in code_file_lines] |
129 | | - |
130 | | - if len(new_path) > 0: |
131 | | - code_file_lines.append(" " + new_path) |
132 | | - |
133 | | - # Remove duplicates and sort |
134 | | - code_file_lines = list(set(code_file_lines)) |
135 | | - code_file_lines.sort() |
136 | | - |
137 | | - # Add back " \\": |
138 | | - code_file_lines = [line + " \\" for line in code_file_lines] |
139 | | - code_file_lines[-1] = code_file_lines[-1][0:-2] |
140 | | - |
141 | | - # print("\n".join(code_file_lines[0:10])) |
142 | | - |
143 | | - file_lines = file_lines[0:code_file_start] + code_file_lines + file_lines[code_file_end:] |
144 | | - |
145 | | - return file_lines |
146 | | - |
147 | | -if target_path.endswith(".cpp"): |
148 | | - file_lines = process_pro_lines(file_lines, "SOURCES += \\", target_path) |
149 | | -elif target_path.endswith(".h") or target_path.endswith(".tpp"): |
150 | | - file_lines = process_pro_lines(file_lines, "HEADERS += \\", target_path) |
151 | | -else: |
152 | | - file_lines = process_pro_lines(file_lines, "SOURCES += \\", "") |
153 | | - file_lines = process_pro_lines(file_lines, "HEADERS += \\", "") |
154 | | - |
155 | | -# print("\n".join(file_lines[0:30])) |
156 | | - |
157 | | -# Add back CRLF |
158 | | -file_lines = [line + "\r\n" for line in file_lines] |
159 | | - |
160 | | -with open(pro_path, "w") as f: |
161 | | - f.writelines(file_lines) |
162 | | -print(f"Writed changes back to {pro_path}") |
163 | | - |
| 103 | +print(f"Writed changes back to {cmake_file_path}") |
164 | 104 |
|
165 | 105 |
|
166 | 106 |
|
0 commit comments