|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from invoke import task |
| 5 | +from invoke.context import Context |
| 6 | +from rich.console import Console |
| 7 | + |
| 8 | +from commitizen.cli import data |
| 9 | + |
| 10 | +project_root = Path(__file__).parent.parent.absolute() |
| 11 | +images_root = project_root / Path("docs") / Path("images") / Path("cli_help") |
| 12 | + |
| 13 | + |
| 14 | +@task |
| 15 | +def task_generate_help_screenshots(ctx: Context) -> None: |
| 16 | + """Generate the screenshot for help message on each tasks""" |
| 17 | + if not os.path.exists(images_root): |
| 18 | + os.makedirs(images_root) |
| 19 | + print(f'created {images_root}') |
| 20 | + |
| 21 | + with ctx.cd(project_root): |
| 22 | + help_cmds = _list_help_cmds() |
| 23 | + for cmd in help_cmds: |
| 24 | + file_name = f"{cmd.replace(' ', '_').replace('-', '_')}.svg" |
| 25 | + _export_cmd_as_svg(ctx, cmd, f"{images_root}/{file_name}") |
| 26 | + |
| 27 | +def _list_help_cmds() -> list[str]: |
| 28 | + cmds = [ |
| 29 | + f"{data['prog']} --help" |
| 30 | + ] + list( |
| 31 | + map(lambda sub_c: f"{data['prog']} {sub_c['name'] if isinstance(sub_c['name'], str) else sub_c['name'][0]} --help", |
| 32 | + data['subcommands']['commands']) |
| 33 | + ) |
| 34 | + |
| 35 | + return cmds |
| 36 | + |
| 37 | +def _export_cmd_as_svg(ctx: Context, cmd: str, file_name: str) -> None: |
| 38 | + stdout = ctx.run(cmd, hide="both").stdout |
| 39 | + console = Console(record=True, width=80) |
| 40 | + console.print(f"$ {cmd}\n{stdout}") |
| 41 | + console.save_svg(file_name, title="") |
| 42 | + |
| 43 | +# entry point |
| 44 | +def run_generate_help_screenshots(): |
| 45 | + ctx = Context() |
| 46 | + task_generate_help_screenshots(ctx) |
0 commit comments