Debian Codegraph @

scripts/gen_cli_help_screenshots.py

raw
1 import os
2 import subprocess
3 from itertools import chain
4 from pathlib import Path
5
6 from rich.console import Console
7
8 from commitizen.cli import data
9
10
11 def gen_cli_help_screenshots() -> None:
12 """Generate the screenshot for help message on each cli command and save them as svg files."""
13 images_root = Path(__file__).parent.parent / "docs" / "images" / "cli_help"
14 images_root.mkdir(parents=True, exist_ok=True)
15
16 cz_commands = (
17 command["name"] if isinstance(command["name"], str) else command["name"][0]
18 for command in data["subcommands"]["commands"] # type: ignore[index]
19 )
20 for cmd in chain(
21 ["cz --help"], (f"cz {cz_command} --help" for cz_command in cz_commands)
22 ):
23 file_name = f"{cmd.replace(' ', '_').replace('-', '_')}.svg"
24 _export_cmd_as_svg(cmd, images_root / file_name)
25
26
27 def _export_cmd_as_svg(cmd: str, file_path: Path) -> None:
28 console = Console(record=True, width=80, file=open(os.devnull, "w"))
29
30 print("Processing command:", cmd)
31
32 console.print(f"$ {cmd}")
33 stdout = subprocess.run(cmd, shell=True, capture_output=True).stdout.decode("utf-8")
34 console.print(stdout)
35 console.save_svg(file_path.as_posix(), title="")
36
37 print("Saved to:", file_path.as_posix())
38
39
40 if __name__ == "__main__":
41 gen_cli_help_screenshots()