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
|
"""Functions used to generate source files during build time
All such functions are invoked in a subprocess on Windows to prevent build flakiness.
"""
import os
from io import StringIO
from platform_methods import subprocess_main
def parse_template(inherits, source, delimiter):
script_template = {
"inherits": inherits,
"name": "",
"description": "",
"version": "",
"script": "",
"space-indent": "4",
}
meta_prefix = delimiter + " meta-"
meta = ["name", "description", "version", "space-indent"]
with open(source) as f:
lines = f.readlines()
for line in lines:
if line.startswith(meta_prefix):
line = line[len(meta_prefix) :]
for m in meta:
if line.startswith(m):
strip_lenght = len(m) + 1
script_template[m] = line[strip_lenght:].strip()
else:
script_template["script"] += line
if script_template["space-indent"] != "":
indent = " " * int(script_template["space-indent"])
script_template["script"] = script_template["script"].replace(indent, "_TS_")
if script_template["name"] == "":
script_template["name"] = os.path.splitext(os.path.basename(source))[0].replace("_", " ").title()
script_template["script"] = (
script_template["script"].replace('"', '\\"').lstrip().replace("\n", "\\n").replace("\t", "_TS_")
)
return (
'{ String("'
+ script_template["inherits"]
+ '"), String("'
+ script_template["name"]
+ '"), String("'
+ script_template["description"]
+ '"), String("'
+ script_template["script"]
+ '")'
+ " },\n"
)
def make_templates(target, source, env):
dst = target[0]
s = StringIO()
s.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n\n")
s.write("#ifndef _CODE_TEMPLATES_H\n")
s.write("#define _CODE_TEMPLATES_H\n\n")
s.write('#include "core/object/object.h"\n')
s.write('#include "core/object/script_language.h"\n')
delimiter = "#" # GDScript single line comment delimiter by default.
if source:
ext = os.path.splitext(source[0])[1]
if ext == ".cs":
delimiter = "//"
parsed_template_string = ""
number_of_templates = 0
for filepath in source:
node_name = os.path.basename(os.path.dirname(filepath))
parsed_template = parse_template(node_name, filepath, delimiter)
parsed_template_string += "\t" + parsed_template
number_of_templates += 1
s.write("\nstatic const int TEMPLATES_ARRAY_SIZE = " + str(number_of_templates) + ";\n")
s.write("\nstatic const struct ScriptLanguage::ScriptTemplate TEMPLATES[" + str(number_of_templates) + "] = {\n")
s.write(parsed_template_string)
s.write("};\n")
s.write("\n#endif\n")
with open(dst, "w") as f:
f.write(s.getvalue())
s.close()
if __name__ == "__main__":
subprocess_main(globals())
|