2024-03-11 18:05:37 +00:00
|
|
|
"""Functions used to generate source files during build time"""
|
2021-10-11 09:30:59 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
2024-03-11 18:05:37 +00:00
|
|
|
dst = str(target[0])
|
2024-03-10 17:09:48 +00:00
|
|
|
with StringIO() as s:
|
|
|
|
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:
|
2024-03-11 18:05:37 +00:00
|
|
|
ext = os.path.splitext(str(source[0]))[1]
|
2024-03-10 17:09:48 +00:00
|
|
|
if ext == ".cs":
|
|
|
|
delimiter = "//"
|
|
|
|
|
|
|
|
parsed_template_string = ""
|
|
|
|
number_of_templates = 0
|
|
|
|
|
|
|
|
for filepath in source:
|
2024-03-11 18:05:37 +00:00
|
|
|
filepath = str(filepath)
|
2024-03-10 17:09:48 +00:00
|
|
|
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"
|
|
|
|
)
|
2021-10-11 09:30:59 +00:00
|
|
|
|
2024-03-10 17:09:48 +00:00
|
|
|
s.write(parsed_template_string)
|
2021-10-11 09:30:59 +00:00
|
|
|
|
2024-03-10 17:09:48 +00:00
|
|
|
s.write("};\n")
|
2021-10-11 09:30:59 +00:00
|
|
|
|
2024-03-10 17:09:48 +00:00
|
|
|
s.write("\n#endif\n")
|
2021-10-11 09:30:59 +00:00
|
|
|
|
2024-03-10 17:09:48 +00:00
|
|
|
with open(dst, "w", encoding="utf-8", newline="\n") as f:
|
|
|
|
f.write(s.getvalue())
|