-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathadd_new_file.py
More file actions
executable file
·106 lines (77 loc) · 3.38 KB
/
add_new_file.py
File metadata and controls
executable file
·106 lines (77 loc) · 3.38 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python3
"""
Add a new file to SourceFiles.cmake, the CMake file that sets all code paths
Usage: python3 add_new_file.py <path to new file relative to Arduino-Source/
The script will try to find the root code folder "Arduino-Source/" from the path of the current directory
where this script is executed.
Then it will add the new file path (as the input parameter of this program) to proper places in SourceFiles.cmake.
Example usage:
python3 add_new_file.py Source/PokemonSV/Inference/Map/PokemonSV_MapFlyMenuDetectorDetector.h
python3 add_new_file.py ../Common/Cpp/Color.cpp
"""
import sys
import os
from typing import List, Tuple
# Target path for the new flie, like
target_path = ""
if len (sys.argv) > 1:
target_path = sys.argv[1]
print(f"Adding a new file path {target_path}")
# Sanitize target path:
SerialPrograms_prefix = "SerialPrograms/"
if target_path.startswith(SerialPrograms_prefix):
target_path = target_path[len(SerialPrograms_prefix):]
Common_prefix = "Common/"
if target_path.startswith(Common_prefix) or target_path.startswith("ClientSource") or target_path.startswith("3rdParty"):
target_path = "../" + target_path
cur_folder_path = os.path.abspath(os.getcwd())
# print(cur_folder_path)
split_path = cur_folder_path.split(os.sep)
code_root_pos = split_path.index('Arduino-Source')
code_root_path = os.sep.join(split_path[0:code_root_pos+1])
# print(code_root_path)
def read_lines(file_path: str) -> List[str]:
with open(file_path, "r") as f:
file_lines = f.readlines()
file_lines = [line.rstrip() for line in file_lines]
return file_lines
def get_code_file_range(file_lines: List[str], starting_line: str, ending_line: str) -> Tuple[int, int]:
"""
...
<starting_line>
code_line_1.cpp
code_line_2.cpp
<endling_line>
...
Return the tuple of indices (start, end) in the input `file_lines` so that code lines can be found
by file_lines[start:end]
"""
code_file_list_start_pos = file_lines.index(starting_line) + 1
code_file_list_end_pos = code_file_list_start_pos
for line_no in range(code_file_list_start_pos, len(file_lines)):
line = file_lines[line_no]
if line == ending_line:
code_file_list_end_pos = line_no
break
return code_file_list_start_pos, code_file_list_end_pos
cmake_file_path = os.path.join(code_root_path, "SerialPrograms", "cmake", "SourceFiles.cmake")
print(f"SourceFiles.cmake path: {cmake_file_path}")
file_lines = read_lines(cmake_file_path)
old_file_lines = file_lines
code_file_start, code_file_end = get_code_file_range(file_lines, "file(GLOB LIBRARY_SOURCES", ")")
code_file_lines = set(file_lines[code_file_start:code_file_end])
old_num_code_files = len(code_file_lines)
print(f"{old_num_code_files} lines from SourceFiles.cmake are for code file paths")
# Add new file
if len(target_path) > 0:
code_file_lines.add(" " + target_path)
code_file_lines = list(code_file_lines)
code_file_lines.sort()
if len(target_path) > 0 and len(code_file_lines) == old_num_code_files:
print("Warning: you are adding an existing file! The added file is ignored")
# print(f"\"{code_file_lines[0]}\"")
file_lines = file_lines[0:code_file_start] + code_file_lines + file_lines[code_file_end:]
file_lines = [line + "\r\n" for line in file_lines]
with open(cmake_file_path, "w") as f:
f.writelines(file_lines)
print(f"Writed changes back to {cmake_file_path}")