| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #!/usr/bin/env python3
- """Embed spry CLI template files as Vala const strings (mkconst-style)."""
- import os
- import re
- import sys
- def const_name(filename):
- base = filename
- for ext in ('.vala', '.html', '.build', '.md'):
- if base.endswith(ext):
- base = base[: -len(ext)]
- break
- base = re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', base)
- base = re.sub(r'[^A-Za-z0-9]', '_', base)
- return base.upper()
- def vala_literal(text):
- text = text.replace('\\', '\\\\').replace('"', '\\"')
- text = text.replace('\r', '\\r').replace('\t', '\\t').replace('\n', '\\n')
- return '"%s"' % text
- def main():
- if len(sys.argv) < 3:
- sys.stderr.write('usage: mkconst.py OUTPUT TEMPLATE...\n')
- return 1
- output, templates = sys.argv[1], sys.argv[2:]
- with open(output, 'w', encoding='utf-8') as out:
- out.write('// Generated by tools/spry/mkconst.py - do not edit.\n')
- out.write('namespace Spry.Cli {\n\n')
- out.write(' public class Templates : GLib.Object {\n')
- for path in templates:
- with open(path, 'r', encoding='utf-8') as handle:
- text = handle.read()
- out.write('\n public const string %s = %s;\n'
- % (const_name(os.path.basename(path)), vala_literal(text)))
- out.write(' }\n}\n')
- return 0
- if __name__ == '__main__':
- sys.exit(main())
|