release: 0.2.2 fix post-install packaging and robustness
This commit is contained in:
@@ -5,5 +5,5 @@ from dataclasses import dataclass
|
||||
class AppConfig:
|
||||
"""Application configuration."""
|
||||
|
||||
app_name: str = "Python AI Dev Template"
|
||||
app_name: str = "cd-browser"
|
||||
debug: bool = False
|
||||
|
||||
@@ -19,8 +19,18 @@ def list_directories(path: Path, show_hidden: bool = False) -> list[DirectoryEnt
|
||||
directory_path = path.expanduser().resolve()
|
||||
entries: list[DirectoryEntry] = []
|
||||
|
||||
for child in directory_path.iterdir():
|
||||
if not child.is_dir():
|
||||
try:
|
||||
children = list(directory_path.iterdir())
|
||||
except PermissionError:
|
||||
return []
|
||||
|
||||
for child in children:
|
||||
try:
|
||||
is_directory = child.is_dir()
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
if not is_directory:
|
||||
continue
|
||||
if not show_hidden and child.name.startswith("."):
|
||||
continue
|
||||
@@ -41,8 +51,18 @@ def has_subdirectories(path: Path, show_hidden: bool = False) -> bool:
|
||||
|
||||
directory_path = path.expanduser().resolve()
|
||||
|
||||
for child in directory_path.iterdir():
|
||||
if not child.is_dir():
|
||||
try:
|
||||
children = list(directory_path.iterdir())
|
||||
except PermissionError:
|
||||
return False
|
||||
|
||||
for child in children:
|
||||
try:
|
||||
is_directory = child.is_dir()
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
if not is_directory:
|
||||
continue
|
||||
|
||||
if not show_hidden and child.name.startswith("."):
|
||||
|
||||
84
src/cd_browser_post_install.py
Normal file
84
src/cd_browser_post_install.py
Normal file
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Post-install script for cd-browser to guide users on shell integration."""
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def _profile_for_shell(shell: str) -> str:
|
||||
if shell == "zsh":
|
||||
return "~/.zshrc"
|
||||
|
||||
return "~/.bashrc"
|
||||
|
||||
|
||||
def _is_cd_function_present(profile_path: str) -> bool:
|
||||
if not os.path.exists(profile_path):
|
||||
return False
|
||||
|
||||
with open(profile_path, encoding="utf-8") as profile_file:
|
||||
return "cd_() {" in profile_file.read()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Print shell integration instructions after installation."""
|
||||
print("\n" + "=" * 60)
|
||||
print("🎉 cd-browser installed successfully!")
|
||||
print("=" * 60)
|
||||
print()
|
||||
print(
|
||||
"To enable the 'cd_' command, you need to add a function to your shell profile."
|
||||
)
|
||||
print()
|
||||
|
||||
shell = os.environ.get("SHELL", "").split("/")[-1]
|
||||
profile = _profile_for_shell(shell)
|
||||
|
||||
cd_function = """cd_() {
|
||||
local target
|
||||
target="$(cd_browser)" || return
|
||||
if [ -n "$target" ] && [ -d "$target" ]; then
|
||||
cd "$target"
|
||||
fi
|
||||
}"""
|
||||
|
||||
print(f"Copy this to the end of {profile}:")
|
||||
print()
|
||||
print(cd_function)
|
||||
print()
|
||||
|
||||
profile_path = os.path.expanduser(profile)
|
||||
|
||||
try:
|
||||
response = (
|
||||
input("Do you want me to add it automatically? (y/n): ").strip().lower()
|
||||
)
|
||||
if response == "y":
|
||||
if _is_cd_function_present(profile_path):
|
||||
print(f"\nℹ️ cd_() already exists in {profile}, no changes made.")
|
||||
else:
|
||||
with open(profile_path, "a", encoding="utf-8") as f:
|
||||
f.write("\n" + cd_function + "\n")
|
||||
print(f"\n✅ Added to {profile}")
|
||||
print(f"Run 'source {profile}' or restart your terminal to activate cd_.")
|
||||
else:
|
||||
print(f"\nAdd the function to {profile} manually.")
|
||||
print(f"Then run 'source {profile}' or restart terminal.")
|
||||
except EOFError:
|
||||
print(
|
||||
f"\nNo interactive input detected. Add the function to {profile} manually."
|
||||
)
|
||||
print(f"Then run 'source {profile}' or restart terminal.")
|
||||
except KeyboardInterrupt:
|
||||
print("\n\nSetup cancelled. You can add the function manually later.")
|
||||
sys.exit(0)
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print(
|
||||
f"For uninstall: after 'pip uninstall cd-browser', remove cd_() from {profile}."
|
||||
)
|
||||
print("=" * 60 + "\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user