Skip to content

Commit c21d6de

Browse files
committed
[2024] Added subproject registration to init.py script
1 parent e60577f commit c21d6de

File tree

2 files changed

+85
-9
lines changed

2 files changed

+85
-9
lines changed

2024/build.zig.zon

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
.version = "1.0.0",
44
.minimum_zig_version = "0.13.0",
55
.dependencies = .{
6-
.day_00_zig_template = .{
7-
.path = "00/zig_template",
8-
},
9-
.day_02_red_nosed_reports = .{
10-
.path = "02/red_nosed_reports",
11-
},
12-
.day_06_guard_gallivant = .{
13-
.path = "06/guard_gallivant",
14-
},
6+
.day_00_zig_template = .{ .path = "00/zig_template" },
7+
.day_02_red_nosed_reports = .{ .path = "02/red_nosed_reports" },
8+
.day_06_guard_gallivant = .{ .path = "06/guard_gallivant" },
159
},
1610
.paths = .{
1711
"build.zig",

2024/init.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,73 @@ def init_project(
206206
file.write(search_and_replace(day, name, contents))
207207

208208

209+
def subproject_registration(zon: bool) -> str:
210+
"""Generate a subproject string.
211+
212+
Generate a subproject string which registers the subproject in the
213+
root project (build.zig and build.zig.zon).
214+
215+
Args:
216+
zon (bool): Wether to create the subproject string for
217+
build.zig.zon or build.zig.
218+
219+
Returns:
220+
str: The subproject registration string
221+
"""
222+
subproject = ""
223+
if zon:
224+
subproject += " .day_"
225+
subproject += get_day(opts.day) + "_" + snake_case(opts.name)
226+
subproject += ' = .{ .path = "'
227+
subproject += get_day(opts.day) + "/" + snake_case(opts.name) + '" },\n'
228+
else:
229+
subproject += " add_subproject("
230+
subproject += 'b, target, optimize, test_step, benchmark_step, "'
231+
subproject += get_day(opts.day) + '", "'
232+
subproject += snake_case(opts.name) + '");\n'
233+
234+
return subproject
235+
236+
237+
def register_subproject(
238+
filename: str, subproject_pattern: str, registration_string: str
239+
) -> None:
240+
"""Register the subproject in the gven config file.
241+
242+
Args:
243+
filename (str): The config file.
244+
subproject_pattern (str): The regex that matches existing subprojects.
245+
registration_string (str): New subproject string.
246+
"""
247+
config_file = Path(__file__).parent / filename
248+
config: list[str] = []
249+
with open(config_file, "r") as file:
250+
last_line_matched = False
251+
subproject_added = False
252+
for line in file.readlines():
253+
if not subproject_added:
254+
subproject_added = last_line_matched
255+
256+
m = re.match(subproject_pattern, line)
257+
if m and len(m.groups()) == 1:
258+
last_line_matched = True
259+
subproject_added = int(m.group(1)) > opts.day
260+
261+
# Add subproject if the day of the next entry is larger
262+
# or if this is the last entry in the subproject list
263+
if subproject_added:
264+
config.append(registration_string)
265+
266+
config.append(line)
267+
268+
# Add subproject entry at the end of the file
269+
if last_line_matched and not subproject_added:
270+
config.append(registration_string)
271+
272+
with open(config_file, "w") as file:
273+
file.writelines(config)
274+
275+
209276
# -------------------------------------------------------------------- #
210277

211278

@@ -231,6 +298,21 @@ def main() -> int:
231298
# Initialize project
232299
init_project(opts.day, opts.name)
233300

301+
# Initialize as a subproject
302+
register_subproject(
303+
"build.zig",
304+
r".*add_subproject\(.*([0-9]{2}).*\).*",
305+
subproject_registration(zon=False),
306+
)
307+
308+
register_subproject(
309+
"build.zig.zon", r".*_([0-9]{2})_.*", subproject_registration(zon=True)
310+
)
311+
312+
register_subproject(
313+
"build.zig.zon", r'.*"([0-9]{2})".*', ' "' + get_day(opts.day) + '",\n'
314+
)
315+
234316
return 0
235317

236318

0 commit comments

Comments
 (0)