From 964e3da6679e7e493ee42a4612d39fb1ec358048 Mon Sep 17 00:00:00 2001 From: Gravitiny Team Date: Sat, 3 Oct 2026 03:50:43 +0300 Subject: [PATCH] feat(setup): add automated User PATH configuration via registry and shell profile --- gravitiny/catalog/strings.json | 8 ++-- gravitiny/src/cli.py | 74 ++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/gravitiny/catalog/strings.json b/gravitiny/catalog/strings.json index 243906f..581fe43 100644 --- a/gravitiny/catalog/strings.json +++ b/gravitiny/catalog/strings.json @@ -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": { diff --git a/gravitiny/src/cli.py b/gravitiny/src/cli.py index 8c290c4..e7f5b33 100644 --- a/gravitiny/src/cli.py +++ b/gravitiny/src/cli.py @@ -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 + + # 1. Check current process PATH + if bin_str.lower() in os.environ.get("PATH", "").lower(): + already_in_path = True - if is_in_path: + # 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