build.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import json
  2. import os
  3. import shutil
  4. import subprocess
  5. def main():
  6. print('=> rebuilding wasm')
  7. subprocess.check_call(['wasm-pack', 'build', '--target', 'web'])
  8. os.makedirs('out', exist_ok=True)
  9. build_index()
  10. print('=> moving wasm output')
  11. for root, _, files in os.walk('pkg'):
  12. for f in files:
  13. shutil.copy(
  14. os.path.join(root, f),
  15. os.path.join('out', f)
  16. )
  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. main()