#!/usr/bin/env python3 """Generates src/core/ControllerProfilesData.h from resources/controllers/*.lynxctl (the built-in MIDI controller profiles, docs/CONTROLLERS.md). Run it from the repository root whenever a profile file changes; the core tests compare the header with the files. Profiles another profile extends come first (a file's "extends = " must already be known when it is read).""" import pathlib, re, sys root = pathlib.Path(__file__).resolve().parent.parent files = sorted((root / "resources" / "controllers").glob("*.lynxctl"), key=lambda p: p.stem) texts = {p.stem: p.read_text(encoding="utf-8").replace("\r\n", "\n") for p in files} order, seen = [], set() def visit(stem): if stem in seen or stem not in texts: return seen.add(stem) for parent in re.findall(r"^\s*extends\s*=\s*(\S+)\s*$", texts[stem], re.M): visit(parent) order.append(stem) for stem in texts: visit(stem) out = ["#pragma once", "", "// Generated by tools/make_controllers.py from resources/controllers/*.lynxctl. Do not edit.", "", "namespace lynx::controller", "{", "struct BuiltInFile", "{", " const char* id;", " const char* text;", "};", "", "// (ends with an empty entry)", "inline const BuiltInFile builtInProfiles[] = {"] for stem in order: text = texts[stem] if ')lynxctl"' in text: sys.exit(f"{stem}: the text holds the raw string's end marker") out.append(f' {{ "{stem}", R"lynxctl({text})lynxctl" }},') out += [" { nullptr, nullptr },", "};", "} // namespace lynx::controller", ""] (root / "src" / "core" / "ControllerProfilesData.h").write_text("\n".join(out), encoding="utf-8", newline="\n") print(f"{len(order)} profiles")