aboutsummaryrefslogtreecommitdiff
path: root/scripts/formatcode.py
blob: 1a214380d8e3bc72c9b77ab4cdfdd2fc669fdd6a (plain) (blame)
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
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("..")