forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_code_formatting.py
More file actions
66 lines (53 loc) · 2.36 KB
/
apply_code_formatting.py
File metadata and controls
66 lines (53 loc) · 2.36 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
# Created with python 3.11.4
# This script applies basic formatting and cleanups to the various CPP files.
# Just run it.
import glob
import os
def apply_formatting(line: str) -> str:
# Strip useless comments behind scope end
scopeEndIndex = line.find('}')
if scopeEndIndex >= 0:
if scopeEndIndex == 0 or line[:scopeEndIndex].isspace():
afterScopeIndex = scopeEndIndex + 1
while afterScopeIndex < len(line) and line[afterScopeIndex] == ';':
afterScopeIndex += 1
commentBeginIndex = line.find("//")
namespaceCommentBeginIndex = line.find("// namespace")
if commentBeginIndex >= 0 and namespaceCommentBeginIndex < 0:
if afterScopeIndex == commentBeginIndex or line[afterScopeIndex:commentBeginIndex].isspace():
line = line[:commentBeginIndex]
# remove trailing whitespace
line = line.rstrip()
line += "\n"
return line
def main():
current_dir = os.path.dirname(os.path.abspath(__file__))
root_dir = os.path.join(current_dir, "..", "..")
root_dir = os.path.normpath(root_dir)
core_dir = os.path.join(root_dir, "Core")
generals_dir = os.path.join(root_dir, "Generals")
generalsmd_dir = os.path.join(root_dir, "GeneralsMD")
utility_dir = os.path.join(root_dir, "Dependencies", "Utility")
fileNames = []
for ext in ["*.cpp", "*.h", "*.inl"]:
fileNames.extend(glob.glob(os.path.join(core_dir, '**', ext), recursive=True))
fileNames.extend(glob.glob(os.path.join(generals_dir, '**', ext), recursive=True))
fileNames.extend(glob.glob(os.path.join(generalsmd_dir, '**', ext), recursive=True))
fileNames.extend(glob.glob(os.path.join(utility_dir, '**', ext), recursive=True))
for fileName in fileNames:
with open(fileName, 'r', encoding="cp1252") as file:
try:
lines = file.readlines()
except UnicodeDecodeError:
continue # Not good.
with open(fileName, 'w', encoding="cp1252") as file:
for line in lines:
line = apply_formatting(line)
file.write(line)
if lines:
lastLine = lines[-1]
if lastLine and lastLine[-1] != '\n':
file.write("\n") # write new line to end of file
return
if __name__ == "__main__":
main()