#!/usr/bin/env bash
# gradle_clean — Limpia build artifacts de un proyecto Android.
#
# Uso como libreria: source bash/functions/infra/gradle_clean.sh
# Uso directo:       bash bash/functions/infra/gradle_clean.sh <project_dir>

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=./gradle_run.sh
source "$SCRIPT_DIR/gradle_run.sh"

# ---------------------------------------------------------------------------
# gradle_clean <project_dir>
#
# Ejecuta `gradlew clean` en el proyecto y luego elimina los directorios
# de cache .gradle y build (best-effort).
#
# Exits:
#   0  — gradle clean exitoso (los rm son best-effort, no afectan exit code)
#   *  — exit code propagado de gradle_run / gradlew
# ---------------------------------------------------------------------------
gradle_clean() {
    local project_dir="$1"

    gradle_run "$project_dir" "clean" || return $?

    # Best-effort: eliminar caches locales; ignorar errores si no existen
    rm -rf "${project_dir}/.gradle" 2>/dev/null || true
    rm -rf "${project_dir}/build"   2>/dev/null || true

    return 0
}

# ---------------------------------------------------------------------------
# Ejecucion directa
# ---------------------------------------------------------------------------
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
    gradle_clean "$@"
fi
