130 lines
4.7 KiB
Python
130 lines
4.7 KiB
Python
import os
|
|
import shutil
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Ensure local package can be imported during installation
|
|
workspace_root = Path(__file__).resolve().parent
|
|
if str(workspace_root) not in sys.path:
|
|
sys.path.insert(0, str(workspace_root))
|
|
|
|
from gravitiny.src.installer import AppletInstaller
|
|
from gravitiny.src.manifest import load_manager_manifest
|
|
|
|
|
|
def install_gravitiny_core() -> int:
|
|
user_home = Path.home()
|
|
gravitiny_home = user_home / ".gravitiny"
|
|
core_dir = gravitiny_home / "core"
|
|
bin_dir = gravitiny_home / "bin"
|
|
applets_dir = gravitiny_home / "applets"
|
|
|
|
print("Installing Gravitiny Core...")
|
|
|
|
bin_dir.mkdir(parents=True, exist_ok=True)
|
|
applets_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
# 1. Copy Core package into ~/.gravitiny/core
|
|
source_gravitiny = workspace_root / "gravitiny"
|
|
if core_dir.exists():
|
|
AppletInstaller._safe_rmtree(core_dir)
|
|
|
|
core_dir.mkdir(parents=True, exist_ok=True)
|
|
for item in source_gravitiny.iterdir():
|
|
if item.name in ["applets", "__pycache__", ".git"]:
|
|
continue
|
|
dest_item = core_dir / item.name
|
|
if item.is_dir():
|
|
shutil.copytree(item, dest_item)
|
|
else:
|
|
shutil.copy2(item, dest_item)
|
|
|
|
# 2. Generate Master Gravitiny Shim pointing to ~/.gravitiny/core/main.py
|
|
is_windows = os.name == "nt"
|
|
python_exe = sys.executable
|
|
core_main = core_dir / "main.py"
|
|
|
|
if is_windows:
|
|
master_shim = bin_dir / "gravitiny.cmd"
|
|
with open(master_shim, "w", encoding="utf-8") as f:
|
|
f.write(f"@echo off\nsetlocal\n\"{python_exe}\" \"{core_main}\" %*\n")
|
|
else:
|
|
master_shim = bin_dir / "gravitiny"
|
|
with open(master_shim, "w", encoding="utf-8") as f:
|
|
f.write(f"#!/usr/bin/env sh\nexec \"{python_exe}\" \"{core_main}\" \"$@\"\n")
|
|
try:
|
|
os.chmod(master_shim, 0o755)
|
|
except Exception:
|
|
pass
|
|
|
|
# 3. Configure PATH
|
|
bin_str = str(bin_dir)
|
|
already_in_path = False
|
|
|
|
if bin_str.lower() in os.environ.get("PATH", "").lower():
|
|
already_in_path = True
|
|
|
|
if not already_in_path and is_windows:
|
|
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.strip()]
|
|
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
|
|
already_in_path = True
|
|
except Exception:
|
|
pass
|
|
elif not already_in_path and not is_windows:
|
|
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')
|
|
already_in_path = True
|
|
except Exception:
|
|
pass
|
|
|
|
manifest = load_manager_manifest()
|
|
print("=" * 65)
|
|
print(f" {manifest.get('name', 'Gravitiny')} Core v{manifest.get('version', '0.1.0')} installed successfully!")
|
|
print("=" * 65)
|
|
print(f" Core Location : {core_dir}")
|
|
print(f" Bin Location : {bin_dir} (Registered in PATH)")
|
|
print(f" Applets Root : {applets_dir}")
|
|
print()
|
|
print(" [IMPORTANT] Installation is completely self-contained.")
|
|
print(" You may now safely DELETE this cloned/downloaded repository folder.")
|
|
print()
|
|
print(" Next steps:")
|
|
print(" 1. Restart your terminal window.")
|
|
print(" 2. Run: gravitiny search dictionary")
|
|
print(" 3. Run: gravitiny install sec")
|
|
print("=" * 65)
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(install_gravitiny_core())
|