refactor: Decouple roles.
This commit is contained in:
@@ -8,26 +8,6 @@ import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from castle_cli.config import ensure_dirs, load_config
|
||||
from castle_cli.manifest import ComponentManifest
|
||||
|
||||
|
||||
def _sync_cmd(manifest: ComponentManifest) -> list[str] | None:
|
||||
"""Derive the sync command from the manifest's runner."""
|
||||
run = manifest.run
|
||||
if run is None:
|
||||
# No runner — check for build commands (frontends)
|
||||
if manifest.build and manifest.build.commands:
|
||||
# Frontends declare build commands; infer from source dir at call site
|
||||
return None
|
||||
return None
|
||||
|
||||
match run.runner:
|
||||
case "python":
|
||||
return ["uv", "sync"]
|
||||
case "node":
|
||||
return [run.package_manager, "install"]
|
||||
case _:
|
||||
return None
|
||||
|
||||
|
||||
def run_sync(args: argparse.Namespace) -> int:
|
||||
@@ -44,11 +24,12 @@ def run_sync(args: argparse.Namespace) -> int:
|
||||
if result.returncode != 0:
|
||||
print("Warning: git submodule update failed (may not be a git repo)")
|
||||
|
||||
# Sync dependencies in each project
|
||||
# Sync dependencies in each component's source directory
|
||||
all_ok = True
|
||||
synced_dirs: set[Path] = set()
|
||||
for name, manifest in config.components.items():
|
||||
source_dir = manifest.source_dir
|
||||
|
||||
for name, comp in config.components.items():
|
||||
source_dir = comp.source_dir
|
||||
if not source_dir:
|
||||
continue
|
||||
project_dir = config.root / source_dir
|
||||
@@ -56,14 +37,16 @@ def run_sync(args: argparse.Namespace) -> int:
|
||||
if project_dir in synced_dirs or not project_dir.is_dir():
|
||||
continue
|
||||
|
||||
cmd = _sync_cmd(manifest)
|
||||
# Determine sync command based on project type
|
||||
cmd = None
|
||||
if (project_dir / "pyproject.toml").exists():
|
||||
cmd = ["uv", "sync"]
|
||||
elif (project_dir / "package.json").exists():
|
||||
pm = "pnpm" if (project_dir / "pnpm-lock.yaml").exists() else "npm"
|
||||
cmd = [pm, "install"]
|
||||
|
||||
if cmd is None:
|
||||
# No runner — check if it's a frontend with a package.json
|
||||
if manifest.build and (project_dir / "package.json").exists():
|
||||
pm = "pnpm" if (project_dir / "pnpm-lock.yaml").exists() else "npm"
|
||||
cmd = [pm, "install"]
|
||||
else:
|
||||
continue
|
||||
continue
|
||||
|
||||
label = cmd[0]
|
||||
print(f"\nSyncing {name} ({label})...")
|
||||
@@ -75,28 +58,33 @@ def run_sync(args: argparse.Namespace) -> int:
|
||||
print(" OK")
|
||||
synced_dirs.add(project_dir)
|
||||
|
||||
# Install components as uv tools or symlinks
|
||||
# Install components and python-runner services as uv tools
|
||||
uv_path = shutil.which("uv") or "uv"
|
||||
installed_dirs: set[Path] = set()
|
||||
|
||||
for name, manifest in config.components.items():
|
||||
# Install if: has install.path, or is a python runner service
|
||||
if not (
|
||||
(manifest.install and manifest.install.path)
|
||||
or (manifest.run and manifest.run.runner == "python")
|
||||
):
|
||||
# Install components with install.path
|
||||
for name, comp in config.components.items():
|
||||
if not (comp.install and comp.install.path):
|
||||
continue
|
||||
|
||||
source = manifest.source_dir
|
||||
source = comp.source_dir
|
||||
if not source:
|
||||
continue
|
||||
_try_install(config.root / source, name, comp, uv_path, installed_dirs)
|
||||
|
||||
# Install python-runner services
|
||||
for name, svc in config.services.items():
|
||||
if svc.run.runner != "python":
|
||||
continue
|
||||
# Find source from component reference
|
||||
source = None
|
||||
if svc.component and svc.component in config.components:
|
||||
source = config.components[svc.component].source_dir
|
||||
if not source:
|
||||
continue
|
||||
source_dir = config.root / source
|
||||
|
||||
if source_dir in installed_dirs:
|
||||
continue
|
||||
if (source_dir / "pyproject.toml").exists():
|
||||
# Python package — uv tool install
|
||||
if source_dir in installed_dirs:
|
||||
continue
|
||||
print(f"\nInstalling {name}...")
|
||||
result = subprocess.run(
|
||||
[uv_path, "tool", "install", "--editable", str(source_dir), "--force"],
|
||||
@@ -113,21 +101,53 @@ def run_sync(args: argparse.Namespace) -> int:
|
||||
print(f" {name}: OK")
|
||||
installed_dirs.add(source_dir)
|
||||
|
||||
elif source_dir.is_file():
|
||||
# Script file — symlink to ~/.local/bin/
|
||||
alias = name
|
||||
if manifest.install and manifest.install.path and manifest.install.path.alias:
|
||||
alias = manifest.install.path.alias
|
||||
if not shutil.which(alias):
|
||||
link = Path.home() / ".local" / "bin" / alias
|
||||
link.parent.mkdir(parents=True, exist_ok=True)
|
||||
if not link.exists():
|
||||
link.symlink_to(source_dir)
|
||||
print(f"\n Linked {alias} → {source_dir}")
|
||||
|
||||
if all_ok:
|
||||
print("\nAll projects synced.")
|
||||
else:
|
||||
print("\nSync completed with warnings.")
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def _try_install(
|
||||
source_dir: Path,
|
||||
name: str,
|
||||
comp: object,
|
||||
uv_path: str,
|
||||
installed_dirs: set[Path],
|
||||
) -> bool:
|
||||
"""Try to install a component. Returns True if installed."""
|
||||
if source_dir in installed_dirs:
|
||||
return False
|
||||
|
||||
if (source_dir / "pyproject.toml").exists():
|
||||
print(f"\nInstalling {name}...")
|
||||
result = subprocess.run(
|
||||
[uv_path, "tool", "install", "--editable", str(source_dir), "--force"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
if "already installed" in result.stderr.lower():
|
||||
print(f" {name}: already installed")
|
||||
else:
|
||||
print(f" Warning: {result.stderr.strip()}")
|
||||
return False
|
||||
else:
|
||||
print(f" {name}: OK")
|
||||
installed_dirs.add(source_dir)
|
||||
return True
|
||||
|
||||
elif source_dir.is_file():
|
||||
alias = name
|
||||
if comp.install and comp.install.path and comp.install.path.alias:
|
||||
alias = comp.install.path.alias
|
||||
if not shutil.which(alias):
|
||||
link = Path.home() / ".local" / "bin" / alias
|
||||
link.parent.mkdir(parents=True, exist_ok=True)
|
||||
if not link.exists():
|
||||
link.symlink_to(source_dir)
|
||||
print(f"\n Linked {alias} → {source_dir}")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user