Ensure ~/.gravitiny/bin is automatically registered in Windows User Registry PATH during first-run setup
This commit is contained in:
@@ -12,7 +12,7 @@ try:
|
||||
from gravitiny.src.launcher import AppletLauncher
|
||||
from gravitiny.src.manifest import load_manager_manifest
|
||||
from gravitiny.src.registry import RegistryClient
|
||||
from gravitiny.src.shims import ShimGenerator
|
||||
from gravitiny.src.shims import ShimGenerator, ensure_path_configured
|
||||
from gravitiny.src.uninstaller import GravitinyUninstaller
|
||||
except ImportError:
|
||||
from src.catalog import catalog
|
||||
@@ -21,7 +21,7 @@ except ImportError:
|
||||
from src.launcher import AppletLauncher
|
||||
from src.manifest import load_manager_manifest
|
||||
from src.registry import RegistryClient
|
||||
from src.shims import ShimGenerator
|
||||
from src.shims import ShimGenerator, ensure_path_configured
|
||||
from src.uninstaller import GravitinyUninstaller
|
||||
|
||||
|
||||
@@ -472,6 +472,9 @@ def _check_first_run_alias_prompt() -> None:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Ensure bin_dir is permanently registered in User PATH
|
||||
ensure_path_configured(bin_dir)
|
||||
|
||||
# If interactive terminal, prompt user for custom alias
|
||||
if sys.stdin.isatty():
|
||||
try:
|
||||
|
||||
+52
-2
@@ -9,12 +9,60 @@ except ImportError:
|
||||
from src.manifest import AppletManifest
|
||||
|
||||
|
||||
def ensure_path_configured(bin_dir: Optional[Path] = None) -> bool:
|
||||
"""Ensures bin_dir is permanently in Windows User Registry Environment Path or POSIX shell rc file."""
|
||||
if bin_dir is None:
|
||||
bin_dir = Path.home() / ".gravitiny" / "bin"
|
||||
bin_str = str(bin_dir)
|
||||
|
||||
if os.name == "nt":
|
||||
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)
|
||||
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
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
else:
|
||||
try:
|
||||
home = Path.home()
|
||||
for rc in [".bashrc", ".zshrc", ".profile"]:
|
||||
rc_file = 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')
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
class ShimGenerator:
|
||||
"""Generates and manages cross-platform command shims in the Gravitiny bin directory."""
|
||||
|
||||
def __init__(self, bin_dir: Optional[Path] = None):
|
||||
if bin_dir is None:
|
||||
# Default to ~/.gravitiny/bin or local gravitiny/bin
|
||||
user_home = Path.home()
|
||||
bin_dir = user_home / ".gravitiny" / "bin"
|
||||
self.bin_dir = Path(bin_dir)
|
||||
@@ -72,6 +120,7 @@ class ShimGenerator:
|
||||
pass
|
||||
created_paths.append(shim_file)
|
||||
|
||||
ensure_path_configured(self.bin_dir)
|
||||
return created_paths
|
||||
|
||||
def generate_master_shim(self, alias: str = "gravitiny") -> Path:
|
||||
@@ -102,7 +151,6 @@ class ShimGenerator:
|
||||
)
|
||||
with open(shim_file, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
return shim_file
|
||||
else:
|
||||
shim_file = self.bin_dir / clean_name
|
||||
if core_main.exists():
|
||||
@@ -121,6 +169,8 @@ class ShimGenerator:
|
||||
os.chmod(shim_file, 0o755)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
ensure_path_configured(self.bin_dir)
|
||||
return shim_file
|
||||
|
||||
def list_shims(self) -> List[Path]:
|
||||
|
||||
Reference in New Issue
Block a user