160 lines
6 KiB
Python
160 lines
6 KiB
Python
#+------------------------------------------------------------------+
|
|
#| Imports |
|
|
#+------------------------------------------------------------------+
|
|
# Importamos todo
|
|
from Src.Comands.Defines import *
|
|
import os
|
|
|
|
#+------------------------------------------------------------------+
|
|
#| Comando install |
|
|
#+------------------------------------------------------------------+
|
|
@tsndep.command()
|
|
@click.argument('url', required=True)
|
|
@click.argument('branch', required=True)
|
|
@click.option('--path_install', required=False, type=str, default=None, help='Custom path where the repository will be installed')
|
|
def install(url, branch, path_install):
|
|
"""Download/update dependencies"""
|
|
|
|
# Obtener ruta
|
|
root = path_install or os.getcwd()
|
|
root = os.path.abspath(root)
|
|
|
|
# Print rut
|
|
repo_name = url.rstrip("/").split("/")[-1]
|
|
click.echo(f"[INSTALL:{repo_name}] Root: {root}")
|
|
|
|
# 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):
|
|
click.echo(f"[INSTALL:{repo_name}] Failed to clone repository", err=True)
|
|
return
|
|
else:
|
|
click.echo(f"[INSTALL:{repo_name}] Repository cloned at: {repo_path}")
|
|
|
|
# Procesar dependencias
|
|
visited : set = set()
|
|
|
|
# Run
|
|
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")
|
|
else:
|
|
click.echo(f"[INSTALL:{repo_name}] Installation completed with some errors")
|
|
|
|
#+------------------------------------------------------------------+
|
|
#| Comando add |
|
|
#+------------------------------------------------------------------+
|
|
@tsndep.command()
|
|
@click.argument('url', required=True)
|
|
@click.argument('branch', required=True)
|
|
@click.option('--name', required=True, type=str, help='Repository name')
|
|
@click.option('--comment', required=False, type=str, default='', help='Comment about the repository')
|
|
def add(url, branch, name, comment):
|
|
"""Add new dependency to dependencies.json"""
|
|
|
|
# Obtener path del JSON
|
|
json_path: str = os.path.join(os.getcwd(), "dependencies.json")
|
|
|
|
# Verificar que exista
|
|
if not os.path.exists(json_path):
|
|
click.echo(f"[ADD:{name}] Repository does not have dependencies.json", err=True)
|
|
return
|
|
|
|
# Cargar JSON
|
|
data: dict = JsonV.load_json(json_path)
|
|
|
|
if not data:
|
|
click.echo(f"[ADD:{name}] Failed to load JSON", err=True)
|
|
return
|
|
|
|
# Validar estructura
|
|
if "repos" not in data or not isinstance(data["repos"], list):
|
|
click.echo(f"[ADD:{name}] JSON does not have valid structure (missing 'repos')", err=True)
|
|
return
|
|
|
|
# Verificar que el repo no exista ya
|
|
if any(repo["name"] == name for repo in data["repos"]):
|
|
click.echo(f"[ADD:{name}] Repository '{name}' already exists", err=True)
|
|
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):
|
|
click.echo(f"[ADD:{name}] Added successfully")
|
|
click.echo(f"[ADD:{name}] URL: {url}")
|
|
click.echo(f"[ADD:{name}] Branch: {branch}")
|
|
else:
|
|
click.echo(f"[ADD:{name}] Error saving JSON", err=True)
|
|
|
|
#+------------------------------------------------------------------+
|
|
#| Comando remove |
|
|
#+------------------------------------------------------------------+
|
|
@tsndep.command()
|
|
@click.argument('repo_name', required=True)
|
|
def remove(repo_name):
|
|
"""Remove a dependency from dependencies.json"""
|
|
|
|
# Obtenemos el path json
|
|
json_path: str = os.path.join(os.getcwd(), "dependencies.json")
|
|
|
|
# Check de existencia
|
|
if not os.path.exists(json_path):
|
|
click.echo(f"[REMOVE:{repo_name}] Repository does not have dependencies.json", err=True)
|
|
return
|
|
|
|
# Cargamos y validamos
|
|
data: dict = JsonV.load_json(json_path)
|
|
if not data:
|
|
click.echo(f"[REMOVE:{repo_name}] Failed to load JSON", err=True)
|
|
return
|
|
|
|
if "repos" not in data:
|
|
click.echo(f"[REMOVE:{repo_name}] JSON does not have valid structure", err=True)
|
|
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:
|
|
click.echo(f"[REMOVE:{repo_name}] Repository not found", err=True)
|
|
return
|
|
|
|
# Guardar
|
|
if JsonV.save_json(json_path, data):
|
|
click.echo(f"[REMOVE:{repo_name}] Removed successfully")
|
|
else:
|
|
click.echo(f"[REMOVE:{repo_name}] Error saving JSON", err=True)
|
|
|
|
|
|
#+------------------------------------------------------------------+
|
|
#| Comando update |
|
|
#+------------------------------------------------------------------+
|
|
@tsndep.command()
|
|
def update(repo_name):
|
|
"""Update repositories and dependencies"""
|
|
|
|
# Variables inciales
|
|
root : str = os.getcwd()
|
|
repo_basename: str = os.path.basename(root)
|
|
visited : set = set()
|
|
|
|
# Ejecutamos
|
|
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")
|
|
else:
|
|
click.echo(f"[UPDATE:{repo_basename}] Update completed with errors", err=True)
|