render_release_body.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import json
  2. import os
  3. import sys
  4. from pathlib import Path
  5. PLACEHOLDERS = {
  6. "{{KSU_VERSION}}": lambda: os.environ.get("KSU_VERSION", "unknown"),
  7. "{{KSU_GIT_TAG}}": lambda: os.environ.get("KSU_GIT_TAG", "no-tag"),
  8. "{{KSUN_BRANCH}}": lambda: os.environ.get("KSUN_BRANCH", "dev"),
  9. "{{KSUN_COMMIT}}": lambda: os.environ.get("KSUN_COMMIT", "unknown"),
  10. "{{KSU_MANAGER}}": lambda: os.environ.get("KSU_MANAGER", "Placeholder"),
  11. }
  12. def render_markdown(template_path: Path):
  13. text = template_path.read_text()
  14. commits_path = template_path.parent / "commits.json"
  15. commits = json.loads(commits_path.read_text()) if commits_path.exists() else {}
  16. for placeholder, getter in PLACEHOLDERS.items():
  17. text = text.replace(placeholder, getter())
  18. susfs_branches = []
  19. for branch, commit in commits.get("susfs", {}).items():
  20. susfs_branches.append(f"**{branch}**\n`{commit}`")
  21. if "{{SUSFS_BRANCHS}}" in text:
  22. text = text.replace("{{SUSFS_BRANCHS}}", "\n".join(susfs_branches))
  23. if "{{SUSFS_BRANCHES}}" in text:
  24. text = text.replace("{{SUSFS_BRANCHES}}", "\n".join(susfs_branches))
  25. print(text, end="")
  26. config_path = Path(sys.argv[1])
  27. if config_path.suffix.lower() == ".md":
  28. render_markdown(config_path)
  29. sys.exit(0)
  30. # Backward-compatible JSON renderer for older release configs.
  31. def emit(text=""):
  32. print(text)
  33. def emit_list(items):
  34. if isinstance(items, list):
  35. for item in items:
  36. emit(f"- {item}")
  37. def emit_description(value):
  38. if isinstance(value, list):
  39. for line in value:
  40. emit(line)
  41. elif value:
  42. emit(str(value))
  43. data = json.loads(config_path.read_text())
  44. commits_path = config_path.parent / "commits.json"
  45. commits = json.loads(commits_path.read_text()) if commits_path.exists() else {}
  46. emit("**IMPORTANT DISCLAIMER**")
  47. for line in data["release"]["disclaimer"]:
  48. emit(line)
  49. kernelsu = data.get("kernelsu", {})
  50. emit()
  51. emit(f"## {kernelsu.get('name', 'KernelSU-Next')}")
  52. emit(f"- Version: {os.environ.get('KSU_VERSION', kernelsu.get('version', 'unknown'))}")
  53. emit(f"- Tag: {os.environ.get('KSU_GIT_TAG', kernelsu.get('tag', 'no-tag'))}")
  54. emit(f"- Branch: {os.environ.get('KSUN_BRANCH', kernelsu.get('branch', 'dev'))}")
  55. emit(f"- Commit: {os.environ.get('KSUN_COMMIT', kernelsu.get('commit', 'unknown'))}")
  56. if kernelsu.get("url"):
  57. emit(f"- URL: {kernelsu['url']}")
  58. if kernelsu.get("manager"):
  59. emit(f"- Manager: {kernelsu['manager']}")
  60. skip_keys = {"release", "kernelsu"}
  61. for key in data.keys():
  62. if key in skip_keys:
  63. continue
  64. section = data[key]
  65. emit()
  66. emit(f"## {section.get('name', key)}")
  67. if section.get("description"):
  68. emit_description(section["description"])
  69. if section.get("version"):
  70. emit(f"- Version: {section['version']}")
  71. if section.get("tag"):
  72. emit(f"- Tag: {section['tag']}")
  73. if section.get("branch"):
  74. emit(f"- Branch: {section['branch']}")
  75. if key == "susfs" and "susfs" in commits:
  76. emit("- Branches:")
  77. for branch, commit in commits["susfs"].items():
  78. emit(f"**{branch}**")
  79. emit(f"`{commit}`")
  80. if section.get("items"):
  81. emit_list(section["items"])
  82. if section.get("url"):
  83. emit(f"- URL: {section['url']}")