.agent/scripts/atualizar-submodulo.sh

128 lines
5 KiB
Bash
Executable file
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# ==============================================================================
# Script de Ciclo de Vida: Atualização do Submódulo .agent e Runtime Bun
# - Sincroniza o submódulo .agent na branch master (1x por sessão de conversa)
# - Verifica e atualiza o Bun com `bun upgrade --stable` (1x por dia)
# Compatível com CLI e Lifecycle Hook do Antigravity (PreInvocation).
# ==============================================================================
set -e
# Garantir que o diretório padrão do Bun esteja no PATH em execuções não-interativas (Hook)
export PATH="$HOME/.bun/bin:$PATH"
INPUT_JSON=""
IS_PIPE=0
if [ ! -t 0 ]; then
IS_PIPE=1
INPUT_JSON=$(cat 2>/dev/null || true)
fi
# Extrair conversationId se chamado via Hook Antigravity
CONV_ID=""
if [ -n "$INPUT_JSON" ]; then
CONV_ID=$(echo "$INPUT_JSON" | grep -o '"conversationId":[[:space:]]*"[^"]*' | sed 's/.*"//' || true)
fi
# Controle de verificação diária do Bun (1x por dia: YYYY-MM-DD)
BUN_STAMP_FILE="/tmp/.antigravity_bun_upgrade_date"
TODAY=$(date +%Y-%m-%d)
LAST_BUN_CHECK=$(cat "$BUN_STAMP_FILE" 2>/dev/null || true)
NEEDS_BUN_CHECK=0
if [ "$LAST_BUN_CHECK" != "$TODAY" ]; then
NEEDS_BUN_CHECK=1
fi
# Idempotência por sessão: se o submódulo já foi atualizado nesta conversa E o Bun já foi verificado hoje
if [ -n "$CONV_ID" ]; then
LOCK_FILE="/tmp/.antigravity_agent_updated_${CONV_ID}"
if [ -f "$LOCK_FILE" ] && [ "$NEEDS_BUN_CHECK" -eq 0 ]; then
echo "{}"
exit 0
fi
fi
# Executar verificação diária do Bun (1x por dia)
BUN_STATUS_MSG=""
if [ "$NEEDS_BUN_CHECK" -eq 1 ]; then
if command -v bun >/dev/null 2>&1; then
if bun upgrade --stable >/dev/null 2>&1; then
echo "$TODAY" > "$BUN_STAMP_FILE" 2>/dev/null || true
BUN_VER=$(bun --version 2>/dev/null || true)
BUN_STATUS_MSG=" Bun verificado/atualizado hoje (bun upgrade --stable -> v${BUN_VER})."
else
BUN_STATUS_MSG=" Aviso: não foi possível verificar atualização diária do Bun (bun upgrade --stable)."
fi
else
BUN_STATUS_MSG=" Aviso: binário 'bun' não encontrado no PATH para verificação diária."
fi
fi
# Se a sessão já havia atualizado o submódulo antes (ex: virou o dia na mesma conversa), retornar status do Bun
if [ -n "$CONV_ID" ] && [ -f "$LOCK_FILE" ]; then
if [ "$IS_PIPE" -eq 1 ] && [ -n "$INPUT_JSON" ] && [ -n "$BUN_STATUS_MSG" ]; then
echo "{\"injectSteps\":[{\"ephemeralMessage\":\"ℹ️${BUN_STATUS_MSG}\"}]}"
elif [ "$IS_PIPE" -eq 1 ]; then
echo "{}"
else
echo "✅${BUN_STATUS_MSG}"
fi
exit 0
fi
# Detectar diretório raiz do superprojeto
SUPER_DIR=$(git rev-parse --show-superproject-working-tree 2>/dev/null || true)
if [ -z "$SUPER_DIR" ]; then
if [ -f ".gitmodules" ]; then
SUPER_DIR="."
elif [ -f "../.gitmodules" ]; then
SUPER_DIR=".."
elif [ -f "../../.gitmodules" ]; then
SUPER_DIR="../.."
else
SUPER_DIR="."
fi
fi
if [ -f "$SUPER_DIR/.gitmodules" ]; then
# Garantir configuração branch = master no .gitmodules
CURRENT_BRANCH=$(git -C "$SUPER_DIR" config -f .gitmodules submodule..agent.branch 2>/dev/null || true)
if [ "$CURRENT_BRANCH" != "master" ]; then
git -C "$SUPER_DIR" config -f .gitmodules submodule..agent.branch master 2>/dev/null || true
fi
# Executar a atualização remota do submódulo .agent preservando commits locais (rebase)
if git -C "$SUPER_DIR" submodule update --init --recursive --remote --rebase .agent >/dev/null 2>&1 || \
git -C "$SUPER_DIR" submodule update --init --recursive --remote .agent >/dev/null 2>&1; then
# Remover script legado menu/graphify-atualizar.sh (substituído por .agent/scripts/graphify.sh) e garantir .gitignore
rm -f "$SUPER_DIR/menu/graphify-atualizar.sh" 2>/dev/null || true
if [ -f "$SUPER_DIR/.agent/scripts/graphify.sh" ]; then
bash "$SUPER_DIR/.agent/scripts/graphify.sh" gitignore >/dev/null 2>&1 || true
fi
[ -n "$CONV_ID" ] && touch "$LOCK_FILE" 2>/dev/null || true
if [ "$IS_PIPE" -eq 1 ] && [ -n "$INPUT_JSON" ]; then
echo "{\"injectSteps\":[{\"ephemeralMessage\":\"ℹ️ Submódulo .agent sincronizado com sucesso na branch master (git submodule update --init --recursive --remote).${BUN_STATUS_MSG}\"}]}"
else
echo "✅ Submódulo .agent sincronizado com sucesso na branch master.${BUN_STATUS_MSG}"
fi
exit 0
else
if [ "$IS_PIPE" -eq 1 ] && [ -n "$INPUT_JSON" ]; then
echo "{\"injectSteps\":[{\"ephemeralMessage\":\"⚠️ Não foi possível sincronizar o submódulo .agent com origin/master (sem conexão ou erro de rede). Continuando com versão local.${BUN_STATUS_MSG}\"}]}"
else
echo "⚠️ Não foi possível sincronizar o submódulo .agent com origin/master. Continuando com versão local.${BUN_STATUS_MSG}"
fi
exit 0
fi
fi
if [ "$IS_PIPE" -eq 1 ]; then
if [ -n "$INPUT_JSON" ] && [ -n "$BUN_STATUS_MSG" ]; then
echo "{\"injectSteps\":[{\"ephemeralMessage\":\"ℹ️${BUN_STATUS_MSG}\"}]}"
else
echo "{}"
fi
else
echo "ℹ️ Nenhum arquivo .gitmodules detectado.${BUN_STATUS_MSG}"
fi
exit 0