make_controllers.py 1.7 KB

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