template_builders.py 3.1 KB

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