diff options
Diffstat (limited to 'scripts/formatcode.py')
| -rw-r--r-- | scripts/formatcode.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/scripts/formatcode.py b/scripts/formatcode.py new file mode 100644 index 000000000..1a214380d --- /dev/null +++ b/scripts/formatcode.py @@ -0,0 +1,49 @@ +import os +import fileinput + +def is_header_missing(f): + with open(f) as reader: + lines = reader.read().lstrip().splitlines() + if len(lines) > 0: return not lines[0].startswith("// ") + return True + +def add_headers(files, header): + for line in fileinput.input(files, inplace=True): + if fileinput.isfirstline(): + [ print(h) for h in header.splitlines() ] + print(line, end="") + +def scan_tree(root): + files = [] + header_files = [] + with os.scandir(root) as dirs: + for entry in dirs: + if entry.is_dir(): + scan_tree(os.path.join(root, entry.name)) + elif entry.name.endswith(".cpp") or entry.name.endswith(".h"): + print("... formatting: %s"%(entry.name)) + full_path = os.path.join(root, entry.name) + files.append(full_path) + if is_header_missing(full_path): + header_files.append(full_path) + args = "" + if files: + os.system("clang-format -i " + " ".join(files)) + if header_files: + add_headers(header_files, "// Copyright Epic Games, Inc. All Rights Reserved.\n\n") + +def scan_zen(root): + with os.scandir(root) as dirs: + for entry in dirs: + if entry.is_dir() and entry.name.startswith("zen"): + scan_tree(os.path.join(root, entry.name)) + +while True: + if (os.path.isfile(".clang-format")): + scan_zen(".") + quit() + else: + cwd = os.getcwd() + if os.path.dirname(cwd) == cwd: + quit() + os.chdir("..") |