refactor(self-mgmt): adapt update/uninstall to system-python editable install
This commit is contained in:
@@ -1,51 +1,31 @@
|
|||||||
ROOT_DIR := $(shell pwd)
|
ROOT_DIR := $(shell pwd)
|
||||||
VENV_MYTOOLKIT := $(ROOT_DIR)/.venv/bin/mytoolkit
|
|
||||||
USER_LOCAL_MYTOOLKIT := $(HOME)/.local/bin/mytoolkit
|
|
||||||
COMP_DIR := $(HOME)/.local/bin/completions
|
COMP_DIR := $(HOME)/.local/bin/completions
|
||||||
# Use user's login shell for completion detection
|
PYTHON ?= python3
|
||||||
SHELL_NAME := $(notdir $(basename $(shell echo $$SHELL)))
|
|
||||||
|
|
||||||
.PHONY: help install uninstall _symlink-mytoolkit _install-completions _uninstall-completions
|
.PHONY: help install uninstall
|
||||||
|
|
||||||
help:
|
help:
|
||||||
@echo "Usage: make [target]"
|
@echo "Usage: make [target]"
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo " install Sync venv, symlink bin, install completions"
|
@echo " install Install mytoolkit in editable mode (current Python)"
|
||||||
@echo " uninstall Remove bin and completions"
|
@echo " uninstall Uninstall mytoolkit and remove completions"
|
||||||
|
|
||||||
install:
|
install:
|
||||||
@cd "$(ROOT_DIR)" && \
|
$(PYTHON) -m pip install -e "$(ROOT_DIR)" --upgrade
|
||||||
if command -v uv >/dev/null 2>&1; then \
|
@echo "Installing completions…"
|
||||||
uv sync; \
|
|
||||||
else \
|
|
||||||
test -x .venv/bin/pip || python3 -m venv .venv; \
|
|
||||||
.venv/bin/pip install -e .; \
|
|
||||||
fi
|
|
||||||
@$(MAKE) _symlink-mytoolkit
|
|
||||||
@$(MAKE) _install-completions
|
|
||||||
|
|
||||||
_symlink-mytoolkit:
|
|
||||||
@test -x "$(VENV_MYTOOLKIT)" || { echo "error: missing $(VENV_MYTOOLKIT)"; exit 1; }
|
|
||||||
@mkdir -p "$(HOME)/.local/bin"
|
|
||||||
@ln -sf "$(VENV_MYTOOLKIT)" "$(USER_LOCAL_MYTOOLKIT)"
|
|
||||||
@echo "Linked $(USER_LOCAL_MYTOOLKIT) -> $(VENV_MYTOOLKIT)"
|
|
||||||
|
|
||||||
_install-completions:
|
|
||||||
@mkdir -p "$(COMP_DIR)"
|
@mkdir -p "$(COMP_DIR)"
|
||||||
ifeq ($(SHELL_NAME),zsh)
|
@SHELL_NAME=$$(basename "$$SHELL"); \
|
||||||
@_MYTOOLKIT_COMPLETE=zsh_source "$(VENV_MYTOOLKIT)" > "$(COMP_DIR)/_mytoolkit" 2>/dev/null && \
|
if [ "$$SHELL_NAME" = "zsh" ]; then \
|
||||||
echo "Installed zsh completion: $(COMP_DIR)/_mytoolkit" || \
|
_MYTOOLKIT_COMPLETE=zsh_source mytoolkit > "$(COMP_DIR)/_mytoolkit" && \
|
||||||
echo "Warning: failed to generate zsh completion"
|
echo "zsh completion: $(COMP_DIR)/_mytoolkit" || \
|
||||||
else ifeq ($(SHELL_NAME),bash)
|
echo "Warning: zsh completion failed"; \
|
||||||
@_MYTOOLKIT_COMPLETE=bash_source "$(VENV_MYTOOLKIT)" > "$(COMP_DIR)/mytoolkit.bash" 2>/dev/null && \
|
elif [ "$$SHELL_NAME" = "bash" ]; then \
|
||||||
echo "Installed bash completion: $(COMP_DIR)/mytoolkit.bash" || \
|
_MYTOOLKIT_COMPLETE=bash_source mytoolkit > "$(COMP_DIR)/mytoolkit.bash" && \
|
||||||
echo "Warning: failed to generate bash completion"
|
echo "bash completion: $(COMP_DIR)/mytoolkit.bash" || \
|
||||||
endif
|
echo "Warning: bash completion failed"; \
|
||||||
|
fi
|
||||||
|
|
||||||
uninstall: _uninstall-completions
|
uninstall:
|
||||||
@rm -f "$(USER_LOCAL_MYTOOLKIT)"
|
$(PYTHON) -m pip uninstall mytoolkit -y
|
||||||
@echo "Removed $(USER_LOCAL_MYTOOLKIT)"
|
rm -f "$(COMP_DIR)/_mytoolkit" "$(COMP_DIR)/mytoolkit.bash"
|
||||||
|
@echo "Uninstalled mytoolkit and removed completions"
|
||||||
_uninstall-completions:
|
|
||||||
@rm -f "$(COMP_DIR)/_mytoolkit" "$(COMP_DIR)/mytoolkit.bash"
|
|
||||||
@echo "Removed completions"
|
|
||||||
|
|||||||
+24
-31
@@ -20,43 +20,33 @@ def _run(cmd: list[str], cwd: Path | None = None, check: bool = True) -> None:
|
|||||||
raise click.Exit(result.returncode)
|
raise click.Exit(result.returncode)
|
||||||
|
|
||||||
|
|
||||||
def _has_uv() -> bool:
|
|
||||||
return shutil.which("uv") is not None
|
|
||||||
|
|
||||||
|
|
||||||
def _install(root: Path) -> None:
|
def _install(root: Path) -> None:
|
||||||
venv_toolkit = root / ".venv" / "bin" / "mytoolkit"
|
"""Install or update using the current Python's pip (editable)."""
|
||||||
user_local = Path.home() / ".local" / "bin" / "mytoolkit"
|
click.secho("Installing mytoolkit (editable)…", fg="cyan")
|
||||||
comp_dir = Path.home() / ".local" / "bin" / "completions"
|
_run([sys.executable, "-m", "pip", "install", "-e", str(root), "--upgrade"])
|
||||||
|
|
||||||
# 1. Sync venv / install package
|
entry_point = shutil.which("mytoolkit")
|
||||||
click.secho("Installing package…", fg="cyan")
|
if entry_point:
|
||||||
if _has_uv():
|
click.secho(f"Entry point: {entry_point}", fg="green")
|
||||||
_run(["uv", "sync"], cwd=root)
|
|
||||||
else:
|
else:
|
||||||
venv_pip = root / ".venv" / "bin" / "pip"
|
click.secho("Warning: mytoolkit not found in PATH after install", fg="yellow")
|
||||||
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():
|
_install_completions()
|
||||||
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
|
def _install_completions() -> None:
|
||||||
|
venv_toolkit = shutil.which("mytoolkit")
|
||||||
|
if not venv_toolkit:
|
||||||
|
click.secho("Warning: cannot generate completions (mytoolkit not in PATH)", fg="yellow")
|
||||||
|
return
|
||||||
|
|
||||||
|
comp_dir = Path.home() / ".local" / "bin" / "completions"
|
||||||
comp_dir.mkdir(parents=True, exist_ok=True)
|
comp_dir.mkdir(parents=True, exist_ok=True)
|
||||||
shell = os.environ.get("SHELL", "")
|
shell = os.environ.get("SHELL", "")
|
||||||
if "zsh" in shell:
|
if "zsh" in shell:
|
||||||
comp_file = comp_dir / "_mytoolkit"
|
comp_file = comp_dir / "_mytoolkit"
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
[str(venv_toolkit)],
|
[venv_toolkit],
|
||||||
env={**os.environ, "_MYTOOLKIT_COMPLETE": "zsh_source"},
|
env={**os.environ, "_MYTOOLKIT_COMPLETE": "zsh_source"},
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
@@ -69,7 +59,7 @@ def _install(root: Path) -> None:
|
|||||||
elif "bash" in shell:
|
elif "bash" in shell:
|
||||||
comp_file = comp_dir / "mytoolkit.bash"
|
comp_file = comp_dir / "mytoolkit.bash"
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
[str(venv_toolkit)],
|
[venv_toolkit],
|
||||||
env={**os.environ, "_MYTOOLKIT_COMPLETE": "bash_source"},
|
env={**os.environ, "_MYTOOLKIT_COMPLETE": "bash_source"},
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
@@ -82,9 +72,14 @@ def _install(root: Path) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def _uninstall() -> None:
|
def _uninstall() -> None:
|
||||||
user_local = Path.home() / ".local" / "bin" / "mytoolkit"
|
click.secho("Uninstalling mytoolkit package…", fg="cyan")
|
||||||
|
subprocess.run(
|
||||||
|
[sys.executable, "-m", "pip", "uninstall", "mytoolkit", "-y"],
|
||||||
|
check=False,
|
||||||
|
)
|
||||||
|
|
||||||
comp_dir = Path.home() / ".local" / "bin" / "completions"
|
comp_dir = Path.home() / ".local" / "bin" / "completions"
|
||||||
for f in (comp_dir / "_mytoolkit", comp_dir / "mytoolkit.bash", user_local):
|
for f in (comp_dir / "_mytoolkit", comp_dir / "mytoolkit.bash"):
|
||||||
if f.exists() or f.is_symlink():
|
if f.exists() or f.is_symlink():
|
||||||
f.unlink()
|
f.unlink()
|
||||||
click.secho(f"Removed {f}", fg="green")
|
click.secho(f"Removed {f}", fg="green")
|
||||||
@@ -105,5 +100,3 @@ def uninstall_cmd():
|
|||||||
click.secho("Uninstalling mytoolkit…", fg="cyan")
|
click.secho("Uninstalling mytoolkit…", fg="cyan")
|
||||||
_uninstall()
|
_uninstall()
|
||||||
click.secho("mytoolkit removed.", fg="green")
|
click.secho("mytoolkit removed.", fg="green")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user