-
-
Notifications
You must be signed in to change notification settings - Fork 22.1k
/
Copy pathtemplate_builders.py
72 lines (60 loc) · 2.49 KB
/
template_builders.py
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
"""Functions used to generate source files during build time"""
import os
import methods
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, "r", encoding="utf-8") 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_length = len(m) + 1
script_template[m] = line[strip_length:].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 (
f'{{ String("{script_template["inherits"]}"), '
+ f'String("{script_template["name"]}"), '
+ f'String("{script_template["description"]}"), '
+ f'String("{script_template["script"]}") }},'
)
def make_templates(target, source, env):
delimiter = "#" # GDScript single line comment delimiter by default.
if source:
ext = os.path.splitext(str(source[0]))[1]
if ext == ".cs":
delimiter = "//"
parsed_templates = []
for filepath in source:
filepath = str(filepath)
node_name = os.path.basename(os.path.dirname(filepath))
parsed_templates.append(parse_template(node_name, filepath, delimiter))
parsed_template_string = "\n\t".join(parsed_templates)
with methods.generated_wrapper(str(target[0])) as file:
file.write(f"""\
#include "core/object/object.h"
#include "core/object/script_language.h"
inline constexpr int TEMPLATES_ARRAY_SIZE = {len(parsed_templates)};
static const struct ScriptLanguage::ScriptTemplate TEMPLATES[TEMPLATES_ARRAY_SIZE] = {{
{parsed_template_string}
}};
""")