default_theme_icons_builders.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """Functions used to generate source files during build time
  2. All such functions are invoked in a subprocess on Windows to prevent build flakiness.
  3. """
  4. import os
  5. from io import StringIO
  6. from platform_methods import subprocess_main
  7. # See also `editor/icons/editor_icons_builders.py`.
  8. def make_default_theme_icons_action(target, source, env):
  9. dst = target[0]
  10. svg_icons = source
  11. icons_string = StringIO()
  12. for f in svg_icons:
  13. fname = str(f)
  14. icons_string.write('\t"')
  15. with open(fname, "rb") as svgf:
  16. b = svgf.read(1)
  17. while len(b) == 1:
  18. icons_string.write("\\" + str(hex(ord(b)))[1:])
  19. b = svgf.read(1)
  20. icons_string.write('"')
  21. if fname != svg_icons[-1]:
  22. icons_string.write(",")
  23. icons_string.write("\n")
  24. s = StringIO()
  25. s.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n\n")
  26. s.write('#include "modules/modules_enabled.gen.h"\n\n')
  27. s.write("#ifndef _DEFAULT_THEME_ICONS_H\n")
  28. s.write("#define _DEFAULT_THEME_ICONS_H\n")
  29. s.write("static const int default_theme_icons_count = {};\n\n".format(len(svg_icons)))
  30. s.write("#ifdef MODULE_SVG_ENABLED\n")
  31. s.write("static const char *default_theme_icons_sources[] = {\n")
  32. s.write(icons_string.getvalue())
  33. s.write("};\n")
  34. s.write("#endif // MODULE_SVG_ENABLED\n\n")
  35. s.write("static const char *default_theme_icons_names[] = {\n")
  36. index = 0
  37. for f in svg_icons:
  38. fname = str(f)
  39. # Trim the `.svg` extension from the string.
  40. icon_name = os.path.basename(fname)[:-4]
  41. s.write('\t"{0}"'.format(icon_name))
  42. if fname != svg_icons[-1]:
  43. s.write(",")
  44. s.write("\n")
  45. index += 1
  46. s.write("};\n")
  47. s.write("#endif\n")
  48. with open(dst, "w") as f:
  49. f.write(s.getvalue())
  50. s.close()
  51. icons_string.close()
  52. if __name__ == "__main__":
  53. subprocess_main(globals())