From 4c39ad7e2cfd1a2ff06086834c2a5e9e6fbdf45d Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Fri, 6 Jun 2025 05:01:54 -0400 Subject: [PATCH] Add author_builder SCons builder function --- SConstruct | 3 +++ build/author_info.py | 48 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 build/author_info.py diff --git a/SConstruct b/SConstruct index 77f3e8d..1b08f4c 100644 --- a/SConstruct +++ b/SConstruct @@ -13,6 +13,7 @@ from build.option_handler import OptionsClass from build.glob_recursive import GlobRecursive from build.git_info import get_git_info, git_builder from build.license_info import license_builder +from build.author_info import author_builder from build.cache import show_progress def normalize_path(val, env): @@ -277,6 +278,8 @@ env.GlobRecursive = GlobRecursive env.get_git_info = get_git_info env.license_builder = license_builder env.git_builder = git_builder +env.author_builder = author_builder + def to_raw_cstring(value: Union[str, List[str]]) -> str: MAX_LITERAL = 35 * 1024 diff --git a/build/author_info.py b/build/author_info.py new file mode 100644 index 0000000..93175a6 --- /dev/null +++ b/build/author_info.py @@ -0,0 +1,48 @@ +def author_builder(target, source, env): + name_prefix = env.get("name_prefix", "project") + prefix_upper = name_prefix.upper() + sections = env.get("sections", {"Developers": "AUTHORS_DEVELOPERS"}) + + def get_buffer() -> bytes: + with open(str(source[0]), "rb") as file: + return file.read() + + buffer = get_buffer() + + reading = False + + with open(str(target[0]), "wt", encoding="utf-8", newline="\n") as file: + file.write("/* THIS FILE IS GENERATED. EDITS WILL BE LOST. */\n\n") + file.write( + """\ +#pragma once + +#include +#include + +namespace OpenVic { +""" + ) + + def close_section(): + file.write("\t});\n") + + for line in buffer.decode().splitlines(): + if line.startswith(" ") and reading: + file.write(f'\t\t"{env.to_escaped_cstring(line).strip()}",\n') + elif line.startswith("## "): + if reading: + close_section() + file.write("\n") + reading = False + section = sections.get(line[3:].strip(), None) + if section: + file.write( + f"\tstatic constexpr std::array {prefix_upper}_{section} = std::to_array({{\n" + ) + reading = True + + if reading: + close_section() + + file.write("}")