-
Notifications
You must be signed in to change notification settings - Fork 13
/
entrypoint.py
59 lines (43 loc) · 1.72 KB
/
entrypoint.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
"""Script to compile Typst source files."""
import logging
import subprocess
import sys
def compile(filename: str, options: list[str]) -> bool:
"""Compiles a Typst file with the specified global options.
Returns True if the typst command exited with status 0, False otherwise.
"""
command = ["typst"] + options + ["compile", filename]
logging.debug("Running: " + " ".join(command))
result = subprocess.run(command, capture_output=True, text=True)
try:
result.check_returncode()
except subprocess.CalledProcessError:
logging.error(f"Compiling {filename} failed with stderr: \n {result.stderr}")
return False
return True
def main():
logging.basicConfig(level=logging.INFO)
# Parse the positional arguments, expected in the following form
# 1. The Typst files to compile in a line separated string
# 2. The global Typst CLI options, in a line separated string. It means each
# whitespace separated field should be on its own line.
source_files = sys.argv[1].splitlines()
options = sys.argv[2].splitlines()
version = subprocess.run(
["typst", "--version"], capture_output=True, text=True
).stdout
logging.info(f"Using version {version}")
success: dict[str, bool] = {}
for filename in source_files:
filename = filename.strip()
if filename == "":
continue
logging.info(f"Compiling {filename}…")
success[filename] = compile(filename, options)
# Log status of each input files.
for filename, status in success.items():
logging.info(f"{filename}: {'✔' if status else '❌'}")
if not all(success.values()):
sys.exit(1)
if __name__ == "__main__":
main()