| 1 | #!/usr/bin/env bash |
| 2 | # Sincroniza el core compartido (repo elffuss) a web/js/. Ejecuta tras clonar |
| 3 | # el core al lado, o cuando el core cambie. Los archivos quedan vendorizados |
| 4 | # (este repo es autocontenido: clona y abre, sin build). |
| 5 | # |
| 6 | # Antes copiaba a ciegas, y un cambio hecho aquí que el core no tenía se perdía |
| 7 | # en silencio en la siguiente sincronización (pasó con la parada del proveedor |
| 8 | # API, y context.js, mind.js y otros llevan tiempo distintos). Ahora compara el |
| 9 | # CÓDIGO de cada fichero, sin comentarios (el core los tiene en inglés): si es |
| 10 | # el mismo, lo copia; si no, NO lo pisa, lo dice y termina con error. Puede ser |
| 11 | # que el core vaya por delante (entonces --force) o que aquí haya algo que el |
| 12 | # core no tiene (entonces hay que llevarlo al core antes). |
| 13 | # |
| 14 | # ./sync-core.sh [ruta/al/core] [--force] |
| 15 | set -euo pipefail |
| 16 | cd "$(dirname "$0")" |
| 17 | FORCE=0; CORE=../elffuss/core |
| 18 | for a in "$@"; do if [ "$a" = "--force" ]; then FORCE=1; else CORE="$a"; fi; done |
| 19 | [ -d "$CORE" ] || { echo "No encuentro el core en $CORE (clona github.com/KikoCis/elffuss al lado)"; exit 1; } |
| 20 | |
| 21 | # ¿Mismo código, sin contar comentarios ni líneas en blanco? (sale 0 si sí) |
| 22 | mismo_codigo() { |
| 23 | python3 -I - "$1" "$2" <<'PY' |
| 24 | import sys |
| 25 | REGEX_PREV = set('(,=:[!&|?{};+-*%<>~^') |
| 26 | ANTES_DE_REGEX = ('return', 'typeof', 'case', 'do', 'else', 'in', 'of', 'void', 'yield', 'await') |
| 27 | def codigo(src): |
| 28 | out, i, n, ultimo, palabra = [], 0, len(src), '', '' |
| 29 | while i < n: |
| 30 | c, sig = src[i], src[i + 1] if i + 1 < n else '' |
| 31 | if c == '/' and sig == '/': |
| 32 | j = src.find('\n', i); i = n if j < 0 else j; continue |
| 33 | if c == '/' and sig == '*': |
| 34 | j = src.find('*/', i + 2); i = n if j < 0 else j + 2; continue |
| 35 | if c in '"\'': |
| 36 | j = i + 1 |
| 37 | while j < n and src[j] != c and src[j] != '\n': |
| 38 | j += 2 if src[j] == '\\' else 1 |
| 39 | out.append(src[i:j + 1]); ultimo = c; i = j + 1; continue |
| 40 | if c == '`': |
| 41 | j, prof = i + 1, 0 |
| 42 | while j < n: |
| 43 | if src[j] == '\\': j += 2; continue |
| 44 | if src[j] == '$' and j + 1 < n and src[j + 1] == '{': prof += 1; j += 2; continue |
| 45 | if src[j] == '}' and prof: prof -= 1; j += 1; continue |
| 46 | if src[j] == '`' and not prof: break |
| 47 | j += 1 |
| 48 | out.append(src[i:j + 1]); ultimo = '`'; i = j + 1; continue |
| 49 | if c == '/' and (ultimo == '' or ultimo in REGEX_PREV or palabra in ANTES_DE_REGEX): |
| 50 | j, clase = i + 1, False |
| 51 | while j < n and src[j] != '\n': |
| 52 | if src[j] == '\\': j += 2; continue |
| 53 | if src[j] == '[': clase = True |
| 54 | elif src[j] == ']': clase = False |
| 55 | elif src[j] == '/' and not clase: break |
| 56 | j += 1 |
| 57 | j += 1 |
| 58 | while j < n and src[j].isalpha(): j += 1 |
| 59 | out.append(src[i:j]); ultimo = '/'; i = j; continue |
| 60 | out.append(c) |
| 61 | if not c.isspace(): |
| 62 | ultimo = c |
| 63 | palabra = (palabra + c) if (c.isalnum() or c in '_$') else '' |
| 64 | elif palabra: |
| 65 | pass |
| 66 | i += 1 |
| 67 | return [l.rstrip() for l in ''.join(out).split('\n') if l.strip()] |
| 68 | a, b = (open(p, encoding='utf-8').read() for p in sys.argv[1:3]) |
| 69 | sys.exit(0 if codigo(a) == codigo(b) else 1) |
| 70 | PY |
| 71 | } |
| 72 | |
| 73 | distintos=() |
| 74 | for f in context.js embed.js skills.js md.js splash-gl.js ceo.js mind.js humanize.js telemetry.js providers/api.js; do |
| 75 | if [ "$FORCE" = 0 ] && [ -f "web/js/$f" ] && ! mismo_codigo "$CORE/$f" "web/js/$f"; then |
| 76 | distintos+=("$f"); continue |
| 77 | fi |
| 78 | cp "$CORE/$f" "web/js/$f" |
| 79 | done |
| 80 | if [ ${#distintos[@]} -gt 0 ]; then |
| 81 | echo "⚠️ No sincronizados: su código aquí no es el del core (comentarios aparte):" |
| 82 | for f in "${distintos[@]}"; do echo " web/js/$f"; done |
| 83 | echo " Si aquí hay algo que el core no tiene, llévalo antes al core; si es el core el que va por delante, ./sync-core.sh --force." |
| 84 | exit 1 |
| 85 | fi |
| 86 | echo "✳ core sincronizado desde $CORE" |