Skip to content

Commit ff15d52

Browse files
committed
refactor: Add a function for rendering templates
1 parent 5c1df35 commit ff15d52

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

x.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,46 +76,44 @@ def write_file(file, contents):
7676
f.write(contents)
7777

7878
def update_debian():
79-
template = read_file("Dockerfile-debian.template")
80-
slim_template = read_file("Dockerfile-slim.template")
81-
8279
for release in debian_releases:
8380
arch_cases_str = arch_cases_start("$(dpkg --print-architecture)")
8481
for debian_arch in release.arches:
8582
arch_cases_str += arch_case(debian_arch.dpkg, debian_arch.rust)
8683
arch_cases_str += arch_cases_end()
8784

8885
for channel in supported_channels:
89-
rendered = template \
90-
.replace("%%RUST-VERSION%%", channel.rust_version) \
91-
.replace("%%RUSTUP-VERSION%%", rustup_version) \
92-
.replace("%%TAG%%", release.name) \
93-
.replace("%%ARCH-CASE%%", arch_cases_str)
94-
write_file(f"{channel.name}/{release.name}/Dockerfile", rendered)
95-
96-
rendered = slim_template \
97-
.replace("%%RUST-VERSION%%", channel.rust_version) \
98-
.replace("%%RUSTUP-VERSION%%", rustup_version) \
99-
.replace("%%TAG%%", release.name) \
100-
.replace("%%ARCH-CASE%%", arch_cases_str)
101-
write_file(f"{channel.name}/{release.name}/slim/Dockerfile", rendered)
86+
render_template(
87+
"Dockerfile-debian.template",
88+
channel.rust_version,
89+
release.name,
90+
arch_cases_str,
91+
f"{channel.name}/{release.name}/Dockerfile",
92+
)
93+
94+
render_template(
95+
"Dockerfile-slim.template",
96+
channel.rust_version,
97+
release.name,
98+
arch_cases_str,
99+
f"{channel.name}/{release.name}/slim/Dockerfile",
100+
)
102101

103102
def update_alpine():
104103
arch_cases_str = arch_cases_start("$(apk --print-arch)")
105104
for arch in alpine_arches:
106105
arch_cases_str += arch_case(arch.apk, arch.rust)
107106
arch_cases_str += arch_cases_end()
108107

109-
template = read_file("Dockerfile-alpine.template")
110-
111108
for version in alpine_versions:
112109
for channel in supported_channels:
113-
rendered = template \
114-
.replace("%%RUST-VERSION%%", channel.rust_version) \
115-
.replace("%%RUSTUP-VERSION%%", rustup_version) \
116-
.replace("%%TAG%%", version) \
117-
.replace("%%ARCH-CASE%%", arch_cases_str)
118-
write_file(f"{channel.name}/alpine{version}/Dockerfile", rendered)
110+
render_template(
111+
"Dockerfile-alpine.template",
112+
channel.rust_version,
113+
version,
114+
arch_cases_str,
115+
f"{channel.name}/alpine{version}/Dockerfile",
116+
)
119117

120118
def arch_cases_start(arch_cmd):
121119
start = f'arch="{arch_cmd}"; \\\n'
@@ -131,6 +129,21 @@ def arch_case(distro_arch, rust_arch):
131129
rustup_sha256 = rustup_hash(rust_arch)
132130
return f" {distro_arch}) rustArch='{rust_arch}'; rustupSha256='{rustup_sha256}' ;; \\\n"
133131

132+
def render_template(
133+
template_path,
134+
rust_version,
135+
docker_tag,
136+
arch_cases,
137+
rendered_path
138+
):
139+
template = read_file(template_path)
140+
rendered = template \
141+
.replace("%%TAG%%", docker_tag) \
142+
.replace("%%RUST-VERSION%%", rust_version) \
143+
.replace("%%RUSTUP-VERSION%%", rustup_version) \
144+
.replace("%%ARCH-CASE%%", arch_cases)
145+
write_file(rendered_path, rendered)
146+
134147
def update_ci():
135148
file = ".github/workflows/ci.yml"
136149
config = read_file(file)

0 commit comments

Comments
 (0)