feat(setup): add automated User PATH configuration via registry and shell profile
This commit is contained in:
@@ -55,10 +55,10 @@
|
||||
"home_path": "Home Directory: {path}",
|
||||
"bin_path": "Bin Directory: {path}",
|
||||
"applets_path": "Applets Directory: {path}",
|
||||
"path_configured": "Bin directory is in system PATH.",
|
||||
"path_missing": "Bin directory is NOT in system PATH.",
|
||||
"path_instruction_windows": "To add to PATH, run in PowerShell: [Environment]::SetEnvironmentVariable('Path', $env:Path + ';{path}', 'User')",
|
||||
"path_instruction_posix": "To add to PATH, add this line to your shell profile (~/.bashrc or ~/.zshrc):\nexport PATH=\"{path}:$PATH\"",
|
||||
"path_configured": "Bin directory is already in system PATH.",
|
||||
"path_added": "Successfully added '{path}' to your User PATH.",
|
||||
"path_restart_notice": "Please restart your terminal (or dropdown window) for global commands to take effect.",
|
||||
"path_failed": "Could not automatically add to PATH. Please add manually:\n{manual}",
|
||||
"regenerated_shims": "Regenerated all command shims in {path}."
|
||||
},
|
||||
"search": {
|
||||
|
||||
+67
-7
@@ -291,18 +291,78 @@ def cmd_setup() -> int:
|
||||
print(catalog.format("commands.setup.regenerated_shims", path=bin_dir))
|
||||
print()
|
||||
|
||||
# Check PATH status
|
||||
current_path = os.environ.get("PATH", "")
|
||||
is_in_path = str(bin_dir).lower() in current_path.lower()
|
||||
# Check and configure PATH status
|
||||
bin_str = str(bin_dir)
|
||||
already_in_path = False
|
||||
|
||||
if is_in_path:
|
||||
# 1. Check current process PATH
|
||||
if bin_str.lower() in os.environ.get("PATH", "").lower():
|
||||
already_in_path = True
|
||||
|
||||
# 2. Check Windows User Registry
|
||||
if not already_in_path and os.name == "nt":
|
||||
try:
|
||||
import winreg
|
||||
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Environment", 0, winreg.KEY_READ) as key:
|
||||
val, _ = winreg.QueryValueEx(key, "Path")
|
||||
paths = [p.strip().lower() for p in val.split(";") if p.strip()]
|
||||
if bin_str.lower() in paths:
|
||||
already_in_path = True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if already_in_path:
|
||||
print(catalog.get("commands.setup.path_configured"))
|
||||
else:
|
||||
print(catalog.get("commands.setup.path_missing"))
|
||||
# Automatically configure PATH
|
||||
added = False
|
||||
if os.name == "nt":
|
||||
print(catalog.format("commands.setup.path_instruction_windows", path=bin_dir))
|
||||
try:
|
||||
import winreg
|
||||
import ctypes
|
||||
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Environment", 0, winreg.KEY_READ | winreg.KEY_WRITE) as key:
|
||||
try:
|
||||
current_val, val_type = winreg.QueryValueEx(key, "Path")
|
||||
except FileNotFoundError:
|
||||
current_val, val_type = "", winreg.REG_EXPAND_SZ
|
||||
|
||||
paths = [p for p in current_val.split(";") if p]
|
||||
if bin_str.lower() not in [p.lower() for p in paths]:
|
||||
paths.append(bin_str)
|
||||
new_val = ";".join(paths)
|
||||
winreg.SetValueEx(key, "Path", 0, val_type, new_val)
|
||||
added = True
|
||||
try:
|
||||
HWND_BROADCAST = 0xFFFF
|
||||
WM_SETTINGCHANGE = 0x001A
|
||||
SMTO_ABORTIFHUNG = 0x0002
|
||||
res = ctypes.c_ulong()
|
||||
ctypes.windll.user32.SendMessageTimeoutW(
|
||||
HWND_BROADCAST, WM_SETTINGCHANGE, 0, "Environment", SMTO_ABORTIFHUNG, 2000, ctypes.byref(res)
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
added = False
|
||||
else:
|
||||
print(catalog.format("commands.setup.path_instruction_posix", path=bin_dir))
|
||||
try:
|
||||
for rc in [".bashrc", ".zshrc", ".profile"]:
|
||||
rc_file = user_home / rc
|
||||
if rc_file.exists():
|
||||
content = rc_file.read_text(encoding="utf-8")
|
||||
if bin_str not in content:
|
||||
with open(rc_file, "a", encoding="utf-8") as f:
|
||||
f.write(f'\n# Gravitiny CLI PATH\nexport PATH="{bin_str}:$PATH"\n')
|
||||
added = True
|
||||
except Exception:
|
||||
added = False
|
||||
|
||||
if added:
|
||||
print(catalog.format("commands.setup.path_added", path=bin_dir))
|
||||
print(catalog.get("commands.setup.path_restart_notice"))
|
||||
else:
|
||||
manual_hint = f"PowerShell: [Environment]::SetEnvironmentVariable('Path', $env:Path + ';{bin_dir}', 'User')" if os.name == "nt" else f'export PATH="{bin_dir}:$PATH"'
|
||||
print(catalog.format("commands.setup.path_failed", manual=manual_hint))
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user