Files

195 lines
7.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# desplegar.sh — Compila y publica Cómo Gasto en WebControlSigma (CLI Sync / siax-system.net)
# Protocolo estándar de despliegue del ecosistema Telcotronics.
#
# Uso: ./desplegar.sh # Diagnóstico, verificación y ayuda de opciones
# ./desplegar.sh --apk # Compilar APK Android
# ./desplegar.sh --aab # Compilar Android AppBundle (para Google Play)
# ./desplegar.sh --subir # Compilar APK y publicar en siax-system.net
# ./desplegar.sh --solo-subir ARCH # Publicar archivo binario existente a siax-system.net
# ./desplegar.sh --changelog "..." # Registro de cambios opcional para la subida
#
# Requisitos:
# - servers.conf con WEBCONTROL_API_KEY (ignorado por git)
# - Para builds release firmados: android/key.properties
#
set -Eeuo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
BOLD='\033[1m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; RESET='\033[0m'
CONF="$SCRIPT_DIR/servers.conf"
UPLOAD_URL="https://siax-system.net/api/upload_app.php"
FECHA_HOY="$(date +%Y-%m-%d)"
ICON_PATH="$SCRIPT_DIR/store_listing/graficos/icono_512.png"
[[ ! -f "$ICON_PATH" ]] && ICON_PATH="$SCRIPT_DIR/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png"
# ── Leer servers.conf ──────────────────────────────────────────────────────────
if [[ ! -f "$CONF" ]]; then
echo -e "${YELLOW}⚠️ servers.conf no encontrado.${RESET}"
echo " Copia servers.conf.example → servers.conf y configura tu WEBCONTROL_API_KEY."
if [[ "${1:-}" == "--solo-subir" || "${1:-}" == "--subir" ]]; then
exit 1
fi
else
# shellcheck source=/dev/null
source "$CONF"
fi
APP_NOMBRE="${APP_NOMBRE:-Cómo Gasto}"
# ── Leer versión de pubspec.yaml ──────────────────────────────────────────────
VERSION=$(grep '^version:' "$SCRIPT_DIR/pubspec.yaml" | sed 's/version: //' | cut -d'+' -f1 | tr -d '[:space:]')
BUILD_NUM=$(grep '^version:' "$SCRIPT_DIR/pubspec.yaml" | sed 's/version: //' | cut -s -d'+' -f2 | tr -d '[:space:]')
BUILD_NUM="${BUILD_NUM:-1}"
echo -e "${BOLD}📱 App:${RESET} $APP_NOMBRE | ${BOLD}Versión:${RESET} $VERSION (+$BUILD_NUM) | ${BOLD}Fecha:${RESET} $FECHA_HOY"
# ── Parsear argumentos ────────────────────────────────────────────────────────
HACER_APK=false
HACER_AAB=false
SUBIR=false
SOLO_SUBIR=false
ARCHIVO_SUBIR=""
CHANGELOG="Actualización v$VERSION — Modo demo sin conexión y activos de tienda preparados"
while [[ $# -gt 0 ]]; do
case "$1" in
--apk) HACER_APK=true ;;
--aab) HACER_AAB=true ;;
--subir) SUBIR=true; HACER_APK=true ;;
--solo-subir)
SOLO_SUBIR=true
shift
if [[ $# -gt 0 && ! "$1" =~ ^-- ]]; then
ARCHIVO_SUBIR="$1"
fi
;;
--changelog)
shift
CHANGELOG="$1"
;;
-h|--help)
echo ""
echo "Opciones:"
echo " --apk Compilar APK Android"
echo " --aab Compilar Android AppBundle"
echo " --subir Compilar y publicar APK en siax-system.net"
echo " --solo-subir FILE Publicar archivo existente en siax-system.net"
echo " --changelog MSG Mensaje de cambios para el registro en WebControl"
exit 0
;;
*)
echo "Opción desconocida: $1. Usa --help para ver las opciones."
exit 1
;;
esac
shift
done
# ── Función para subir a siax-system.net (WebControl) ─────────────────────────
subir_webcontrol() {
local plataforma="$1"
local archivo="$2"
local changelog="${3:-$CHANGELOG}"
if [[ -z "${WEBCONTROL_API_KEY:-}" || "$WEBCONTROL_API_KEY" == "TU_API_KEY_AQUI" ]]; then
echo -e "${RED}❌ WEBCONTROL_API_KEY no configurado en servers.conf.${RESET}"
return 1
fi
if [[ ! -f "$archivo" ]]; then
echo -e "${RED}❌ Archivo no encontrado para subir: $archivo${RESET}"
return 1
fi
echo -e "\n${BOLD}⬆️ Subiendo $plataforma a WebControl (${archivo})...${RESET}"
local curl_args=(
-sS --connect-timeout 15 --max-time 900 -w '\n%{http_code}'
-X POST "$UPLOAD_URL"
-H "x-api-key: $WEBCONTROL_API_KEY"
-F "nombre=$APP_NOMBRE"
-F "version=$VERSION"
-F "plataforma=$plataforma"
-F "fecha_release=$FECHA_HOY"
-F "archivo=@$archivo"
)
[[ -n "$changelog" ]] && curl_args+=(-F "changelog=$changelog")
[[ -f "$ICON_PATH" ]] && curl_args+=(-F "archivo_ico=@$ICON_PATH")
local resp
resp=$(curl "${curl_args[@]}")
local http_code body
body=$(echo "$resp" | head -n -1)
http_code=$(echo "$resp" | tail -n 1)
if [[ "$http_code" == "200" ]]; then
echo -e " ${GREEN}✅ OK — $plataforma registrado exitosamente (HTTP 200)${RESET}"
echo " Respuesta: $body"
else
echo -e " ${RED}❌ Error HTTP $http_code:${RESET}"
echo " $body"
return 1
fi
}
# ── Ejecución de flujo según banderas ─────────────────────────────────────────
if $SOLO_SUBIR; then
if [[ -z "$ARCHIVO_SUBIR" ]]; then
echo -e "${RED}❌ Debes especificar la ruta del archivo con --solo-subir <archivo>${RESET}"
exit 1
fi
subir_webcontrol "ANDROID" "$ARCHIVO_SUBIR" "$CHANGELOG"
exit 0
fi
if $HACER_APK; then
echo -e "\n${BOLD}🤖 Preparando compilación Android APK...${RESET}"
# Verificar key.properties
if [[ ! -f "android/key.properties" ]]; then
echo -e "${YELLOW}⚠️ android/key.properties no encontrado. Compilando en modo release sin firma de tienda.${RESET}"
fi
flutter build apk --release || {
echo -e "${RED}❌ Fallo al compilar APK.${RESET}"
echo -e " Verifica los requisitos de SDK o embedding documentados en .agente/proyecto_errores.md"
exit 1
}
APK_OUTPUT="build/app/outputs/flutter-apk/app-release.apk"
if [[ -f "$APK_OUTPUT" ]]; then
echo -e "${GREEN}✔ APK generado exitosamente:${RESET} $APK_OUTPUT ($(du -h "$APK_OUTPUT" | cut -f1))"
if $SUBIR; then
subir_webcontrol "ANDROID" "$APK_OUTPUT" "$CHANGELOG"
fi
fi
fi
if $HACER_AAB; then
echo -e "\n${BOLD}📦 Preparando compilación Android AppBundle (AAB)...${RESET}"
flutter build appbundle --release || {
echo -e "${RED}❌ Fallo al compilar AAB.${RESET}"
echo -e " Verifica los requisitos de SDK o embedding documentados en .agente/proyecto_errores.md"
exit 1
}
AAB_OUTPUT="build/app/outputs/bundle/release/app-release.aab"
if [[ -f "$AAB_OUTPUT" ]]; then
echo -e "${GREEN}✔ AAB generado exitosamente:${RESET} $AAB_OUTPUT ($(du -h "$AAB_OUTPUT" | cut -f1))"
fi
fi
if ! $HACER_APK && ! $HACER_AAB && ! $SOLO_SUBIR; then
echo ""
echo -e "${BOLD}Estado de preparación para despliegue:${RESET}"
echo " - servers.conf: $([[ -f "$CONF" ]] && echo -e "${GREEN}Presente${RESET}" || echo -e "${RED}Falta configurar${RESET}")"
echo " - key.properties: $([[ -f "android/key.properties" ]] && echo -e "${GREEN}Presente${RESET}" || echo -e "${YELLOW}Pendiente (usar android/key.properties.example)${RESET}")"
echo " - Activos de tienda: $([[ -f "store_listing/graficos/icono_512.png" ]] && echo -e "${GREEN}Listos en store_listing/graficos/${RESET}" || echo -e "${RED}No encontrados${RESET}")"
echo ""
echo "Para compilar y subir a siax-system.net ejecuta:"
echo " ./desplegar.sh --subir"
echo "O para subir un paquete específico existente:"
echo " ./desplegar.sh --solo-subir <ruta-al-apk>"
fi