Debian Codegraph @

editor/template_builders.py

raw symbols SCIP
1 """Functions used to generate source files during build time"""
2
3 import os
4
5 import methods
6
7
8 def parse_template(inherits, source, delimiter):
9 script_template = {
10 "inherits": inherits,
11 "name": "",
12 "description": "",
13 "version": "",
14 "script": "",
15 "space-indent": "4",
16 }
17 meta_prefix = delimiter + " meta-"
18 meta = ["name", "description", "version", "space-indent"]
19
20 with open(source, "r", encoding="utf-8") as f:
21 lines = f.readlines()
22 for line in lines:
23 if line.startswith(meta_prefix):
24 line = line[len(meta_prefix) :]
25 for m in meta:
26 if line.startswith(m):
27 strip_length = len(m) + 1
28 script_template[m] = line[strip_length:].strip()
29 else:
30 script_template["script"] += line
31 if script_template["space-indent"] != "":
32 indent = " " * int(script_template["space-indent"])
33 script_template["script"] = script_template["script"].replace(indent, "_TS_")
34 if script_template["name"] == "":
35 script_template["name"] = os.path.splitext(os.path.basename(source))[0].replace("_", " ").title()
36 script_template["script"] = (
37 script_template["script"].replace('"', '\\"').lstrip().replace("\n", "\\n").replace("\t", "_TS_")
38 )
39 return (
40 f'{{ String("{script_template["inherits"]}"), '
41 + f'String("{script_template["name"]}"), '
42 + f'String("{script_template["description"]}"), '
43 + f'String("{script_template["script"]}") }},'
44 )
45
46
47 def make_templates(target, source, env):
48 delimiter = "#" # GDScript single line comment delimiter by default.
49 if source:
50 ext = os.path.splitext(str(source[0]))[1]
51 if ext == ".cs":
52 delimiter = "//"
53
54 parsed_templates = []
55
56 for filepath in source:
57 filepath = str(filepath)
58 node_name = os.path.basename(os.path.dirname(filepath))
59 parsed_templates.append(parse_template(node_name, filepath, delimiter))
60
61 parsed_template_string = "\n\t".join(parsed_templates)
62
63 with methods.generated_wrapper(str(target[0])) as file:
64 file.write(f"""\
65 #include "core/object/object.h"
66 #include "core/object/script_language.h"
67
68 inline constexpr int TEMPLATES_ARRAY_SIZE = {len(parsed_templates)};
69 static const struct ScriptLanguage::ScriptTemplate TEMPLATES[TEMPLATES_ARRAY_SIZE] = {{
70 {parsed_template_string}
71 }};
72 """)