feat(kotlin-compose): design system + 33 components + gallery_kt + e2e android emulator + scaffolder fixes

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-11 16:28:50 +02:00
parent 0bdb8454e1
commit cb6d9e61d1
152 changed files with 148262 additions and 25 deletions
+390
View File
@@ -0,0 +1,390 @@
---
name: fn-orquestador
description: "Meta-orquestador (Fase 6) del ciclo reactivo. Toma un issue o task_spec y recorre CONSTRUIR → EJECUTAR → RECOPILAR → ANALIZAR → MEJORAR despachando a fn-constructor/executor/recopilador/analizador/mejorador hasta convergencia, estancamiento, timeout o tope de iteraciones. Trabaja SIEMPRE en rama sandbox `auto/<issue>`, NUNCA mergea a master, persiste progreso en `task_runs`. Issue 0069."
model: sonnet
tools: Read, Write, Bash, Glob, Grep, Edit
---
# Agente Orquestador — Fase 6 (meta) del Ciclo Reactivo
Cierras la promesa autonoma del registry: "lanzar tarea, irse, volver con resultado". Tu rol es **recorrer las 5 fases del bucle reactivo solo**, despachando a los subagentes especializados, hasta que la tarea converja o se decida parar.
NO escribes codigo de aplicacion directamente. NO mergeas a master. NO bypaseas hooks. Solo orquestas.
Referencia completa: `dev/issues/0069-autonomous-agent-loop-self-iterating-tasks.md`.
---
## REGLAS FUNDAMENTALES (no negociables)
1. **Sandbox de rama EN WORKTREE**. Trabajas SIEMPRE en `auto/<issue_id>` dentro de un `git worktree` aislado (default `/tmp/fn_orq_<issue>_<ts>/`). NUNCA en master ni en el working tree principal del repo. Esto permite N orquestadores paralelos y deja intacto el working tree del humano.
2. **No merge automatico**. Al converger, abres PR draft. Humano aprueba.
3. **No `--no-verify`, no `git push --force`, no skip de hooks**. Nunca.
4. **Paths protegidos**. NO tocar:
- `.claude/` (excepto el subdir del task si aplica explicitamente)
- `dev/issues/` (excepto el issue del task)
- Cualquier archivo `.env*`, `*.key`, `*.pem`, credenciales
- `migrations/` ya existentes (solo crear nuevas, nunca editar)
- Lista canonica: `dev/autonomous_protected_paths.json` (si no existe, usar la default de arriba)
5. **Watchdog de progreso**. 2 iteraciones consecutivas con el MISMO set de fails → parar con `status=stalled`.
6. **Auditoria total**. Cada decision se loggea en `task_runs.progress_json` con razonamiento + fase + run_id.
7. **No self-modify**. NO modificas tu propio SKILL.md ni el de otros subagentes en la misma run.
8. **Cero produccion**. NO deploys, NO llamadas a APIs externas con auth, NO tocar BDs productivas.
---
## Pre-condiciones obligatorias
Antes de arrancar el bucle, comprobar:
```bash
# 1. Migration 006_task_runs.sql existe
ls /home/lucas/fn_registry/fn_operations/migrations/006_task_runs.sql 2>/dev/null \
|| { echo "ABORT: migration 006_task_runs.sql ausente. Aplicar issue 0069 paso 1 antes."; exit 2; }
# 2. Subagentes fn-* presentes
for a in fn-constructor fn-executor fn-recopilador fn-analizador fn-mejorador; do
test -f /home/lucas/fn_registry/.claude/agents/$a/SKILL.md \
|| { echo "ABORT: subagente $a ausente"; exit 2; }
done
# 3. master local up-to-date con origin (worktree se creara desde master)
git -C /home/lucas/fn_registry fetch origin master --quiet
LOCAL=$(git -C /home/lucas/fn_registry rev-parse master)
REMOTE=$(git -C /home/lucas/fn_registry rev-parse origin/master)
test "$LOCAL" = "$REMOTE" \
|| { echo "ABORT: master local desincronizado con origin. git pull antes."; exit 2; }
# 4. Branch auto/<issue> NO existe ya (ni local ni en worktrees)
git -C /home/lucas/fn_registry rev-parse --verify "auto/${ISSUE_ID}" >/dev/null 2>&1 \
&& { echo "ABORT: branch auto/${ISSUE_ID} ya existe. Limpiar antes (git branch -D + worktree remove)."; exit 2; }
# 5. gh CLI autenticado (necesario para PR draft al converger)
gh auth status >/dev/null 2>&1 \
|| { echo "ABORT: gh no autenticado, no podra crear PR draft."; exit 2; }
```
**No se exige working tree principal limpio**: el orquestador trabaja en worktree separado.
Si alguna falla → reportar al main thread y salir. NO intentar continuar.
---
## Input
Recibes:
- `issue_id` (ej. `0070`) o `task_spec` inline (objetivo, criterios aceptacion).
- Opcional: `max_iterations` (default 10), `max_minutes` (default 60), `auto_apply_proposals` (`none|safe|aggressive`, default `safe`), `branch` (default `auto/<issue_id>`), `dry_run` (default false).
Task spec mininmo (cuando no hay issue_id):
```yaml
task_id: "<slug>"
type: "feature_app_simple|bugfix_with_repro|refactor_safe|add_e2e_check"
target_app: "<app_id>"
acceptance:
- check: "<verificable programaticamente>"
- check: "..."
```
**Tipos soportados** (issue 0069 §"Tipos de tareas soportadas"):
- `feature_app_simple` — endpoint nuevo + handler + test
- `bugfix_with_repro` — repro reproducible que pasa de fail a pass
- `refactor_safe` — rename/extract con suite igual de verde
- `add_e2e_check` — añadir `e2e_checks` a app sin contrato (delega a `fn-recopilador design-e2e`)
**NO soportados**: diseño arquitectura, decisiones UX, cambios BD productiva, secrets.
---
## Algoritmo
### 0. Setup — worktree aislado
```bash
ISSUE_ID="<input>"
BRANCH="auto/${ISSUE_ID}"
TASK_RUN_ID="task_$(openssl rand -hex 8)"
STARTED_AT=$(date +%s)
WT_ROOT="/tmp/fn_orq_${ISSUE_ID}_${STARTED_AT}"
REPO="/home/lucas/fn_registry"
# Crear worktree aislado desde master (no toca el principal)
git -C "$REPO" worktree add -b "$BRANCH" "$WT_ROOT" master \
|| { echo "ABORT: worktree add fallo"; exit 2; }
# A partir de aqui TODO se hace en $WT_ROOT (cd o git -C)
cd "$WT_ROOT"
# operations.db del app target. Si task no tiene app target, usar el del repo principal:
APP_DB="$WT_ROOT/<app_dir>/operations.db"
[ -f "$APP_DB" ] || APP_DB="$REPO/operations.db"
# Persistir task_run inicial (la BD VIVE EN EL REPO PRINCIPAL para que el humano pueda
# consultarla mientras la run corre — el worktree es desechable)
sqlite3 "$APP_DB" "INSERT INTO task_runs (id, task_id, started_at, status, iterations, last_phase, progress_json)
VALUES ('$TASK_RUN_ID', '$ISSUE_ID', $STARTED_AT, 'running', 0, NULL, '[]');"
```
**Convencion clave**: worktree es **desechable** (codigo, build artifacts), `task_runs` vive en BD persistente del repo principal (auditoria sobrevive aunque borres worktree).
### 1. Loop principal
```
iter = 0
phase = CONSTRUIR
last_fails = null
while iter < max_iterations and elapsed < max_minutes:
iter++
# 1.1 Determinar siguiente fase pendiente
phase = next_phase(task_state, last_phase)
# 1.2 Despachar subagente
output = invoke(phase, prompt_from(task_spec, last_outputs))
# 1.3 Persistir progreso
append_progress(task_run, {iter, phase, output_summary, run_id?})
# 1.4 Logica por fase
if phase == ANALIZAR:
if output.status == "pass":
if all_acceptance_met(task_spec):
converge()
break
else:
phase = CONSTRUIR # siguiente criterio
else: # fail
current_fails = extract_fails(output)
if current_fails == last_fails:
stall()
break
last_fails = current_fails
phase = MEJORAR
if phase == MEJORAR:
proposals = output.proposals
applied = filter_and_apply(proposals, auto_apply_level)
log_applied(applied)
phase = CONSTRUIR # re-validar tras patches
# 1.5 Watchdog needs_human
if requires_human_decision(output):
needs_human()
break
```
### 2. Despacho a subagentes
Usar `Agent` tool con `subagent_type` correcto. Prompt **autocontenido** (paths absolutos, IDs, criterio exito).
**CRITICO**: pasar `WT_ROOT` (worktree path) en cada prompt y exigir al subagente trabajar dentro de el. Subagentes NO deben tocar el repo principal `/home/lucas/fn_registry/`.
Patron prompt:
```
Working dir: <WT_ROOT> # NO /home/lucas/fn_registry
Branch: auto/<issue_id>
Repo principal (solo lectura para registry.db): /home/lucas/fn_registry
...
```
| Fase | subagent_type | Prompt minimo |
|---|---|---|
| CONSTRUIR | `fn-constructor` | "Construir <funcion/tipo> en <lang>/<domain>. Firma: <X>. Pureza: <pure/impure>. Tests obligatorios. Issue: <id>." |
| EJECUTAR | `fn-executor` | "Ejecutar <pipeline_id> con args <X> en <app_dir>. Registrar en operations.db." |
| RECOPILAR | `fn-recopilador` | "Auditar operations.db de <app_dir>. Reportar drift en JSON." |
| ANALIZAR | `fn-analizador` | "Validar <app_id>. Correr e2e_checks. Devolver run_id + status pass/fail + summary." |
| MEJORAR | `fn-mejorador` | "Procesar fallos de run_id=<X> en <app_id>. Crear proposals. Output --json." |
### 3. Filtro de proposals auto-aplicables
`auto_apply_level=safe` (default) acepta proposal SOLO si:
- `created_by = 'reactive_loop'` (vino de fn-mejorador)
- `evidence.run_id` apunta a run real existente
- `kind = 'improve_function'`
- Diff propuesto < 50 lineas (estimar via patch en `evidence.suggested_diff` si existe; si no existe, NO auto-apply)
- NO toca tests existentes (no se "arreglan" tests para que pasen)
- NO añade dependencias nuevas (`go get`, `pnpm add`, `uv add`)
- NO toca paths protegidos
`auto_apply_level=none` → solo crea proposals, nunca aplica.
`auto_apply_level=aggressive` → todas salvo `risk=high` o paths protegidos.
Aplicacion: delegar a `fn-constructor` con prompt "Aplicar proposal <id>. Diff sugerido: <X>. Verificar build despues."
### 4. Convergencia
Condiciones de parada:
| Condicion | status final |
|---|---|
| Todos `acceptance` ✓ + e2e pass + `fn doctor` pass | `converged` |
| Mismo set de fails 2 iter consecutivas | `stalled` |
| `elapsed >= max_minutes` | `timeout` |
| `iter >= max_iterations` | `iterations_exhausted` |
| Output detecta decision humana (libreria nueva, schema breaking) | `needs_human` |
| Pre-condicion fallo / git error / paths protegidos vulnerados | `aborted` |
### 5. PR draft (solo si `converged`)
```bash
git -C "$WT_ROOT" push -u origin "$BRANCH"
gh -R <owner>/<repo> pr create --draft \
--title "auto: <issue_title>" \
--body "<resumen + run_ids + proposals + task_run_id>" \
--base master --head "$BRANCH"
```
NO mergear. Devolver URL al main thread.
### 5.b Cleanup del worktree
Solo borrar worktree si:
- `status=converged` Y PR creado correctamente, O
- `status=aborted|stalled|timeout|iterations_exhausted` Y el humano NO pidio inspeccion.
```bash
# Default: NO borrar. Reportar comando para que humano decida.
echo "Worktree disponible en $WT_ROOT para inspeccion."
echo "Cuando termines: git -C $REPO worktree remove $WT_ROOT && git -C $REPO branch -D $BRANCH"
```
**Regla**: orquestador NUNCA borra worktree automaticamente si hubo fallo. Worktree = evidencia forense. Solo auto-cleanup en `converged` con PR creado.
```bash
# Auto-cleanup post-converge:
if [ "$STATUS" = "converged" ] && [ -n "$PR_URL" ]; then
git -C "$REPO" worktree remove "$WT_ROOT"
# branch sigue en remoto via PR; local se borrara cuando humano cierre PR
fi
```
### 6. Reportar
Output caveman canonico:
```
=== fn-orquestador: <issue_id> ===
status: converged|stalled|timeout|iterations_exhausted|needs_human|aborted
iterations: N / <max>
duration: M min / <max>
branch: auto/<issue_id>
PR draft: <url o "no creado">
proposals: <created> creadas, <applied> auto-aplicadas
last run_id: <run_id> (status: pass|fail)
Iteraciones:
1. construir → ok (3 funciones nuevas: id_a, id_b, id_c)
2. ejecutar → ok (run_id=exec_xxx)
3. analizar → fail (3/8 checks: build, smoke, tests)
4. mejorar → 3 proposals (2 safe-applied, 1 needs human)
5. construir → ok (re-build tras patches)
6. analizar → pass (8/8)
7. recopilar → ok (operations.db integra)
8. CONVERGED
Siguientes pasos humano:
- Revisar PR <url>
- fn proposal list -s pending --target-id <id>
- Si no aceptas, git branch -D auto/<issue_id>
```
---
## Persistencia: tabla `task_runs`
Schema (de issue 0069 §"Nueva tabla task_runs"):
```sql
CREATE TABLE task_runs (
id TEXT PRIMARY KEY,
task_id TEXT NOT NULL,
started_at INTEGER NOT NULL,
finished_at INTEGER,
status TEXT NOT NULL, -- running|converged|stalled|timeout|iterations_exhausted|needs_human|aborted
iterations INTEGER NOT NULL DEFAULT 0,
last_phase TEXT,
last_run_id TEXT,
progress_json TEXT NOT NULL DEFAULT '[]'
);
```
Vive en `operations.db` del app target (NO en registry.db). Si el task no tiene app target (refactor cross-cutting), usar `<repo_root>/operations.db` (excepcion documentada).
Cada `progress_json` entry:
```json
{"iter": N, "phase": "construir", "ts": <epoch>, "subagent": "fn-constructor",
"input_summary": "...", "output_summary": "...", "run_id": "..." }
```
---
## Reglas de comportamiento
1. **Briefing autocontenido** a cada subagente. Nunca asumir contexto compartido.
2. **Verificar output**: leer diff/run_id real, no fiarse del resumen del subagente.
3. **No paralelo dentro de una iteracion** (las fases son secuenciales). PARALELO OK entre tareas distintas: cada `fn-orquestador` corre en SU worktree `/tmp/fn_orq_<issue>_<ts>/`, sin pisarse. N orquestadores simultaneos = N worktrees + N branches `auto/<X>`, `auto/<Y>`.
4. **Caveman en stdout** del orquestador. Telemetry estructurada en `task_runs`.
5. **Stop > recovery**. Ante duda, abortar con `status=needs_human`, NO improvisar fixes.
6. **No tocar `.git` directamente** salvo `checkout`, `add`, `commit`, `push`. Nada de `reset --hard`, `rebase -i`, `branch -D`.
7. **Commits atomicos** por fase: `chore(auto): <fase> iter N — <descripcion corta>`. Co-authored por agente que ejecuto.
---
## Errores comunes
| Sintoma | Causa | Accion |
|---|---|---|
| `task_runs` no existe | migration 006 no aplicada | abortar pre-condicion 1 |
| `worktree add` falla con "already exists" | branch o dir previo no limpiado | `git worktree prune` + `git branch -D auto/<id>`, reintentar |
| Subagente toca `/home/lucas/fn_registry/` en vez de worktree | prompt sin `WT_ROOT` explicito | rebriefing con working dir explicito |
| `master` desincronizado con origin | falta `git pull` | abortar pre-condicion 3 |
| Loop infinito (mismo fail siempre) | watchdog ausente o desactivado | watchdog OBLIGATORIO, no skipear |
| Subagente devuelve output ambiguo | prompt insuficiente | rebriefing con paths/IDs explicitos |
| PR draft falla creacion | `gh` no autenticado o branch sin push | reportar `needs_human`, NO retry agresivo |
| Disk full / sqlite locked | concurrencia con otra task | abortar, NO forzar |
---
## Composicion con otras fases
- **Pre-orquestador**: humano define `dev/issues/<NNNN>.md` con criterios verificables programaticamente. Sin issue verificable, NO arrancar.
- **Durante**: orquestador despacha a las 5 fases. Cada subagente respeta SUS reglas (purity, registry-first, etc.).
- **Post-orquestador**: humano revisa PR draft + proposals. Acepta, modifica o descarta.
- **NO orquestes a otro `fn-orquestador`**. Una run no spawn-ea otra. Recursion = abort.
---
## Salida JSON opcional
Si `--json`:
```json
{
"task_run_id": "task_a1b2c3d4",
"issue_id": "0070",
"status": "converged",
"iterations": 8,
"duration_s": 1240,
"branch": "auto/0070",
"pr_url": "https://gitea.../pulls/42",
"proposals_created": 3,
"proposals_applied": 2,
"last_run_id": "run_xxx",
"phases": [
{"iter": 1, "phase": "construir", "status": "ok", "ts": 1234},
...
]
}
```
Util para integraciones (CI, dashboard, otra automatizacion). NO para spawn-ear otro orquestador.
---
## Limites duros
- `max_iterations`: 10 default, ceiling 30.
- `max_minutes`: 60 default, ceiling 240.
- Diff total por iteracion: 500 lineas. Si excede → `needs_human`.
- Proposals auto-aplicadas por run: 5. Si excede → resto a `pending`.
- Recursividad: 0. NO spawn de otro orquestador.
+53
View File
@@ -0,0 +1,53 @@
# /new-cpp-app — Crear app C++ nueva con scaffolder estandar
Wrapper sobre el pipeline `init_cpp_app_bash_pipelines`. Genera la estructura canonica que cumple `cpp/PATTERNS.md` y `.claude/rules/cpp_apps.md` (main.cpp con `cfg.about/log/panels`, sin `app_menubar` manual, dockspace via framework), registra la app en `cpp/CMakeLists.txt`, crea repo Gitea `dataforge/<name>` y ejecuta `fn index`.
```bash
cd /home/lucas/fn_registry
./fn run init_cpp_app $ARGUMENTS
```
## Uso
```
/new-cpp-app <name> [--project <p>] [--domain <d>] [--desc "..."] [--tags "a,b"]
```
## Ejemplos
```bash
# App suelta en cpp/apps/<name>/
/new-cpp-app my_tool --desc "Herramienta para X"
# App dentro de un proyecto
/new-cpp-app finance_panel --project budget --desc "Panel de finanzas" --tags "finance,dashboard"
```
## Que genera
```
<dir>/
main.cpp # Plantilla canonica: panels[] + cfg.about + cfg.log + run_app(cfg, render)
CMakeLists.txt # add_imgui_app(<name> main.cpp)
app.md # Frontmatter completo (lang:cpp, framework:imgui, dir_path, repo_url)
```
Mas registro en `cpp/CMakeLists.txt`, repo Gitea con commit inicial, y `fn index` para que aparezca en `registry.db`.
## Despues de crear
1. Editar `app.md` y completar `uses_functions` cuando la app consuma funciones del registry.
2. Anadir las funciones al `CMakeLists.txt` como paths absolutos: `${CMAKE_SOURCE_DIR}/functions/<dom>/<func>.cpp`.
3. Build: `/compile <name>` o `cd cpp && cmake --build build --target <name> -j`.
## Cuando NO usar
NUNCA — esta es la unica via para crear apps C++ nuevas. Si el scaffolder no cubre un caso, modificar la plantilla en `bash/functions/pipelines/init_cpp_app.sh`. Escribir `main.cpp + CMakeLists.txt + app.md` a mano esta prohibido por `.claude/rules/cpp_apps.md`.
## Auditoria post-creacion
```
fn doctor cpp-apps
```
Lista apps que se desvian del estandar (sin `cfg.about`, con `app_menubar` manual, dockspace duplicado, etc.).
+80
View File
@@ -0,0 +1,80 @@
---
description: "Recordatorio operativo para usar subagentes fn (constructor/executor/recopilador/analizador/mejorador) y paralelizar trabajo independiente"
---
# /subagentes — usa subagentes fn y paraleliza
Recuerda: antes de escribir codigo nuevo o ejecutar pipelines en serie, **delega a subagentes** y **paraleliza** llamadas independientes (un mensaje, varios `Agent` calls).
---
## Mapa de subagentes fn (ciclo reactivo)
| Fase | Agente | Cuando dispararlo |
|---|---|---|
| 1 CONSTRUIR | `fn-constructor` | Falta funcion/tipo/test reutilizable. NUNCA escribir inline en `apps/` si es reutilizable |
| 2 EJECUTAR | `fn-executor` | Correr pipeline/funcion del registry + registrar ejecucion en `operations.db` |
| 3 RECOPILAR | `fn-recopilador` | Auditar integridad de `operations.db`. Modo `design-e2e <app>` propone bloque `e2e_checks` |
| 4 ANALIZAR | `fn-analizador` | Ejecutar `e2e_checks` de `app.md`, veredicto pass/fail, persistir en `e2e_runs` |
| 5 MEJORAR | `fn-mejorador` | Convertir fallos de `e2e_runs` en `proposals` con evidencia trazable |
| 6 META | `fn-orquestador` | Recorrer fases 1-5 solo hasta convergencia. Sandbox `auto/<issue>`. Issue 0069 |
**Pre-condiciones de `fn-orquestador`** (abortara si no se cumplen):
- Migration `fn_operations/migrations/006_task_runs.sql` aplicada
- Issue con criterios de aceptacion **verificables programaticamente** (no "funciona bien")
- `master` local up-to-date con `origin/master`
- Branch `auto/<issue>` NO existe ya (limpiar previo si hace falta)
- `gh` autenticado (PR draft al converger)
- Tipo soportado: `feature_app_simple`, `bugfix_with_repro`, `refactor_safe`, `add_e2e_check`
**Aislamiento por worktree**: cada run crea `/tmp/fn_orq_<issue>_<ts>/` via `git worktree add`. Working tree principal del usuario queda intacto. N orquestadores paralelos = N worktrees independientes. `task_runs` persiste en BD del repo principal (auditoria sobrevive aunque borres worktree).
## Otros subagentes utiles
- `Explore` — busquedas amplias en codebase (>3 queries) sin contaminar contexto principal
- `general-purpose` — research multi-step open-ended
## Reglas duras
1. **Paralelo real**: tareas independientes → un mensaje con varios `Agent` calls. NO en serie.
2. **Briefing autocontenido**: subagente no ve historial. Pasar paths absolutos, IDs, criterio exito.
3. **No delegar comprension**: nada de "haz lo que veas". Especificar que cambiar, donde, por que.
4. **Verificar output**: leer diff/resultado, no confiar en resumen del subagente.
5. **No duplicar**: si delegas research, no lo repitas tu.
## Patrones canonicos de paralelismo
- 3 funciones de registry independientes → 3 `fn-constructor` en paralelo
- Auditar N apps → N `fn-recopilador` en paralelo
- Validar varias apps → N `fn-analizador` en paralelo
- Build cpp + tests py + audit operations.db → 3 calls paralelos
- Tras `fn-analizador` con fallos → `fn-mejorador` por cada `run_id`
- Tarea multi-fase autonoma (issue con criterios verificables) → `fn-orquestador` (1 sola run, NO recursivo)
## Anti-patrones
- Escribir funcion reutilizable inline en `apps/` (debe ir a `functions/` via `fn-constructor`)
- Lanzar subagentes en serie cuando son independientes
- Prompt de 1 linea sin contexto ("arregla esto")
- Invocar subagente y luego hacer tu mismo el trabajo
- Spawn `fn-orquestador` sin migration 006 o sin issue verificable (abortara)
- `fn-orquestador` recursivo (un orquestador no spawn-ea otro)
## Checklist pre-respuesta
- ¿>1 tarea independiente? → paralelizar
- ¿Hace falta funcion/tipo nuevo? → `fn-constructor`, NO inline
- ¿Hay que ejecutar/auditar/validar? → fase 2/3/4 segun toque
- ¿`e2e_runs` con fallos? → `fn-mejorador`
- ¿Issue con criterios verificables + tipo soportado? → `fn-orquestador` (chequear pre-condiciones)
- ¿Research amplio (>3 queries)? → `Explore`
## Plantilla minima de prompt para subagente
```
Contexto: <que repo, que app, que objetivo>
Input: <paths absolutos, IDs registry, run_id si aplica>
Tarea: <accion concreta y acotada>
Criterio exito: <como sabe que termino>
Limites: <que NO debe tocar>
```
+14
View File
@@ -6,6 +6,20 @@
Esta regla NO duplica esos documentos — los señala como obligatorios y añade convenciones estructurales que no aparecen alli.
### Scaffolder canonico — OBLIGATORIO
**REGLA DURA:** crear apps C++ nuevas SIEMPRE con `fn run init_cpp_app <name> [--project <p>] [--desc "..."]`. NUNCA escribir `main.cpp` + `CMakeLists.txt` + `app.md` desde cero a mano en `cpp/apps/` ni `projects/*/apps/`. Tampoco copiar otra app y renombrar — la deriva entre patrones es lo que estamos eliminando.
Si el scaffolder no cubre un caso (ej. necesitas plantilla diferente, layout custom desde el primer dia), **modificas el scaffolder**, no escribes la app a mano. La plantilla canonica es codigo, no decoracion.
Razones:
- Garantiza `cfg.about` + `cfg.log` + `cfg.panels` + framework defaults aplicados.
- Genera frontmatter `app.md` valido (framework, dir_path, repo_url) para `fn index`.
- Registra `add_subdirectory` en `cpp/CMakeLists.txt` (raiz o bloque `_DIR` para projects).
- Crea repo Gitea `dataforge/<name>` con master + commit inicial.
Pipeline: `init_cpp_app_bash_pipelines`. Slash command equivalente: `/new-cpp-app`. Auditoria: `fn doctor cpp-apps`.
### 1. Ubicacion
| Caso | Donde vive |
+7 -1
View File
@@ -13,12 +13,13 @@
### Comandos
```bash
fn doctor # Corre TODOS los checks (artefacts + services + sync + uses-functions + unused)
fn doctor # Corre TODOS los checks (artefacts + services + sync + uses-functions + unused + cpp-apps)
fn doctor artefacts # Solo artefactos: git/venv/app.md/upstream
fn doctor services # Solo apps con tag 'service' + systemctl + puerto
fn doctor sync # Solo drift pc_locations BD vs disco local
fn doctor uses-functions # Solo audit imports reales vs uses_functions
fn doctor unused # Solo funciones huerfanas del registry
fn doctor cpp-apps # Conformidad C++ con cpp/PATTERNS.md (cfg.about/log, no app_menubar manual, no DockSpace duplicado)
fn doctor --json # Salida JSON (cualquier subcomando) — para agentes/scripts
```
@@ -32,6 +33,7 @@ fn doctor --json # Salida JSON (cualquier subcomando) — para agentes
| `sync` | `pc_locations_drift_go_infra` |
| `uses-functions` | `audit_uses_functions_go_infra` |
| `unused` | `find_unused_functions_go_infra` |
| `cpp-apps` | `audit_cpp_apps_go_infra` |
Cada subcomando es un wrapper fino. Toda la logica vive en la funcion. Si quieres usar la salida en otro programa Go, importa la funcion directamente.
@@ -58,6 +60,10 @@ Texto humano por defecto (tabwriter). `--json` produce array/objeto serializable
| `port not listening` | `port_kill_bash_infra <port>` (si zombie) y relanzar |
| `missing_in_app_md` | Editar `app.md` y añadir el ID a `uses_functions` |
| `unused` (funcion huerfana) | Decidir: usar, deprecar (tag), o borrar |
| `manual_app_menubar_call` | Borrar `fn_ui::app_menubar(...)` del render — el framework ya lo dibuja |
| `manual_DockSpaceOverViewport_*` | Borrar la llamada o setear `cfg.auto_dockspace = false` si la app gestiona docking propio |
| `missing_cfg_about` / `missing_cfg_log` | Anadir `cfg.about = {...}` / `cfg.log = {"<name>.log", 1}` antes de `fn::run_app` |
| `app.md_missing_*` | Regenerar via plantilla del scaffolder (`/new-cpp-app`) o anadir campos a mano |
| Backup viejo | `backup_all_bash_pipelines ~/backups/fn_registry` |
### Para agentes
+1
View File
@@ -80,3 +80,4 @@ Thumbs.db
broken_paths.txt
imgui.ini
prompts/
kotlin/functions/ui/
+98
View File
@@ -0,0 +1,98 @@
---
name: adb_wsl
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "source adb_wsl.sh [ADB=<path>] [ANDROID_SDK_WIN=<sdk_root>]"
description: "Wrapper sourceable para usar adb.exe Windows desde WSL2. Resuelve binario, convierte paths, espera boot del emulador."
tags: ["android", "adb", "wsl", "windows"]
params:
- name: ADB
desc: "Env var opcional. Path absoluto a adb.exe. Si no se fija, se construye desde ANDROID_SDK_WIN o el default /mnt/c/Users/lucas/AppData/Local/Android/Sdk."
- name: ANDROID_SDK_WIN
desc: "Env var opcional. Raiz del Android SDK montado en WSL. Default: /mnt/c/Users/lucas/AppData/Local/Android/Sdk."
output: "Source-able shell helpers: adb_run, adb_devices, adb_wsl_to_win, adb_wait_boot. Define ADB env var apuntando a Windows adb.exe via ANDROID_SDK_WIN."
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/adb_wsl.sh"
---
## Uso
```bash
# Sourcear (usa SDK default)
source bash/functions/infra/adb_wsl.sh
# Sourcear con SDK custom
ANDROID_SDK_WIN=/mnt/d/Android/Sdk source bash/functions/infra/adb_wsl.sh
# Sourcear con binario fijo
ADB=/mnt/c/my/tools/adb.exe source bash/functions/infra/adb_wsl.sh
```
## Funciones expuestas
### `adb_run "<args...>"`
Ejecuta `$ADB` con los argumentos dados. Retorna el exit code de `adb.exe`.
```bash
adb_run shell ls /sdcard/
adb_run install app.apk
```
### `adb_devices`
Alias de `adb_run devices`. Lista dispositivos/emuladores conectados.
```bash
adb_devices
# List of devices attached
# emulator-5554 device
```
### `adb_wsl_to_win <path_wsl>`
Convierte un path WSL a formato Windows con `wslpath -w`. Si `wslpath` no está disponible retorna el path sin convertir.
```bash
win_path=$(adb_wsl_to_win /home/lucas/proyecto/app.apk)
# C:\Users\lucas\AppData\Local\... (o la ruta Windows equivalente)
adb_run install "$win_path"
```
### `adb_wait_boot [timeout_s]`
Espera a que el emulador/dispositivo complete el boot (`sys.boot_completed = 1`). Útil tras lanzar un AVD en CI.
```bash
adb_wait_boot # timeout 120s
adb_wait_boot 60 # timeout 60s
```
Retorna `0` si el boot se completó, `1` si expiró el timeout.
## Smoke test
```bash
bash bash/functions/infra/adb_wsl.sh --self-test
# OK
```
## Notas
- El script es **source-able**: define funciones en el shell actual, no crea subshell.
- `ADB` se resuelve una sola vez al sourcing. Si el binario no existe en disco, la carga falla con mensaje en stderr y `return 1` / `exit 1`.
- `adb_wait_boot` hace polling cada 3 segundos. Ajustar `interval` si el emulador es especialmente lento.
- En WSL2 `wslpath` siempre está disponible; el fallback existe para entornos Linux puros que accidentalmente sourceen el archivo.
- Si el emulador requiere `-s <serial>`, pasar el flag directamente a `adb_run`: `adb_run -s emulator-5554 shell ...`.
---
+130
View File
@@ -0,0 +1,130 @@
#!/usr/bin/env bash
# adb_wsl — Wrapper sourceable para usar adb.exe Windows desde WSL2.
# Uso: source bash/functions/infra/adb_wsl.sh
# Smoke test: bash bash/functions/infra/adb_wsl.sh --self-test
# ---------------------------------------------------------------------------
# Resolver ADB
# ---------------------------------------------------------------------------
# El caller puede fijar ADB antes de sourcing para apuntar a otro binario.
if [[ -z "${ADB:-}" ]]; then
_sdk_root="${ANDROID_SDK_WIN:-/mnt/c/Users/lucas/AppData/Local/Android/Sdk}"
ADB="${_sdk_root}/platform-tools/adb.exe"
unset _sdk_root
fi
if [[ ! -f "$ADB" ]]; then
echo "adb_wsl: ADB no encontrado en '$ADB'. Fija ADB= o ANDROID_SDK_WIN= antes de sourcear." >&2
# Solo abortamos si el script se ejecuta directamente; si se sourcea,
# permitimos continuar para que el caller maneje el error.
return 1 2>/dev/null || exit 1
fi
# ---------------------------------------------------------------------------
# adb_run "<args...>"
# Ejecuta el ADB Windows con los argumentos dados.
# Retorna el exit code de adb.exe.
# ---------------------------------------------------------------------------
adb_run() {
"$ADB" "$@"
}
# ---------------------------------------------------------------------------
# adb_devices
# Lista dispositivos ADB conectados.
# ---------------------------------------------------------------------------
adb_devices() {
adb_run devices
}
# ---------------------------------------------------------------------------
# adb_wsl_to_win <path_wsl>
# Convierte un path WSL a formato Windows usando wslpath.
# Si wslpath no está disponible retorna el path tal cual.
# ---------------------------------------------------------------------------
adb_wsl_to_win() {
local path_wsl="$1"
if command -v wslpath &>/dev/null; then
wslpath -w "$path_wsl"
else
echo "$path_wsl"
fi
}
# ---------------------------------------------------------------------------
# adb_wait_boot [timeout_s]
# Espera a que el dispositivo/emulador complete el boot (sys.boot_completed=1).
# timeout_s: segundos máximos de espera (default 120).
# Retorna 0 si boot completado, 1 si timeout.
# ---------------------------------------------------------------------------
adb_wait_boot() {
local timeout_s="${1:-120}"
local elapsed=0
local interval=3
while (( elapsed < timeout_s )); do
local val
val=$(adb_run shell getprop sys.boot_completed 2>/dev/null | tr -d '[:space:]')
if [[ "$val" == "1" ]]; then
return 0
fi
sleep "$interval"
(( elapsed += interval ))
done
echo "adb_wsl: timeout ${timeout_s}s esperando boot del dispositivo." >&2
return 1
}
# ---------------------------------------------------------------------------
# adb_pick_serial [--serial <S>] [...]
# Resuelve el serial a usar para multi-device. Lee --serial X de los args.
# Setea globals ADB_PICK_SERIAL y ADB_PICK_REST (no usa stdout para evitar
# perder los globals via subshell de $()).
# Exit 1 si no hay device disponible.
#
# Uso tipico:
# adb_pick_serial "$@" || { echo "no device" >&2; exit 3; }
# local serial="$ADB_PICK_SERIAL"
# set -- "${ADB_PICK_REST[@]}"
# ---------------------------------------------------------------------------
adb_pick_serial() {
ADB_PICK_SERIAL="${ADB_SERIAL:-}"
ADB_PICK_REST=()
while [[ $# -gt 0 ]]; do
case "$1" in
--serial) ADB_PICK_SERIAL="$2"; shift 2 ;;
--serial=*) ADB_PICK_SERIAL="${1#--serial=}"; shift ;;
*) ADB_PICK_REST+=("$1"); shift ;;
esac
done
if [[ -z "$ADB_PICK_SERIAL" ]]; then
ADB_PICK_SERIAL=$(adb_run devices 2>/dev/null | awk '/(emulator-|device$)/ && !/List of/ {print $1; exit}')
fi
if [[ -z "$ADB_PICK_SERIAL" ]]; then
echo "adb_wsl: ningun device/emulador conectado." >&2
return 1
fi
if ! adb_run devices 2>/dev/null | awk '{print $1}' | grep -qx "$ADB_PICK_SERIAL"; then
echo "adb_wsl: serial '$ADB_PICK_SERIAL' no encontrado en adb devices." >&2
return 1
fi
return 0
}
# ---------------------------------------------------------------------------
# adb_s <serial> <args...>
# Atajo: adb_run -s <serial> <args...>
# ---------------------------------------------------------------------------
adb_s() {
local serial="$1"; shift
adb_run -s "$serial" "$@"
}
# ---------------------------------------------------------------------------
# Smoke test (solo si invocado directamente con --self-test)
# ---------------------------------------------------------------------------
if [[ "${1:-}" == "--self-test" ]]; then
adb_run version || exit 1
echo "OK"
fi
@@ -0,0 +1,66 @@
---
name: android_apk_install
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_apk_install([--serial S], apk_path: string, package_name?: string, activity_name?: string) -> void"
description: "Instala APK en device/emulador via adb y opcionalmente lanza la app. Multi-emulator via --serial."
tags: [android, adb, apk, wsl]
params:
- name: "--serial <S>"
desc: "Optional target device/emulator serial. Default: first device detected by adb_pick_serial."
- name: apk_path
desc: "WSL path to APK file"
- name: package_name
desc: "Optional app package id (e.g. com.fnregistry.voiceguide). Launches the app if provided."
- name: activity_name
desc: "Optional activity (.MainActivity or fully qualified). Only used with package_name. If omitted, launches via monkey LAUNCHER intent."
output: "Stdout con pasos. Exit 0 = install + launch OK. Exit !=0 si install fallo o APK no encontrado."
uses_functions: ["adb_wsl_bash_infra"]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_apk_install.sh"
---
## Ejemplo
```bash
# Solo instalar
android_apk_install /home/lucas/builds/app-debug.apk
# Instalar y lanzar con activity explícita
android_apk_install /home/lucas/builds/app-debug.apk com.fnregistry.voiceguide .MainActivity
# Instalar y lanzar sin activity (usa monkey LAUNCHER)
android_apk_install /home/lucas/builds/app-debug.apk com.fnregistry.voiceguide
# Llamada directa desde shell (no sourced)
bash bash/functions/infra/android_apk_install.sh /path/to/app.apk com.example.app .MainActivity
# Override ADB path
ADB=/custom/path/adb.exe bash bash/functions/infra/android_apk_install.sh /path/to/app.apk
```
## Notas
- Requiere WSL2 con `adb.exe` Windows accesible. El path por defecto es
`/mnt/c/Users/lucas/AppData/Local/Android/Sdk/platform-tools/adb.exe`.
Se puede sobreescribir con `ADB=...` o `ANDROID_SDK_WIN=<sdk_root>` antes
de invocar.
- `wslpath` se usa para convertir el path WSL a formato Windows (`C:\...`).
Si no está disponible (entorno no-WSL), se usa el path tal cual.
- La instalación usa `adb install -r` (reinstala si ya existe).
- Si `package_name` se da sin `activity_name`, la app se lanza via
`adb shell monkey -p <pkg> -c android.intent.category.LAUNCHER 1`,
que es equivalente a pulsar el icono del launcher.
- El script se puede sourcear (para usar la función en otros scripts) o
ejecutar directamente. Cuando se ejecuta directamente, delega en
`android_apk_install "$@"`.
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
# android_apk_install — Instala APK en device/emulador via adb y opcionalmente lanza la app.
# Multi-emulator via --serial <S>.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Source helpers (adb_run, adb_pick_serial, adb_s, adb_wsl_to_win)
# shellcheck source=/dev/null
source "$SCRIPT_DIR/adb_wsl.sh"
# ---------------------------------------------------------------------------
# android_apk_install [--serial <S>] <apk_path> [package_name] [activity_name]
# ---------------------------------------------------------------------------
android_apk_install() {
local serial
adb_pick_serial "$@" || { echo "android_apk_install: no device/emulator." >&2; return 3; }
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
local apk="${1:-}"
local package="${2:-}"
local activity="${3:-}"
if [[ -z "$apk" ]]; then
echo "android_apk_install: se requiere apk_path como primer argumento." >&2
return 1
fi
if [[ ! -f "$apk" ]]; then
echo "android_apk_install: APK no encontrado en '$apk'." >&2
return 1
fi
local win_path
win_path=$(adb_wsl_to_win "$apk")
echo "android_apk_install: instalando '$win_path' on $serial ..."
adb_s "$serial" install -r "$win_path"
echo "android_apk_install: instalacion completada."
if [[ -n "$package" ]]; then
if [[ -n "$activity" ]]; then
echo "android_apk_install: lanzando $package/$activity ..."
adb_s "$serial" shell am start -n "$package/$activity"
else
echo "android_apk_install: lanzando $package via monkey LAUNCHER ..."
adb_s "$serial" shell monkey -p "$package" -c android.intent.category.LAUNCHER 1
fi
echo "android_apk_install: app lanzada."
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_apk_install "$@"
fi
+52
View File
@@ -0,0 +1,52 @@
---
name: android_app_clear
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_app_clear([--serial <S>], package: string) -> void"
description: "Wipe app data + cache via pm clear. App keeps installed but factory-state. Multi-emulator via --serial."
tags: [android, adb, app, clear, reset]
params:
- name: "--serial <S>"
desc: "Optional target device/emulator serial. Auto-detected if omitted."
- name: package
desc: "App package whose data to clear (e.g. com.example.app)."
output: "Stdout 'cleared data for <pkg> on <serial>'. Exit 0 si pm clear OK."
uses_functions: ["adb_wsl_bash_infra"]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_app_clear.sh"
---
## Ejemplo
```bash
# Limpiar datos de una app (autodetecta device)
android_app_clear com.example.myapp
# Con serial explícito
android_app_clear --serial emulator-5554 com.example.myapp
# Llamada directa
bash bash/functions/infra/android_app_clear.sh com.example.myapp
bash bash/functions/infra/android_app_clear.sh --serial emulator-5554 com.example.myapp
```
## Notas
- Usa `pm clear` internamente — borra SharedPreferences, bases de datos internas,
caché y archivos de la app. La app queda como recién instalada.
- El source de `adb_wsl.sh` resuelve el binario `adb.exe` Windows desde WSL2.
Se puede sobreescribir con `ADB=...` o `ANDROID_SDK_WIN=<sdk_root>` antes de invocar.
- `adb_pick_serial` consume `--serial <S>` de los args y deja el resto en
`ADB_PICK_REST`. Si no se da, autodetecta el primer device/emulador activo.
- Exit 3 si no hay ningún device conectado (propagado desde `adb_pick_serial`).
- Exit 1 si no se pasa package.
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env bash
# android_app_clear — Wipe app data + cache via pm clear. App stays installed.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=./adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"
# ---------------------------------------------------------------------------
# android_app_clear [--serial <S>] <package>
#
# --serial <S> Optional target device/emulator serial.
# package App package whose data+cache to clear (e.g. com.example.app).
#
# Calls: adb shell pm clear <package>
# The app remains installed but is reset to factory state (no data, no cache).
# Exit 0 on success, exit 1 on bad args, exit 3 if no device found.
# ---------------------------------------------------------------------------
android_app_clear() {
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
local pkg="${1:-}"
if [[ -z "$pkg" ]]; then
echo "android_app_clear: se requiere <package> como argumento." >&2
return 1
fi
adb_s "$serial" shell pm clear "$pkg"
echo "cleared data for $pkg on $serial"
}
# Run directly if not sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_app_clear "$@"
fi
+57
View File
@@ -0,0 +1,57 @@
---
name: android_app_info
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_app_info([--serial <S>], package, [--json]) -> stdout"
description: "Inspect installed app: version, target SDK, activities via dumpsys package."
tags: [android, adb, app, info, dumpsys]
params:
- name: "--serial <S>"
desc: "Optional ADB serial to target a specific device/emulator. Auto-detected if omitted."
- name: "package"
desc: "Android package name to inspect (e.g. com.example.myapp)."
- name: "--json"
desc: "Emit parsed JSON with versionName, versionCode, targetSdk, launcherActivity instead of raw dumpsys output."
output: "Raw dumpsys package output, or JSON object {package, versionName, versionCode, targetSdk, launcherActivity}. Outputs JSON null if package not installed (--json mode). Exit 2 if package not found in raw mode, exit 3 if no device."
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_app_info.sh"
---
## Ejemplo
```bash
# Raw dumpsys (full output)
source bash/functions/infra/android_app_info.sh
android_app_info com.example.myapp
# Target specific device
android_app_info --serial emulator-5554 com.example.myapp
# Parsed JSON
android_app_info com.example.myapp --json
# {"package":"com.example.myapp","versionName":"2.1.0","versionCode":210,"targetSdk":34,"launcherActivity":"com.example.myapp/.MainActivity"}
# Package not installed → JSON null
android_app_info com.not.installed --json
# null
```
## Notas
- Sources `adb_wsl.sh` para resolver el binario ADB Windows desde WSL2 y las helpers `adb_pick_serial` / `adb_s`.
- `--serial` se consume via `adb_pick_serial`; el resto de los args quedan en `ADB_PICK_REST` y se re-asignan con `set --`.
- JSON parsing usa `grep`/`sed`/`awk` sobre la salida de `dumpsys package`. Campos faltantes se emiten como string vacío o 0; no se usa `jq` para no requerir dependencias externas.
- `launcherActivity` se extrae buscando el bloque `android.intent.action.MAIN` / `android.intent.category.LAUNCHER` en el listado de intent filters.
- Exit codes: 0 = OK, 1 = arg/adb error, 2 = package not found (raw mode), 3 = no device.
---
+93
View File
@@ -0,0 +1,93 @@
#!/usr/bin/env bash
# android_app_info — Inspect installed app via dumpsys package.
# Usage: android_app_info [--serial <S>] <package> [--json]
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/adb_wsl.sh"
android_app_info() {
# Resolve serial (consumes --serial from args, leaves rest in ADB_PICK_REST)
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
# Parse remaining args: package + --json flag
local pkg=""
local want_json=0
while [[ $# -gt 0 ]]; do
case "$1" in
--json) want_json=1; shift ;;
-*) echo "android_app_info: unknown flag '$1'" >&2; return 1 ;;
*)
if [[ -z "$pkg" ]]; then
pkg="$1"
else
echo "android_app_info: unexpected argument '$1'" >&2
return 1
fi
shift ;;
esac
done
if [[ -z "$pkg" ]]; then
echo "android_app_info: package argument required" >&2
return 1
fi
local dump
dump=$(adb_s "$serial" shell dumpsys package "$pkg" 2>&1)
local rc=$?
if [[ $rc -ne 0 ]]; then
echo "android_app_info: adb dumpsys failed (exit $rc)" >&2
return 1
fi
# If dumpsys returns nothing meaningful for the package, treat as not installed
if ! echo "$dump" | grep -q "Package \["; then
if [[ $want_json -eq 1 ]]; then
echo "null"
else
echo "android_app_info: package '$pkg' not found on device" >&2
return 2
fi
return 0
fi
if [[ $want_json -eq 0 ]]; then
echo "$dump"
return 0
fi
# --- JSON extraction ---
local versionName versionCode targetSdk launcherActivity
versionName=$(echo "$dump" | grep -m1 'versionName=' \
| sed 's/.*versionName=\([^ ]*\).*/\1/')
versionCode=$(echo "$dump" | grep -m1 'versionCode=' \
| sed 's/.*versionCode=\([0-9]*\).*/\1/')
targetSdk=$(echo "$dump" | grep -m1 'targetSdk=' \
| sed 's/.*targetSdk=\([0-9]*\).*/\1/')
# Primary/launcher activity: look for MAIN/LAUNCHER category block
launcherActivity=$(echo "$dump" | awk '
/android.intent.action.MAIN/ { found=1 }
found && /[a-zA-Z0-9_.]+\/[a-zA-Z0-9_.]+/ {
match($0, /[a-zA-Z0-9_.]+\/[a-zA-Z0-9_.]+/)
print substr($0, RSTART, RLENGTH)
exit
}
')
# Emit JSON, quoting strings safely
printf '{"package":"%s","versionName":"%s","versionCode":%s,"targetSdk":%s,"launcherActivity":"%s"}\n' \
"$pkg" \
"${versionName:-}" \
"${versionCode:-0}" \
"${targetSdk:-0}" \
"${launcherActivity:-}"
}
# Run if invoked directly
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
android_app_info "$@"
fi
+44
View File
@@ -0,0 +1,44 @@
---
name: android_app_kill
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_app_kill([--serial <S>], package: string) -> void"
description: "Force-stop running app via am force-stop. Multi-emulator via --serial."
tags: [android, adb, app, kill, force-stop]
params:
- name: "--serial <S>"
desc: "Optional target device/emulator serial. Auto-detected if omitted."
- name: "package"
desc: "App package to force-stop (e.g. com.example.myapp)."
output: "Stdout 'killed <pkg> on <serial>'. Exit 0."
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_app_kill.sh"
---
## Ejemplo
```bash
# Detener app en el emulador activo
android_app_kill com.example.myapp
# Detener app en un dispositivo concreto
android_app_kill --serial emulator-5554 com.example.myapp
```
## Notas
Usa `adb_pick_serial` de `adb_wsl.sh` para resolver el dispositivo objetivo.
Si `--serial` no se pasa, autodetecta el primer device/emulador disponible.
Sale con exit 3 si no hay ningun device conectado.
`am force-stop` detiene todos los procesos y servicios de la app de forma inmediata.
+24
View File
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
# android_app_kill — Force-stop a running Android app via am force-stop.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"
android_app_kill() {
local serial pkg
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
pkg="${1:?android_app_kill: package name required}"
adb_s "$serial" shell am force-stop "$pkg"
echo "killed $pkg on $serial"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_app_kill "$@"
fi
@@ -0,0 +1,49 @@
---
name: android_app_launch
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_app_launch([--serial <S>], package: string, [activity: string]) -> void"
description: "Launch app activity via am start. Multi-emulator via --serial."
tags: [android, adb, app, launch, activity]
params:
- name: "--serial <S>"
desc: "Optional target serial. Default: first device"
- name: "package"
desc: "App package id"
- name: "activity"
desc: "Optional activity. If omitted, launches via LAUNCHER intent"
output: "Stdout 'launched <pkg> on <serial>'. Exit 0 ok, 3 no device."
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_app_launch.sh"
---
## Ejemplo
```bash
# Lanzar actividad principal explicitamente
android_app_launch com.foo.bar .MainActivity
# Lanzar por LAUNCHER intent (detecta actividad principal automaticamente)
android_app_launch com.foo.bar
# Multi-emulador: elegir serial concreto
android_app_launch --serial emulator-5554 com.foo.bar .MainActivity
```
## Notas
Usa `adb_pick_serial` de `adb_wsl.sh` para resolver el serial objetivo.
Si no hay ningun device/emulador disponible, sale con exit code 3.
Si `activity` no se especifica, usa `monkey -p <pkg> -c android.intent.category.LAUNCHER 1`
para lanzar la actividad principal sin necesidad de conocerla de antemano.
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
# android_app_launch — Launch an Android app via adb am start or monkey LAUNCHER intent.
# Usage: android_app_launch [--serial <S>] <package> [<activity>]
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/adb_wsl.sh"
android_app_launch() {
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
local pkg="${1:-}"
local activity="${2:-}"
if [[ -z "$pkg" ]]; then
echo "android_app_launch: package is required." >&2
return 1
fi
if [[ -n "$activity" ]]; then
adb_s "$serial" shell am start -n "$pkg/$activity"
else
adb_s "$serial" shell monkey -p "$pkg" -c android.intent.category.LAUNCHER 1
fi
echo "launched $pkg on $serial"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_app_launch "$@"
fi
@@ -0,0 +1,52 @@
---
name: android_app_uninstall
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_app_uninstall([--serial <S>] package [--keep-data]) -> void"
description: "Uninstall app via adb uninstall. Optionally keep data with --keep-data."
tags: [android, adb, app, uninstall]
params:
- name: "--serial <S>"
desc: "Optional target device/emulator serial. Auto-detects first connected device if omitted."
- name: "package"
desc: "Android package name to uninstall (e.g. com.example.myapp). Mandatory positional argument."
- name: "--keep-data"
desc: "Keep app data + cache after uninstall (passes -k to pm uninstall)."
output: "Stdout 'uninstalled <pkg> on <serial>'. Exit 0 OK."
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_app_uninstall.sh"
---
## Ejemplo
```bash
# Desinstalar en el device por defecto
android_app_uninstall com.example.myapp
# Desinstalar en un device concreto
android_app_uninstall --serial emulator-5554 com.example.myapp
# Desinstalar conservando datos y cache
android_app_uninstall --serial emulator-5554 com.example.myapp --keep-data
```
## Notas
Sourcea `adb_wsl.sh` para resolver el binario `adb.exe` en WSL2 y usar
`adb_pick_serial` / `adb_s`. Si no hay ningún device conectado y no se
pasa `--serial`, la función falla con exit 1 antes de invocar adb.
El flag `--keep-data` pasa `-k` a `adb uninstall`, equivalente a
`pm uninstall -k` — el APK se elimina pero los datos y la caché de la app
permanecen en el dispositivo.
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
# android_app_uninstall — Desinstala una app Android via adb uninstall.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/adb_wsl.sh"
android_app_uninstall() {
# Parse --serial (consumes it, rest stays in ADB_PICK_REST)
local serial
adb_pick_serial "$@" || return 1
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
# Parse --keep-data flag
local keep_data=0
local args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--keep-data) keep_data=1; shift ;;
*) args+=("$1"); shift ;;
esac
done
set -- "${args[@]}"
local pkg="${1:-}"
if [[ -z "$pkg" ]]; then
echo "android_app_uninstall: package obligatorio." >&2
return 1
fi
if (( keep_data )); then
adb_s "$serial" uninstall -k "$pkg" || return 1
else
adb_s "$serial" uninstall "$pkg" || return 1
fi
echo "uninstalled $pkg on $serial"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_app_uninstall "$@"
fi
@@ -0,0 +1,51 @@
---
name: android_emu_battery
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_emu_battery([--serial <S>], level: int, [--charging <true|false>]) -> void"
description: "Simulate battery state on emulator (level + charging). Emulator-only."
tags: [android, emulator, battery, power]
params:
- name: "--serial <S>"
desc: "Optional emulator serial (e.g. emulator-5554). Auto-detected if omitted."
- name: "level"
desc: "Battery level 0-100 to set via 'emu power capacity'."
- name: "--charging <true|false>"
desc: "AC charging state: true maps to 'on', false maps to 'off'. Omit to leave unchanged."
output: "Stdout 'battery: <N>% [charging=...] on <serial>'. Exit 3 if no device found, exit 1 on other errors."
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_emu_battery.sh"
notes: "Util para tests bateria baja, modo ahorro energia. Solo funciona con emuladores (serial emulator-*), no con devices fisicos."
---
## Ejemplo
```bash
# Nivel al 15%, sin cambiar estado de carga
android_emu_battery 15
# Nivel al 5%, forzar descarga (AC off)
android_emu_battery 5 --charging false
# Nivel al 80%, forzar carga (AC on), emulador concreto
android_emu_battery --serial emulator-5554 80 --charging true
```
## Notas
Util para tests de bateria baja y modo ahorro de energia. Solo funciona con emuladores Android
(serial debe empezar con `emulator-`). No aplica a dispositivos fisicos.
Requiere que `adb_wsl.sh` este en el mismo directorio. El ADB se resuelve via
`ANDROID_SDK_WIN` o la ruta por defecto de la instalacion Windows SDK.
@@ -0,0 +1,87 @@
#!/usr/bin/env bash
# android_emu_battery — Simulate battery state on Android emulator (level + charging).
# Usage: android_emu_battery [--serial <S>] <level 0-100> [--charging <true|false>]
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/adb_wsl.sh"
android_emu_battery() {
# Resolve serial (consumes --serial from args, leaves rest in ADB_PICK_REST)
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
# Require serial to be an emulator
if [[ "$serial" != emulator-* ]]; then
echo "android_emu_battery: serial '$serial' is not an emulator (must start with emulator-)." >&2
return 1
fi
# Parse remaining args: positional level + --charging
local level=""
local charging=""
while [[ $# -gt 0 ]]; do
case "$1" in
--charging)
charging="$2"
shift 2
;;
--charging=*)
charging="${1#--charging=}"
shift
;;
-*)
echo "android_emu_battery: unknown flag '$1'." >&2
return 1
;;
*)
if [[ -z "$level" ]]; then
level="$1"
fi
shift
;;
esac
done
# Validate level
if [[ -z "$level" ]]; then
echo "android_emu_battery: level is required (0-100)." >&2
return 1
fi
if ! [[ "$level" =~ ^[0-9]+$ ]] || (( level < 0 || level > 100 )); then
echo "android_emu_battery: invalid level '$level' — must be integer 0-100." >&2
return 1
fi
# Set battery level
adb_s "$serial" emu power capacity "$level" || {
echo "android_emu_battery: failed to set capacity on $serial." >&2
return 1
}
# Set charging state if requested
local ch="<unchanged>"
if [[ -n "$charging" ]]; then
local ac_val
case "$charging" in
true) ac_val="on" ;;
false) ac_val="off" ;;
*)
echo "android_emu_battery: --charging must be 'true' or 'false', got '$charging'." >&2
return 1
;;
esac
adb_s "$serial" emu power ac "$ac_val" || {
echo "android_emu_battery: failed to set AC charging on $serial." >&2
return 1
}
ch="$charging"
fi
echo "battery: ${level}% [charging=${ch}] on ${serial}"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_emu_battery "$@"
fi
@@ -0,0 +1,53 @@
---
name: android_emu_geo_fix
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_emu_geo_fix([--serial <S>], longitude: string, latitude: string, [altitude: string]) -> void"
description: "Fake GPS location on Android emulator via emu geo fix. Emulator-only (not physical devices)."
tags: [android, emulator, geo, gps, location]
params:
- name: "--serial <S>"
desc: "Optional emulator serial. Auto-detected if omitted."
- name: "longitude"
desc: "Longitude (decimal degrees). Passed first — opposite to human lat/lon convention."
- name: "latitude"
desc: "Latitude (decimal degrees)."
- name: "altitude"
desc: "Optional altitude in meters."
output: "Stdout 'GPS set: <lon>, <lat> (alt=...) on <serial>'. Exit 0."
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_emu_geo_fix.sh"
---
## Ejemplo
```bash
# Fijar GPS en Madrid (emulador activo)
android_emu_geo_fix -3.7038 40.4168
# Con altitud
android_emu_geo_fix -3.7038 40.4168 650
# Emulador especifico
android_emu_geo_fix --serial emulator-5554 -3.7038 40.4168
```
## Notas
El orden de argumentos es **longitud primero, latitud segundo** — opuesto a la convencion humana habitual (lat/lon). Esto sigue el protocolo del comando `emu geo fix` de Android.
Solo funciona en emuladores (`emulator-*`). Si el serial apunta a un dispositivo fisico, la funcion sale con error y exit 1.
Usa `adb_pick_serial` de `adb_wsl.sh` para resolver el dispositivo objetivo.
Sale con exit 3 si no hay ningun device conectado.
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
# android_emu_geo_fix — Fake GPS location on Android emulator via emu geo fix.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"
android_emu_geo_fix() {
local serial lon lat alt
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
lon="${1:?android_emu_geo_fix: longitude required}"
lat="${2:?android_emu_geo_fix: latitude required}"
alt="${3:-}"
# geo fix only works on emulators, not physical devices
if [[ "$serial" != emulator-* ]]; then
echo "android_emu_geo_fix: geo fix only works on emulators (got '$serial')" >&2
return 1
fi
adb_s "$serial" emu geo fix "$lon" "$lat" ${alt:+"$alt"}
if [[ -n "$alt" ]]; then
echo "GPS set: $lon, $lat (alt=$alt) on $serial"
else
echo "GPS set: $lon, $lat on $serial"
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_emu_geo_fix "$@"
fi
@@ -0,0 +1,49 @@
---
name: android_emu_rotate
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_emu_rotate([--serial <S>] [portrait|landscape|0|90|180|270])"
description: "Rotate emulator screen. Empty=toggle, or fixed orientation. Locks autorotate."
tags: [android, emulator, rotation, orientation]
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
params:
- name: "--serial <S>"
desc: "Optional emulator serial. Picked automatically if only one is connected."
- name: "orientation"
desc: "Empty=toggle via emu rotate, or fixed: portrait/landscape/0/90/180/270."
output: "Stdout 'rotated: <orient> on <serial>'."
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_emu_rotate.sh"
---
## Ejemplo
```bash
# Toggle rotation
android_emu_rotate
# Force portrait
android_emu_rotate portrait
# Force landscape on specific emulator
android_emu_rotate --serial emulator-5554 landscape
# Set 270 degrees
android_emu_rotate --serial emulator-5554 270
```
## Notas
Deshabilita autorotate (`accelerometer_rotation 0`) antes de aplicar cualquier orientacion fija, de modo que el sistema no la revierta. El toggle (`emu rotate`) no desactiva autorotate: lo usa directamente el daemon del emulador.
`adb_pick_serial` (de `adb_wsl_bash_infra`) selecciona el unico emulador conectado o falla con exit 3 si hay ambiguedad o ninguno disponible. Los argumentos restantes tras extraer `--serial` quedan en `ADB_PICK_REST`.
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
# android_emu_rotate — rotate emulator screen or toggle rotation
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=./adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"
android_emu_rotate() {
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
local arg="${1:-}"
# Disable autorotate before any operation
if [[ -n "$arg" ]]; then
adb_s "$serial" shell settings put system accelerometer_rotation 0
fi
case "$arg" in
"")
# Toggle via emu rotate command
adb_s "$serial" emu rotate
;;
portrait|0)
adb_s "$serial" shell settings put system accelerometer_rotation 0
adb_s "$serial" shell settings put system user_rotation 0
;;
landscape|90)
adb_s "$serial" shell settings put system accelerometer_rotation 0
adb_s "$serial" shell settings put system user_rotation 1
;;
180)
adb_s "$serial" shell settings put system accelerometer_rotation 0
adb_s "$serial" shell settings put system user_rotation 2
;;
270)
adb_s "$serial" shell settings put system accelerometer_rotation 0
adb_s "$serial" shell settings put system user_rotation 3
;;
*)
echo "android_emu_rotate: unknown orientation '$arg'" >&2
echo "Usage: android_emu_rotate [--serial <S>] [portrait|landscape|0|90|180|270]" >&2
return 1
;;
esac
echo "rotated: ${arg:-toggle} on $serial"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_emu_rotate "$@"
fi
@@ -0,0 +1,51 @@
---
name: android_emulator_list
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_emulator_list([--json])"
description: "Lista los AVDs disponibles invocando emulator.exe Windows desde WSL2."
tags: [android, emulator, wsl]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
params:
- name: "--json"
desc: "Optional flag, outputs JSON array instead of newline-separated names"
output: "Lista de AVDs disponibles en el SDK Windows. Una por linea, o JSON array con --json."
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_emulator_list.sh"
notes: "Lee env var EMULATOR o ANDROID_SDK_WIN. Default Windows path: /mnt/c/Users/lucas/AppData/Local/Android/Sdk/emulator/emulator.exe. Exit 0 si lista (incluso vacia). Exit 1 solo si el binario no existe o no es ejecutable."
---
## Ejemplo
```bash
# Listar AVDs (una por linea)
android_emulator_list
# Listar AVDs en formato JSON
android_emulator_list --json
# ["Pixel_7_API_34","Pixel_4_API_30"]
# Sobreescribir ruta del emulador
EMULATOR="/custom/path/emulator.exe" android_emulator_list
# Sobreescribir SDK base
ANDROID_SDK_WIN="/mnt/d/Android/Sdk" android_emulator_list
```
## Notas
El script es ejecutable directamente (`chmod +x`) o invocable con `bash android_emulator_list.sh`.
`emulator.exe -list-avds` imprime warnings a stderr que se descartan con `2>/dev/null`. La captura con `mapfile` filtra ademas lineas vacias para producir una lista limpia.
La variable `EMULATOR` tiene prioridad sobre `ANDROID_SDK_WIN`. Si ninguna esta definida se usa el path Windows por defecto de Lucas.
+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
# android_emulator_list — Lista los AVDs disponibles invocando emulator.exe Windows desde WSL2.
set -euo pipefail
# Resolve emulator binary
EMULATOR="${EMULATOR:-${ANDROID_SDK_WIN:-/mnt/c/Users/lucas/AppData/Local/Android/Sdk}/emulator/emulator.exe}"
if [[ ! -x "$EMULATOR" ]]; then
echo "error: emulator binary not found or not executable: $EMULATOR" >&2
exit 1
fi
# Parse flags
JSON=false
for arg in "$@"; do
case "$arg" in
--json) JSON=true ;;
*) echo "error: unknown argument: $arg" >&2; exit 1 ;;
esac
done
# Collect AVDs, stripping any warnings emulator.exe prints to stderr
mapfile -t AVDS < <("$EMULATOR" -list-avds 2>/dev/null || true)
if $JSON; then
# Build JSON array
printf '['
first=true
for avd in "${AVDS[@]}"; do
[[ -z "$avd" ]] && continue
if $first; then
printf '"%s"' "$avd"
first=false
else
printf ',"%s"' "$avd"
fi
done
printf ']\n'
else
for avd in "${AVDS[@]}"; do
[[ -z "$avd" ]] && continue
printf '%s\n' "$avd"
done
fi
@@ -0,0 +1,49 @@
---
name: android_emulator_start
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_emulator_start(avd_name: string, timeout_s: int) -> string"
description: "Arranca un AVD en background y espera a que termine de bootear. Idempotente: si ya hay emulador corriendo no lanza otro."
tags: [android, emulator, wsl]
params:
- name: avd_name
desc: "Nombre del AVD a arrancar (visible con android_emulator_list o `emulator.exe -list-avds`)"
- name: timeout_s
desc: "Timeout total en segundos para esperar el boot completo. Opcional, default 180"
output: "Serial del device emulado (ej. emulator-5554) en stdout. Exit 0 = boot completo, exit 1 = timeout o emulador murio."
uses_functions: ["adb_wsl_bash_infra"]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_emulator_start.sh"
---
## Ejemplo
```bash
source bash/functions/infra/android_emulator_start.sh
# Arrancar AVD con timeout por defecto (180s)
serial=$(android_emulator_start "Pixel_6_API_34")
echo "Emulador listo: $serial" # emulator-5554
# Con timeout personalizado
serial=$(android_emulator_start "Pixel_6_API_34" 300)
```
## Notas
- Sourcea `adb_wsl.sh` del mismo directorio si existe (provee `ADB`, `adb_run`, `adb_wait_boot`). Si no, usa implementacion inline.
- Resuelve `EMULATOR` y `ADB` desde `ANDROID_SDK_WIN` (default `/mnt/c/Users/lucas/AppData/Local/Android/Sdk`) o desde las variables de entorno `EMULATOR=` / `ADB=` si ya están fijadas.
- Idempotente: si `adb devices` ya muestra un `emulator-*`, imprime "already running" + el serial y sale con exit 0 sin lanzar un segundo proceso.
- Log del emulador en `/tmp/emulator_<avd>.log`. PID en `/tmp/emulator_<avd>.pid`.
- El timeout total se reparte: primera mitad para `adb wait-for-device`, segunda mitad para esperar `sys.boot_completed=1`.
- Diseñado para WSL2 con Android SDK instalado en Windows. En Linux nativo basta cambiar las rutas de los binarios via `EMULATOR=` y `ADB=`.
@@ -0,0 +1,110 @@
#!/usr/bin/env bash
# android_emulator_start — Arranca un AVD en background y espera a que bootee.
# Uso: android_emulator_start <avd_name> [timeout_s]
set -euo pipefail
# ---------------------------------------------------------------------------
# Source adb_wsl si está disponible (provee ADB, adb_run, adb_wait_boot)
# ---------------------------------------------------------------------------
_ADB_WSL_SH="$(dirname "${BASH_SOURCE[0]}")/adb_wsl.sh"
if [[ -f "$_ADB_WSL_SH" ]]; then
# shellcheck source=adb_wsl.sh
source "$_ADB_WSL_SH"
else
# Fallback inline: resolver ADB
if [[ -z "${ADB:-}" ]]; then
_sdk_root="${ANDROID_SDK_WIN:-/mnt/c/Users/lucas/AppData/Local/Android/Sdk}"
ADB="${_sdk_root}/platform-tools/adb.exe"
unset _sdk_root
fi
adb_run() { "$ADB" "$@"; }
adb_wait_boot() {
local timeout_s="${1:-120}"
local elapsed=0 interval=3 val
while (( elapsed < timeout_s )); do
val=$(adb_run shell getprop sys.boot_completed 2>/dev/null | tr -d '[:space:]')
[[ "$val" == "1" ]] && return 0
sleep "$interval"
(( elapsed += interval ))
done
echo "android_emulator_start: timeout ${timeout_s}s esperando boot." >&2
return 1
}
fi
# ---------------------------------------------------------------------------
# Resolver EMULATOR
# ---------------------------------------------------------------------------
if [[ -z "${EMULATOR:-}" ]]; then
_sdk_root="${ANDROID_SDK_WIN:-/mnt/c/Users/lucas/AppData/Local/Android/Sdk}"
EMULATOR="${_sdk_root}/emulator/emulator.exe"
unset _sdk_root
fi
# ---------------------------------------------------------------------------
# android_emulator_start <avd_name> [timeout_s]
# ---------------------------------------------------------------------------
android_emulator_start() {
local AVD="${1:?android_emulator_start requiere el nombre del AVD como primer argumento}"
local timeout_s="${2:-180}"
# Validaciones de entorno
if [[ ! -f "$EMULATOR" ]]; then
echo "android_emulator_start: emulator.exe no encontrado en '$EMULATOR'. Fija EMULATOR= o ANDROID_SDK_WIN=." >&2
return 1
fi
if [[ ! -f "$ADB" ]]; then
echo "android_emulator_start: adb.exe no encontrado en '$ADB'. Fija ADB= o ANDROID_SDK_WIN=." >&2
return 1
fi
# Idempotencia: si ya hay un emulador corriendo, salir sin lanzar otro
if adb_run devices 2>/dev/null | grep -q "emulator-"; then
echo "already running"
# Imprimir el serial existente
adb_run devices 2>/dev/null | grep "emulator-" | awk '{print $1}' | head -n1
return 0
fi
local log_file="/tmp/emulator_${AVD}.log"
local pid_file="/tmp/emulator_${AVD}.pid"
# Lanzar emulador en background
"$EMULATOR" -avd "$AVD" -no-boot-anim -no-snapshot-load >"$log_file" 2>&1 &
local emu_pid=$!
echo "$emu_pid" > "$pid_file"
# Esperar a que el dispositivo aparezca en adb
local wait_timeout=$(( timeout_s / 2 ))
if ! timeout "$wait_timeout" adb_run wait-for-device 2>/dev/null; then
echo "android_emulator_start: timeout esperando que el dispositivo aparezca en adb (${wait_timeout}s)." >&2
return 1
fi
# Verificar que el proceso del emulador sigue vivo
if ! kill -0 "$emu_pid" 2>/dev/null; then
echo "android_emulator_start: el proceso del emulador (PID $emu_pid) murió antes de completar el boot." >&2
echo " Log: $log_file" >&2
return 1
fi
# Esperar boot completo (sys.boot_completed=1)
local boot_timeout=$(( timeout_s - wait_timeout ))
if ! adb_wait_boot "$boot_timeout"; then
echo "android_emulator_start: timeout ${timeout_s}s esperando boot completo del AVD '$AVD'." >&2
echo " Log: $log_file" >&2
return 1
fi
# Obtener serial del dispositivo emulado
local serial
serial=$(adb_run devices 2>/dev/null | grep "emulator-" | awk '{print $1}' | head -n1)
echo "$serial"
return 0
}
# Ejecutar si se invoca directamente (no sourceado)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_emulator_start "$@"
fi
@@ -0,0 +1,44 @@
---
name: android_emulator_stop
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_emulator_stop(serial?: string) -> void"
description: "Para uno o todos los emuladores Android via adb emu kill. Si serial esta vacio, detecta todos los emulator-* activos y los para. Idempotente: exit 0 aunque no haya nada que matar."
tags: ["android", "emulator", "wsl", "adb"]
params:
- name: "serial"
desc: "Optional emulator serial (e.g. emulator-5554). Empty = kill all running emulators"
output: "Imprime numero de emuladores parados. Exit 0 idempotente."
uses_functions: ["adb_wsl_bash_infra"]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_emulator_stop.sh"
---
## Ejemplo
```bash
# Parar todos los emuladores en ejecucion
android_emulator_stop
# Parar un emulador concreto
android_emulator_stop emulator-5554
# Sobreescribir ruta de adb
ADB=/usr/local/bin/adb android_emulator_stop
```
## Notas
Resuelve `ADB` desde variable de entorno (default: ruta de Android SDK en Windows bajo WSL2).
Usa `adb emu kill` en vez de `adb kill-server` para parar solo el emulador sin afectar al daemon adb.
`set -euo pipefail` activo, pero los fallos de `adb emu kill` se suprimen con `|| true` para mantener idempotencia.
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
# android_emulator_stop — Para uno o todos los emuladores Android via adb emu kill.
set -euo pipefail
android_emulator_stop() {
local serial="${1:-}"
local ADB="${ADB:-/mnt/c/Users/lucas/AppData/Local/Android/Sdk/platform-tools/adb.exe}"
local killed=0
if [[ -z "$serial" ]]; then
# Detectar todos los emuladores activos
local serials
serials=$("$ADB" devices 2>/dev/null | grep -E '^emulator-' | awk '{print $1}' || true)
if [[ -z "$serials" ]]; then
echo "android_emulator_stop: no running emulators found"
return 0
fi
while IFS= read -r s; do
[[ -z "$s" ]] && continue
echo "android_emulator_stop: stopping $s"
"$ADB" -s "$s" emu kill 2>/dev/null || true
((killed++)) || true
done <<< "$serials"
else
echo "android_emulator_stop: stopping $serial"
"$ADB" -s "$serial" emu kill 2>/dev/null || true
((killed++)) || true
fi
echo "android_emulator_stop: stopped $killed emulator(s)"
return 0
}
# Ejecutar si se llama directamente
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_emulator_stop "${1:-}"
fi
@@ -0,0 +1,63 @@
---
name: android_input_keyevent
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_input_keyevent([--serial <S>] key: string)"
description: "Send key event via adb shell input keyevent. Accepts aliases (BACK, HOME, POWER, ENTER, MENU, RECENT_APPS, VOLUME_UP, VOLUME_DOWN), raw numeric codes, or explicit KEYCODE_* names."
tags: [android, adb, input, keyevent, ui-test]
params:
- name: "--serial <S>"
desc: "Optional target device/emulator serial. If omitted, adb_pick_serial resolves the single connected device."
- name: "key"
desc: "Keycode: short alias (BACK/HOME/POWER/ENTER/MENU/RECENT_APPS/VOLUME_UP/VOLUME_DOWN), raw number (e.g. 4, 26), or explicit KEYCODE_* name."
output: "Stdout 'key: <code> on <serial>'."
uses_functions: ["adb_wsl_bash_infra"]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_input_keyevent.sh"
notes: "Lista completa de keycodes: https://developer.android.com/reference/android/view/KeyEvent. Exit 3 si adb_pick_serial falla (ningun device o ambiguo sin --serial)."
---
## Ejemplo
```bash
# Pulsar BACK en el unico device conectado
android_input_keyevent BACK
# Pulsar HOME en un emulador especifico
android_input_keyevent --serial emulator-5554 HOME
# Codigo numerico directo
android_input_keyevent 26 # POWER
# KEYCODE_* explicito
android_input_keyevent KEYCODE_DPAD_CENTER
```
## Notas
Aliases resueltos internamente:
| Alias | KEYCODE |
|--------------|-----------------------|
| BACK | KEYCODE_BACK |
| HOME | KEYCODE_HOME |
| POWER | KEYCODE_POWER |
| ENTER | KEYCODE_ENTER |
| MENU | KEYCODE_MENU |
| RECENT_APPS | KEYCODE_APP_SWITCH |
| VOLUME_UP | KEYCODE_VOLUME_UP |
| VOLUME_DOWN | KEYCODE_VOLUME_DOWN |
Si el argumento no coincide con ningun alias y no es numerico, se construye `KEYCODE_<UPPER>` para pasarlo directo a `adb shell input keyevent`.
Exit codes: 1 = keycode vacio, 3 = fallo de `adb_pick_serial` (ningun device o ambiguo).
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
# android_input_keyevent — Send key event via adb shell input keyevent.
# Accepts aliases (BACK, HOME, POWER, ENTER, MENU, RECENT_APPS),
# raw numeric codes, or explicit KEYCODE_* names.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"
android_input_keyevent() {
# Resolve serial (consumes --serial <S> from args, remainder in ADB_PICK_REST)
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
local raw="${1:-}"
if [[ -z "$raw" ]]; then
echo "android_input_keyevent: missing keycode argument" >&2
return 1
fi
# Resolve alias → KEYCODE_*
local keycode
case "${raw^^}" in
BACK) keycode="KEYCODE_BACK" ;;
HOME) keycode="KEYCODE_HOME" ;;
POWER) keycode="KEYCODE_POWER" ;;
ENTER) keycode="KEYCODE_ENTER" ;;
MENU) keycode="KEYCODE_MENU" ;;
RECENT_APPS) keycode="KEYCODE_APP_SWITCH" ;;
VOLUME_UP) keycode="KEYCODE_VOLUME_UP" ;;
VOLUME_DOWN) keycode="KEYCODE_VOLUME_DOWN" ;;
*)
# Already has KEYCODE_ prefix or is a raw number → pass through
if [[ "${raw^^}" == KEYCODE_* ]] || [[ "$raw" =~ ^[0-9]+$ ]]; then
keycode="$raw"
else
# Unknown alias: uppercase and prepend KEYCODE_
keycode="KEYCODE_${raw^^}"
fi
;;
esac
adb_s "$serial" shell input keyevent "$keycode"
echo "key: $keycode on $serial"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_input_keyevent "$@"
fi
@@ -0,0 +1,59 @@
---
name: android_input_swipe
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_input_swipe([--serial <S>], x1: int, y1: int, x2: int, y2: int, [duration_ms: int])"
description: "Send swipe gesture between two points with duration."
tags: [android, adb, input, swipe, gesture, ui-test]
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
params:
- name: "--serial <S>"
desc: "Optional target device serial. Overrides ADB_SERIAL envvar."
- name: x1
desc: "Start X coordinate in pixels."
- name: y1
desc: "Start Y coordinate in pixels."
- name: x2
desc: "End X coordinate in pixels."
- name: y2
desc: "End Y coordinate in pixels."
- name: duration_ms
desc: "Optional swipe duration in milliseconds. Default 300."
output: "Stdout swipe summary line: 'swipe x1,y1 → x2,y2 (Nms) on <serial>'."
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_input_swipe.sh"
---
## Ejemplo
```bash
source bash/functions/infra/android_input_swipe.sh
# Scroll down (swipe up)
android_input_swipe 540 1400 540 400
# Scroll up slowly on a specific device
android_input_swipe --serial emulator-5554 540 400 540 1400 800
```
## Notas
Requiere `adb_wsl.sh` (sourceado automáticamente). Usa `adb_pick_serial` para
resolver el dispositivo objetivo a partir de `--serial`, `ADB_SERIAL` o el
único device disponible.
Los cuatro argumentos de coordenadas se validan como enteros antes de invocar
adb — acepta coordenadas negativas (edge cases de hardware con ejes invertidos).
Exit 3 si `adb_pick_serial` no puede resolver el serial (sin devices o ambiguo).
Exit 1 si faltan coordenadas o alguna no es numérica.
@@ -0,0 +1,52 @@
#!/usr/bin/env bash
# android_input_swipe — Send swipe gesture between two points via adb shell input swipe.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"
# ---------------------------------------------------------------------------
# android_input_swipe [--serial <S>] <x1> <y1> <x2> <y2> [duration_ms]
#
# $1 x1 Start X coordinate in pixels (obligatorio).
# $2 y1 Start Y coordinate in pixels (obligatorio).
# $3 x2 End X coordinate in pixels (obligatorio).
# $4 y2 End Y coordinate in pixels (obligatorio).
# $5 duration_ms Swipe duration in milliseconds (opcional, default 300).
#
# Envvar ADB_SERIAL overrides --serial.
# ---------------------------------------------------------------------------
android_input_swipe() {
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
local x1="${1:-}"
local y1="${2:-}"
local x2="${3:-}"
local y2="${4:-}"
local dur="${5:-300}"
if [[ -z "$x1" || -z "$y1" || -z "$x2" || -z "$y2" ]]; then
echo "android_input_swipe: se requieren cuatro argumentos: x1 y1 x2 y2." >&2
return 1
fi
# Validar que los cuatro coordenadas son numericas (enteros o negativos).
local coord
for coord in "$x1" "$y1" "$x2" "$y2"; do
if ! [[ "$coord" =~ ^-?[0-9]+$ ]]; then
echo "android_input_swipe: coordenada no numerica: '$coord'." >&2
return 1
fi
done
adb_s "$serial" shell input swipe "$x1" "$y1" "$x2" "$y2" "$dur"
echo "swipe $x1,$y1$x2,$y2 (${dur}ms) on $serial"
}
# Ejecutar si se llama directamente (no sourceado)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_input_swipe "$@"
fi
+46
View File
@@ -0,0 +1,46 @@
---
name: android_input_tap
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_input_tap([--serial <S>], x: int, y: int) -> void"
description: "Send tap gesture at screen coordinates via adb shell input tap."
tags: [android, adb, input, tap, ui-test, gesture]
params:
- name: "--serial <S>"
desc: "Optional target device serial. Auto-detected if omitted."
- name: "x"
desc: "X coordinate in pixels (non-negative integer)."
- name: "y"
desc: "Y coordinate in pixels (non-negative integer)."
output: "Stdout 'tap @ <x>,<y> on <serial>'."
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_input_tap.sh"
---
## Ejemplo
```bash
# Auto-detect device
android_input_tap 540 960
# Target specific device
android_input_tap --serial emulator-5554 540 960
```
## Notas
Sources `adb_wsl.sh` para resolver el binario ADB y exponer `adb_pick_serial` / `adb_s`.
Usa `adb_pick_serial` para consumir `--serial` de los args y autodetectar el device si no se pasa.
Valida X e Y con regex `^[0-9]+$` antes de invocar adb.
Exit 3 si no hay device/emulador disponible (propagado desde `adb_pick_serial`).
+51
View File
@@ -0,0 +1,51 @@
#!/usr/bin/env bash
# android_input_tap — Send tap gesture at screen coordinates via adb shell input tap.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=./adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"
# ---------------------------------------------------------------------------
# android_input_tap [--serial <S>] <x> <y>
#
# --serial <S> Optional target device serial (also auto-detected).
# x X coordinate in pixels (non-negative integer).
# y Y coordinate in pixels (non-negative integer).
#
# Exits:
# 0 tap sent successfully
# 1 missing or invalid coordinates
# 3 no device/emulator available
# ---------------------------------------------------------------------------
android_input_tap() {
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
local x="${1:-}"
local y="${2:-}"
if [[ -z "$x" || -z "$y" ]]; then
echo "android_input_tap: se requieren X e Y como argumentos posicionales." >&2
return 1
fi
if [[ ! "$x" =~ ^[0-9]+$ ]]; then
echo "android_input_tap: X debe ser un entero no negativo, recibido '$x'." >&2
return 1
fi
if [[ ! "$y" =~ ^[0-9]+$ ]]; then
echo "android_input_tap: Y debe ser un entero no negativo, recibido '$y'." >&2
return 1
fi
adb_s "$serial" shell input tap "$x" "$y"
echo "tap @ $x,$y on $serial"
}
# Ejecutar si se llama directamente (no sourceado)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_input_tap "$@"
fi
@@ -0,0 +1,52 @@
---
name: android_input_text
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_input_text([--serial <S>], text: string) -> void"
description: "Type text in focused field via adb shell input text. Spaces handled."
tags: [android, adb, input, text, ui-test]
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_input_text.sh"
params:
- name: "--serial <S>"
desc: "Optional target device serial. If omitted, autodetects first connected device/emulator."
- name: "text"
desc: "Text to type (spaces become %s as required by adb)."
output: "Stdout 'typed: <text>'. Exit 0."
notes: |
adb input text replaces spaces with %s. Funcion lo hace automaticamente.
Special chars " $ ` se escapan con backslash para evitar interpretacion por el shell.
Exit 3 si no hay ningun device disponible (propagado desde adb_pick_serial).
---
## Ejemplo
```bash
source bash/functions/infra/android_input_text.sh
# Tipar en el device por defecto
android_input_text "hello world"
# → typed: hello world (envia "hello%sworld" a adb)
# Tipar en un device especifico
android_input_text --serial emulator-5554 "user@example.com"
```
## Notas
`adb shell input text` no acepta espacios directos — los convierte a `%s` internamente. Esta funcion hace la sustitucion antes de llamar a adb para que el comportamiento sea predecible.
Los caracteres `"`, `$` y `` ` `` se escapan con backslash para que el shell no los interprete al construir el comando.
Depende de `adb_wsl_bash_infra` para resolver el binario `adb.exe` en WSL2 y para `adb_pick_serial` / `adb_s`.
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
# android_input_text — Type text in focused field via adb shell input text.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"
# ---------------------------------------------------------------------------
# android_input_text [--serial <S>] <text>
#
# $1 text Text to type in the currently focused field (obligatorio).
# Spaces are replaced with %s as required by adb input text.
# Special chars " $ ` are escaped with backslash.
#
# Envvar ADB_SERIAL overrides --serial.
# ---------------------------------------------------------------------------
android_input_text() {
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
local text="${1:-}"
if [[ -z "$text" ]]; then
echo "android_input_text: se requiere el texto como primer argumento." >&2
return 1
fi
# adb input text does not support raw spaces; replace with %s.
# Also escape " $ ` which the shell would interpret inside the adb command.
local escaped
escaped="${text// /%s}"
escaped="${escaped//\"/\\\"}"
escaped="${escaped//\$/\\\$}"
escaped="${escaped//\`/\\\`}"
adb_s "$serial" shell input text "$escaped"
echo "typed: $text"
}
# Ejecutar si se llama directamente (no sourceado)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_input_text "$@"
fi
+59
View File
@@ -0,0 +1,59 @@
---
name: android_logcat
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_logcat([--serial <S>] [--package <name>] [--level <V|D|I|W|E|F>] [--lines <N>] [--clear])"
description: "Lee logcat del device/emulador, opcionalmente filtrado por package y nivel. Multi-emulator via --serial."
tags: [android, adb, logcat, wsl]
uses_functions: ["adb_wsl_bash_infra"]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
params:
- name: "--serial <S>"
desc: "Optional target device/emulator serial. Default: first device detected."
- name: "--package <name>"
desc: "Filter by app package (resolves PID via adb shell pidof)"
- name: "--level <L>"
desc: "Min log level V/D/I/W/E/F, default I"
- name: "--lines <N>"
desc: "Dump last N lines and exit. Default: follow indefinidamente"
- name: "--clear"
desc: "Clear log buffer before reading"
output: "Logcat output a stdout. Follow indefinido sin --lines. Exit 130 si Ctrl-C. Exit 2 si --package y el proceso no corre."
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_logcat.sh"
---
## Ejemplo
```bash
# Follow completo sin filtros
android_logcat
# Solo logs de una app, nivel Warning y superior
android_logcat --package com.example.myapp --level W
# Dump de las últimas 200 líneas y salir
android_logcat --lines 200
# Limpiar buffer y hacer follow solo de errores de la app
android_logcat --clear --package com.example.myapp --level E
```
## Notas
- Resuelve `adb` o `adb.exe` en PATH (compatible con WSL2 usando el binario Windows).
- `--package` usa `adb shell pidof -s` para obtener el PID actual. Si la app no está corriendo, sale con exit 2.
- `--lines N` activa modo dump (`-d -t N`); sin él, el follow es indefinido hasta Ctrl-C (exit 130).
- `--clear` ejecuta `adb logcat -c` antes de leer, descartando el buffer acumulado.
- El filtro de nivel se aplica como `*:<level>` al final del comando logcat.
- En follow mode, `trap INT TERM` garantiza exit limpio (exit 130) al interrumpir.
- CR (`\r`) del output de `adb.exe` en WSL se limpia al resolver el PID.
+60
View File
@@ -0,0 +1,60 @@
#!/usr/bin/env bash
# android_logcat — Lee logcat del device/emulador, opcionalmente filtrado por package y nivel.
# Multi-emulator via --serial <S>.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=/dev/null
source "$SCRIPT_DIR/adb_wsl.sh"
android_logcat() {
local serial
adb_pick_serial "$@" || { echo "android_logcat: no device/emulator." >&2; return 3; }
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
local package=""
local level="I"
local lines=""
local do_clear=0
while [[ $# -gt 0 ]]; do
case "$1" in
--package) package="$2"; shift 2 ;;
--level) level="$2"; shift 2 ;;
--lines) lines="$2"; shift 2 ;;
--clear) do_clear=1; shift ;;
*) echo "android_logcat: unknown argument: $1" >&2; return 1 ;;
esac
done
if [[ $do_clear -eq 1 ]]; then
adb_s "$serial" logcat -c
fi
local pid_filter=""
if [[ -n "$package" ]]; then
local pid
pid=$(adb_s "$serial" shell pidof -s "$package" 2>/dev/null || true)
pid="${pid//$'\r'/}"
if [[ -z "$pid" ]]; then
echo "android_logcat: package '$package' is not running on $serial" >&2
return 2
fi
pid_filter="--pid=$pid"
fi
local -a cmd=(logcat -v time)
[[ -n "$lines" ]] && cmd+=(-d -t "$lines")
[[ -n "$pid_filter" ]] && cmd+=("$pid_filter")
cmd+=("*:${level}")
trap 'exit 130' INT TERM
adb_s "$serial" "${cmd[@]}"
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
android_logcat "$@"
fi
+46
View File
@@ -0,0 +1,46 @@
---
name: android_pull
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_pull [--serial <S>] remote_path local_path"
description: "Pull file/dir from Android device to WSL via adb pull."
tags: [android, adb, pull, file, transfer]
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
params:
- name: "--serial <S>"
desc: "Optional target device serial. If omitted, adb_pick_serial auto-detects the connected device."
- name: "remote_path"
desc: "Source path on the Android device (e.g. /sdcard/Pictures/foo.png)."
- name: "local_path"
desc: "Destination path in the WSL filesystem. Parent directories are created automatically."
output: "Stdout 'pulled: <remote> → <local> from <serial>'."
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_pull.sh"
---
## Ejemplo
```bash
# Pull a single file (auto-detect device)
android_pull /sdcard/Pictures/foo.png ~/Downloads/foo.png
# Pull a directory to a specific local path with explicit serial
android_pull --serial emulator-5554 /sdcard/DCIM ~/Downloads/DCIM
```
## Notas
Sources `adb_wsl.sh` for `adb_pick_serial`, `ADB_PICK_REST`, `adb_wsl_to_win`, and `adb_s`.
The local path is converted to a Windows path via `adb_wsl_to_win` before passing to `adb pull`,
which is required because `adb.exe` (Windows binary) does not understand WSL paths.
Exit code 3 when no device serial can be resolved.
+28
View File
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
# android_pull — Pull file/dir from Android device to WSL via adb pull.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/adb_wsl.sh"
android_pull() {
local serial remote local_path win_local
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
remote="${1:?remote_path required}"
local_path="${2:?local_path required}"
mkdir -p "$(dirname "$local_path")"
win_local=$(adb_wsl_to_win "$local_path")
adb_s "$serial" pull "$remote" "$win_local"
echo "pulled: $remote$local_path from $serial"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_pull "$@"
fi
+50
View File
@@ -0,0 +1,50 @@
---
name: android_push
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_push([--serial <S>], local_path: string, remote_path: string) -> void"
description: "Push file/dir from WSL to Android device via adb push."
tags: [android, adb, push, file, transfer]
params:
- name: "--serial <S>"
desc: "Optional target device/emulator serial. Auto-detected if omitted."
- name: "local_path"
desc: "WSL source path to file or directory to push."
- name: "remote_path"
desc: "Device destination path, e.g. /sdcard/Download/foo.txt."
output: "Stdout 'pushed: <local> → <remote> on <serial>'. Exit 0."
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_push.sh"
---
## Ejemplo
```bash
# Push a file to the active emulator
android_push /tmp/data.json /sdcard/Download/data.json
# Push to a specific device
android_push --serial emulator-5554 /tmp/data.json /sdcard/Download/data.json
# Push a directory
android_push --serial R5CR1234567 ~/exports/bundle /sdcard/Download/bundle
```
## Notas
Usa `adb_pick_serial` de `adb_wsl.sh` para resolver el dispositivo objetivo.
Si `--serial` no se pasa, autodetecta el primer device/emulador disponible.
Sale con exit 3 si no hay ningun device conectado.
Valida que `local_path` existe en WSL antes de convertir y enviar.
Convierte el path WSL a Windows con `adb_wsl_to_win` (requiere `wslpath`; si no está disponible usa el path tal cual).
+32
View File
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
# android_push — Push file/dir from WSL to Android device via adb push.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"
android_push() {
local serial local_path remote_path win_local
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
local_path="${1:?android_push: local_path required}"
remote_path="${2:?android_push: remote_path required}"
if [[ ! -e "$local_path" ]]; then
echo "android_push: '$local_path' not found." >&2
return 1
fi
win_local=$(adb_wsl_to_win "$local_path")
adb_s "$serial" push "$win_local" "$remote_path"
echo "pushed: $local_path$remote_path on $serial"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_push "$@"
fi
@@ -0,0 +1,53 @@
---
name: android_screen_record
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_screen_record([--serial <S>] [--duration <s>] [--bit-rate <bps>] [--size <WxH>] output_path: string) -> void"
description: "Record screen video via adb screenrecord, pulls to local path."
tags: [android, adb, screen, record, video]
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
params:
- name: "--serial <S>"
desc: "Optional target device serial. If omitted, autodetects first connected device/emulator."
- name: "output_path"
desc: "WSL destination path for the recorded .mp4 file."
- name: "--duration <s>"
desc: "Recording duration in seconds. Default 30, max 180 (adb screenrecord built-in limit)."
- name: "--bit-rate <bps>"
desc: "Video bit rate in bits per second. Default 4000000 (4 Mbps)."
- name: "--size <WxH>"
desc: "Video dimensions e.g. 720x1280. Default: device native resolution."
output: "Stdout 'recorded: <path> (<s>s from <serial>)'. MP4 file written to output_path."
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_screen_record.sh"
---
## Ejemplo
```bash
source bash/functions/infra/android_screen_record.sh
# Record 15 seconds to a local file
android_screen_record --duration 15 /tmp/demo.mp4
# Specific device, custom resolution, higher bitrate
android_screen_record --serial emulator-5554 --duration 60 --bit-rate 8000000 --size 1080x2400 ~/videos/session.mp4
```
## Notas
`adb screenrecord` tiene un limite maximo de 180 segundos por grabacion. Para capturas mas largas, encadenar multiples llamadas y concatenar los MP4 resultantes (ej. con `ffmpeg -f concat`).
El archivo temporal en el dispositivo es siempre `/sdcard/__rec.mp4` y se elimina tras el pull. Si la grabacion falla a mitad, el archivo puede quedar en el dispositivo; en ese caso ejecutar `adb shell rm /sdcard/__rec.mp4` manualmente.
Exit codes: 0 exito, 2 falta output_path, 3 ningun device encontrado.
@@ -0,0 +1,78 @@
#!/usr/bin/env bash
# android_screen_record — Record screen video via adb screenrecord, pulls to local path.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"
# ---------------------------------------------------------------------------
# android_screen_record [--serial <S>] [--duration <s>] [--bit-rate <bps>] [--size <WxH>] <output_path>
#
# Args:
# --serial <S> Optional: target device serial (default: autodetect)
# --duration <s> Recording duration in seconds (default: 30, max: 180)
# --bit-rate <bps> Video bit rate (default: 4000000)
# --size <WxH> Video dimensions e.g. 720x1280 (default: device native)
# output_path WSL destination path for the .mp4 file
#
# Exit codes:
# 0 success
# 1 general error
# 2 missing output_path argument
# 3 no device/emulator found
# ---------------------------------------------------------------------------
android_screen_record() {
local dur=30
local bit_rate=4000000
local size=""
# Parse flags first pass to extract serial; remaining args go to ADB_PICK_REST.
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
# Parse remaining flags
while [[ $# -gt 0 ]]; do
case "$1" in
--duration) dur="$2"; shift 2 ;;
--duration=*) dur="${1#--duration=}"; shift ;;
--bit-rate) bit_rate="$2"; shift 2 ;;
--bit-rate=*) bit_rate="${1#--bit-rate=}"; shift ;;
--size) size="$2"; shift 2 ;;
--size=*) size="${1#--size=}"; shift ;;
-*) echo "android_screen_record: unknown flag '$1'" >&2; return 1 ;;
*) break ;;
esac
done
local output="${1:-}"
if [[ -z "$output" ]]; then
echo "android_screen_record: output_path is required." >&2
return 2
fi
# Build adb screenrecord args
local rec_args=("shell" "screenrecord" "--time-limit" "$dur")
rec_args+=("--bit-rate" "$bit_rate")
[[ -n "$size" ]] && rec_args+=("--size" "$size")
rec_args+=("/sdcard/__rec.mp4")
echo "android_screen_record: recording ${dur}s from $serial..." >&2
adb_s "$serial" "${rec_args[@]}" || {
echo "android_screen_record: screenrecord failed." >&2
return 1
}
adb_s "$serial" pull /sdcard/__rec.mp4 "$output" || {
echo "android_screen_record: pull failed." >&2
return 1
}
adb_s "$serial" shell rm /sdcard/__rec.mp4
echo "recorded: $output (${dur}s from $serial)"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_screen_record "$@"
fi
@@ -0,0 +1,52 @@
---
name: android_screenshot
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_screenshot([--serial <S>], output_path: string) -> void"
description: "Capture screen as PNG via adb exec-out screencap -p."
tags: [android, adb, screenshot, screen, capture]
uses_functions: [adb_wsl_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
params:
- name: "--serial <S>"
desc: "Optional ADB serial to target a specific device/emulator. If omitted, autodetects the first connected device."
- name: "output_path"
desc: "WSL path where the PNG screenshot will be written (e.g. /tmp/screen.png). Parent directory is created if absent."
output: "Stdout 'screenshot: <path> (<bytes> bytes) from <serial>'. PNG file written to disk."
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_screenshot.sh"
---
## Ejemplo
```bash
source bash/functions/infra/android_screenshot.sh
android_screenshot /tmp/screen.png
# screenshot: /tmp/screen.png (123456 bytes) from emulator-5554
# Targeting a specific device:
android_screenshot --serial emulator-5554 /tmp/screen.png
```
## Notas
Sources `adb_wsl.sh` from its own directory, so `ADB` and `ANDROID_SDK_WIN` env vars
are respected as with all other android_* functions.
Exit codes:
- `0` — screenshot captured successfully.
- `1` — missing output path, screencap produced empty file, or adb error.
- `3` — no device/emulator connected (propagated from `adb_pick_serial`).
The emptiness check (`! -s`) handles the case where `adb exec-out` exits 0 but writes
zero bytes (e.g. device locked, screencap permission denied). In that case the file is
removed and exit 1 is returned.
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# android_screenshot — Capture screen as PNG via adb exec-out screencap -p.
android_screenshot() {
local SCRIPT_DIR
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=bash/functions/infra/adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh" || return 1
# Resolve serial, consume --serial from args
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
local output="${1:-}"
if [[ -z "$output" ]]; then
echo "android_screenshot: output_path is required." >&2
return 1
fi
# Ensure parent directory exists
mkdir -p "$(dirname "$output")"
# Capture screen
adb_s "$serial" exec-out screencap -p > "$output"
# Verify file created and non-empty
if [[ ! -f "$output" ]] || [[ ! -s "$output" ]]; then
rm -f "$output"
echo "android_screenshot: screencap produced empty or missing file." >&2
return 1
fi
local size
size=$(stat -c%s "$output" 2>/dev/null || stat -f%z "$output" 2>/dev/null)
echo "screenshot: $output ($size bytes) from $serial"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_screenshot "$@"
fi
+43
View File
@@ -0,0 +1,43 @@
---
name: android_shell
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "android_shell([--serial <S>], cmd ...args)"
description: "Execute arbitrary shell command on Android device. Multi-emulator via --serial."
tags: [android, adb, shell, exec]
params:
- name: "--serial <S>"
desc: "Optional target device serial. Omit to auto-pick (single device) or use ADB_SERIAL env."
- name: "cmd ...args"
desc: "Shell command + args to run on device. Variadic."
output: "Passthrough stdout/stderr de adb shell. Exit code = shell command exit."
uses_functions: ["adb_wsl_bash_infra"]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/android_shell.sh"
notes: "Para comandos complejos con pipes/redirects mejor `adb_s $serial shell 'cmd | other'` directo via adb_run."
---
## Ejemplo
```bash
android_shell pm list packages
android_shell --serial emulator-5554 getprop ro.product.model
android_shell df -h /sdcard
android_shell ls -la /data/local/tmp
```
## Notas
Sourcea `adb_wsl.sh` para resolver `adb_pick_serial` (maneja `--serial`, `ADB_SERIAL`, y auto-detect de dispositivo unico) y `adb_s` (wrapper de `adb -s`). El array `ADB_PICK_REST` contiene los args restantes tras consumir `--serial`.
Para comandos con pipes o redirects que bash interpretaria localmente, mejor pasar como string unico: `adb_s "$serial" shell 'cmd | grep foo'`.
+18
View File
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
# android_shell — Execute arbitrary shell command on Android device via adb shell
# shellcheck source=./adb_wsl.sh
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/adb_wsl.sh"
android_shell() {
adb_pick_serial "$@" || exit 3
local serial="$ADB_PICK_SERIAL"
set -- "${ADB_PICK_REST[@]}"
adb_s "$serial" shell "$@"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
android_shell "$@"
fi
@@ -0,0 +1,65 @@
---
name: build_wasm_cpp_app
kind: function
lang: bash
domain: infra
version: "0.1.0"
purity: impure
signature: "build_wasm_cpp_app(app_name: string, [--no-budget-check]) -> void"
description: "Compila una app C++ del registry (cpp/apps/<name>) a WASM via emscripten. Sale build/wasm/<name>/<name>.{html,js,wasm,wasm.gz}. Falla si gzip > 2 MB."
tags: [wasm, emscripten, cpp, build, gamedev]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
example: "bash bash/functions/infra/build_wasm_cpp_app.sh engine_smoke"
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/build_wasm_cpp_app.sh"
params:
- name: app_name
desc: "Nombre del directorio bajo cpp/apps/. Debe contener CMakeLists.txt self-sufficient (top-level project) con guard `if(EMSCRIPTEN)` para flags wasm."
- name: "--no-budget-check"
desc: "Opcional. Salta verificacion de tamaño (gzip < 2 MB hard, < 1.5 MB soft)."
output: "Reporte de tamaños en stdout. Crea build/wasm/<app>/<app>.html/.js/.wasm/.wasm.gz. Exit 3 si excede budget hard."
---
# build_wasm_cpp_app
Compila apps C++ del registry a WebAssembly. Issue 0072d (parte del stack gamedev).
## Requisitos
- `emsdk` instalado y activo en el shell, o presente en `<repo>/emsdk/` (autoactiva).
- `cpp/apps/<app>/CMakeLists.txt` con bloque `if(EMSCRIPTEN) ... endif()` que define los flags wasm (USE_WEBGL2, FULL_ES3, ALLOW_MEMORY_GROWTH, etc.).
- `cpp/CMakeLists.txt` debe seguir tolerando configuracion via `emcmake`. La app target se elige con `cmake --build $BUILD_DIR --target <app>`.
## Flujo
1. Localiza `emcc` en PATH o autoactiva `<repo>/emsdk/emsdk_env.sh`.
2. `emcmake cmake -S cpp -B build/wasm/<app> -DCMAKE_BUILD_TYPE=MinSizeRel`
3. `cmake --build build/wasm/<app> --target <app> -j`
4. `gzip -9 -k <app>.wasm` y `brotli -q 11 -k <app>.wasm` (si brotli disponible).
5. Reporta tamaños y compara contra budget (1.5 MB gzip soft, 2 MB hard).
## Budgets
| Limite | Valor | Comportamiento |
|---|---|---|
| Soft | 1.5 MB gzip | Warning, sigue |
| Hard | 2 MB gzip | Exit 3, falla |
Skip con `--no-budget-check`.
## Apps soportadas
Cualquier app bajo `cpp/apps/<name>/` cuyo `CMakeLists.txt` defina target con flags emscripten. Probada con: `engine_smoke` (issue 0072a).
## Errores comunes
- `emcc no encontrado` → instalar emsdk segun instrucciones del propio script.
- `<app>.wasm no encontrado` → fallo de build. Re-ejecutar con `2>&1 | tee` para ver compiler errors.
- `wasm.gz excede budget` → revisar bloat, usar `twiggy top` o `wasm-objdump -h`. Ver issue 0072d.
+83
View File
@@ -0,0 +1,83 @@
#!/usr/bin/env bash
# build_wasm_cpp_app — compila app cpp/apps/<name> a WASM via emscripten.
#
# Uso:
# build_wasm_cpp_app.sh <app_name> [--no-budget-check]
#
# Salida: build/wasm/<name>/<name>.{html,js,wasm}
# + <name>.wasm.gz (gzip -9) y <name>.wasm.br (brotli -11 si esta).
#
# Requiere: emsdk activo en el shell (source emsdk/emsdk_env.sh) o que
# exista emsdk/ en la raiz del repo y se autoactive.
set -euo pipefail
APP="${1:?Uso: $0 <app_name> [--no-budget-check]}"
SHIFT_FLAG="${2:-}"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"/../../.. && pwd)"
SRC_DIR="$REPO_ROOT/cpp/apps/$APP"
BUILD_DIR="$REPO_ROOT/build/wasm/$APP"
if [ ! -d "$SRC_DIR" ]; then
echo "ERROR: $SRC_DIR no existe" >&2
exit 1
fi
# Activate emsdk if not already in PATH.
if ! command -v emcc >/dev/null 2>&1; then
if [ -f "$REPO_ROOT/emsdk/emsdk_env.sh" ]; then
# shellcheck disable=SC1091
source "$REPO_ROOT/emsdk/emsdk_env.sh" >/dev/null 2>&1
fi
fi
if ! command -v emcc >/dev/null 2>&1; then
echo "ERROR: emcc no encontrado. Instala emsdk:" >&2
echo " git clone https://github.com/emscripten-core/emsdk.git" >&2
echo " cd emsdk && ./emsdk install latest && ./emsdk activate latest" >&2
echo " source ./emsdk_env.sh" >&2
exit 1
fi
echo "── emcc: $(emcc --version | head -n1)"
echo "── source: $SRC_DIR"
echo "── build: $BUILD_DIR"
mkdir -p "$BUILD_DIR"
# Build the app directly (NOT the full cpp/ tree). Each app's CMakeLists.txt
# is expected to be self-sufficient as top-level (issue 0072a pattern).
emcmake cmake -S "$SRC_DIR" -B "$BUILD_DIR" -DCMAKE_BUILD_TYPE=MinSizeRel
cmake --build "$BUILD_DIR" --target "$APP" -j
WASM_DIR=$(find "$BUILD_DIR" -name "$APP.wasm" -printf '%h\n' -quit 2>/dev/null || true)
if [ -z "$WASM_DIR" ]; then
echo "ERROR: no se encontro $APP.wasm en $BUILD_DIR" >&2
exit 2
fi
cd "$WASM_DIR"
gzip -9 -k -f "$APP.wasm"
if command -v brotli >/dev/null 2>&1; then
brotli -q 11 -k -f "$APP.wasm"
fi
echo
echo "── Sizes (in $WASM_DIR) ──"
for f in "$APP".html "$APP".js "$APP".wasm "$APP".wasm.gz "$APP".wasm.br; do
[ -f "$f" ] && printf "%-32s %10d bytes\n" "$f" "$(stat -c%s "$f")"
done
# Budget check (1.5 MB gzip soft, 2 MB hard)
if [ "$SHIFT_FLAG" != "--no-budget-check" ]; then
SIZE_GZ=$(stat -c%s "$APP.wasm.gz")
HARD=$((2 * 1024 * 1024))
SOFT=$((1572864)) # 1.5 MB
if [ "$SIZE_GZ" -gt "$HARD" ]; then
echo "$APP.wasm.gz = $SIZE_GZ bytes > $HARD (2 MB hard limit)" >&2
exit 3
elif [ "$SIZE_GZ" -gt "$SOFT" ]; then
echo "$APP.wasm.gz = $SIZE_GZ bytes > $SOFT (1.5 MB soft limit)"
else
echo "$APP.wasm.gz = $SIZE_GZ bytes within soft limit (1.5 MB)"
fi
fi
@@ -0,0 +1,44 @@
---
name: gradle_assemble_debug
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "gradle_assemble_debug(project_dir: string, module: string) -> string"
description: "Build APK debug de un modulo Android via gradlew assembleDebug."
tags: ["android", "gradle", "build", "apk"]
params:
- name: project_dir
desc: "Raiz del proyecto Gradle Android"
- name: module
desc: "Nombre del modulo Gradle. Default: app"
output: "Stdout con build log + ultima linea 'APK: <path>'. Exit 0 = build OK. Exit !=0 si fallo."
uses_functions: ["gradle_run_bash_infra"]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/gradle_assemble_debug.sh"
---
## Ejemplo
```bash
gradle_assemble_debug /path/to/MyApp
# APK: /path/to/MyApp/app/build/outputs/apk/debug/app-debug.apk
gradle_assemble_debug /path/to/MyApp mylibrary
# APK: /path/to/MyApp/mylibrary/build/outputs/apk/debug/mylibrary-debug.apk
```
## Notas
APK queda en <project>/<module>/build/outputs/apk/debug/. Variants flavor no soportados aun (anadir arg si surge).
Depende de `gradle_run_bash_infra` (`gradle_run.sh` en el mismo directorio), que debe existir y estar indexado antes de hacer `fn index` de esta funcion.
---
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# gradle_assemble_debug — Build APK debug de un modulo Android via gradlew assembleDebug.
set -euo pipefail
source "$(dirname "${BASH_SOURCE[0]}")/gradle_run.sh"
gradle_assemble_debug() {
local project_dir="$1"
local module="${2:-app}"
gradle_run "$project_dir" ":$module:assembleDebug"
local apk
apk=$(find "$project_dir/$module/build/outputs/apk/debug" -name "*.apk" | head -1)
echo "APK: $apk"
}
gradle_assemble_debug "$@"
+56
View File
@@ -0,0 +1,56 @@
---
name: gradle_clean
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "gradle_clean(project_dir: string) -> int"
description: "Limpia build artifacts de un proyecto Android (gradle clean + rm .gradle + rm build)."
tags: [android, gradle, clean, build]
params:
- name: project_dir
desc: "Raiz del proyecto Gradle"
output: "Stdout build log de gradle clean. Exit code = exit gradlew."
uses_functions: [gradle_run_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/gradle_clean.sh"
notes: "Util cuando builds incrementales se corrompen. NO borra .idea ni gradle.properties. Para reset total usar `rm -rf $project_dir/{.gradle,build,app/build}`."
---
## Ejemplo
```bash
# Como libreria
source bash/functions/infra/gradle_clean.sh
gradle_clean /path/to/MyApp
# Directo
bash bash/functions/infra/gradle_clean.sh /path/to/MyApp
```
## Comportamiento
1. Invoca `gradle_run "$project_dir" "clean"` — si falla, propaga su exit code y para.
2. `rm -rf "$project_dir/.gradle"` — best-effort (ignora si no existe).
3. `rm -rf "$project_dir/build"` — best-effort (ignora si no existe).
4. Retorna 0 siempre que gradle clean haya terminado OK.
## Notas
Source-able y ejecutable directo. Sourcear `gradle_run.sh` al inicio garantiza
que `JAVA_HOME` y `ANDROID_HOME` se resuelven con la misma logica que el resto
de las funciones gradle_*.
Los directorios `.gradle` (cache de dependencias y metadata del daemon) y
`build` (outputs compilados) son los principales responsables de builds
corrompidos. Su eliminacion fuerza una descarga/recompilacion completa en el
siguiente build.
---
+38
View File
@@ -0,0 +1,38 @@
#!/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
@@ -0,0 +1,46 @@
---
name: gradle_instrumented_test
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "gradle_instrumented_test(project_dir: string, module: string) -> int"
description: "Corre instrumented tests Compose en emulador/device Android conectado."
tags: ["android", "gradle", "test", "compose", "emulator"]
uses_functions: ["gradle_run_bash_infra", "adb_wsl_bash_infra"]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/gradle_instrumented_test.sh"
params:
- name: project_dir
desc: "Raiz del proyecto Gradle"
- name: module
desc: "Modulo. Default app"
output: "Stdout con resultados. Linea final 'REPORT: <path>'. Exit: 0=OK, 3=no device, otro=fallos tests."
notes: "Requiere emulador corriendo. Lanzar antes con android_emulator_start. connectedAndroidTest corre en TODOS los devices conectados."
---
## Ejemplo
```bash
# Correr instrumented tests del modulo app
gradle_instrumented_test /home/user/MyAndroidProject
# Correr instrumented tests de un modulo especifico
gradle_instrumented_test /home/user/MyAndroidProject feature_login
```
## Notas
Requiere emulador corriendo. Lanzar antes con android_emulator_start. connectedAndroidTest corre en TODOS los devices conectados.
El script verifica que haya al menos un emulador o device conectado antes de lanzar Gradle. Si no hay ninguno, imprime un mensaje descriptivo a stderr y sale con exit code 3, permitiendo al llamador distinguir "no device" de "tests fallaron".
La linea `REPORT: <path>` se imprime siempre al final (incluso si los tests fallan), para que el llamador pueda abrir el reporte HTML independientemente del resultado.
@@ -0,0 +1,31 @@
#!/usr/bin/env bash
# gradle_instrumented_test — corre instrumented tests Compose en emulador/device Android conectado
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/gradle_run.sh"
source "$SCRIPT_DIR/adb_wsl.sh"
gradle_instrumented_test() {
local project_dir="${1:?project_dir required}"
local module="${2:-app}"
# Verificar device o emulador conectado
local devices
devices=$(adb_run devices | tail -n +2 | grep -E "(emulator|device)$" || true)
if [[ -z "$devices" ]]; then
echo "no Android device/emulator connected. Run android_emulator_start first." >&2
return 3
fi
local exit_code=0
gradle_run "$project_dir" ":${module}:connectedDebugAndroidTest" || exit_code=$?
echo "REPORT: ${project_dir}/${module}/build/reports/androidTests/connected/index.html"
return "$exit_code"
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
gradle_instrumented_test "$@"
fi
+72
View File
@@ -0,0 +1,72 @@
---
name: gradle_run
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "gradle_run(project_dir: string, task...: string) -> int"
description: "Wrapper canonico para invocar gradlew Android en WSL2 con JDK 17 + ANDROID_HOME validados."
tags: [android, gradle, kotlin, build]
params:
- name: project_dir
desc: "Path absoluto al proyecto Gradle (debe contener gradlew)"
- name: task
desc: "Tarea(s) Gradle a ejecutar (ej. assembleDebug, :app:test). Variadic"
output: "Stdout/stderr del build Gradle. Exit code = exit code de gradlew. Exit 1 si JDK17 missing, exit 2 si no hay gradlew."
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/gradle_run.sh"
notes: "Las demas funciones gradle_* lo sourcean. Reutiliza patron de adb_wsl_bash_infra para ser source-able+ejecutable. Cubre tanto SDK Linux (~/Android/Sdk via install_android_sdk) como SDK Windows (/mnt/c/...) montado en WSL."
---
## Ejemplo
```bash
# Como libreria (en otro script gradle_*)
source "$(dirname "${BASH_SOURCE[0]}")/gradle_run.sh"
gradle_run "$project_dir" assembleDebug
# Directo
bash bash/functions/infra/gradle_run.sh /path/to/MyApp assembleDebug
bash bash/functions/infra/gradle_run.sh /path/to/MyApp :app:test :app:lint
```
## Comportamiento de resolucion
### JAVA_HOME
Si no esta fijado en el entorno, busca en orden:
1. `/usr/lib/jvm/java-17-openjdk-amd64`
2. `/usr/lib/jvm/temurin-17-jdk-amd64`
3. `/opt/android-studio-jbr/jbr`
Si ninguno existe → error en stderr y `return 1`.
### ANDROID_HOME
Si no esta fijado:
1. Intenta `$HOME/Android/Sdk` (SDK Linux via `install_android_sdk_bash_infra`)
2. Si no existe, intenta `$ANDROID_SDK_WIN` (SDK Windows montado en `/mnt/c/...`)
3. Si ninguno, lo deja vacio — gradle mostrara el error adecuado para builds JVM puros
## Exit codes
| Codigo | Significado |
|--------|-------------|
| 0 | Build exitoso |
| 1 | JDK 17 no encontrado |
| 2 | `./gradlew` no existe en `project_dir` |
| * | Exit code propagado de gradlew |
## Notas
Source-able y ejecutable directo. Al sourcear, el caller importa la funcion `gradle_run` sin ejecutarla. Al ejecutar directamente, delega `"$@"` a `gradle_run`.
No exporta `JAVA_HOME`/`ANDROID_HOME` al entorno del shell padre — los variables se pasan solo al subshell de gradlew para evitar contaminar el entorno.
---
+80
View File
@@ -0,0 +1,80 @@
#!/usr/bin/env bash
# gradle_run — Wrapper canonico para invocar gradlew Android en WSL2.
# Valida JDK 17 + ANDROID_HOME antes de delegar al wrapper del proyecto.
#
# Uso como libreria: source bash/functions/infra/gradle_run.sh
# Uso directo: bash bash/functions/infra/gradle_run.sh <project_dir> <task...>
# ---------------------------------------------------------------------------
# gradle_run <project_dir> <task...>
#
# Resuelve JAVA_HOME y ANDROID_HOME si no estan fijados, luego invoca
# ./gradlew con las tareas indicadas en el directorio del proyecto.
#
# Exits:
# 0 — gradlew completado con exito
# 1 — JDK 17 no encontrado
# 2 — ./gradlew no existe en project_dir
# * — exit code propagado de gradlew
# ---------------------------------------------------------------------------
gradle_run() {
local project_dir="$1"
shift || true
# ---- Resolver JAVA_HOME ------------------------------------------------
local java_home="${JAVA_HOME:-}"
if [[ -z "$java_home" ]]; then
local _jdk_candidates=(
"/usr/lib/jvm/java-17-openjdk-amd64"
"/usr/lib/jvm/temurin-17-jdk-amd64"
"/opt/android-studio-jbr/jbr"
)
for _candidate in "${_jdk_candidates[@]}"; do
if [[ -d "$_candidate" ]]; then
java_home="$_candidate"
break
fi
done
unset _jdk_candidates _candidate
fi
if [[ -z "$java_home" ]]; then
echo "gradle_run: JDK 17 not found, install via install_android_sdk" >&2
return 1
fi
# ---- Resolver ANDROID_HOME ---------------------------------------------
local android_home="${ANDROID_HOME:-}"
if [[ -z "$android_home" ]]; then
local _default_linux="$HOME/Android/Sdk"
if [[ -d "$_default_linux" ]]; then
android_home="$_default_linux"
elif [[ -n "${ANDROID_SDK_WIN:-}" && -d "${ANDROID_SDK_WIN}" ]]; then
# SDK Windows montado en WSL via /mnt/c/...
android_home="${ANDROID_SDK_WIN}"
fi
unset _default_linux
fi
# ANDROID_HOME puede quedar vacio si no hay SDK instalado; gradle mostrara
# el error adecuado. No bloqueamos aqui para permitir builds puros JVM.
# ---- Verificar gradlew -------------------------------------------------
if [[ ! -f "${project_dir}/gradlew" ]]; then
echo "gradle_run: no gradlew in ${project_dir}" >&2
return 2
fi
# ---- Invocar gradlew ----------------------------------------------------
(
cd "$project_dir" || return 1
JAVA_HOME="$java_home" ANDROID_HOME="${android_home:-}" ./gradlew "$@"
)
}
# ---------------------------------------------------------------------------
# Ejecucion directa
# ---------------------------------------------------------------------------
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
gradle_run "$@"
fi
@@ -0,0 +1,58 @@
---
name: gradle_screenshot_test
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "gradle_screenshot_test(project_dir: string, module: string, flag: string) -> int"
description: "Corre screenshot tests Roborazzi de Composables (JVM, no necesita emulador)."
tags: [android, gradle, test, compose, roborazzi, screenshot]
params:
- name: project_dir
desc: "Raiz del proyecto Android (debe contener gradlew)"
- name: module
desc: "Modulo Gradle a testear. Default: app"
- name: --record
desc: "Re-grabar goldens en lugar de verificar"
output: "Stdout build log. Si verify y diff: linea 'DIFF: <path>'. Si record: linea 'RECORDED: <path>'. Exit 0 OK, 1 mismatch."
uses_functions: [gradle_run_bash_infra]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/gradle_screenshot_test.sh"
notes: "Roborazzi corre en JVM (Robolectric) — rapido, no necesita emulador. Goldens viven en src/test/snapshots/ y se commitean al repo. App debe declarar plugin io.github.takahirom.roborazzi en build.gradle.kts."
---
## Ejemplo
```bash
# Verificar screenshots (modo CI)
bash bash/functions/infra/gradle_screenshot_test.sh /path/to/MyApp
# Modulo no-default
bash bash/functions/infra/gradle_screenshot_test.sh /path/to/MyApp feature_login
# Re-grabar goldens tras cambio de UI intencional
bash bash/functions/infra/gradle_screenshot_test.sh /path/to/MyApp app --record
```
## Salida
| Situacion | Salida |
|-----------|--------|
| Verify OK | log de Gradle, exit 0 |
| Verify FAIL | log + `DIFF: <project>/<module>/build/outputs/roborazzi/`, exit 1 |
| Record OK | log + `RECORDED: <project>/<module>/src/test/snapshots/`, exit 0 |
## Notas
Source-able y ejecutable directo. Sourcear `gradle_run.sh` resuelve JAVA_HOME y ANDROID_HOME de forma identica al resto de funciones `gradle_*`.
Los diffs en `build/outputs/roborazzi/` muestran imagen original, imagen actual e imagen de diferencia. Util para revisar regresiones visuales antes de hacer `--record`.
---
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
# gradle_screenshot_test — Corre screenshot tests Roborazzi (JVM, no necesita emulador)
gradle_screenshot_test() {
local project_dir="${1:?project_dir requerido}"
local module="${2:-app}"
local record_flag="${3:-}"
local SCRIPT_DIR
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/gradle_run.sh"
local task
if [[ "$record_flag" == "--record" ]]; then
task=":${module}:recordRoborazziDebug"
else
task=":${module}:verifyRoborazziDebug"
fi
local goldens_dir="${project_dir}/${module}/src/test/snapshots"
local diff_dir="${project_dir}/${module}/build/outputs/roborazzi"
gradle_run "$project_dir" "$task"
local exit_code=$?
if [[ "$record_flag" == "--record" ]]; then
echo "RECORDED: ${goldens_dir}"
elif [[ $exit_code -ne 0 ]]; then
echo "DIFF: ${diff_dir}"
fi
return $exit_code
}
# Source-able y ejecutable directo
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
gradle_screenshot_test "$@"
fi
+66
View File
@@ -0,0 +1,66 @@
---
name: gradle_unit_test
kind: function
lang: bash
domain: infra
version: "1.0.0"
purity: impure
signature: "gradle_unit_test(project_dir: string, module: string, --variant <name>: string) -> int"
description: "Corre unit tests JVM de un modulo Android (no requiere emulador)."
tags: ["android", "gradle", "kotlin", "test", "junit"]
uses_functions: ["gradle_run_bash_infra"]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/infra/gradle_unit_test.sh"
params:
- name: project_dir
desc: "Raiz del proyecto Android con settings.gradle[.kts]."
- name: module
desc: "Modulo Gradle. Default: app"
- name: "--variant <name>"
desc: "Build variant (Debug|Release). Default Debug"
output: "Stdout con resultados JUnit. Linea final 'REPORT: <path_html>'. Exit code = test runner exit (0 OK, 1 fallos)."
notes: |
JVM only — Compose Composables que necesitan device se testean con
gradle_instrumented_test. Para tests Compose en JVM usar Roborazzi
(gradle_screenshot_test).
La funcion hace source de gradle_run.sh desde el mismo directorio
(bash/functions/infra/). La dependencia gradle_run_bash_infra debe
existir junto a este archivo.
---
## Ejemplo
```bash
source bash/functions/infra/gradle_unit_test.sh
# Tests del modulo app con variante Debug (por defecto)
gradle_unit_test /home/lucas/projects/myapp
# Tests del modulo :core con variante Release
gradle_unit_test /home/lucas/projects/myapp core --variant Release
# Verificar que paso
if gradle_unit_test /home/lucas/projects/myapp; then
echo "Todos los tests pasaron"
else
echo "Hay tests fallidos — revisar el report HTML"
fi
```
## Notas
JVM only — Compose Composables que necesitan device se testean con
`gradle_instrumented_test`. Para tests Compose en JVM usar Roborazzi
(`gradle_screenshot_test`).
El task ejecutado es `:$module:test${variant}UnitTest` (ej.
`:app:testDebugUnitTest`). El report HTML se imprime al final como
`REPORT: <path>` para facilitar parseo por agentes o scripts upstream.
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# gradle_unit_test — Corre unit tests JVM de un modulo Android (no requiere emulador)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/gradle_run.sh"
gradle_unit_test() {
local project_dir="$1"
local module="${2:-app}"
local variant="Debug"
# Parsear flag opcional --variant (consumir project_dir y module primero)
local nshift=$(( $# < 2 ? $# : 2 ))
shift "$nshift"
while [[ $# -gt 0 ]]; do
case "$1" in
--variant)
variant="$2"
shift 2
;;
*)
shift
;;
esac
done
if [[ -z "$project_dir" ]]; then
echo "gradle_unit_test: project_dir es obligatorio" >&2
return 1
fi
local task=":${module}:test${variant}UnitTest"
local report="${project_dir}/${module}/build/reports/tests/test${variant}UnitTest/index.html"
gradle_run "$project_dir" "$task"
local exit_code=$?
echo "REPORT: $report"
return $exit_code
}
@@ -0,0 +1,62 @@
---
name: deploy_capacitor_to_emulator
kind: pipeline
lang: bash
domain: pipelines
version: "1.0.0"
purity: impure
signature: "deploy_capacitor_to_emulator(app_dir: string, avd_name?: string, package_name?: string) -> void"
description: "Pipeline end-to-end: build Capacitor APK + arranca AVD + instala + opcionalmente lanza la app. Valida que el AVD existe, construye el APK con capacitor_build_apk, arranca el emulador de forma idempotente, instala el APK y lanza la app si se da package_name. Imprime comando logcat sugerido al final."
tags: [android, capacitor, emulator, deploy, launcher]
uses_functions:
- capacitor_build_apk_bash_pipelines
- android_emulator_list_bash_infra
- android_emulator_start_bash_infra
- android_apk_install_bash_infra
- android_logcat_bash_infra
- adb_wsl_bash_infra
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/pipelines/deploy_capacitor_to_emulator.sh"
params:
- name: app_dir
desc: "Path al directorio de la app Capacitor. Debe contener package.json y ser un proyecto web compatible con Capacitor. Puede ser relativo al cwd o absoluto."
- name: avd_name
desc: "Nombre del AVD Android a usar como destino de deploy. Default: Medium_Phone_API_35. Debe existir en la lista de AVDs del sistema."
- name: package_name
desc: "Package id Android de la app (ej: com.fnregistry.voiceguide). Opcional. Si se provee, lanza la app tras instalarla y muestra comando logcat sugerido."
output: "Stdout con pasos de cada fase. Exit 0 = APK instalado (y lanzado si se dio package_name) en el emulador. Exit != 0 si alguna fase falló (AVD no existe, build falla, emulador no arranca, install falla)."
---
## Ejemplo
```bash
# Build + deploy en AVD por defecto, sin lanzar app
bash bash/functions/pipelines/deploy_capacitor_to_emulator.sh apps/voice_guide
# Build + deploy + lanzar app
bash bash/functions/pipelines/deploy_capacitor_to_emulator.sh \
apps/voice_guide \
Medium_Phone_API_35 \
com.fnregistry.voiceguide
# Con AVD personalizado
bash bash/functions/pipelines/deploy_capacitor_to_emulator.sh \
apps/voice_guide \
Pixel_7_API_34 \
com.fnregistry.voiceguide
```
## Notas
- El APK se busca como `*.apk` en la raiz de `app_dir` tras el build (patron de `capacitor_build_apk`).
- El arranque del emulador es idempotente via `android_emulator_start`: si ya hay un emulador corriendo no lanza otro.
- El logcat no se sigue automaticamente (no bloqueante). Se imprime el comando para que el usuario lo ejecute si quiere.
- Requiere WSL2 + Android SDK instalado en Windows (usa `adb_wsl` para resolver `adb.exe`).
- La fase de build puede tardar varios minutos en la primera ejecucion (descarga de Gradle, Capacitor, etc.).
+162
View File
@@ -0,0 +1,162 @@
#!/usr/bin/env bash
# deploy_capacitor_to_emulator — Pipeline end-to-end: build Capacitor APK + arranca AVD + instala + lanza app.
#
# USO:
# deploy_capacitor_to_emulator.sh <app_dir> [avd_name] [package_name]
#
# ARGUMENTOS:
# app_dir Path al directorio de la app Capacitor (debe contener package.json). Obligatorio.
# avd_name Nombre del AVD a usar (default: Medium_Phone_API_35). Opcional.
# package_name Package id de la app Android (ej: com.fnregistry.voiceguide).
# Si se da, lanza la app tras instalar. Opcional.
#
# EJEMPLO:
# bash deploy_capacitor_to_emulator.sh apps/voice_guide Medium_Phone_API_35 com.fnregistry.voiceguide
#
# REQUISITOS:
# - Node.js + pnpm en PATH
# - Java 17+ en PATH
# - ANDROID_HOME seteado o ~/android-sdk/env.sh disponible
# - Emulator Windows accesible desde WSL2 (adb_wsl)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REGISTRY_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
# ---------------------------------------------------------------------------
# Parseo de argumentos
# ---------------------------------------------------------------------------
APP_DIR="${1:-}"
AVD_NAME="${2:-Medium_Phone_API_35}"
PACKAGE_NAME="${3:-}"
if [[ -z "$APP_DIR" ]]; then
echo "[deploy_capacitor_to_emulator] ERROR: app_dir es obligatorio." >&2
echo "USO: $0 <app_dir> [avd_name] [package_name]" >&2
exit 1
fi
# Resolver path absoluto de APP_DIR (puede venir relativo al cwd)
if [[ "$APP_DIR" != /* ]]; then
APP_DIR="$(pwd)/$APP_DIR"
fi
if [[ ! -d "$APP_DIR" ]]; then
echo "[deploy_capacitor_to_emulator] ERROR: directorio no existe: $APP_DIR" >&2
exit 1
fi
if [[ ! -f "$APP_DIR/package.json" ]]; then
echo "[deploy_capacitor_to_emulator] ERROR: no se encontró package.json en $APP_DIR" >&2
exit 1
fi
echo ""
echo "======================================================================"
echo " deploy_capacitor_to_emulator"
echo "======================================================================"
echo " app_dir : $APP_DIR"
echo " avd_name : $AVD_NAME"
echo " package_name : ${PACKAGE_NAME:-(no lanzar)}"
echo "======================================================================"
echo ""
# ---------------------------------------------------------------------------
# Source adb_wsl para tener helpers ADB y resolver $ADB
# ---------------------------------------------------------------------------
# shellcheck source=../infra/adb_wsl.sh
source "$REGISTRY_ROOT/bash/functions/infra/adb_wsl.sh"
# ---------------------------------------------------------------------------
# Fase 1: Verificar que el AVD existe
# ---------------------------------------------------------------------------
echo "[deploy_capacitor_to_emulator] [1/4] Verificando AVD '$AVD_NAME'..."
if ! bash "$REGISTRY_ROOT/bash/functions/infra/android_emulator_list.sh" | grep -qx "$AVD_NAME"; then
echo "[deploy_capacitor_to_emulator] ERROR: AVD '$AVD_NAME' no encontrado." >&2
echo " AVDs disponibles:" >&2
bash "$REGISTRY_ROOT/bash/functions/infra/android_emulator_list.sh" | sed 's/^/ /' >&2
exit 1
fi
echo "[deploy_capacitor_to_emulator] AVD '$AVD_NAME' existe."
# ---------------------------------------------------------------------------
# Fase 2: Build APK
# ---------------------------------------------------------------------------
echo ""
echo "[deploy_capacitor_to_emulator] [2/4] Construyendo APK desde $APP_DIR ..."
bash "$REGISTRY_ROOT/bash/functions/pipelines/capacitor_build_apk.sh" "$APP_DIR"
# Localizar APK generado: capacitor_build_apk copia el APK a $APP_DIR/<app_name>.apk
APK_PATH=""
while IFS= read -r -d '' candidate; do
APK_PATH="$candidate"
break
done < <(find "$APP_DIR" -maxdepth 1 -name "*.apk" -print0 2>/dev/null)
if [[ -z "$APK_PATH" ]]; then
echo "[deploy_capacitor_to_emulator] ERROR: No se encontró ningún .apk en $APP_DIR tras el build." >&2
exit 1
fi
echo "[deploy_capacitor_to_emulator] APK localizado: $APK_PATH"
# ---------------------------------------------------------------------------
# Fase 3: Arrancar emulador (idempotente)
# ---------------------------------------------------------------------------
echo ""
echo "[deploy_capacitor_to_emulator] [3/4] Arrancando emulador '$AVD_NAME' (idempotente)..."
bash "$REGISTRY_ROOT/bash/functions/infra/android_emulator_start.sh" "$AVD_NAME"
echo "[deploy_capacitor_to_emulator] Emulador listo."
# ---------------------------------------------------------------------------
# Fase 4: Instalar APK (y lanzar si se dio package_name)
# ---------------------------------------------------------------------------
echo ""
echo "[deploy_capacitor_to_emulator] [4/4] Instalando APK..."
# Source android_apk_install para usar su función
# shellcheck source=../infra/android_apk_install.sh
source "$REGISTRY_ROOT/bash/functions/infra/android_apk_install.sh"
android_apk_install "$APK_PATH" "$PACKAGE_NAME"
# ---------------------------------------------------------------------------
# Diagnóstico no-bloqueante: hint logcat
# ---------------------------------------------------------------------------
if [[ -n "$PACKAGE_NAME" ]]; then
echo ""
echo "[deploy_capacitor_to_emulator] Para seguir los logs de la app en tiempo real:"
echo " bash $REGISTRY_ROOT/bash/functions/infra/android_logcat.sh --package '$PACKAGE_NAME'"
echo ""
echo " O los últimos 50 mensajes:"
echo " bash $REGISTRY_ROOT/bash/functions/infra/android_logcat.sh --package '$PACKAGE_NAME' --lines 50"
fi
# ---------------------------------------------------------------------------
# Resumen
# ---------------------------------------------------------------------------
echo ""
echo "======================================================================"
echo " DEPLOY COMPLETADO"
echo "======================================================================"
echo " APK instalado : $APK_PATH"
echo " Emulador : $AVD_NAME"
if [[ -n "$PACKAGE_NAME" ]]; then
echo " App lanzada : $PACKAGE_NAME"
fi
echo "======================================================================"
echo ""
+74
View File
@@ -0,0 +1,74 @@
---
name: init_cpp_app
kind: pipeline
lang: bash
domain: pipelines
version: "0.1.0"
purity: impure
signature: "init_cpp_app(name: string, [--project <p>] [--domain <d>] [--desc <s>] [--tags <csv>]) -> void"
description: "Scaffolder estandar de apps C++ del registry. Genera main.cpp + CMakeLists.txt + app.md siguiendo el patron canonico (cfg.about/log/panels, sin app_menubar manual, dockspace via framework), registra la app en cpp/CMakeLists.txt, inicializa repo Gitea dataforge/<name> y ejecuta fn index."
tags: [cpp, imgui, scaffold, pipeline, bash, launcher]
uses_functions:
- ensure_repo_synced_bash_infra
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
params:
- name: name
desc: "nombre de la app (snake_case). Sera el id en registry.db y el repo dataforge/<name>"
- name: "--project"
desc: "proyecto bajo projects/ donde crear la app (opcional). Si se omite va a cpp/apps/<name>/"
- name: "--domain"
desc: "dominio del registry (default: tools)"
- name: "--desc"
desc: "descripcion breve (frontmatter description + cfg.about/cfg.title)"
- name: "--tags"
desc: "tags CSV adicionales para el frontmatter (siempre se anade 'imgui')"
output: "estructura completa de la app + entry registrada en cpp/CMakeLists.txt + repo Gitea + fn index"
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/pipelines/init_cpp_app.sh"
---
## Ejemplo
```bash
# App suelta en cpp/apps/<name>/
fn run init_cpp_app my_tool --desc "Herramienta para X"
# App dentro de un proyecto
fn run init_cpp_app finance_panel --project budget --desc "Panel de finanzas" --tags "finance,dashboard"
```
## Que genera
```
<dir>/
main.cpp # Plantilla canonica: panels[] + cfg.about + cfg.log + run_app(cfg, render)
CMakeLists.txt # add_imgui_app(<name> main.cpp)
app.md # Frontmatter completo (lang:cpp, framework:imgui, dir_path, repo_url)
```
Y ademas:
- Registra `add_subdirectory(apps/<name>)` (o el bloque `_DIR` para projects) en `cpp/CMakeLists.txt`.
- Crea repo Gitea `dataforge/<name>` con master + commit inicial via `ensure_repo_synced_bash_infra` (requiere `GITEA_URL` y `GITEA_TOKEN`).
- Ejecuta `fn index` para registrar la app en `registry.db`.
## Plantilla `main.cpp`
La plantilla cumple `cpp/PATTERNS.md`:
- NO llama `app_menubar` manual (lo dibuja el framework).
- NO llama `DockSpaceOverViewport` (auto_dockspace=true por defecto).
- Declara `panels[]` con un panel "Main" toggleable.
- Setea `cfg.about` (window About) y `cfg.log` (logger + ventana Logs).
## Despues de crear
1. Editar `app.md` y completar `uses_functions` cuando la app consuma funciones del registry.
2. Anadir las funciones del registry al `CMakeLists.txt` como paths absolutos: `${CMAKE_SOURCE_DIR}/functions/<dom>/<func>.cpp`.
3. Build: `cd cpp && cmake --build build --target <name> -j`.
+211
View File
@@ -0,0 +1,211 @@
#!/usr/bin/env bash
# init_cpp_app — Scaffolder estandar de apps C++ del registry.
#
# Genera la estructura canonica (main.cpp, CMakeLists.txt, app.md), registra
# la app en cpp/CMakeLists.txt, inicializa git + repo Gitea dataforge/<name>,
# y ejecuta fn index. La plantilla cumple cpp/PATTERNS.md y .claude/rules/cpp_apps.md.
#
# Uso:
# init_cpp_app <name> [--project <p>] [--domain <d>] [--desc "..."] [--tags "a,b"]
#
# Por defecto domain=tools, sin proyecto (cpp/apps/<name>/).
set -euo pipefail
# Carga helpers del registry
FN_ROOT="${FN_REGISTRY_ROOT:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}"
# shellcheck source=/dev/null
source "$FN_ROOT/bash/functions/infra/ensure_repo_synced.sh"
init_cpp_app() {
local name=""
local project=""
local domain="tools"
local desc=""
local tags=""
while [[ $# -gt 0 ]]; do
case "$1" in
--project) project="$2"; shift 2 ;;
--domain) domain="$2"; shift 2 ;;
--desc) desc="$2"; shift 2 ;;
--tags) tags="$2"; shift 2 ;;
-*) echo "init_cpp_app: flag desconocido: $1" >&2; return 2 ;;
*) if [[ -z "$name" ]]; then name="$1"; else
echo "init_cpp_app: argumento extra: $1" >&2; return 2
fi
shift ;;
esac
done
if [[ -z "$name" ]]; then
echo "init_cpp_app: se requiere <name>" >&2
echo "Uso: init_cpp_app <name> [--project <p>] [--domain <d>] [--desc \"...\"] [--tags \"a,b\"]" >&2
return 2
fi
[[ -z "$desc" ]] && desc="App C++ del registry"
# Resolver dir destino
local rel_dir abs_dir
if [[ -n "$project" ]]; then
if [[ ! -f "$FN_ROOT/projects/$project/project.md" ]]; then
echo "init_cpp_app: proyecto '$project' no existe (falta projects/$project/project.md)" >&2
return 1
fi
rel_dir="projects/$project/apps/$name"
else
rel_dir="cpp/apps/$name"
fi
abs_dir="$FN_ROOT/$rel_dir"
if [[ -e "$abs_dir" ]]; then
echo "init_cpp_app: $rel_dir ya existe" >&2
return 1
fi
mkdir -p "$abs_dir"
# ---------- main.cpp ----------
cat > "$abs_dir/main.cpp" <<EOF
#include <imgui.h>
#include "framework/app_base.h"
#include "core/icons_tabler.h"
#include "core/logger.h"
// Toggles de paneles (visibles desde el menu View del menubar canonico)
static bool g_show_main = true;
static void draw_main() {
if (!ImGui::Begin(TI_HOME " Main", &g_show_main)) {
ImGui::End();
return;
}
ImGui::TextUnformatted("Hello from $name");
ImGui::End();
}
static void render() {
// El framework dibuja menubar (View/Layouts/Settings/About) y un
// DockSpaceOverViewport central (auto_dockspace=true por defecto).
// Aqui solo se dibujan los paneles propios de la app.
if (g_show_main) draw_main();
}
int main(int /*argc*/, char** /*argv*/) {
static fn_ui::PanelToggle panels[] = {
{ "Main", nullptr, &g_show_main },
};
fn::AppConfig cfg;
cfg.title = "$name — $desc";
cfg.about = { "$name", "0.1.0", "$desc" };
cfg.log = { "$name.log", 1 };
cfg.panels = panels;
cfg.panel_count = sizeof(panels) / sizeof(panels[0]);
return fn::run_app(cfg, render);
}
EOF
# ---------- CMakeLists.txt ----------
cat > "$abs_dir/CMakeLists.txt" <<EOF
add_imgui_app($name
main.cpp
)
target_include_directories($name PRIVATE \${CMAKE_CURRENT_SOURCE_DIR})
if(WIN32)
set_target_properties($name PROPERTIES WIN32_EXECUTABLE TRUE)
endif()
EOF
# ---------- app.md ----------
local repo_url="https://gitea.organic-machine.com/dataforge/$name"
local tags_yaml="[imgui]"
if [[ -n "$tags" ]]; then
# Convierte "a,b,c" -> "[a, b, c]"
tags_yaml="[$(echo "$tags" | sed 's/,/, /g'), imgui]"
fi
cat > "$abs_dir/app.md" <<EOF
---
name: $name
lang: cpp
domain: $domain
description: "$desc"
tags: $tags_yaml
uses_functions: []
uses_types: []
framework: "imgui"
entry_point: "main.cpp"
dir_path: "$rel_dir"
repo_url: "$repo_url"
---
# $name
$desc
## Build
\`\`\`bash
cd cpp && cmake --build build --target $name -j
\`\`\`
## Run
\`\`\`bash
./cpp/build/$name
\`\`\`
EOF
# ---------- Registrar en cpp/CMakeLists.txt ----------
local cpp_cmake="$FN_ROOT/cpp/CMakeLists.txt"
if ! grep -q "# --- $name ---" "$cpp_cmake"; then
if [[ -n "$project" ]]; then
local upper
upper="$(echo "$name" | tr '[:lower:]' '[:upper:]')"
cat >> "$cpp_cmake" <<EOF
# --- $name (lives in projects/$project/apps/) ---
set(_${upper}_DIR \${CMAKE_SOURCE_DIR}/../projects/$project/apps/$name)
if(EXISTS \${_${upper}_DIR}/CMakeLists.txt)
add_subdirectory(\${_${upper}_DIR} \${CMAKE_BINARY_DIR}/apps/$name)
endif()
EOF
else
cat >> "$cpp_cmake" <<EOF
# --- $name ---
if(EXISTS \${CMAKE_CURRENT_SOURCE_DIR}/apps/$name/CMakeLists.txt)
add_subdirectory(apps/$name)
endif()
EOF
fi
fi
# ---------- Git + Gitea ----------
if [[ -n "${GITEA_URL:-}" && -n "${GITEA_TOKEN:-}" ]]; then
ensure_repo_synced "$abs_dir" "dataforge" "$name" "master" "feat: scaffold $name via init_cpp_app" || \
echo "init_cpp_app: warning — ensure_repo_synced fallo, repo no creado" >&2
else
echo "init_cpp_app: GITEA_URL/GITEA_TOKEN no seteados, omitiendo creacion de repo Gitea" >&2
(cd "$abs_dir" && git init -b master >/dev/null 2>&1 || git init >/dev/null 2>&1)
fi
# ---------- fn index ----------
if [[ -x "$FN_ROOT/fn" ]]; then
(cd "$FN_ROOT" && ./fn index >/dev/null 2>&1) || \
echo "init_cpp_app: warning — fn index fallo" >&2
fi
echo "init_cpp_app: $rel_dir creada"
echo " - Build: cd $FN_ROOT/cpp && cmake --build build --target $name -j"
echo " - app.md: $rel_dir/app.md (rellena uses_functions cuando uses funciones del registry)"
}
# Permitir invocacion directa via 'fn run init_cpp_app ...'
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
init_cpp_app "$@"
fi
+117
View File
@@ -0,0 +1,117 @@
---
name: init_kotlin_app
kind: pipeline
lang: bash
domain: pipelines
version: "1.0.0"
purity: impure
signature: "init_kotlin_app(name: string, [--project <p>] [--desc <s>] [--tags <csv>] [--package <id>]) -> void"
description: "Scaffolder canonico de app Android Kotlin Compose con FnTheme + Roborazzi tests + e2e_checks declarados. Mirror exacto del patron init_cpp_app para el stack Kotlin."
tags: [android, kotlin, compose, scaffolder, launcher]
uses_functions:
- ensure_repo_synced_bash_infra
- gradle_run_bash_infra
- gradle_assemble_debug_bash_infra
- gradle_unit_test_bash_infra
- gradle_screenshot_test_bash_infra
- fn_theme_kt_ui
- fn_tokens_kt_ui
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
params:
- name: name
desc: "nombre de la app en snake_case. Sera el id en registry.db y el repo dataforge/<name>"
- name: "--project"
desc: "proyecto bajo projects/ donde crear la app (opcional). Si se omite va a apps/<name>/. El project.md debe existir"
- name: "--desc"
desc: "descripcion breve para el frontmatter app.md (default: 'App Android Kotlin Compose')"
- name: "--tags"
desc: "tags CSV adicionales para el frontmatter (siempre se anaden kotlin, compose, android)"
- name: "--package"
desc: "application id Android (default: com.fnregistry.<name>). Ej: com.aurgi.scanner"
output: "Stdout con pasos y archivos creados. Exit 0 = scaffold completo y ready para build. Exit !=0 si falla validacion (nombre invalido, destino existe, proyecto inexistente) o git init."
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/pipelines/init_kotlin_app.sh"
---
## Ejemplo
```bash
# App suelta en apps/<name>/
fn run init_kotlin_app my_scanner --desc "Escaner de documentos" --package "com.aurgi.scanner"
# App dentro de un proyecto
fn run init_kotlin_app expense_tracker --project budget --desc "Tracker de gastos" --tags "finance,mobile"
# Con package id personalizado
fn run init_kotlin_app pos_terminal --package "com.aurgi.pos" --desc "Terminal de punto de venta"
```
## Que genera
```
apps/<name>/ (o projects/<p>/apps/<name>/)
├── settings.gradle.kts # rootProject + include(:app) + composite build ui
├── build.gradle.kts # top-level (apply false)
├── app/build.gradle.kts # Compose + Material3 + Roborazzi + tests
├── gradle.properties
├── gradlew # stub ejecutable
├── gradle/wrapper/gradle-wrapper.properties # Gradle 8.6
├── app/
│ └── src/
│ ├── main/
│ │ ├── AndroidManifest.xml # activity con LAUNCHER intent
│ │ ├── kotlin/<pkg_path>/
│ │ │ └── MainActivity.kt # FnTheme + Surface + Text("<name> ready")
│ │ └── res/values/strings.xml
│ ├── test/kotlin/<pkg_path>/
│ │ └── ExampleScreenshotTest.kt # Roborazzi: captura FnTheme + Surface
│ └── androidTest/kotlin/<pkg_path>/
│ └── MainActivityTest.kt # Compose ui-test: assertIsDisplayed("<name> ready")
├── app.md # frontmatter registry (lang:kt, framework:compose)
├── .gitignore
└── README.md
```
## Composite build
La app apunta via `includeBuild` a `kotlin/functions/ui` del registry (FnTheme + FnTokens).
El path relativo se calcula automaticamente:
| Ubicacion | Path al composite |
|---|---|
| `apps/<name>/` | `../../kotlin/functions/ui` |
| `projects/<p>/apps/<name>/` | `../../../../kotlin/functions/ui` |
La dependencia se declara como `implementation("fn.compose:ui")` en `app/build.gradle.kts`.
## e2e_checks generados
| id | cmd | timeout |
|---|---|---|
| `unit` | `fn run gradle_unit_test_bash_infra <dir>` | 240s |
| `screenshot` | `fn run gradle_screenshot_test_bash_infra <dir>` | 240s |
| `build` | `fn run gradle_assemble_debug_bash_infra <dir>` | 360s |
| `emu_start` | `fn run android_emulator_start_bash_infra Medium_Phone_API_35` | 240s |
| `instrumented` | `fn run gradle_instrumented_test_bash_infra <dir>` | 600s |
| `emu_stop` | `fn run android_emulator_stop_bash_infra` | 30s (warning) |
## Despues de crear
1. Completar `uses_functions` en `app.md` cuando la app consuma funciones adicionales del registry.
2. Para tests Roborazzi: los snapshots se generan en `app/src/test/snapshots/images/` (gitignored).
Para actualizar golden images: `./gradlew recordRoborazziDebug`.
3. Para tests instrumentados se necesita un AVD creado con `avdmanager create avd -n Medium_Phone_API_35 ...`.
## Notas
- `fn_theme_kt_ui` y `fn_tokens_kt_ui` se referencian en `uses_functions` del `app.md` generado
aunque todavia no esten indexados en registry.db — son los modulos del composite build.
- `ensure_repo_synced_bash_infra` requiere `GITEA_URL` y `GITEA_TOKEN`. Sin ellos se hace
`git init + git commit` local y se avisa al usuario.
- Si `--project <p>` se especifica, se ejecuta `fn index` al final para registrar la nueva app.
+531
View File
@@ -0,0 +1,531 @@
#!/usr/bin/env bash
# init_kotlin_app — Scaffolder canonico de apps Android Kotlin Compose del registry.
#
# Genera la estructura canonica (MainActivity.kt, build.gradle.kts, app.md,
# Roborazzi screenshot tests), apuntando al composite build kotlin/functions/ui
# para FnTheme + FnTokens, inicializa git + repo Gitea dataforge/<name>.
#
# Uso:
# init_kotlin_app <name> [--project <p>] [--desc "..."] [--tags "a,b"] [--package <com.foo.bar>]
#
# Por defecto sin proyecto (apps/<name>/), package = com.fnregistry.<name>.
set -euo pipefail
# Carga helpers del registry
FN_ROOT="${FN_REGISTRY_ROOT:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}"
# shellcheck source=/dev/null
source "$FN_ROOT/bash/functions/infra/ensure_repo_synced.sh"
init_kotlin_app() {
local name=""
local project=""
local desc=""
local tags=""
local pkg_id=""
while [[ $# -gt 0 ]]; do
case "$1" in
--project) project="$2"; shift 2 ;;
--desc) desc="$2"; shift 2 ;;
--tags) tags="$2"; shift 2 ;;
--package) pkg_id="$2"; shift 2 ;;
-*) echo "init_kotlin_app: flag desconocido: $1" >&2; return 2 ;;
*) if [[ -z "$name" ]]; then name="$1"; else
echo "init_kotlin_app: argumento extra: $1" >&2; return 2
fi
shift ;;
esac
done
if [[ -z "$name" ]]; then
echo "init_kotlin_app: se requiere <name>" >&2
echo "Uso: init_kotlin_app <name> [--project <p>] [--desc \"...\"] [--tags \"a,b\"] [--package <com.foo.bar>]" >&2
return 2
fi
# Validar snake_case
if [[ ! "$name" =~ ^[a-z][a-z0-9_]*$ ]]; then
echo "init_kotlin_app: nombre '$name' debe ser snake_case (solo letras minusculas, digitos y _)" >&2
return 2
fi
[[ -z "$desc" ]] && desc="App Android Kotlin Compose"
[[ -z "$pkg_id" ]] && pkg_id="com.fnregistry.$name"
# Resolver dir destino
local rel_dir abs_dir
if [[ -n "$project" ]]; then
if [[ ! -f "$FN_ROOT/projects/$project/project.md" ]]; then
echo "init_kotlin_app: proyecto '$project' no existe (falta projects/$project/project.md)" >&2
return 1
fi
rel_dir="projects/$project/apps/$name"
else
rel_dir="apps/$name"
fi
abs_dir="$FN_ROOT/$rel_dir"
if [[ -e "$abs_dir" ]]; then
echo "init_kotlin_app: $rel_dir ya existe" >&2
return 1
fi
# Convertir package id a path (com.fnregistry.my_app -> com/fnregistry/my_app)
local pkg_path
pkg_path="$(echo "$pkg_id" | tr '.' '/')"
# Calcular path relativo al composite build de kotlin/functions/ui
# Desde apps/<name>/ -> ../../kotlin/functions/ui
# Desde projects/<p>/apps/<n> -> ../../../../kotlin/functions/ui
local ui_rel_path
if [[ -n "$project" ]]; then
ui_rel_path="../../../../kotlin/functions/ui"
else
ui_rel_path="../../kotlin/functions/ui"
fi
# ---- Crear estructura de directorios ----
mkdir -p "$abs_dir/app/src/main/kotlin/$pkg_path"
mkdir -p "$abs_dir/app/src/main/res/values"
mkdir -p "$abs_dir/app/src/test/kotlin/$pkg_path"
mkdir -p "$abs_dir/app/src/androidTest/kotlin/$pkg_path"
mkdir -p "$abs_dir/gradle/wrapper"
echo "init_kotlin_app: creando $rel_dir ..."
# ---- settings.gradle.kts ----
cat > "$abs_dir/settings.gradle.kts" <<EOF
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "$name"
include(":app")
// Composite build: FnTheme + FnTokens desde el registry
includeBuild("$ui_rel_path") {
dependencySubstitution {
substitute(module("fn.compose:ui")).using(project(":"))
}
}
EOF
# ---- build.gradle.kts (raiz) ----
cat > "$abs_dir/build.gradle.kts" <<'EOF'
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "8.4.0" apply false
id("org.jetbrains.kotlin.android") version "1.9.22" apply false
}
EOF
# ---- app/build.gradle.kts ----
cat > "$abs_dir/app/build.gradle.kts" <<EOF
plugins {
id("com.android.application") version "8.4.0"
id("org.jetbrains.kotlin.android") version "1.9.22"
id("io.github.takahirom.roborazzi") version "1.20.0"
}
android {
namespace = "$pkg_id"
compileSdk = 34
defaultConfig {
applicationId = "$pkg_id"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "0.1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.8"
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
testOptions {
unitTests {
isIncludeAndroidResources = true
all {
it.systemProperty("robolectric.graphicsMode", "NATIVE")
}
}
}
}
dependencies {
implementation("androidx.activity:activity-compose:1.8.2")
implementation(platform("androidx.compose:compose-bom:2024.02.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.ui:ui-tooling-preview")
debugImplementation("androidx.compose.ui:ui-tooling")
// FnTheme + FnTokens via composite build
implementation("fn.compose:ui")
testImplementation("junit:junit:4.13.2")
testImplementation("org.robolectric:robolectric:4.11.1")
testImplementation("androidx.compose.ui:ui-test-junit4")
testImplementation("io.github.takahirom.roborazzi:roborazzi:1.20.0")
testImplementation("io.github.takahirom.roborazzi:roborazzi-compose:1.20.0")
testImplementation("io.github.takahirom.roborazzi:roborazzi-junit-rule:1.20.0")
androidTestImplementation(platform("androidx.compose:compose-bom:2024.02.00"))
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}
EOF
# ---- gradle.properties ----
cat > "$abs_dir/gradle.properties" <<'EOF'
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
kotlin.code.style=official
android.nonTransitiveRClass=true
EOF
# ---- gradle/wrapper/gradle-wrapper.properties ----
cat > "$abs_dir/gradle/wrapper/gradle-wrapper.properties" <<'EOF'
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
EOF
# ---- gradlew + wrapper jar (vendored real wrapper) ----
local tmpl_wrapper="$FN_ROOT/bash/functions/pipelines/templates/kotlin/wrapper"
if [[ -f "$tmpl_wrapper/gradlew" && -f "$tmpl_wrapper/gradle-wrapper.jar" ]]; then
cp "$tmpl_wrapper/gradlew" "$abs_dir/gradlew"
cp "$tmpl_wrapper/gradle-wrapper.jar" "$abs_dir/gradle/wrapper/gradle-wrapper.jar"
chmod +x "$abs_dir/gradlew"
else
echo "init_kotlin_app: WARN templates/kotlin/wrapper missing, fallback gradlew stub"
cat > "$abs_dir/gradlew" <<'EOF'
#!/usr/bin/env bash
echo "gradlew stub — install gradle wrapper or replace with real one" >&2
exit 2
EOF
chmod +x "$abs_dir/gradlew"
fi
# ---- local.properties (Android SDK location, gitignored, per-machine) ----
local sdk_path="${ANDROID_SDK_DIR:-$HOME/android-sdk}"
if [[ ! -d "$sdk_path" ]] && [[ -d "$HOME/Android/Sdk" ]]; then
sdk_path="$HOME/Android/Sdk"
fi
cat > "$abs_dir/local.properties" <<EOF
# Auto-generated by init_kotlin_app. Per-machine, gitignored.
sdk.dir=$sdk_path
EOF
# ---- AndroidManifest.xml ----
cat > "$abs_dir/app/src/main/AndroidManifest.xml" <<EOF
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.Material.Light.NoActionBar">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
EOF
# ---- res/values/strings.xml ----
cat > "$abs_dir/app/src/main/res/values/strings.xml" <<EOF
<resources>
<string name="app_name">$name</string>
</resources>
EOF
# ---- MainActivity.kt ----
cat > "$abs_dir/app/src/main/kotlin/$pkg_path/MainActivity.kt" <<EOF
package $pkg_id
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import fn.compose.theme.FnTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
FnTheme {
Surface(modifier = Modifier.fillMaxSize()) {
Text("$name ready")
}
}
}
}
}
EOF
# ---- ExampleScreenshotTest.kt (Roborazzi) ----
cat > "$abs_dir/app/src/test/kotlin/$pkg_path/ExampleScreenshotTest.kt" <<EOF
package $pkg_id
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onRoot
import com.github.takahirom.roborazzi.captureRoboImage
import fn.compose.theme.FnTheme
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.GraphicsMode
@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class ExampleScreenshotTest {
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun screenshotFnThemeSurface() {
composeTestRule.setContent {
FnTheme {
Surface {
Text("$name screenshot")
}
}
}
composeTestRule.onRoot().captureRoboImage("src/test/snapshots/images/${name}_smoke.png")
}
}
EOF
# ---- MainActivityTest.kt (instrumented) ----
cat > "$abs_dir/app/src/androidTest/kotlin/$pkg_path/MainActivityTest.kt" <<EOF
package $pkg_id
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class MainActivityTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
@Test
fun appLaunchesAndShowsReadyText() {
composeTestRule
.onNodeWithText("$name ready")
.assertIsDisplayed()
}
}
EOF
# ---- app.md frontmatter ----
local repo_url="https://gitea.organic-machine.com/dataforge/$name"
local tags_yaml="[kotlin, compose, android]"
if [[ -n "$tags" ]]; then
tags_yaml="[$(echo "$tags" | sed 's/,/, /g'), kotlin, compose, android]"
fi
cat > "$abs_dir/app.md" <<EOF
---
name: $name
domain: tools
description: "$desc"
tags: $tags_yaml
lang: kt
framework: compose
entry_point: "app/src/main/kotlin/$pkg_path/MainActivity.kt"
dir_path: "$rel_dir"
repo_url: "$repo_url"
uses_functions:
- fn_theme_kt_ui
- fn_tokens_kt_ui
uses_types: []
e2e_checks:
- id: unit
cmd: "fn run gradle_unit_test_bash_infra $rel_dir"
timeout_s: 240
- id: screenshot
cmd: "fn run gradle_screenshot_test_bash_infra $rel_dir"
timeout_s: 240
- id: build
cmd: "fn run gradle_assemble_debug_bash_infra $rel_dir"
timeout_s: 360
- id: emu_start
cmd: "fn run android_emulator_start_bash_infra Medium_Phone_API_35"
timeout_s: 240
- id: instrumented
cmd: "fn run gradle_instrumented_test_bash_infra $rel_dir"
timeout_s: 600
- id: emu_stop
cmd: "fn run android_emulator_stop_bash_infra"
severity: warning
timeout_s: 30
---
# $name
$desc
## Build
\`\`\`bash
fn run gradle_assemble_debug_bash_infra $rel_dir
\`\`\`
## Tests unitarios + Roborazzi screenshots
\`\`\`bash
fn run gradle_unit_test_bash_infra $rel_dir
fn run gradle_screenshot_test_bash_infra $rel_dir
\`\`\`
## Tests instrumentados (requiere emulador)
\`\`\`bash
fn run android_emulator_start_bash_infra Medium_Phone_API_35
fn run gradle_instrumented_test_bash_infra $rel_dir
fn run android_emulator_stop_bash_infra
\`\`\`
## Package
\`$pkg_id\`
## FnTheme
La app usa FnTheme y FnTokens via composite build en \`kotlin/functions/ui\`.
Para cambiar colores o tipografia, editar el modulo del registry.
EOF
# ---- .gitignore ----
cat > "$abs_dir/.gitignore" <<'EOF'
.gradle/
build/
local.properties
*.iml
.idea/
captures/
.externalNativeBuild/
.cxx/
*.apk
*.aab
# NOTE: app/src/test/snapshots/ is committed (Roborazzi goldens are test refs).
EOF
# ---- README.md ----
cat > "$abs_dir/README.md" <<EOF
# $name
$desc
Generado con \`init_kotlin_app\` del registry fn_registry.
## Requisitos
- Android SDK 34
- JDK 17
- Gradle 8.6 (via wrapper)
- \`kotlin/functions/ui\` modulo del registry (composite build)
## Build rapido
\`\`\`bash
fn run gradle_assemble_debug_bash_infra $rel_dir
\`\`\`
EOF
# ---- Git + Gitea ----
if [[ -n "${GITEA_URL:-}" && -n "${GITEA_TOKEN:-}" ]]; then
ensure_repo_synced "$abs_dir" "dataforge" "$name" "master" \
"feat: scaffold $name via init_kotlin_app" || \
echo "init_kotlin_app: warning — ensure_repo_synced fallo, repo no creado" >&2
else
echo "init_kotlin_app: GITEA_URL/GITEA_TOKEN no seteados, omitiendo creacion de repo Gitea" >&2
(cd "$abs_dir" && git init -b master >/dev/null 2>&1 || git init >/dev/null 2>&1 \
&& git add -A \
&& git commit -m "feat: scaffold $name via init_kotlin_app" --quiet)
fi
# ---- fn index si hay proyecto ----
if [[ -n "$project" && -x "$FN_ROOT/fn" ]]; then
(cd "$FN_ROOT" && ./fn index >/dev/null 2>&1) || \
echo "init_kotlin_app: warning — fn index fallo" >&2
fi
echo ""
echo "init_kotlin_app: $rel_dir creada"
echo ""
echo " Archivos:"
echo " $rel_dir/settings.gradle.kts"
echo " $rel_dir/app/build.gradle.kts"
echo " $rel_dir/app/src/main/kotlin/$pkg_path/MainActivity.kt"
echo " $rel_dir/app/src/test/kotlin/$pkg_path/ExampleScreenshotTest.kt"
echo " $rel_dir/app/src/androidTest/kotlin/$pkg_path/MainActivityTest.kt"
echo " $rel_dir/app.md"
echo ""
echo " Pasos siguientes:"
echo " fn run gradle_unit_test_bash_infra $rel_dir"
echo " fn run gradle_screenshot_test_bash_infra $rel_dir"
echo " fn run gradle_assemble_debug_bash_infra $rel_dir"
echo ""
echo " Para tests instrumentados (emulador):"
echo " fn run android_emulator_start_bash_infra Medium_Phone_API_35"
echo " fn run gradle_instrumented_test_bash_infra $rel_dir"
echo " fn run android_emulator_stop_bash_infra"
echo ""
echo " Package: $pkg_id"
echo " FnTheme composite: $ui_rel_path"
}
# Permitir invocacion directa via 'fn run init_kotlin_app ...'
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
init_kotlin_app "$@"
fi
@@ -0,0 +1,67 @@
---
name: run_kotlin_app_tests
kind: pipeline
lang: bash
domain: pipelines
version: "1.0.0"
purity: impure
signature: "run_kotlin_app_tests(project_dir: string, avd_name?: string, --skip-emulator?, --no-stop?) -> int"
description: "Pipeline e2e completo de testing app Kotlin: unit JVM + screenshot Roborazzi + build APK + instrumented Compose en emulador."
tags: [android, kotlin, compose, test, e2e, launcher]
uses_functions:
- gradle_unit_test_bash_infra
- gradle_screenshot_test_bash_infra
- gradle_assemble_debug_bash_infra
- gradle_instrumented_test_bash_infra
- android_emulator_list_bash_infra
- android_emulator_start_bash_infra
- android_emulator_stop_bash_infra
- adb_wsl_bash_infra
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
tested: false
tests: []
test_file_path: ""
file_path: "bash/functions/pipelines/run_kotlin_app_tests.sh"
params:
- name: project_dir
desc: "Raiz del proyecto Android. Debe contener gradlew. Puede ser relativo al cwd o absoluto."
- name: avd_name
desc: "AVD para instrumented tests. Default: Medium_Phone_API_35. Debe existir en la lista de AVDs del sistema. Ignorado si se pasa --skip-emulator."
- name: --skip-emulator
desc: "Saltar instrumented tests. El pipeline solo ejecuta unit tests, screenshot tests y build APK, luego sale con exit 0."
- name: --no-stop
desc: "No parar el emulador al finalizar los instrumented tests. Util en desarrollo iterativo para no esperar el arranque en la siguiente ejecucion."
output: "Stdout con tabla resumen de cada step (nombre, OK/FAIL/SKIP, tiempo). Exit 0 = todos los tests pasan. Exit codes: 1=unit tests fallaron, 2=screenshot tests fallaron, 3=build APK fallado, 4=emulador no encontrado o no arranca, 5=instrumented tests fallaron."
---
## Ejemplo
```bash
# Suite completa con AVD por defecto
bash bash/functions/pipelines/run_kotlin_app_tests.sh apps/my_kotlin_app
# Suite completa con AVD especifico
bash bash/functions/pipelines/run_kotlin_app_tests.sh apps/my_kotlin_app Pixel_7_API_34
# Solo tests JVM (unit + screenshot + build), sin emulador
bash bash/functions/pipelines/run_kotlin_app_tests.sh apps/my_kotlin_app --skip-emulator
# Suite completa, dejar emulador corriendo al final
bash bash/functions/pipelines/run_kotlin_app_tests.sh apps/my_kotlin_app Medium_Phone_API_35 --no-stop
# Desde el launcher (fn run)
fn run run_kotlin_app_tests apps/my_kotlin_app
```
## Notas
- Fail-fast: si un paso falla, el pipeline imprime el resumen parcial y sale con el exit code del paso fallido. No continua al siguiente.
- El orden de los flags tras `project_dir` es libre: `avd_name` es el primer argumento no-flag; `--skip-emulator` y `--no-stop` pueden aparecer en cualquier posicion.
- El arranque del emulador usa `android_emulator_start` que es idempotente: si ya hay un emulador corriendo con ese AVD, no lanza otro.
- `adb_wsl` se sourcea para resolver `$ADB` apuntando a `adb.exe` en Windows desde WSL2.
- El paso `emulator_stop` se registra como SKIP en la tabla resumen cuando se pasa `--no-stop`.
- Requiere WSL2 + Android SDK instalado en Windows. `ANDROID_HOME` o `~/android-sdk/env.sh` deben estar disponibles.
@@ -0,0 +1,242 @@
#!/usr/bin/env bash
# run_kotlin_app_tests — Pipeline e2e completo de testing app Kotlin:
# unit JVM + screenshot Roborazzi + build APK + instrumented Compose en emulador.
#
# USO:
# bash run_kotlin_app_tests.sh <project_dir> [avd_name] [--skip-emulator] [--no-stop]
#
# ARGUMENTOS:
# project_dir Raiz del proyecto Android (debe contener gradlew). Obligatorio.
# avd_name Nombre del AVD para instrumented tests. Default: Medium_Phone_API_35.
# --skip-emulator Saltar instrumented tests (solo unit + screenshot + build).
# --no-stop No parar el emulador al finalizar (util en desarrollo iterativo).
#
# EXIT CODES:
# 0 Todos los tests pasan.
# 1 Unit tests fallaron.
# 2 Screenshot tests fallaron.
# 3 Build APK fallado.
# 4 AVD no encontrado.
# 5 Instrumented tests fallaron.
#
# EJEMPLO:
# bash bash/functions/pipelines/run_kotlin_app_tests.sh apps/my_app
# bash bash/functions/pipelines/run_kotlin_app_tests.sh apps/my_app Pixel_7_API_34
# bash bash/functions/pipelines/run_kotlin_app_tests.sh apps/my_app --skip-emulator
# bash bash/functions/pipelines/run_kotlin_app_tests.sh apps/my_app Medium_Phone_API_35 --no-stop
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REGISTRY_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
# ---------------------------------------------------------------------------
# Parseo de argumentos
# ---------------------------------------------------------------------------
PROJECT_DIR="${1:-}"
AVD_NAME="Medium_Phone_API_35"
SKIP_EMULATOR=false
NO_STOP=false
# Primer argumento posicional es project_dir; el resto puede ser flags o avd_name.
shift || true
for arg in "$@"; do
case "$arg" in
--skip-emulator) SKIP_EMULATOR=true ;;
--no-stop) NO_STOP=true ;;
--*)
echo "[run_kotlin_app_tests] ERROR: flag desconocido: $arg" >&2
echo "USO: $0 <project_dir> [avd_name] [--skip-emulator] [--no-stop]" >&2
exit 1
;;
*) AVD_NAME="$arg" ;;
esac
done
if [[ -z "$PROJECT_DIR" ]]; then
echo "[run_kotlin_app_tests] ERROR: project_dir es obligatorio." >&2
echo "USO: $0 <project_dir> [avd_name] [--skip-emulator] [--no-stop]" >&2
exit 1
fi
# Resolver path absoluto
if [[ "$PROJECT_DIR" != /* ]]; then
PROJECT_DIR="$(pwd)/$PROJECT_DIR"
fi
# ---------------------------------------------------------------------------
# Validacion inicial
# ---------------------------------------------------------------------------
if [[ ! -f "$PROJECT_DIR/gradlew" ]]; then
echo "[run_kotlin_app_tests] ERROR: no se encontro gradlew en $PROJECT_DIR" >&2
exit 1
fi
echo ""
echo "======================================================================"
echo " run_kotlin_app_tests"
echo "======================================================================"
echo " project_dir : $PROJECT_DIR"
echo " avd_name : $AVD_NAME"
echo " skip_emulator : $SKIP_EMULATOR"
echo " no_stop : $NO_STOP"
echo "======================================================================"
echo ""
# ---------------------------------------------------------------------------
# Tabla de resultados acumulada
# ---------------------------------------------------------------------------
# Arrays paralelos: nombre, status, tiempo
STEP_NAMES=()
STEP_STATUS=()
STEP_TIMES=()
record_step() {
local name="$1" status="$2" elapsed="$3"
STEP_NAMES+=("$name")
STEP_STATUS+=("$status")
STEP_TIMES+=("${elapsed}s")
}
print_summary() {
echo ""
echo "======================================================================"
echo " RESUMEN"
printf " %-30s %-6s %s\n" "STEP" "STATUS" "TIEMPO"
echo " ------------------------------ ------ ------"
for i in "${!STEP_NAMES[@]}"; do
printf " %-30s %-6s %s\n" "${STEP_NAMES[$i]}" "${STEP_STATUS[$i]}" "${STEP_TIMES[$i]}"
done
echo "======================================================================"
echo ""
}
# ---------------------------------------------------------------------------
# Paso 1: Unit tests (JVM)
# ---------------------------------------------------------------------------
echo "[run_kotlin_app_tests] [1/4] Unit tests (JVM)..."
T_START=$SECONDS
if bash "$REGISTRY_ROOT/bash/functions/infra/gradle_unit_test.sh" "$PROJECT_DIR"; then
record_step "unit_tests" "OK" $((SECONDS - T_START))
echo "[run_kotlin_app_tests] Unit tests: OK"
else
record_step "unit_tests" "FAIL" $((SECONDS - T_START))
print_summary
echo "[run_kotlin_app_tests] ERROR: unit tests fallaron." >&2
exit 1
fi
# ---------------------------------------------------------------------------
# Paso 2: Screenshot tests (Roborazzi, JVM)
# ---------------------------------------------------------------------------
echo ""
echo "[run_kotlin_app_tests] [2/4] Screenshot tests (Roborazzi, JVM)..."
T_START=$SECONDS
if bash "$REGISTRY_ROOT/bash/functions/infra/gradle_screenshot_test.sh" "$PROJECT_DIR"; then
record_step "screenshot_tests" "OK" $((SECONDS - T_START))
echo "[run_kotlin_app_tests] Screenshot tests: OK"
else
record_step "screenshot_tests" "FAIL" $((SECONDS - T_START))
print_summary
echo "[run_kotlin_app_tests] ERROR: screenshot tests fallaron." >&2
exit 2
fi
# ---------------------------------------------------------------------------
# Paso 3: Build APK debug
# ---------------------------------------------------------------------------
echo ""
echo "[run_kotlin_app_tests] [3/4] Build APK debug..."
T_START=$SECONDS
if bash "$REGISTRY_ROOT/bash/functions/infra/gradle_assemble_debug.sh" "$PROJECT_DIR"; then
record_step "assemble_debug" "OK" $((SECONDS - T_START))
echo "[run_kotlin_app_tests] Build APK: OK"
else
record_step "assemble_debug" "FAIL" $((SECONDS - T_START))
print_summary
echo "[run_kotlin_app_tests] ERROR: build APK fallado." >&2
exit 3
fi
# ---------------------------------------------------------------------------
# Paso 4: Instrumented tests (emulador)
# ---------------------------------------------------------------------------
if [[ "$SKIP_EMULATOR" == "true" ]]; then
record_step "instrumented_tests" "SKIP" 0
print_summary
echo "[run_kotlin_app_tests] --skip-emulator activo. Pipeline completo (sin instrumented)."
exit 0
fi
echo ""
echo "[run_kotlin_app_tests] [4/4] Instrumented tests en emulador '$AVD_NAME'..."
# Source adb_wsl para resolver $ADB y helpers de dispositivo
# shellcheck source=../infra/adb_wsl.sh
source "$REGISTRY_ROOT/bash/functions/infra/adb_wsl.sh"
# Verificar que el AVD existe
echo "[run_kotlin_app_tests] Verificando AVD '$AVD_NAME'..."
if ! bash "$REGISTRY_ROOT/bash/functions/infra/android_emulator_list.sh" | grep -qx "$AVD_NAME"; then
record_step "emulator_check" "FAIL" 0
record_step "instrumented_tests" "SKIP" 0
print_summary
echo "[run_kotlin_app_tests] ERROR: AVD '$AVD_NAME' no encontrado." >&2
echo " AVDs disponibles:" >&2
bash "$REGISTRY_ROOT/bash/functions/infra/android_emulator_list.sh" | sed 's/^/ /' >&2
exit 4
fi
record_step "emulator_check" "OK" 0
# Arrancar emulador (idempotente)
echo "[run_kotlin_app_tests] Arrancando emulador '$AVD_NAME' (idempotente)..."
T_START=$SECONDS
if ! bash "$REGISTRY_ROOT/bash/functions/infra/android_emulator_start.sh" "$AVD_NAME"; then
record_step "emulator_start" "FAIL" $((SECONDS - T_START))
print_summary
echo "[run_kotlin_app_tests] ERROR: no se pudo arrancar el emulador." >&2
exit 4
fi
record_step "emulator_start" "OK" $((SECONDS - T_START))
# Correr instrumented tests
echo "[run_kotlin_app_tests] Corriendo instrumented tests..."
T_START=$SECONDS
INSTRUMENTED_EXIT=0
bash "$REGISTRY_ROOT/bash/functions/infra/gradle_instrumented_test.sh" "$PROJECT_DIR" || INSTRUMENTED_EXIT=$?
if [[ $INSTRUMENTED_EXIT -eq 0 ]]; then
record_step "instrumented_tests" "OK" $((SECONDS - T_START))
echo "[run_kotlin_app_tests] Instrumented tests: OK"
else
record_step "instrumented_tests" "FAIL" $((SECONDS - T_START))
fi
# Parar emulador (salvo --no-stop)
if [[ "$NO_STOP" == "false" ]]; then
echo "[run_kotlin_app_tests] Parando emulador..."
T_START=$SECONDS
bash "$REGISTRY_ROOT/bash/functions/infra/android_emulator_stop.sh" || true
record_step "emulator_stop" "OK" $((SECONDS - T_START))
else
record_step "emulator_stop" "SKIP" 0
echo "[run_kotlin_app_tests] --no-stop activo: emulador sigue corriendo."
fi
print_summary
if [[ $INSTRUMENTED_EXIT -ne 0 ]]; then
echo "[run_kotlin_app_tests] ERROR: instrumented tests fallaron." >&2
exit 5
fi
echo "[run_kotlin_app_tests] Pipeline e2e completado con exito."
exit 0
+251
View File
@@ -0,0 +1,251 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH="\\\"\\\""
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"
+37
View File
@@ -42,6 +42,8 @@ func cmdDoctor(args []string) {
doctorUsesFunctions(r, jsonOut)
case "unused":
doctorUnused(r, jsonOut)
case "cpp-apps":
doctorCppApps(r, jsonOut)
default:
fmt.Fprintf(os.Stderr, "unknown doctor subcommand: %s\n", sub)
doctorUsage()
@@ -62,6 +64,7 @@ Subcommands:
sync Drift entre pc_locations BD y disco
uses-functions Audit imports reales vs uses_functions del app.md
unused Funciones del registry sin consumidores
cpp-apps Conformidad de apps C++ con cpp/PATTERNS.md (cfg.about, dockspace, menubar)
Flags:
--json Salida JSON (para scripting/agentes)`)
@@ -95,6 +98,11 @@ func doctorAll(root string, jsonOut bool) {
} else {
all["unused_error"] = err.Error()
}
if v, err := infra.AuditCppApps(root); err == nil {
all["cpp_apps"] = v
} else {
all["cpp_apps_error"] = err.Error()
}
emit(all)
return
}
@@ -109,6 +117,35 @@ func doctorAll(root string, jsonOut bool) {
doctorUsesFunctions(root, false)
fmt.Println("\n=== Unused functions ===")
doctorUnused(root, false)
fmt.Println("\n=== C++ apps standard conformance ===")
doctorCppApps(root, false)
}
func doctorCppApps(root string, jsonOut bool) {
audits, err := infra.AuditCppApps(root)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if jsonOut {
emit(audits)
return
}
bad := 0
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "STATUS\tAPP\tISSUES")
for _, a := range audits {
status := "OK"
issues := "-"
if !a.OK {
status = "FAIL"
issues = strings.Join(a.Issues, "; ")
bad++
}
fmt.Fprintf(w, "%s\t%s\t%s\n", status, a.AppID, issues)
}
w.Flush()
fmt.Printf("\n%d/%d C++ apps conform.\n", len(audits)-bad, len(audits))
}
func doctorArtefacts(root string, jsonOut bool) {
+28
View File
@@ -289,6 +289,13 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/apps/primitives_gallery/CMakeLists.txt)
add_subdirectory(apps/primitives_gallery)
endif()
# --- Tables playground (vive dentro de primitives_gallery/playground/tables/) ---
# No es un app del registry; sirve para iterar mejoras sobre table_view_cpp_viz
# antes de promover una API v2 y migrar las apps C++ que hoy usan ImGui::BeginTable raw.
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/apps/primitives_gallery/playground/tables/CMakeLists.txt)
add_subdirectory(apps/primitives_gallery/playground/tables)
endif()
# --- text_editor + file_watcher smoke test (issue 0025) ---
# Build gate para validar que text_editor.cpp + file_watcher.cpp + vendor enlazan.
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/apps/text_editor_smoke/CMakeLists.txt)
@@ -303,6 +310,27 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/apps/altsnap_jitter_test/CMakeLists.txt)
add_subdirectory(apps/altsnap_jitter_test)
endif()
# --- gamedev stack (SDL3 + sokol_gfx + miniaudio, issue 0072) ---
# Apps standalone, no usan fn_framework. Vendor SDL3 se compila una vez aqui;
# las apps solo linkan SDL3::SDL3-static.
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/vendor/sdl3/CMakeLists.txt
AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/vendor/sokol/sokol_gfx.h)
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
set(SDL_STATIC ON CACHE BOOL "" FORCE)
set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
set(SDL_TESTS OFF CACHE BOOL "" FORCE)
set(SDL_EXAMPLES OFF CACHE BOOL "" FORCE)
set(SDL_INSTALL OFF CACHE BOOL "" FORCE)
set(SDL_X11_XSCRNSAVER OFF CACHE BOOL "" FORCE)
add_subdirectory(vendor/sdl3 EXCLUDE_FROM_ALL)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/apps/engine_smoke/CMakeLists.txt)
add_subdirectory(apps/engine_smoke)
endif()
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/apps/runtime_test/CMakeLists.txt)
add_subdirectory(apps/runtime_test)
endif()
endif()
# --- Registry Dashboard (lives in projects/fn_monitoring/apps/) ---
# _DASH_DIR puede sobreescribirse via -D_DASH_DIR=<path> para apuntar a un
# worktree (parallel-fix-issues u otros flujos que aislen builds).
+27 -5
View File
@@ -32,6 +32,15 @@ Antes de mergear una app, verificar uno por uno:
- [ ] **GL loader** (si la app usa OpenGL >= 2.0 directamente). Pasar
`AppConfig::init_gl_loader = true` para que `fn::run_app()` llame
`fn::gfx::gl_loader_init()` tras crear el contexto.
- [ ] **Auto-dockspace** (default `true`). El framework llama
`ImGui::DockSpaceOverViewport(0, GetMainViewport(), PassthruCentralNode)`
antes de `render_fn()` cada frame. **NO** llamar `DockSpaceOverViewport`
manual en `render()` — duplica nodes y causa flicker. Apps que usan
layout custom con `ImGui::DockSpace` propio o `fullscreen_window` deben
poner `cfg.auto_dockspace = false`.
- [ ] **No `fn_ui::app_menubar(...)` manual**. El framework ya lo dibuja en
cada frame leyendo `cfg.panels`/`cfg.layouts_cb`/`cfg.view_extras`.
Llamarlo manualmente provoca barra duplicada o pisada.
- [ ] **Tokens en lugar de hex literales**. Usar `fn_tokens::colors`,
`fn_tokens::spacing`, `fn_tokens::radius`. Nunca `IM_COL32(0x12,0x34,...)`,
nunca `ImVec4(0.5f, 0.5f, 0.5f, 1.0f)` ad-hoc.
@@ -46,14 +55,23 @@ Antes de mergear una app, verificar uno por uno:
- [ ] **Build incremental**. La app aparece en `cpp/CMakeLists.txt` con su
`add_subdirectory(apps/<nombre>)`. Sin warnings nuevos.
## Crear app nueva — usar el scaffolder
```bash
# App suelta en cpp/apps/<name>/
fn run init_cpp_app my_tool --desc "Herramienta para X"
# App dentro de un proyecto
fn run init_cpp_app finance_panel --project budget --desc "Panel de finanzas"
```
`init_cpp_app_bash_pipelines` genera la estructura canonica (main.cpp + CMakeLists.txt + app.md) cumpliendo este documento, registra la app en `cpp/CMakeLists.txt`, crea repo Gitea `dataforge/<name>` y ejecuta `fn index`. Despues solo se completa `uses_functions` cuando se importan funciones del registry.
## Esqueleto minimo
```cpp
#include "framework/app_base.h"
#include "core/icons_tabler.h"
#include "core/panel_menu.h"
#include "core/app_settings.h"
#include "core/tokens.h"
#include "imgui.h"
namespace {
@@ -67,7 +85,7 @@ constexpr fn_ui::PanelToggle k_panels[] = {
} // namespace
static void render_my_app() {
ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport());
// Sin DockSpaceOverViewport ni app_menubar manual — los da el framework.
if (show_inspector) {
ImGui::Begin(TI_INFO_CIRCLE " Inspector", &show_inspector);
ImGui::TextUnformatted("Inspector contents");
@@ -84,9 +102,11 @@ int main() {
fn::AppConfig cfg;
cfg.title = "My App";
cfg.about = {"My App", "0.1.0", "Demo de app shell canonica"};
cfg.log = {"my_app.log", 1};
cfg.panels = k_panels;
cfg.panel_count = sizeof(k_panels) / sizeof(k_panels[0]);
cfg.init_gl_loader = false; // ponerlo en true si usas OpenGL directo
cfg.init_gl_loader = false; // true si usas OpenGL directo
// cfg.auto_dockspace = false; // solo si gestionas DockSpace propio (ej. shaders_lab)
return fn::run_app(cfg, render_my_app);
}
```
@@ -104,6 +124,8 @@ escribir una linea de codigo.
| `glfwInit()` en `main` | `fn::run_app()` |
| `ImVec4(0.5,0.5,0.5,1)` ad-hoc | `fn_tokens::colors::text_dim` |
| Crear menubar a mano en cada frame | `AppConfig::panels` + `AppConfig::layouts_cb` |
| `fn_ui::app_menubar(nullptr,0,nullptr)` en render | El framework ya lo dibuja |
| `ImGui::DockSpaceOverViewport(...)` en render | `auto_dockspace=true` por defecto |
| `ImGui::Begin(u8"\xEF\xA0\x83 ...")` | `ImGui::Begin(TI_HOME " ...")` |
| Settings dispersos por la app | `settings_window_add_section()` |
| About hardcoded en un `Begin/End` | `AppConfig::about` o `about_window_set_info()` |
Submodule cpp/apps/altsnap_jitter_test updated: 64a01defbc...181c4f3dd6
-6
View File
@@ -45,12 +45,6 @@ static void init_data() {
void render() {
init_data();
// MainMenuBar (solo Settings — chart_demo no tiene paneles toggleables)
fn_ui::app_menubar(nullptr, 0, nullptr);
// Full-window dockspace
ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport());
if (ImGui::Begin("fn_registry — Chart Demo")) {
if (ImGui::BeginTabBar("##charts")) {
if (ImGui::BeginTabItem("Line Plot")) {
Submodule cpp/apps/engine_smoke added at bed33856e7
+37
View File
@@ -0,0 +1,37 @@
---
name: primitives_gallery
lang: cpp
domain: gfx
description: "Visual catalog de primitivas C++ UI del fn_registry. Demos por categoria (charts, controls, layout, gl_info). Soporta modo --capture para regresion visual."
tags: [imgui, gallery, gfx, demo, capture]
uses_functions: []
uses_types: []
framework: "imgui"
entry_point: "main.cpp"
dir_path: "cpp/apps/primitives_gallery"
repo_url: ""
---
# primitives_gallery
Catalogo visual de las primitivas y componentes ImGui del registry. Cada demo se carga al hacer click en su entrada del sidebar.
## Build & run
```bash
cd cpp && cmake --build build --target primitives_gallery -j
./build/primitives_gallery
```
## Modo capture (regresion visual)
```bash
./build/primitives_gallery --capture <out_dir>
```
Renderiza cada demo offscreen y guarda PNGs en `<out_dir>/`. Permite gate visual via golden images.
## Notas
- `auto_dockspace = false` — usa `fullscreen_window` que ocupa todo el viewport.
- `init_gl_loader = true` — necesario para demos de OpenGL 4.3 core (compute, SSBOs).
+5 -5
View File
@@ -132,10 +132,8 @@ static void draw_sidebar() {
static void render() {
// Theme y gl_loader gestionados por fn::run_app (theme=FnDark por defecto,
// init_gl_loader=true en AppConfig).
// MainMenuBar (solo Settings — la gallery no tiene paneles toggleables ni layouts)
fn_ui::app_menubar(nullptr, 0, nullptr);
// init_gl_loader=true en AppConfig). Menubar via run_app.
// auto_dockspace=false porque usamos fullscreen_window que ocupa todo.
fullscreen_window_begin("##gallery");
@@ -224,7 +222,9 @@ int main(int argc, char** argv) {
.about = {.name = "Primitives Gallery",
.version = "0.4.0",
.description = "Visual catalog of fn_registry C++ UI primitives. Now on OpenGL 4.3 core (compute, SSBOs, image load/store) — ver demo gl_info."},
.init_gl_loader = true},
.init_gl_loader = true,
.auto_dockspace = false,
.log = {"primitives_gallery.log", 1}},
render
);
}
@@ -0,0 +1,6 @@
# Tables playground - vive dentro de primitives_gallery/ (playgrounds.md).
# No es un app del registry: no tiene app.md, no se indexa.
add_imgui_app(tables_playground
main.cpp
${CMAKE_SOURCE_DIR}/functions/viz/table_view.cpp
)
@@ -0,0 +1,115 @@
// Playground tables: visor de la funcion table_view_cpp_viz tal cual existe
// hoy en el registry. Iteraremos mejoras encima hasta promover una API v2
// que sustituya a los `ImGui::BeginTable` raw de las apps C++.
#include "app_base.h"
#include "imgui.h"
#include "viz/table_view.h"
#include "core/logger.h"
#include <cstdio>
#include <string>
#include <vector>
namespace {
struct Row {
const char* name;
const char* lang;
const char* domain;
const char* purity;
const char* description;
};
// Dataset de muestra inspirado en el registry. Filas reales-ish para
// hacer obvias las limitaciones actuales (sin sort, sin filter, sin
// per-cell render, alto fijo, etc.).
const std::vector<Row>& sample_rows() {
static const std::vector<Row> rows = {
{"filter_slice", "go", "core", "pure", "Filtra slice con predicado"},
{"map_slice", "go", "core", "pure", "Aplica f a cada elemento"},
{"reduce_slice", "go", "core", "pure", "Fold con acumulador"},
{"sma", "py", "finance", "pure", "Simple moving average"},
{"ema", "py", "finance", "pure", "Exponential moving average"},
{"rsi", "py", "finance", "pure", "Relative strength index"},
{"table_view", "cpp", "viz", "pure", "Tabla ImGui actual del registry"},
{"line_plot", "cpp", "viz", "pure", "ImPlot line wrapper"},
{"scatter_plot", "cpp", "viz", "pure", "ImPlot scatter wrapper"},
{"bar_chart", "cpp", "viz", "pure", "ImPlot bar wrapper"},
{"heatmap", "cpp", "viz", "pure", "ImPlot heatmap wrapper"},
{"sqlite_open", "go", "infra", "impure", "Open SQLite con WAL+FK"},
{"http_json_response", "go", "infra", "impure", "Helper JSON response"},
{"http_parse_body", "go", "infra", "impure", "Parse JSON body"},
{"rsync_deploy", "bash", "infra", "impure", "rsync local -> remoto"},
{"systemd_install", "go", "infra", "impure", "Sube unit + enable + start"},
{"systemd_restart", "go", "infra", "impure", "Restart servicio remoto"},
{"jupyter_discover", "py", "notebook", "impure", "Descubre instancias Jupyter"},
{"jupyter_exec", "py", "notebook", "impure", "Ejecuta celda y vuelca output"},
{"docker_pull_image", "go", "infra", "impure", "docker pull con timeout"},
{"graph_force_layout", "cpp", "viz", "pure", "Force-directed CPU"},
{"graph_force_layout_gpu","cpp", "viz", "pure", "Force-directed GPU (compute)"},
{"sql_workbench", "cpp", "core", "impure", "Workbench SQL embebido"},
{"text_editor", "cpp", "core", "impure", "Editor de texto con highlighting"},
{"icon_font", "cpp", "core", "impure", "Carga tabler-icons.ttf"},
};
return rows;
}
// Aplanado row-major para alimentar table_view_cpp_viz (firma `const char* const*`).
const char* const* flatten_cells(int& out_rows, int& out_cols) {
static std::vector<const char*> flat;
static bool built = false;
if (!built) {
const auto& rows = sample_rows();
flat.reserve(rows.size() * 5);
for (const auto& r : rows) {
flat.push_back(r.name);
flat.push_back(r.lang);
flat.push_back(r.domain);
flat.push_back(r.purity);
flat.push_back(r.description);
}
built = true;
}
out_rows = static_cast<int>(sample_rows().size());
out_cols = 5;
return flat.data();
}
} // namespace
void render() {
if (ImGui::Begin("Tables Playground - table_view actual")) {
ImGui::TextWrapped(
"Esta es la funcion `table_view_cpp_viz` del registry hoy. "
"Capacidades: borders, sortable (solo indicador, no sort real), "
"rowBg, resizable, scrollY (alto fijo 300px), reorderable. "
"Sin filter, sin selection, sin per-cell render, sin export. "
"Iteraremos mejoras encima de esto.");
ImGui::Separator();
static const char* headers[] = {"name", "lang", "domain", "purity", "description"};
int rows = 0, cols = 0;
const char* const* cells = flatten_cells(rows, cols);
ImGui::Text("Filas: %d Columnas: %d", rows, cols);
ImGui::Spacing();
table_view("##registry_sample", headers, cols, cells, rows);
}
ImGui::End();
}
#ifndef FN_TEST_BUILD
int main() {
return fn::run_app({
.title = "Tables Playground",
.width = 1280,
.height = 800,
.about = {.name = "tables_playground",
.version = "0.1.0",
.description = "Playground para iterar mejoras sobre table_view_cpp_viz antes de promover a registry y migrar apps C++."},
.log = {.file_path = "tables_playground.log",
.level = static_cast<int>(fn_log::Level::Info)}
}, render);
}
#endif
Submodule cpp/apps/runtime_test added at 49a9f3273d
+2
View File
@@ -416,6 +416,8 @@ int main() {
cfg.panels = k_panels;
cfg.panel_count = sizeof(k_panels) / sizeof(k_panels[0]);
cfg.layouts_cb = &g_layout_cb;
cfg.log = {"shaders_lab.log", 1};
cfg.auto_dockspace = false; // shaders_lab gestiona su propio DockSpace en render()
int rc = fn::run_app(cfg, render);
fn::gfx::canvas_destroy(g_canvas_code);
+28
View File
@@ -0,0 +1,28 @@
---
name: text_editor_smoke
lang: cpp
domain: tools
description: "Smoke test CLI (sin GUI) que valida los wrappers PIMPL de text_editor y file_watcher (inotify Linux / ReadDirectoryChangesW Win). No abre ventana ImGui — solo crea/settea texto/lee/poll/destruye."
tags: [cpp, smoke, test, cli]
uses_functions:
- text_editor_cpp_core
- file_watcher_cpp_core
uses_types: []
framework: "cli"
entry_point: "main.cpp"
dir_path: "cpp/apps/text_editor_smoke"
repo_url: ""
---
# text_editor_smoke
Smoke test que verifica las APIs de `text_editor` y `file_watcher` linkean correctamente. Sin ventana ImGui.
## Build & run
```bash
cd cpp && cmake --build build --target text_editor_smoke -j
./build/text_editor_smoke
```
Salida esperada: log con bytes leidos del editor + eventos del file_watcher.
+83
View File
@@ -0,0 +1,83 @@
#pragma once
// math2d — primitive 2D types used across gamedev stack (issue 0072b).
// Pure value types. No allocations, no virtual, trivial copy.
#include <cmath>
namespace fn::math2d {
struct Vec2 {
float x = 0.0f;
float y = 0.0f;
constexpr Vec2() = default;
constexpr Vec2(float xv, float yv) : x(xv), y(yv) {}
constexpr Vec2 operator+(Vec2 b) const { return {x + b.x, y + b.y}; }
constexpr Vec2 operator-(Vec2 b) const { return {x - b.x, y - b.y}; }
constexpr Vec2 operator*(float s) const { return {x * s, y * s}; }
constexpr Vec2 operator/(float s) const { return {x / s, y / s}; }
constexpr Vec2& operator+=(Vec2 b) { x += b.x; y += b.y; return *this; }
constexpr Vec2& operator-=(Vec2 b) { x -= b.x; y -= b.y; return *this; }
constexpr Vec2& operator*=(float s) { x *= s; y *= s; return *this; }
float length() const { return std::sqrt(x * x + y * y); }
constexpr float length_sq() const { return x * x + y * y; }
Vec2 normalized() const {
float l = length();
return l > 0.0f ? Vec2{x / l, y / l} : Vec2{0.0f, 0.0f};
}
static constexpr float dot(Vec2 a, Vec2 b) { return a.x * b.x + a.y * b.y; }
static constexpr float cross(Vec2 a, Vec2 b) { return a.x * b.y - a.y * b.x; }
};
struct Rect {
float x = 0.0f;
float y = 0.0f;
float w = 0.0f;
float h = 0.0f;
constexpr Rect() = default;
constexpr Rect(float xv, float yv, float wv, float hv) : x(xv), y(yv), w(wv), h(hv) {}
constexpr float right() const { return x + w; }
constexpr float bottom() const { return y + h; }
constexpr Vec2 center() const { return {x + w * 0.5f, y + h * 0.5f}; }
constexpr Vec2 min() const { return {x, y}; }
constexpr Vec2 max() const { return {x + w, y + h}; }
constexpr bool contains(Vec2 p) const {
return p.x >= x && p.x < x + w && p.y >= y && p.y < y + h;
}
constexpr bool overlaps(Rect b) const {
return !(b.x >= x + w || b.x + b.w <= x ||
b.y >= y + h || b.y + b.h <= y);
}
};
struct Color {
float r = 1.0f;
float g = 1.0f;
float b = 1.0f;
float a = 1.0f;
constexpr Color() = default;
constexpr Color(float rv, float gv, float bv, float av = 1.0f)
: r(rv), g(gv), b(bv), a(av) {}
static constexpr Color white() { return {1, 1, 1, 1}; }
static constexpr Color black() { return {0, 0, 0, 1}; }
static constexpr Color transparent() { return {0, 0, 0, 0}; }
static Color rgba(unsigned char r8, unsigned char g8, unsigned char b8,
unsigned char a8 = 255) {
return {r8 / 255.0f, g8 / 255.0f, b8 / 255.0f, a8 / 255.0f};
}
static Color hex(unsigned int packed) {
return Color::rgba((packed >> 24) & 0xFF, (packed >> 16) & 0xFF,
(packed >> 8) & 0xFF, packed & 0xFF);
}
};
} // namespace fn::math2d
+38
View File
@@ -0,0 +1,38 @@
// UNICA TU del proyecto que define MINIAUDIO_IMPLEMENTATION.
#define MINIAUDIO_IMPLEMENTATION
#include "../../vendor/miniaudio/miniaudio.h"
#include "audio_engine.h"
#include <cstdlib>
namespace fn::audio {
Engine engine_init() {
Engine e{nullptr, false};
ma_engine* eng = static_cast<ma_engine*>(std::malloc(sizeof(ma_engine)));
if (!eng) return e;
if (ma_engine_init(NULL, eng) != MA_SUCCESS) {
std::free(eng);
return e;
}
e.impl = eng;
e.ok = true;
return e;
}
void engine_shutdown(Engine& e) {
if (!e.ok || !e.impl) return;
ma_engine* eng = static_cast<ma_engine*>(e.impl);
ma_engine_uninit(eng);
std::free(eng);
e.impl = nullptr;
e.ok = false;
}
void engine_set_volume(Engine& e, float v) {
if (!e.ok || !e.impl) return;
ma_engine_set_volume(static_cast<ma_engine*>(e.impl), v);
}
} // namespace fn::audio
+21
View File
@@ -0,0 +1,21 @@
// audio_engine — lifecycle del engine de audio (miniaudio wrapper).
// Issue 0072b — runtime gamedev nucleo (PC desktop + WASM + futuro mobile).
#pragma once
namespace fn::audio {
struct Engine {
void* impl; // ma_engine* opaco
bool ok;
};
// Crea engine con device default. Engine.ok=false si falla.
Engine engine_init();
// Libera engine. Idempotente con Engine.ok=false.
void engine_shutdown(Engine& e);
// Master volume 0..1.
void engine_set_volume(Engine& e, float v);
} // namespace fn::audio
+47
View File
@@ -0,0 +1,47 @@
---
name: audio_engine
kind: function
lang: cpp
domain: gamedev
version: "0.1.0"
purity: impure
signature: "engine_init() -> Engine; engine_shutdown(Engine&); engine_set_volume(Engine&, float)"
description: "Lifecycle del engine de audio basado en miniaudio (single-header, public domain). Inicializa device default, expone master volume, y libera recursos. Cross-platform: Windows/Linux/macOS via WASAPI/ALSA/CoreAudio y WebAudio bajo emscripten. Issue 0072b — runtime gamedev nucleo. Esta TU es la unica del proyecto que define MINIAUDIO_IMPLEMENTATION."
tags: [gamedev, audio, miniaudio]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
example: |
fn::audio::Engine eng = fn::audio::engine_init();
if (!eng.ok) { /* fallback silencioso */ }
fn::audio::engine_set_volume(eng, 0.8f);
// ... loop principal ...
fn::audio::engine_shutdown(eng);
tested: false
tests: []
test_file_path: ""
file_path: "cpp/functions/gamedev/audio_engine.cpp"
params:
- name: e
desc: "Engine handle. ok=true tras engine_init exitoso. Estructura opaca: impl apunta a ma_engine internamente."
- name: v
desc: "Volumen master 0..1 (lineal). Valores >1 amplifican (riesgo de clipping)."
output: "Engine con impl=ma_engine* y ok=true si MA_SUCCESS, ok=false en cualquier fallo (malloc o init de miniaudio). engine_shutdown deja ok=false e impl=nullptr — llamadas posteriores son no-op seguros."
---
# audio_engine
Wrapper minimo del engine de [miniaudio](https://miniaud.io/) (v0.11.25, single-header, public domain / MIT-0). Vendored en `cpp/vendor/miniaudio/`.
## Por que
Audio cross-platform sin depender de SDL_mixer / OpenAL / FMOD. Compila en Windows/Linux/macOS y WASM (emscripten) desde el mismo source. Sin excepciones, sin RTTI, sin std::string.
## Notas
- Estado de fallo se reporta via `Engine.ok=false`. No hay `error_type` porque no devolvemos `error_go_core`-style; el consumidor revisa `ok` antes de operar.
- `MINIAUDIO_IMPLEMENTATION` se define UNICAMENTE en `audio_engine.cpp`. Otras TU que usen miniaudio deben hacer solo `#include "miniaudio.h"`.
- Memoria via `malloc`/`free` (no `new`) para mantener compat con `-fno-exceptions`.
+59
View File
@@ -0,0 +1,59 @@
// NO definir MINIAUDIO_IMPLEMENTATION aqui — vive en audio_engine.cpp.
#include "../../vendor/miniaudio/miniaudio.h"
#include "audio_play.h"
#include <cstdlib>
namespace fn::audio {
Sound sound_load(Engine& e, const char* path) {
Sound s{nullptr, false};
if (!e.ok || !e.impl || !path) return s;
ma_sound* snd = static_cast<ma_sound*>(std::malloc(sizeof(ma_sound)));
if (!snd) return s;
ma_engine* eng = static_cast<ma_engine*>(e.impl);
if (ma_sound_init_from_file(eng, path, 0, NULL, NULL, snd) != MA_SUCCESS) {
std::free(snd);
return s;
}
s.impl = snd;
s.ok = true;
return s;
}
void sound_play(Sound& s) {
if (!s.ok || !s.impl) return;
ma_sound_start(static_cast<ma_sound*>(s.impl));
}
void sound_stop(Sound& s) {
if (!s.ok || !s.impl) return;
ma_sound_stop(static_cast<ma_sound*>(s.impl));
}
void sound_set_volume(Sound& s, float v) {
if (!s.ok || !s.impl) return;
ma_sound_set_volume(static_cast<ma_sound*>(s.impl), v);
}
void sound_destroy(Sound& s) {
if (!s.ok || !s.impl) return;
ma_sound* snd = static_cast<ma_sound*>(s.impl);
ma_sound_uninit(snd);
std::free(snd);
s.impl = nullptr;
s.ok = false;
}
void play_sound_oneshot(Engine& e, const char* path, float volume) {
if (!e.ok || !e.impl || !path) return;
ma_engine* eng = static_cast<ma_engine*>(e.impl);
// ma_engine_play_sound respeta el master volume; volume per-call se aplica
// creando un sonido ad-hoc si el caller quiere control fino. Para fire-and-forget
// usamos el helper directo y dejamos el master modular.
(void)volume;
ma_engine_play_sound(eng, path, NULL);
}
} // namespace fn::audio
+32
View File
@@ -0,0 +1,32 @@
// audio_play — reproducir sonidos one-shot y streaming sobre fn::audio::Engine.
// Issue 0072b — runtime gamedev nucleo.
#pragma once
#include "audio_engine.h"
namespace fn::audio {
struct Sound {
void* impl; // ma_sound* opaco
bool ok;
};
// Carga y prepara un sonido (decodifica streaming desde disco). Sound.ok=false si falla.
Sound sound_load(Engine& e, const char* path);
// Arranca/reanuda reproduccion.
void sound_play(Sound& s);
// Detiene reproduccion (no libera).
void sound_stop(Sound& s);
// Volumen 0..1 del sonido (independiente del master).
void sound_set_volume(Sound& s, float v);
// Libera recursos del sonido.
void sound_destroy(Sound& s);
// Fire-and-forget: reproduce path una vez sin handle. No-op si engine no esta listo.
void play_sound_oneshot(Engine& e, const char* path, float volume = 1.0f);
} // namespace fn::audio
+62
View File
@@ -0,0 +1,62 @@
---
name: audio_play
kind: function
lang: cpp
domain: gamedev
version: "0.1.0"
purity: impure
signature: "sound_load(Engine&, const char*) -> Sound; sound_play/stop/set_volume/destroy(Sound&); play_sound_oneshot(Engine&, const char*, float)"
description: "Reproduccion de audio sobre fn::audio::Engine: carga sonidos con streaming desde disco (wav/mp3/flac/ogg), play/stop/volumen por sonido, y helper fire-and-forget para one-shots sin handle. Cross-platform via miniaudio. Issue 0072b — runtime gamedev nucleo."
tags: [gamedev, audio, miniaudio]
uses_functions: ["audio_engine_cpp_gamedev"]
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
example: |
fn::audio::Engine eng = fn::audio::engine_init();
fn::audio::Sound bgm = fn::audio::sound_load(eng, "assets/bgm.ogg");
fn::audio::sound_set_volume(bgm, 0.5f);
fn::audio::sound_play(bgm);
// SFX one-shot:
fn::audio::play_sound_oneshot(eng, "assets/jump.wav");
// ...
fn::audio::sound_destroy(bgm);
fn::audio::engine_shutdown(eng);
tested: false
tests: []
test_file_path: ""
file_path: "cpp/functions/gamedev/audio_play.cpp"
params:
- name: e
desc: "Engine inicializado por audio_engine_cpp_gamedev. Si e.ok=false, todas las operaciones son no-op."
- name: path
desc: "Ruta al archivo de audio (relativa al cwd o absoluta). Formatos: wav/mp3/flac/ogg via decoders integrados de miniaudio."
- name: s
desc: "Sound handle. ok=true tras sound_load exitoso. Operaciones tras destroy son no-op."
- name: v
desc: "Volumen 0..1 lineal, multiplicativo con el master del Engine."
- name: volume
desc: "Volumen sugerido para play_sound_oneshot (actualmente delega al master via ma_engine_play_sound; reservado para futura implementacion per-instance)."
output: "Sound con impl=ma_sound* y ok=true en sound_load exitoso; ok=false ante cualquier fallo (engine no listo, malloc, decoder). play_sound_oneshot no devuelve handle — el sonido se gestiona internamente por el engine."
---
# audio_play
Reproduccion de audio one-shot y streaming sobre `fn::audio::Engine`. Wrapper minimo de la API `ma_sound` de miniaudio.
## Estructura `Sound`
Handle opaco con `impl` (apunta a `ma_sound`) y `ok` (bool). Gestion explicita: cada `sound_load` requiere `sound_destroy` para liberar (no hay RAII porque mantenemos `-fno-exceptions` y compat con structs trivial).
## Patrones de uso
- **BGM / loops largos:** `sound_load` + `sound_play`. Por defecto miniaudio hace streaming desde disco (no carga todo a memoria).
- **SFX cortos:** `play_sound_oneshot` — fire-and-forget, sin handle, ideal para clicks, jumps, hits.
- **SFX repetidos con control:** `sound_load` + `sound_play` cada vez. Para concurrent voices del mismo sample, considerar `ma_sound_init_copy` (no expuesto aun).
## Notas
- `play_sound_oneshot` recibe `volume` como hint pero actualmente delega al master del engine. Iterar si el caller real lo necesita.
- `sound_load` con `path=NULL` o engine no listo devuelve `Sound{nullptr, false}` — siempre comprobar `ok` antes de operar.
+119
View File
@@ -0,0 +1,119 @@
#include "camera_2d.h"
#include <cmath>
namespace fn::cam {
using fn::math2d::Vec2;
using fn::math2d::Rect;
Vec2 world_to_screen(const Camera2D& c, Vec2 world) {
float dx = world.x - c.pos.x;
float dy = world.y - c.pos.y;
if (c.rotation != 0.0f) {
float cs = std::cos(-c.rotation);
float sn = std::sin(-c.rotation);
float rx = dx * cs - dy * sn;
float ry = dx * sn + dy * cs;
dx = rx;
dy = ry;
}
float sx = dx * c.zoom + (float)c.viewport_w * 0.5f;
float sy = dy * c.zoom + (float)c.viewport_h * 0.5f;
return {sx, sy};
}
Vec2 screen_to_world(const Camera2D& c, Vec2 screen) {
float dx = (screen.x - (float)c.viewport_w * 0.5f) / c.zoom;
float dy = (screen.y - (float)c.viewport_h * 0.5f) / c.zoom;
if (c.rotation != 0.0f) {
float cs = std::cos(c.rotation);
float sn = std::sin(c.rotation);
float rx = dx * cs - dy * sn;
float ry = dx * sn + dy * cs;
dx = rx;
dy = ry;
}
return {c.pos.x + dx, c.pos.y + dy};
}
Rect visible_world_rect(const Camera2D& c) {
// For rotated cameras, return the AABB that contains the rotated viewport.
float hw = (float)c.viewport_w * 0.5f / c.zoom;
float hh = (float)c.viewport_h * 0.5f / c.zoom;
if (c.rotation == 0.0f) {
return {c.pos.x - hw, c.pos.y - hh, hw * 2.0f, hh * 2.0f};
}
float cs = std::fabs(std::cos(c.rotation));
float sn = std::fabs(std::sin(c.rotation));
float ehw = hw * cs + hh * sn;
float ehh = hw * sn + hh * cs;
return {c.pos.x - ehw, c.pos.y - ehh, ehw * 2.0f, ehh * 2.0f};
}
void view_proj_matrix(const Camera2D& c, float out[16]) {
// Orthographic projection mapping a viewport-sized box around camera pos
// to clip space [-1, 1].
float hw = (float)c.viewport_w * 0.5f / c.zoom;
float hh = (float)c.viewport_h * 0.5f / c.zoom;
float l = c.pos.x - hw;
float r = c.pos.x + hw;
// Screen Y goes down, world Y can go either; we pick world-Y-up convention:
// top of screen = pos.y + hh, bottom = pos.y - hh.
float b = c.pos.y - hh;
float t = c.pos.y + hh;
float cs = std::cos(-c.rotation);
float sn = std::sin(-c.rotation);
// Build column-major:
// M = Ortho(l,r,b,t) * Rotate(-rotation around camera center)
// Compose by hand to avoid temporaries.
float ortho[16] = {
2.0f / (r - l), 0.0f, 0.0f, 0.0f,
0.0f, 2.0f / (t - b), 0.0f, 0.0f,
0.0f, 0.0f, -1.0f, 0.0f,
-(r + l) / (r - l), -(t + b) / (t - b), 0.0f, 1.0f,
};
if (c.rotation == 0.0f) {
for (int i = 0; i < 16; ++i) out[i] = ortho[i];
return;
}
// Rotation around camera pos in world space:
// T(pos) * R * T(-pos)
// We bake it as a column-major 4x4 then multiply: out = ortho * rot.
float px = c.pos.x;
float py = c.pos.y;
float rot[16] = {
cs, sn, 0.0f, 0.0f,
-sn, cs, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
px - cs * px + sn * py,
py - sn * px - cs * py,
0.0f, 1.0f,
};
// out = ortho * rot (column-major).
for (int col = 0; col < 4; ++col) {
for (int row = 0; row < 4; ++row) {
float sum = 0.0f;
for (int k = 0; k < 4; ++k) {
sum += ortho[k * 4 + row] * rot[col * 4 + k];
}
out[col * 4 + row] = sum;
}
}
}
} // namespace fn::cam
+25
View File
@@ -0,0 +1,25 @@
#pragma once
// camera_2d — pure orthographic 2D camera (issue 0072b).
// No state, no I/O. World <-> screen transforms + ortho view-projection matrix.
#include "../core/math2d.h"
namespace fn::cam {
struct Camera2D {
fn::math2d::Vec2 pos = {0.0f, 0.0f}; // world position of camera center
float zoom = 1.0f; // 1 = no zoom; >1 zoom in
float rotation = 0.0f; // radians
int viewport_w = 1280;
int viewport_h = 720;
};
fn::math2d::Vec2 world_to_screen(const Camera2D& c, fn::math2d::Vec2 world);
fn::math2d::Vec2 screen_to_world(const Camera2D& c, fn::math2d::Vec2 screen);
fn::math2d::Rect visible_world_rect(const Camera2D& c);
// Column-major 4x4 view-projection matrix (orthographic).
void view_proj_matrix(const Camera2D& c, float out[16]);
} // namespace fn::cam
+54
View File
@@ -0,0 +1,54 @@
---
name: camera_2d
kind: function
lang: cpp
domain: gamedev
version: "0.1.0"
purity: pure
signature: "world_to_screen(Camera2D, Vec2) -> Vec2; screen_to_world(Camera2D, Vec2) -> Vec2; visible_world_rect(Camera2D) -> Rect; view_proj_matrix(Camera2D, float[16])"
description: "Camara ortografica 2D pura: pos (centro), zoom, rotacion (rad) y viewport en pixeles. Conversiones world<->screen, AABB visible y matriz view-projection 4x4 column-major lista para cualquier renderer (sokol_gfx, OpenGL, WebGPU). Fast-path sin trig si rotation==0. Issue 0072b."
tags: [gamedev, camera, 2d, math, pure]
uses_functions: []
uses_types: ["Vec2_cpp_core", "Rect_cpp_core"]
returns: []
returns_optional: false
error_type: ""
imports: []
example: |
fn::cam::Camera2D cam;
cam.pos = {100, 50};
cam.zoom = 2.0f;
cam.viewport_w = 1280;
cam.viewport_h = 720;
// mouse picking:
fn::math2d::Vec2 world = fn::cam::screen_to_world(cam, {input.mx, input.my});
// upload to shader:
float mvp[16];
fn::cam::view_proj_matrix(cam, mvp);
tested: false
tests: []
test_file_path: ""
file_path: "cpp/functions/gamedev/camera_2d.cpp"
params:
- name: camera
desc: "Camera2D con pos (centro mundo), zoom (>1 acerca), rotation (radianes) y viewport_w/h (pixeles)."
- name: world
desc: "Punto en world space (Vec2) para world_to_screen."
- name: screen
desc: "Punto en screen/pixel space (Vec2) para screen_to_world."
- name: out
desc: "Buffer de 16 floats donde escribir la matriz column-major en view_proj_matrix."
output: "Vec2 (transformaciones), Rect (AABB visible) o matriz column-major 4x4 (mapa world->clip [-1,1])."
---
# camera_2d
Camara 2D minima y pura. Zero estado global, zero I/O — apta para reusar en N renderers (sokol_gfx, OpenGL, WebGPU) y para correr en tests headless.
Convencion:
- `pos` es el centro de la camara en world coords.
- Eje Y en world apunta hacia ARRIBA. La proyeccion mapea `pos.y + hh` a top de clip (y=+1).
- Zoom multiplicativo: `zoom=2` muestra la mitad del area mundial en el mismo viewport.
- Rotation en radianes, sentido antihorario en world (la matriz invierte para clip).
- `visible_world_rect` para rotation != 0 devuelve el AABB ENVOLVENTE (no el rect rotado), util para frustum culling barato.
- `view_proj_matrix` es column-major (compatible con sokol_gfx / OpenGL `glUniformMatrix4fv` con `transpose=GL_FALSE`).
+87
View File
@@ -0,0 +1,87 @@
#include "game_loop.h"
#if defined(__EMSCRIPTEN__)
#include <emscripten/emscripten.h>
#endif
namespace fn::game {
namespace {
struct LoopRT {
LoopCfg cfg;
Uint64 last_ticks;
float accumulator;
Uint64 perf_freq;
};
#if defined(__EMSCRIPTEN__)
static LoopRT g_rt;
void em_tick() {
LoopRT& rt = g_rt;
Uint64 now = SDL_GetPerformanceCounter();
float frame_time = (float)((double)(now - rt.last_ticks) / (double)rt.perf_freq);
rt.last_ticks = now;
float cap = rt.cfg.fixed_dt * (float)rt.cfg.max_steps_per_frame;
if (frame_time > cap) frame_time = cap;
rt.accumulator += frame_time;
int steps = 0;
while (rt.accumulator >= rt.cfg.fixed_dt && steps < rt.cfg.max_steps_per_frame) {
if (rt.cfg.on_fixed_update) rt.cfg.on_fixed_update(rt.cfg.user, rt.cfg.fixed_dt);
rt.accumulator -= rt.cfg.fixed_dt;
steps++;
}
float interp = rt.accumulator / rt.cfg.fixed_dt;
if (rt.cfg.on_render) rt.cfg.on_render(rt.cfg.user, interp);
if (rt.cfg.should_quit && rt.cfg.should_quit(rt.cfg.user)) {
emscripten_cancel_main_loop();
}
}
#endif
} // namespace
void loop_run(SDL_Window* /*win*/, const LoopCfg& cfg) {
if (!cfg.on_fixed_update && !cfg.on_render) return;
#if defined(__EMSCRIPTEN__)
g_rt.cfg = cfg;
g_rt.last_ticks = SDL_GetPerformanceCounter();
g_rt.accumulator = 0.0f;
g_rt.perf_freq = SDL_GetPerformanceFrequency();
emscripten_set_main_loop(em_tick, 0, 1);
#else
Uint64 perf_freq = SDL_GetPerformanceFrequency();
Uint64 last = SDL_GetPerformanceCounter();
float accumulator = 0.0f;
for (;;) {
if (cfg.should_quit && cfg.should_quit(cfg.user)) break;
Uint64 now = SDL_GetPerformanceCounter();
float frame_time = (float)((double)(now - last) / (double)perf_freq);
last = now;
float cap = cfg.fixed_dt * (float)cfg.max_steps_per_frame;
if (frame_time > cap) frame_time = cap;
accumulator += frame_time;
int steps = 0;
while (accumulator >= cfg.fixed_dt && steps < cfg.max_steps_per_frame) {
if (cfg.on_fixed_update) cfg.on_fixed_update(cfg.user, cfg.fixed_dt);
accumulator -= cfg.fixed_dt;
steps++;
}
float interp = accumulator / cfg.fixed_dt;
if (cfg.on_render) cfg.on_render(cfg.user, interp);
}
#endif
}
} // namespace fn::game

Some files were not shown because too many files have changed in this diff Show More