-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.py
146 lines (136 loc) · 5.49 KB
/
compile.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
import json
import os
import shutil
import subprocess
import sys
from pathlib import Path
def compile_css(extent, github_commit):
# NOTE avoid redundant artifact creation
if Path(f"artifacts/{extent}.css").is_file():
print(f"⚠️ file exists: artifacts/{extent}.css")
return
# NOTE requires `sass` command
subprocess.run(
[
"sass",
"--no-charset",
"--no-source-map",
f"{extent}.scss",
f"artifacts/{extent}.css",
],
check=True,
)
with open(f"artifacts/{extent}.css", "r") as f:
css = f.read()
with open(f"artifacts/{extent}.css", "w") as f:
if github_commit:
f.write(
f"/* see https://github.com/{github_commit[:len(github_commit) - 33]} */\n\n"
)
f.write(css)
def parse_nested_includes(wrapper, variant=None):
html = ""
wrapper.seek(0)
for line in wrapper:
if line.strip().startswith("<!--#include"):
included_file = line.split("'")[1]
if included_file.split(".")[0].endswith("-GROUP"):
fo = open(included_file.replace("GROUP", variant))
html += parse_nested_includes(fo, variant)
fo.close()
else:
fo = open(included_file)
html += parse_nested_includes(fo, variant)
fo.close()
else:
html += line
return html
def main(file):
print(f"🐞 file: {file}")
github_commit = (
f'{os.environ.get("GITHUB_REPOSITORY")}/commit/{os.environ.get("GITHUB_SHA")}'
)
if file.endswith(".scss"):
# NOTE primary scss files do not have named parent directories
extent = Path(file).parent.name if Path(file).parent.name else Path(file).stem
if extent == "common":
for extent in ["libanswers", "libguides"]:
compile_css(extent, github_commit)
else:
compile_css(extent, github_commit)
elif file.endswith(".js"):
extent = Path(file).stem
# NOTE avoid redundant artifact creation
if Path(f"artifacts/{extent}.js").is_file():
print(f"🐞 file exists: artifacts/{extent}.js")
return
shutil.copyfile(f"{extent}.js", f"artifacts/{extent}.js")
with open(f"artifacts/{extent}.js", "r") as f:
js = f.read()
with open(f"artifacts/{extent}.js", "w") as f:
if github_commit:
f.write(
f"// see https://github.com/{github_commit[:len(github_commit) - 33]} //\n\n"
)
f.write(js)
elif file.startswith("widget--"):
# avoid redundant artifact creation
if os.path.isfile(f"artifacts/{file}"):
print(f"🐞 file exists: artifacts/{file}")
return
shutil.copyfile(file, f"artifacts/{file}")
with open(f"artifacts/{file}", "r") as f:
widget = f.read()
with open(f"artifacts/{file}", "w") as f:
f.write(
f"<!-- see https://github.com/{github_commit[:len(github_commit) - 33]} -->\n\n"
)
f.write(widget)
elif file.endswith(".html") or file.endswith(".shtm"):
component = file.split("-")[0]
html = "<!-- WARNING: GENERATED CODE *EDITS WILL BE OVERWRITTEN* -->\n"
if github_commit:
html += f"<!-- see https://github.com/{github_commit[:len(github_commit) - 33]} -->\n\n"
if component == "template" or component == "head":
# NOTE use the file as is with prepended comments
with open(file) as f:
html += f.read()
with open(f'artifacts/{file.split("/")[-1]}', "w") as f:
f.write(html)
elif component == "header" or component == "footer":
# NOTE libguides_groups is set in a GitHub Actions secret
slugs = [g["slug"] for g in json.loads(os.environ.get("GROUPS"))["groups"]]
slugs.append("system")
variants = list(slugs)
print(f"🐞 variants: {variants}")
variant = (
file.split(".")[0].split("-")[-1]
if file.split(".")[0].split("-")[-1] in variants
else None
)
print(f"🐞 variant: {variant}")
# NOTE avoid redundant artifact creation
if os.path.isfile(f'artifacts/{file.split("/")[-1]}') or os.path.isfile(
f"artifacts/{component}--{variant}.html"
):
print(f"🐞 artifacts exist:", os.listdir("artifacts"))
return
wrapper = open(f"{component}-wrapper.shtm")
if variant:
html += parse_nested_includes(wrapper, variant)
with open(f"artifacts/{component}--{variant}.html", "w") as f:
f.write(html)
else:
# NOTE header-wrapper.shtm triggers this condition, for example
print(f"🐞 variants: {variants}")
for variant in variants:
print(f"🐞 variant: {variant}")
# reset output by copying html content into it
output = str(html)
output += parse_nested_includes(wrapper, variant)
with open(f"artifacts/{component}--{variant}.html", "w") as f:
f.write(output)
wrapper.close()
print(f"🐞 artifacts:", os.listdir("artifacts"))
if __name__ == "__main__":
main(sys.argv[1])