TSNDep/Src/Comands/Gestion.py

159 lines
5.9 KiB
Python
Raw Permalink Normal View History

2026-04-24 22:40:27 -05:00
#+------------------------------------------------------------------+
#| Imports |
#+------------------------------------------------------------------+
# Importamos todo
2026-04-25 06:51:23 -05:00
from Src.Comands.Defines import *
2026-04-24 22:40:27 -05:00
#+------------------------------------------------------------------+
#| Comando install |
#+------------------------------------------------------------------+
@tsndep.command()
@click.argument('url', required=True)
@click.argument('branch', required=True)
2026-04-25 06:51:23 -05:00
@click.option('--path_install', required=False, type=str, default=None, help='Custom path where the repository will be installed')
2026-04-24 22:40:27 -05:00
def install(url, branch, path_install):
2026-04-25 06:51:23 -05:00
"""Download/update dependencies"""
2026-04-24 22:40:27 -05:00
# Obtener ruta
root = path_install or os.getcwd()
root = os.path.abspath(root)
# Print rut
2026-04-25 06:51:23 -05:00
repo_name = url.rstrip("/").split("/")[-1]
click.echo(f"[INSTALL:{repo_name}] Root: {root}")
2026-04-24 22:40:27 -05:00
# Creamos la ruta donde se ubicara el repo
repo_path = os.path.join(root, repo_name)
# En caso no se clone damos error
if not GitCommands.clone_repo(url, repo_path, branch):
2026-04-25 06:51:23 -05:00
click.echo(f"[INSTALL:{repo_name}] Failed to clone repository", err=True)
2026-04-24 22:40:27 -05:00
return
else:
2026-04-25 06:51:23 -05:00
click.echo(f"[INSTALL:{repo_name}] Repository cloned at: {repo_path}")
2026-04-24 22:40:27 -05:00
# Procesar dependencias
visited : set = set()
# Run
2026-04-25 06:51:23 -05:00
if GitCommands.process_dependencies(repo_path, root, visited, on_pre_process=Hooks.execute_hooks, on_post_process=Hooks.execute_hooks):
click.echo(f"[INSTALL:{repo_name}] Installation completed successfully")
2026-04-24 22:40:27 -05:00
else:
2026-04-25 06:51:23 -05:00
click.echo(f"[INSTALL:{repo_name}] Installation completed with some errors")
2026-04-24 22:40:27 -05:00
#+------------------------------------------------------------------+
#| Comando add |
#+------------------------------------------------------------------+
@tsndep.command()
@click.argument('url', required=True)
@click.argument('branch', required=True)
2026-04-25 06:51:23 -05:00
@click.option('--name', required=True, type=str, help='Repository name')
@click.option('--comment', required=False, type=str, default='', help='Comment about the repository')
2026-04-24 22:40:27 -05:00
def add(url, branch, name, comment):
2026-04-25 06:51:23 -05:00
"""Add new dependency to dependencies.json"""
2026-04-24 22:40:27 -05:00
# Obtener path del JSON
json_path: str = os.path.join(os.getcwd(), "dependencies.json")
# Verificar que exista
if not os.path.exists(json_path):
2026-04-25 06:51:23 -05:00
click.echo(f"[ADD:{name}] Repository does not have dependencies.json", err=True)
2026-04-24 22:40:27 -05:00
return
# Cargar JSON
data: dict = JsonV.load_json(json_path)
if not data:
2026-04-25 06:51:23 -05:00
click.echo(f"[ADD:{name}] Failed to load JSON", err=True)
2026-04-24 22:40:27 -05:00
return
# Validar estructura
if "repos" not in data or not isinstance(data["repos"], list):
2026-04-25 06:51:23 -05:00
click.echo(f"[ADD:{name}] JSON does not have valid structure (missing 'repos')", err=True)
2026-04-24 22:40:27 -05:00
return
# Verificar que el repo no exista ya
if any(repo["name"] == name for repo in data["repos"]):
2026-04-25 06:51:23 -05:00
click.echo(f"[ADD:{name}] Repository '{name}' already exists", err=True)
2026-04-24 22:40:27 -05:00
return
# Crear nuevo repo
new_repo: dict = {
"name": name,
"url": url,
"rama": branch,
"comment": comment
}
# Agregar al JSON
data["repos"].append(new_repo)
# Guardar
if JsonV.save_json(json_path, data):
2026-04-25 06:51:23 -05:00
click.echo(f"[ADD:{name}] Added successfully")
click.echo(f"[ADD:{name}] URL: {url}")
click.echo(f"[ADD:{name}] Branch: {branch}")
2026-04-24 22:40:27 -05:00
else:
2026-04-25 06:51:23 -05:00
click.echo(f"[ADD:{name}] Error saving JSON", err=True)
2026-04-24 22:40:27 -05:00
#+------------------------------------------------------------------+
#| Comando remove |
#+------------------------------------------------------------------+
@tsndep.command()
@click.argument('repo_name', required=True)
def remove(repo_name):
2026-04-25 06:51:23 -05:00
"""Remove a dependency from dependencies.json"""
2026-04-24 22:40:27 -05:00
# Obtenemos el path json
json_path: str = os.path.join(os.getcwd(), "dependencies.json")
# Check de existencia
if not os.path.exists(json_path):
2026-04-25 06:51:23 -05:00
click.echo(f"[REMOVE:{repo_name}] Repository does not have dependencies.json", err=True)
2026-04-24 22:40:27 -05:00
return
# Cargamos y validamos
data: dict = JsonV.load_json(json_path)
if not data:
2026-04-25 06:51:23 -05:00
click.echo(f"[REMOVE:{repo_name}] Failed to load JSON", err=True)
2026-04-24 22:40:27 -05:00
return
if "repos" not in data:
2026-04-25 06:51:23 -05:00
click.echo(f"[REMOVE:{repo_name}] JSON does not have valid structure", err=True)
2026-04-24 22:40:27 -05:00
return
# Guardar original para comparación
original_count = len(data["repos"])
# Filtrar (remover el que coincida)
data["repos"] = [repo for repo in data["repos"] if repo["name"] != repo_name]
# Verificar que se removió algo
if len(data["repos"]) == original_count:
2026-04-25 06:51:23 -05:00
click.echo(f"[REMOVE:{repo_name}] Repository not found", err=True)
2026-04-24 22:40:27 -05:00
return
# Guardar
if JsonV.save_json(json_path, data):
2026-04-25 06:51:23 -05:00
click.echo(f"[REMOVE:{repo_name}] Removed successfully")
2026-04-24 22:40:27 -05:00
else:
2026-04-25 06:51:23 -05:00
click.echo(f"[REMOVE:{repo_name}] Error saving JSON", err=True)
2026-04-24 22:40:27 -05:00
#+------------------------------------------------------------------+
2026-04-25 06:51:23 -05:00
#| Comando update |
2026-04-24 22:40:27 -05:00
#+------------------------------------------------------------------+
@tsndep.command()
def update(repo_name):
2026-04-25 06:51:23 -05:00
"""Update repositories and dependencies"""
2026-04-24 22:40:27 -05:00
# Variables inciales
2026-04-25 06:51:23 -05:00
root : str = os.getcwd()
repo_basename: str = os.path.basename(root)
visited : set = set()
2026-04-24 22:40:27 -05:00
# Ejecutamos
2026-04-25 06:51:23 -05:00
if GitCommands.update_dependencies(root, root, visited, on_pre_process=Hooks.execute_hooks, on_post_process=Hooks.execute_hook):
click.echo(f"[UPDATE:{repo_basename}] Update completed successfully")
2026-04-24 22:40:27 -05:00
else:
2026-04-25 06:51:23 -05:00
click.echo(f"[UPDATE:{repo_basename}] Update completed with errors", err=True)