-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·94 lines (80 loc) · 3.36 KB
/
build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
import os, time
import json, glob, tarfile
from os import remove as rm, system, path, makedirs
from shutil import copyfile as cp, copytree, rmtree
def run(cmd):
if system(cmd) != 0:
raise Exception(f"Command failed: {cmd}")
def readfile(fn):
with open(fn, 'r', encoding='utf-8') as f:
return f.read()
def gen_translations(src, dst):
result = {}
for fn in glob.glob('*.json', root_dir=src):
lang = fn.replace('.json', '')
result[lang] = json.loads(readfile(src + fn))
with open(dst, 'w', encoding='utf-8') as f:
json.dump(result, f, separators=(',',':'), ensure_ascii=False, sort_keys=True)
def gen_manifest(src, dst):
pkg = json.loads(readfile('package.json'))
result = json.loads(readfile(src))
result['version'] = pkg['version']
with open(dst, 'w', encoding='utf-8') as f:
json.dump(result, f, separators=(',',':'), ensure_ascii=False)
def gen_tar(src, dst):
def reset_tarinfo(tarinfo):
tarinfo.uid = 0
tarinfo.gid = 0
tarinfo.uname = ""
tarinfo.gname = ""
tarinfo.mtime = 0
return tarinfo
with tarfile.open(dst, "w:gz") as tar:
for item in os.listdir(src):
item_path = os.path.join(src, item)
tar.add(item_path, arcname=item, filter=reset_tarinfo)
def combine(dst):
# Insert CSS and JS into HTML
combined = readfile(dst).replace(
'<link rel="stylesheet" href="./app.css">', '<style>\n' + readfile('build/app.css') + '\n</style>'
).replace(
'<link rel="stylesheet" href="./viper_lib.css">', '<style>\n' + readfile('build/viper_lib.css') + '\n</style>'
).replace(
'<script src="./app.js"></script>', '<script>\n' + readfile('build/app.js') + '\n</script>'
).replace(
'<script src="./viper_lib.js"></script>', '<script>\n' + readfile('build/viper_lib.js') + '\n</script>'
)
# Write the combined content
with open(dst, 'w', encoding='utf-8') as f:
f.write(combined)
if __name__ == "__main__":
# Prepare
rmtree("build", ignore_errors=True)
makedirs("build/assets")
cp("./src/webrepl_content.js", "./build/webrepl_content.js")
copytree("./assets", "./build/assets", dirs_exist_ok=True)
gen_translations("./src/lang/", "build/translations.json")
gen_manifest("./src/manifest.json", "build/manifest.json")
gen_tar("src/tools_vfs", "build/assets/tools_vfs.tar.gz")
gen_tar("src/vm_vfs", "build/assets/vm_vfs.tar.gz")
# Build
if not path.isdir("node_modules"):
run("npm install")
run("npx eslint")
run("npm run build")
# Combine everything
combine("build/index.html")
combine("build/bridge.html")
combine("build/benchmark.html")
# Cleanup
#run("rm build/translations.json")
run("rm build/app.css build/viper_lib.css")
run("rm build/app.js build/viper_lib.js")
# Add assets from packages
cp("node_modules/@micropython/micropython-webassembly-pyscript/micropython.wasm", "./build/assets/micropython.wasm")
cp("node_modules/@micropython/micropython-webassembly-pyscript/micropython.mjs", "./build/micropython.mjs")
cp("node_modules/@pybricks/mpy-cross-v6/build/mpy-cross-v6.wasm", "./build/assets/mpy-cross-v6.wasm")
cp("node_modules/@astral-sh/ruff-wasm-web/ruff_wasm_bg.wasm", "./build/assets/ruff_wasm_bg.wasm")
print()
print("Build complete.")