refactor: restructure mytoolkit with new subcommands and md-to-pdf improvements

- Add new subcommands: convert, preflight, server, templates, webpage
- Migrate config from bin/config.json to ~/.config/mytoolkit
- Fix expand_bookmarks to modify writer objects instead of reader
- Improve md_to_pdf with CJK bookmark support
- Update README and project metadata
This commit is contained in:
Zhengshou Lai
2026-05-04 17:19:32 +08:00
parent 38328c28d0
commit 3e4cf618a8
20 changed files with 1846 additions and 214 deletions
+86 -37
View File
@@ -1,60 +1,110 @@
"""Self-management commands for bin."""
import os
import shutil
import subprocess
import sys
from pathlib import Path
import click
def _get_project_root() -> Path:
"""Get mytoolkit project root."""
# This file is at bin/commands/self_mgmt.py
"""Get mytoolkit project root from source tree."""
return Path(__file__).parent.parent.parent
def _run(cmd: list[str], cwd: Path | None = None, check: bool = True) -> None:
result = subprocess.run(cmd, cwd=str(cwd) if cwd else None, check=False)
if check and result.returncode != 0:
raise click.Exit(result.returncode)
def _has_uv() -> bool:
return shutil.which("uv") is not None
def _install(root: Path) -> None:
venv_toolkit = root / ".venv" / "bin" / "mytoolkit"
user_local = Path.home() / ".local" / "bin" / "mytoolkit"
comp_dir = Path.home() / ".local" / "bin" / "completions"
# 1. Sync venv / install package
click.secho("Installing package…", fg="cyan")
if _has_uv():
_run(["uv", "sync"], cwd=root)
else:
venv_pip = root / ".venv" / "bin" / "pip"
if not venv_pip.exists():
_run([sys.executable, "-m", "venv", str(root / ".venv")])
_run([str(venv_pip), "install", "-e", str(root)], cwd=root)
if not venv_toolkit.exists():
click.secho(f"error: missing {venv_toolkit}", fg="red", err=True)
raise click.Exit(1)
# 2. Symlink to ~/.local/bin
click.secho(f"Linking {user_local}{venv_toolkit}", fg="cyan")
user_local.parent.mkdir(parents=True, exist_ok=True)
if user_local.exists() or user_local.is_symlink():
user_local.unlink()
user_local.symlink_to(venv_toolkit)
# 3. Install completions
comp_dir.mkdir(parents=True, exist_ok=True)
shell = os.environ.get("SHELL", "")
if "zsh" in shell:
comp_file = comp_dir / "_mytoolkit"
result = subprocess.run(
[str(venv_toolkit)],
env={**os.environ, "_MYTOOLKIT_COMPLETE": "zsh_source"},
capture_output=True,
text=True,
)
if result.returncode == 0:
comp_file.write_text(result.stdout)
click.secho(f"Installed zsh completion: {comp_file}", fg="green")
else:
click.secho("Warning: failed to generate zsh completion", fg="yellow")
elif "bash" in shell:
comp_file = comp_dir / "mytoolkit.bash"
result = subprocess.run(
[str(venv_toolkit)],
env={**os.environ, "_MYTOOLKIT_COMPLETE": "bash_source"},
capture_output=True,
text=True,
)
if result.returncode == 0:
comp_file.write_text(result.stdout)
click.secho(f"Installed bash completion: {comp_file}", fg="green")
else:
click.secho("Warning: failed to generate bash completion", fg="yellow")
def _uninstall() -> None:
user_local = Path.home() / ".local" / "bin" / "mytoolkit"
comp_dir = Path.home() / ".local" / "bin" / "completions"
for f in (comp_dir / "_mytoolkit", comp_dir / "mytoolkit.bash", user_local):
if f.exists() or f.is_symlink():
f.unlink()
click.secho(f"Removed {f}", fg="green")
@click.command(name="update")
def update_cmd():
"""Update mytoolkit from the local repository."""
root = _get_project_root()
click.secho("Updating mytoolkit...", fg="cyan")
# Run make install
result = subprocess.run(
["make", "install"],
cwd=str(root),
capture_output=True,
text=True,
)
if result.returncode == 0:
click.secho("mytoolkit updated successfully.", fg="green")
else:
click.secho("Update failed:", fg="red", err=True)
click.echo(result.stderr, err=True)
raise click.Exit(1)
click.secho("Updating mytoolkit…", fg="cyan")
_install(root)
click.secho("mytoolkit updated.", fg="green")
@click.command(name="uninstall")
def uninstall_cmd():
"""Uninstall mytoolkit."""
root = _get_project_root()
click.secho("Uninstalling mytoolkit...", fg="cyan")
result = subprocess.run(
["make", "uninstall"],
cwd=str(root),
capture_output=True,
text=True,
)
if result.returncode == 0:
click.secho("mytoolkit uninstalled.", fg="green")
else:
click.secho("Uninstall failed:", fg="red", err=True)
click.echo(result.stderr, err=True)
raise click.Exit(1)
click.secho("Uninstalling mytoolkit…", fg="cyan")
_uninstall()
click.secho("mytoolkit removed.", fg="green")
# Backward compatibility: keep self_cmd group for any existing scripts
@@ -64,6 +114,5 @@ def self_cmd():
pass
# Register commands in the group for backward compatibility
self_cmd.add_command(update_cmd, name="update")
self_cmd.add_command(uninstall_cmd, name="uninstall")