@@ -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