serenity/Meta/embed_as_string.py
Sam Atkins 5d86305596 Meta: Make embed_as_string_view.py produce Strings instead
This is only used for CSS style sheets. One case wants it as a String,
and the others don't care, but will in future also want to have the
source as a String.

(cherry picked from commit 8cbc2116162764479edeec78e4b2b7c41447b643;
amended to fix conflicts in two cmake files, due to serenity
still having more code_generators, and still buildling
OutOfProcessWebView.cpp. Also kept embed_as_string_view because
it's used by stringify_gml. Did this by giving embed_as_string.py
a --type= argument that defaults to string but can also be set
to string-view.)
2024-11-15 23:10:03 -05:00

50 lines
1.8 KiB
Python

#!/usr/bin/env python3
r"""
Embeds a file as a String or StringView, a la #embed from C++23
"""
import argparse
import sys
def main():
parser = argparse.ArgumentParser(
epilog=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('input', help='input file to stringify')
parser.add_argument('--type', choices=['string', 'string-view'],
default='string')
parser.add_argument('-o', '--output', required=True,
help='output file')
parser.add_argument('-n', '--variable-name', required=True,
help='name of the C++ variable')
parser.add_argument('-s', '--namespace', required=False,
help='C++ namespace to put the string into')
args = parser.parse_args()
with open(args.output, 'w') as f:
if args.type == 'string':
f.write("#include <AK/String.h>\n")
elif args.type == 'string-view':
f.write("#include <AK/StringView.h>\n")
if args.namespace:
f.write(f"namespace {args.namespace} {{\n")
if args.type == 'string':
f.write(f"extern String {args.variable_name};\n")
f.write(f"String {args.variable_name} = R\"~~~(")
elif args.type == 'string-view':
f.write(f"extern StringView {args.variable_name};\n")
f.write(f"StringView {args.variable_name} = R\"~~~(")
with open(args.input, 'r') as input:
for line in input.readlines():
f.write(f"{line}")
if args.type == 'string':
f.write(")~~~\"_string;\n")
elif args.type == 'string-view':
f.write(")~~~\"sv;\n")
if args.namespace:
f.write("}\n")
if __name__ == '__main__':
sys.exit(main())