-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
146 lines (115 loc) · 3.55 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from argparse import ArgumentParser
import os
from pathlib import Path
import shlex
import subprocess
from typing import Iterable, Union, Optional, List
class ShellCommand:
parts: Iterable[Union[str, Path]]
cwd: Optional[Path]
def __init__(self, *parts: Union[str, Path], cwd: Optional[Path] = None):
self.parts = parts
self.cwd = cwd
@property
def parts_str(self) -> str:
return shlex.join([str(part) for part in self.parts])
@property
def cwd_str(self) -> Optional[str]:
return None if not self.cwd or self.cwd == Path.cwd() else str(self.cwd)
def __str__(self):
parts_str = self.parts_str
cwd_str = self.cwd_str
if self.cwd:
return f"(cd {cwd_str} && {parts_str})"
return parts_str
def show(self):
print(str(self))
def run(self):
self.show()
subprocess.run(self.parts_str, cwd=self.cwd_str, shell=True)
def main(
lobster_root: Path,
entry: Path,
html: Path,
out_dir: Path,
serve: bool,
no_run: bool,
) -> List[str]:
lobster_executable = lobster_root.joinpath("bin", "lobster")
wasm_root = lobster_root.joinpath("dev", "emscripten")
commands = [
ShellCommand(lobster_executable, "--cpp", entry),
ShellCommand(lobster_executable, "--pak", entry),
ShellCommand("cp", "-a", f"{lobster_root.joinpath('data')}/.", wasm_root.joinpath("assets", "data")),
ShellCommand("cp", entry.parent.joinpath("default.lpak"), wasm_root.joinpath("assets", "default.lpak")),
ShellCommand("make", "-j8", cwd=wasm_root),
ShellCommand("cp", "-a", f"{wasm_root}/.", out_dir),
ShellCommand("cp", html, out_dir.joinpath("index.html")),
]
if serve:
commands.append(
ShellCommand(
"python3",
"-m",
"http.server",
"8080",
"--bind",
"127.0.0.1",
"--directory",
out_dir
)
)
if not no_run:
print()
os.makedirs(out_dir, exist_ok=True)
for command in commands:
command.run()
else:
for command in commands:
command.show()
return "\n".join(str(command) for command in commands)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"--lobster_root",
type=Path,
help="The root directory of the Lobster language.",
default=Path("~/lobster/").expanduser(),
)
parser.add_argument(
"--entry",
type=Path,
help="The Lobster entry script.",
default=Path("./src/main.lobster"),
)
parser.add_argument(
"--html",
type=Path,
help="The index HTML source file to copy to out-dir.",
default=Path("./src/index.html"),
)
parser.add_argument(
"--out-dir",
type=Path,
help="The output directory for HTML, WASM, JS, etc.",
default=Path("./build"),
)
parser.add_argument(
"--serve",
action="store_true",
help="If provided, serve out-dir over an HTTP server.",
)
parser.add_argument(
"--no-run",
action="store_true",
help="If provided, only print the build script and don't run.",
)
args = parser.parse_args()
main(
lobster_root=args.lobster_root,
entry=args.entry,
html=args.html,
out_dir=args.out_dir,
serve=args.serve,
no_run=args.no_run,
)