default_theme_icons_builders.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """
  2. Functions used to generate source files during build time
  3. All such functions are invoked in a subprocess on Windows to prevent build flakiness.
  4. """
  5. import os
  6. from io import StringIO
  7. from platform_methods import subprocess_main
  8. # See also `editor/icons/editor_icons_builders.py`.
  9. def make_default_theme_icons_action(target, source, env):
  10. dst = target[0]
  11. svg_icons = source
  12. with StringIO() as icons_string, StringIO() as s:
  13. for f in svg_icons:
  14. fname = str(f)
  15. icons_string.write('\t"')
  16. with open(fname, "rb") as svgf:
  17. b = svgf.read(1)
  18. while len(b) == 1:
  19. icons_string.write("\\" + str(hex(ord(b)))[1:])
  20. b = svgf.read(1)
  21. icons_string.write('"')
  22. if fname != svg_icons[-1]:
  23. icons_string.write(",")
  24. icons_string.write("\n")
  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", encoding="utf-8", newline="\n") as f:
  49. f.write(s.getvalue())
  50. if __name__ == "__main__":
  51. subprocess_main(globals())