build.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import json
  2. import os
  3. import shutil
  4. import subprocess
  5. import sys
  6. def main():
  7. print('=> cleaning')
  8. shutil.rmtree('out')
  9. print('=> rebuilding wasm')
  10. # subprocess.check_call(['wasm-pack', 'build', '--target', 'web'])
  11. print('=> rebuilding js')
  12. subprocess.check_call(['yarn', 'build'])
  13. os.makedirs('out', exist_ok=True)
  14. build_index()
  15. print('=> moving wasm output')
  16. shutil.copytree('dist', os.path.join('out', 'dist'))
  17. def build_index():
  18. print('=> building index.html')
  19. sources = {}
  20. for root, _, files in os.walk("examples"):
  21. for f in files:
  22. if not f.endswith(".matzo"):
  23. continue
  24. with open(os.path.join(root, f)) as s:
  25. sources[f.removesuffix(".matzo")] = s.read()
  26. with open('index.html') as f:
  27. template = f.read()
  28. examples = '\n'.join((
  29. f'<option value="{s}">{s}.matzo</option>'
  30. for s in sorted(sources.keys())
  31. if s != 'hello'
  32. ))
  33. template = (template
  34. .replace('%SOURCES%', json.dumps(sources))
  35. .replace('%EXAMPLES%', examples))
  36. with open(os.path.join('out', 'index.html'), 'w') as f:
  37. f.write(template)
  38. if __name__ == '__main__':
  39. if '--only-html' in sys.argv:
  40. build_index()
  41. else:
  42. main()