-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
175 lines (134 loc) · 4.54 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
import os
import shutil
import subprocess
import sys
from pathlib import Path
LIBRARY_DIR = Path('src/')
PLUGIN_DIR = Path('plugin/')
TARGET_DIR = Path('target/')
TEST_DIR = Path('tests/')
LICENSE = Path('LICENSE')
CHANGELOG = Path('CHANGELOG.md')
README = 'README.md'
def delete_directory_content(directory):
if directory.exists():
shutil.rmtree(directory)
directory.mkdir(parents=True)
def read_exclude_file(path: Path) -> list[str]:
with open(path) as f:
lines = [line.strip() for line in f.read().splitlines()]
return [os.path.normcase(entry) for entry in lines if entry and not entry.startswith('#')]
def copy_library():
print('Copying library...')
README_PATH = os.path.normcase(LIBRARY_DIR.joinpath(README))
for path, directories, files in os.walk(LIBRARY_DIR):
for file in files:
file_name = Path(path).relative_to(LIBRARY_DIR).joinpath(file)
if os.path.normcase(file_name) != README_PATH:
source = LIBRARY_DIR.joinpath(file_name)
destination = TARGET_DIR.joinpath(file_name)
destination.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(source, destination)
shutil.copyfile(LICENSE, TARGET_DIR.joinpath('LICENSE'))
def build_plugin():
print('Building plugin...')
BUILD_TARGET = 'wasm32-unknown-unknown'
PLUGIN_PATH = PLUGIN_DIR.joinpath('target', BUILD_TARGET, 'release', 'board_n_pieces_plugin.wasm')
PLUGIN_NAME = 'plugin.wasm'
subprocess.run(
['cargo', 'build', '--release', '--target', BUILD_TARGET, '--quiet'],
cwd=PLUGIN_DIR,
check=True,
)
shutil.copyfile(PLUGIN_PATH, TARGET_DIR.joinpath(PLUGIN_NAME))
def build_readme():
print('Building README...')
EXAMPLES_DIR = Path('examples/')
def example_path(n):
return EXAMPLES_DIR.joinpath(f'example-{n}.svg')
final_lines = []
with open(LIBRARY_DIR.joinpath(README)) as f:
initial_readme = f.read()
# Build examples
example_source = [
'#import "lib.typ": *;',
'#set page(width: auto, height: auto, margin: 0cm, fill: none);',
]
example_count = 0
example = []
is_example = False
is_start = True
for line in initial_readme.splitlines():
# Skip initial note.
if is_start:
if line.startswith('>') or line.strip() == '':
continue
is_start = False
# Handle examples.
if is_example and line.startswith('```'):
is_example = False
example_count += 1
example_source.append(f'#page[\n{'\n'.join(example)}\n];')
final_lines.append(line)
final_lines.append('')
final_lines.append(f'![image]({example_path(example_count).as_posix()})')
elif is_example:
if line.startswith('%'):
example.append('#' + line[1:])
else:
final_lines.append(line)
example.append(line)
elif line.startswith('```example'):
final_lines.append('```typ')
is_example = True
example = []
else:
final_lines.append(line)
TARGET_DIR.joinpath(EXAMPLES_DIR).mkdir(parents=True)
subprocess.run(
['typst', 'compile', '-', str(example_path('{n}'))],
input='\n'.join(example_source),
encoding='utf-8',
cwd=TARGET_DIR,
check=True,
)
# Add changelog
with open(CHANGELOG) as f:
initial_changelog = f.read()
final_lines.append('')
final_lines.append('')
for line in initial_changelog.splitlines():
if line.startswith('#'):
final_lines.append('#' + line)
else:
final_lines.append(line)
# Write README
with open(TARGET_DIR.joinpath(README), 'w') as f:
f.write('\n'.join(final_lines))
def test():
print('Running tests...')
LIB_ROOT = TARGET_DIR.joinpath('lib.typ')
REF_DIR = TEST_DIR.joinpath('refs/')
delete_directory_content(REF_DIR)
subprocess.run(
[
'typst', 'compile',
str(TEST_DIR.joinpath('tests.typ')),
str(REF_DIR.joinpath('test-{n}.png')),
'--input', f'lib={os.path.relpath(LIB_ROOT, TEST_DIR)}',
'--root', '.',
],
check=True,
)
def main():
delete_directory_content(TARGET_DIR)
copy_library()
build_plugin()
build_readme()
test()
if __name__ == '__main__':
try:
main()
except subprocess.CalledProcessError:
exit(1)