pyinvoke / invoke

Pythonic task management & command execution.
http://pyinvoke.org
BSD 2-Clause "Simplified" License
4.42k stars 370 forks source link

Document adding print-completion-script to venv #959

Open cleder opened 1 year ago

cleder commented 1 year ago

We can also add the print-completion-script to the venv activate script. here an example how to do it with invoke

import os
import pathlib
import sys
import invoke

ROOT_DIR = pathlib.Path(__file__).parent

@invoke.task
def complete(context):
    """Activate the invoke shell completion."""
    shell = pathlib.Path(os.environ.get("SHELL", "")).name
    completion_script = context.run(
        f"invoke --print-completion-script {shell}",
        hide=True,
    ).stdout
    with open(".invoke-completion.sh", "w", encoding="UTF-8") as f:
        f.write(completion_script)
    venv_activate = pathlib.Path(sys.executable).parent / 'activate'
    with open(venv_activate, "a", encoding="UTF-8") as f:
        f.write(f"\n. {ROOT_DIR / '.invoke-completion.sh'}\n")