viv/scripts/generate-svgs.py

77 lines
1.6 KiB
Python
Raw Permalink Normal View History

2023-08-03 18:17:23 -05:00
#!/usr/bin/env python3
2023-06-01 00:04:02 -05:00
from pathlib import Path
from subprocess import run
from textwrap import dedent
from typing import Tuple
DOCS_PATH = Path(__file__).parent.parent / "docs"
2023-08-22 22:53:25 -05:00
(SAVE_PATH := DOCS_PATH / "svgs").mkdir(exist_ok=True, parents=True)
2023-06-01 00:04:02 -05:00
VIV = Path(__file__).parent.parent / "src" / "viv" / "viv.py"
CLI_DOC_PATH = DOCS_PATH / "cli.md"
2023-08-06 21:56:15 -05:00
(
cmds := dict.fromkeys(
(
"list",
"exe",
"manage",
"freeze",
"shim",
"run",
),
[],
)
2023-08-08 01:08:12 -05:00
).update(
{
"manage": ["update", "purge", "show", "install"],
"env": ["exe", "info", "remove"],
},
2023-08-08 01:08:12 -05:00
)
2023-06-01 00:04:02 -05:00
2023-08-21 15:16:18 -05:00
cli_doc = """\
---
hide: [navigation]
---
# CLI Reference
2023-06-01 00:04:02 -05:00
2023-08-05 12:08:31 -05:00
![help](./svgs/viv-help.svg)
2023-06-01 00:04:02 -05:00
"""
def yartsu(output: Path, args: Tuple[str, str] | str) -> None:
if isinstance(args, str):
args = (args,)
viv_cmd = " ".join(("viv", *args, "--help"))
cmd = f"{viv_cmd} | yartsu -w 70 -t '{viv_cmd}' -o {output}"
run(cmd, shell=True)
2023-08-10 11:08:26 -05:00
2023-06-01 00:04:02 -05:00
2023-08-10 11:08:26 -05:00
if not SAVE_PATH.is_dir():
SAVE_PATH.mkdir(exist_ok=True, parents=True)
2023-06-01 00:04:02 -05:00
yartsu(SAVE_PATH / "viv-help.svg", "")
for cmd, subcmds in cmds.items():
p = SAVE_PATH / f"viv-{cmd}-help.svg"
cli_doc += dedent(
f"""
## {cmd}
2023-08-05 12:08:31 -05:00
![cmd](./svgs/{p.name})
2023-06-01 00:04:02 -05:00
"""
)
yartsu(p, cmd)
for subcmd in subcmds:
p_sub = SAVE_PATH / f"viv-{cmd}-{subcmd}-help.svg"
cli_doc += dedent(
f"""
### {cmd} {subcmd}
2023-08-05 12:08:31 -05:00
![cmd](./svgs/{p_sub.name})
2023-06-01 00:04:02 -05:00
"""
)
yartsu(p_sub, [cmd, subcmd])
CLI_DOC_PATH.write_text(cli_doc)