-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanage.sh
More file actions
executable file
·334 lines (276 loc) · 10.1 KB
/
manage.sh
File metadata and controls
executable file
·334 lines (276 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/bin/bash
set -e
# Colores para output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Idiomas soportados
LANGS=("en" "es")
# Cargar variables de entorno desde .env si existe
if [[ -f .env ]]; then
set -o allexport
source .env
set +o allexport
fi
# Función para mostrar ayuda
show_help() {
echo -e "${BLUE}HUG AI DEV - Gestor de Documentación Multilenguaje${NC}"
echo ""
echo "Uso: $0 [COMANDO] [IDIOMA]"
echo ""
echo "COMANDOS:"
echo " build - Construir la documentación"
echo " serve - Servir la documentación localmente"
echo " deploy - Desplegar a GitHub Pages"
echo " clean - Limpiar archivos generados"
echo " kill - Matar procesos de mkdocs y liberar puertos"
echo " restart - Matar procesos y servir documentación"
echo " help - Mostrar esta ayuda"
echo ""
echo "IDIOMAS:"
echo " en - Inglés"
echo " es - Español"
echo " both - Ambos idiomas"
echo ""
echo "EJEMPLOS:"
echo " $0 build both # Construir ambos idiomas"
echo " $0 serve en # Servir solo inglés en puerto 8000"
echo " $0 serve es # Servir solo español en puerto 8001"
echo " $0 serve both # Servir ambos en puertos 8000 y 8001"
echo " $0 kill # Matar procesos de mkdocs y liberar puertos"
echo " $0 restart both # Matar procesos y servir ambos idiomas"
echo " $0 deploy # Desplegar ambos idiomas a GitHub Pages"
echo ""
}
# Función para verificar dependencias
check_dependencies() {
echo -e "${BLUE}Verificando dependencias...${NC}"
# Verificar Python
if ! command -v python3 &> /dev/null; then
echo -e "${RED}Error: Python 3 no está instalado${NC}"
exit 1
fi
# Verificar ghp-import para deploy
if ! command -v ghp-import &> /dev/null; then
echo -e "${YELLOW}Advertencia: ghp-import no está instalado. Instalando...${NC}"
pip install ghp-import
fi
# Verificar y configurar Cairo en macOS
if [[ "$OSTYPE" == "darwin"* ]]; then
echo -e "${BLUE}Configurando Cairo para macOS...${NC}"
# Verificar si Cairo está instalado con Homebrew
if brew list cairo &> /dev/null; then
echo -e "${YELLOW}Cairo detectado, configurando variables de entorno...${NC}"
# Configurar variables de entorno para que cairosvg encuentre las librerías
export DYLD_LIBRARY_PATH="/opt/homebrew/lib:/opt/homebrew/opt/cairo/lib:$DYLD_LIBRARY_PATH"
export DYLD_FALLBACK_LIBRARY_PATH="/opt/homebrew/lib:/opt/homebrew/opt/cairo/lib:$DYLD_FALLBACK_LIBRARY_PATH"
export PKG_CONFIG_PATH="/opt/homebrew/lib/pkgconfig:/opt/homebrew/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH"
echo -e "${GREEN}✓ Variables de entorno de Cairo configuradas${NC}"
else
echo -e "${YELLOW}Advertencia: Cairo no está instalado. Instalando...${NC}"
brew install cairo pango gdk-pixbuf libffi
# Configurar variables de entorno después de la instalación
export DYLD_LIBRARY_PATH="/opt/homebrew/lib:/opt/homebrew/opt/cairo/lib:$DYLD_LIBRARY_PATH"
export DYLD_FALLBACK_LIBRARY_PATH="/opt/homebrew/lib:/opt/homebrew/opt/cairo/lib:$DYLD_FALLBACK_LIBRARY_PATH"
export PKG_CONFIG_PATH="/opt/homebrew/lib/pkgconfig:/opt/homebrew/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH"
echo -e "${GREEN}✓ Cairo instalado y variables configuradas${NC}"
fi
fi
echo -e "${GREEN}✓ Dependencias verificadas${NC}"
}
# Función para preparar entorno virtual
setup_venv() {
if [[ ! -d ".venv" ]]; then
echo -e "${BLUE}Creando entorno virtual...${NC}"
python3 -m venv .venv
fi
source .venv/bin/activate
echo -e "${BLUE}Instalando dependencias desde requirements.txt...${NC}"
pip install -r requirements.txt
echo -e "${GREEN}✓ Entorno virtual configurado${NC}"
}
# Función para copiar y reemplazar el index de redirección
copy_redirect_index() {
local ga_id=${GOOGLE_ANALYTICS_KEY:-G-XXXXXXXXXX}
if [[ -f "public/index.html" ]]; then
mkdir -p site
sed "s/{{GA_ID}}/$ga_id/g" public/index.html > site/index.html
echo -e "${GREEN}✓ index.html de redirección copiado a site/index.html con GA_ID=${ga_id}${NC}"
else
echo -e "${YELLOW}Advertencia: public/index.html no existe, no se copió index de redirección${NC}"
fi
}
# Función para reemplazar temporalmente el GA_ID en mkdocs-base.yml
backup_and_replace_ga_id() {
local ga_id=${GOOGLE_ANALYTICS_KEY:-G-XXXXXXXXXX}
local yml_file="mkdocs-base.yml"
local backup_file="${yml_file}.bak"
cp "$yml_file" "$backup_file"
# Reemplaza la línea property: !ENV GOOGLE_ANALYTICS_KEY por property: $ga_id
sed -i '' "s/property: !ENV GOOGLE_ANALYTICS_KEY/property: $ga_id/" "$yml_file"
}
restore_ga_id_yml() {
local yml_file="mkdocs-base.yml"
local backup_file="${yml_file}.bak"
if [[ -f "$backup_file" ]]; then
mv "$backup_file" "$yml_file"
fi
}
# Función para construir documentación
build_docs() {
local target=$1
echo -e "${BLUE}Construyendo documentación para: ${target}${NC}"
backup_and_replace_ga_id
# Configurar variables de entorno para Cairo en macOS
if [[ "$OSTYPE" == "darwin"* ]]; then
export DYLD_LIBRARY_PATH="/opt/homebrew/lib:/opt/homebrew/opt/cairo/lib:$DYLD_LIBRARY_PATH"
export DYLD_FALLBACK_LIBRARY_PATH="/opt/homebrew/lib:/opt/homebrew/opt/cairo/lib:$DYLD_FALLBACK_LIBRARY_PATH"
export PKG_CONFIG_PATH="/opt/homebrew/lib/pkgconfig:/opt/homebrew/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH"
fi
if [[ "$target" == "both" ]]; then
echo -e "${YELLOW}Construyendo versión en inglés...${NC}"
mkdocs build -f mkdocs-en.yml -d site/en
echo -e "${YELLOW}Construyendo versión en español...${NC}"
mkdocs build -f mkdocs-es.yml -d site/es
echo -e "${GREEN}✓ Ambas versiones construidas${NC}"
else
echo -e "${YELLOW}Construyendo versión en ${target}...${NC}"
mkdocs build -f "mkdocs-$target.yml" -d "site/$target"
echo -e "${GREEN}✓ Versión ${target} construida${NC}"
fi
restore_ga_id_yml
copy_redirect_index
}
# Función para servir documentación
serve_docs() {
local target=$1
echo -e "${BLUE}Sirviendo documentación para: ${target}${NC}"
# Configurar variables de entorno para Cairo en macOS
if [[ "$OSTYPE" == "darwin"* ]]; then
export DYLD_LIBRARY_PATH="/opt/homebrew/lib:/opt/homebrew/opt/cairo/lib:$DYLD_LIBRARY_PATH"
export DYLD_FALLBACK_LIBRARY_PATH="/opt/homebrew/lib:/opt/homebrew/opt/cairo/lib:$DYLD_FALLBACK_LIBRARY_PATH"
export PKG_CONFIG_PATH="/opt/homebrew/lib/pkgconfig:/opt/homebrew/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH"
fi
if [[ "$target" == "both" ]]; then
echo -e "${YELLOW}Iniciando servidor inglés en http://127.0.0.1:8000${NC}"
mkdocs serve -f mkdocs-en.yml -a 127.0.0.1:8000 &
local en_pid=$!
echo -e "${YELLOW}Iniciando servidor español en http://127.0.0.1:8001${NC}"
mkdocs serve -f mkdocs-es.yml -a 127.0.0.1:8001 &
local es_pid=$!
echo -e "${GREEN}✓ Ambos servidores iniciados${NC}"
echo -e "${BLUE}Presiona Ctrl+C para detener ambos servidores${NC}"
echo -e "${YELLOW}Nota: El selector de idioma funciona correctamente en producción${NC}"
# Esperar a que ambos procesos terminen
wait $en_pid $es_pid
else
local port=8000
[[ "$target" == "es" ]] && port=8001
echo -e "${YELLOW}Iniciando servidor ${target} en http://127.0.0.1:${port}${NC}"
mkdocs serve -f "mkdocs-$target.yml" -a 127.0.0.1:$port
fi
}
# Función para desplegar
deploy_docs() {
echo -e "${BLUE}Iniciando despliegue a GitHub Pages...${NC}"
# Construir ambas versiones
build_docs "both"
# copy_redirect_index ya se llama desde build_docs
# Configurar CNAME para dominio personalizado
echo "docs.hugai.dev" > site/CNAME
# Crear archivo .nojekyll para GitHub Pages
touch site/.nojekyll
echo -e "${YELLOW}Publicando en GitHub Pages...${NC}"
# Verificar si ghp-import está disponible
if command -v ghp-import &> /dev/null; then
ghp-import -n -p -f site
echo -e "${GREEN}✓ Despliegue completado exitosamente${NC}"
echo -e "${BLUE}El sitio estará disponible en: https://docs.hugai.dev${NC}"
else
echo -e "${RED}Error: ghp-import no está disponible${NC}"
echo -e "${YELLOW}Instala ghp-import con: pip install ghp-import${NC}"
exit 1
fi
}
# Función para limpiar
clean_docs() {
echo -e "${BLUE}Limpiando archivos generados...${NC}"
if [[ -d "site" ]]; then
rm -rf site
echo -e "${GREEN}✓ Directorio site eliminado${NC}"
fi
if [[ -d ".cache" ]]; then
rm -rf .cache
echo -e "${GREEN}✓ Directorio .cache eliminado${NC}"
fi
echo -e "${GREEN}✓ Limpieza completada${NC}"
}
# Función para matar procesos de mkdocs serve
kill_mkdocs_processes() {
echo -e "${YELLOW}Matando procesos de mkdocs serve...${NC}"
pkill -f "mkdocs serve" 2>/dev/null || true
echo -e "${GREEN}✓ Procesos de mkdocs serve terminados${NC}"
}
# CLI principal
ACTION=$1
TARGET=$2
# Mostrar ayuda si no hay argumentos o si se solicita
if [[ -z "$ACTION" || "$ACTION" == "help" || "$ACTION" == "-h" || "$ACTION" == "--help" ]]; then
show_help
exit 0
fi
# Validación de argumentos
if [[ -z "$TARGET" ]]; then
case "$ACTION" in
deploy|clean|kill)
# deploy, clean y kill no necesitan target
;;
*)
echo -e "${RED}Error: Se requiere especificar el idioma (en|es|both)${NC}"
echo ""
show_help
exit 1
;;
esac
fi
# Validar idioma si se especifica
if [[ -n "$TARGET" && "$TARGET" != "both" ]]; then
if [[ ! " ${LANGS[@]} " =~ " ${TARGET} " ]]; then
echo -e "${RED}Error: Idioma no válido. Usa: en, es, o both${NC}"
exit 1
fi
fi
# Verificar dependencias
check_dependencies
# Preparar entorno virtual
setup_venv
# Ejecutar acción seleccionada
case "$ACTION" in
build)
build_docs "$TARGET"
;;
serve)
serve_docs "$TARGET"
;;
deploy)
deploy_docs
;;
clean)
clean_docs
;;
kill)
kill_mkdocs_processes
;;
restart)
kill_mkdocs_processes
exec "$0" serve "$TARGET"
;;
*)
echo -e "${RED}Error: Acción no válida: $ACTION${NC}"
echo ""
show_help
exit 1
;;
esac