devs/bin/helix_install.sh
Mauro Rosero P. d9b8745842
[IMPROVED] Script de instalación de Helix con soporte para desinstalación
- Añadida opción -u/--uninstall para el script helix_install.sh
- Implementadas funciones para desinstalar según método de instalación (snap, tarball, paquetes)
- Añadidos nuevos mensajes traducidos en español para la funcionalidad
- Actualizada documentación en README.md
- Se limpian archivos de configuración además de los binarios
- Mejorado manejo de errores y verificación de desinstalación

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-04-05 20:19:05 -05:00

449 lines
12 KiB
Bash
Executable file

#!/bin/bash
#Script : helix_install.sh
#Apps : MRDEVS TOOLS
#Description : Instala/Desinstala Helix Editor (console)
#Author : Mauro Rosero Pérez
#Company Email : mauro@rosero.one
#Personal Email : mauro.rosero@gmail.com
#Created : 2025/03/11 15:00:00
#Modified : 2025/04/05 21:30:00
#Version : 1.3.0
#Use Notes : Uso: ./helix_install.sh [-u|--uninstall]
#==============================================================================
# Derechos de Autor [2025] [Mauro Rosero P. <mauro@rosero.one>]
#==============================================================================
# Este programa es software libre: usted puede redistribuirlo y/o modificarlo
# bajo los términos de la Licencia Pública Affero General de GNU tal como
# lo publica la Free Software Foundation, ya sea la versión 3 de la licencia,
# o (a su elección) cualquier versión posterior.
#
# Este programa se distribuye con la esperanza de que sea útil,
# pero SIN NINGUNA GARANTÍA; sin siquiera la garantía implícita de
# COMERCIABILIDAD o IDONEIDAD PARA UN PROPÓSITO PARTICULAR. Consulte la
# Licencia Pública Affero General de GNU para obtener más detalles.
#
# Debería haber recibido una copia de la Licencia Pública Affero General
# junto con este programa. Si no la recibió, consulte <https://www.gnu.org/licenses/>.
# Procesamiento de parámetros
UNINSTALL=0
for arg in "$@"; do
case $arg in
-u|--uninstall)
UNINSTALL=1
shift
;;
*)
# Ignorar argumentos desconocidos
shift
;;
esac
done
# Configuración inicial
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BIN_BASE="bin"
BIN_LIBS="lib"
BIN_MESG="msg"
BIN_CFGS="config"
# Leer DEVSPATH desde el archivo de configuración o usar "devs" por defecto
if [ -f "$SCRIPT_DIR/$BIN_CFGS/devspath.dat" ]; then
DEVSPATH=$(cat "$SCRIPT_DIR/$BIN_CFGS/devspath.dat")
else
DEVSPATH="devs"
fi
BIN_HOME="$HOME/$DEVSPATH"
VERSION=$(cat "$BIN_HOME/$BIN_BASE/$BIN_CFGS/version")
# CHECK SHELL LANGUAGE
BIN_LANG=${LANG:0:2}
# Importar bibliotecas necesarias
source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/base.lib"
source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/console.lib"
# Cargar mensajes en el idioma del sistema o español por defecto
load_messages "${BIN_HOME}/${BIN_BASE}" "${BIN_MESG}" "${BIN_LANG}" "head"
title="${head_000} ${head_002}"
apps_title="${hx_001}"
# Verificar que 'dialog' esté instalado
if ! command -v dialog &>/dev/null; then
echo "${npm_001}"
exit 1
fi
# Función para limpiar la pantalla y salir
function finish() {
clear
exit
}
# Función para ejecutar comandos con sudo si es necesario
run_cmd() {
if [ "$EUID" -eq 0 ]; then
"$@"
else
echo "$SUDO_PASS" | sudo -S "$@"
fi
# Devuelve el código de estado del último comando ejecutado
return $?
}
# Función para desinstalar Helix mediante Snap
uninstall_helix_snap() {
dialog --backtitle "${title}" --infobox "${hx_016}" 3 40
run_cmd snap remove helix
return $?
}
# Función para desinstalar Helix instalado mediante tarball
uninstall_helix_tarball() {
dialog --backtitle "${title}" --infobox "${hx_016}" 3 40
# Eliminar binario y archivos de runtime
run_cmd rm -f /usr/local/bin/hx
run_cmd rm -rf /usr/local/lib/helix
# Eliminar archivos de configuración en el directorio home del usuario
dialog --backtitle "${title}" --infobox "${hx_021}" 3 40
rm -rf ~/.config/helix
return 0
}
# Función para desinstalar Helix según el sistema operativo
uninstall_helix() {
local OS_TYPE=$(uname -s)
local DISTRO=""
local INSTALL_METHOD=""
dialog --backtitle "${title}" --infobox "${hx_022}" 3 40
# Determinar el método de desinstalación según el sistema operativo
if [[ "$OS_TYPE" == "Linux" ]]; then
if command -v apt-get &> /dev/null; then
DISTRO="Debian/Ubuntu"
# Verificar si es Ubuntu (para usar snap) o Debian
if grep -qi "ubuntu" /etc/os-release; then
# Verificar si helix está instalado por snap
if snap list 2>/dev/null | grep -q "helix"; then
INSTALL_METHOD="snap"
else
INSTALL_METHOD="tarball"
fi
else
# Para Debian generalmente se instala por tarball
INSTALL_METHOD="tarball"
fi
elif command -v pacman &> /dev/null; then
DISTRO="Arch Linux"
INSTALL_METHOD="package"
run_cmd pacman -Rns --noconfirm helix
elif command -v dnf &> /dev/null; then
DISTRO="Fedora"
INSTALL_METHOD="package"
run_cmd dnf remove -y helix
elif command -v yum &> /dev/null; then
DISTRO="CentOS/RHEL"
# Probablemente instalado por tarball
INSTALL_METHOD="tarball"
else
# Si no se reconoce el gestor de paquetes, asumir tarball
INSTALL_METHOD="tarball"
fi
elif [[ "$OS_TYPE" == "FreeBSD" ]]; then
DISTRO="FreeBSD"
INSTALL_METHOD="package"
run_cmd pkg remove -y helix
elif [[ "$OS_TYPE" == "Darwin" ]]; then
DISTRO="macOS"
INSTALL_METHOD="package"
brew uninstall helix
else
dialog_error_box "${head_error}" "${os_nofound}"
return 1
fi
# Seleccionar el método de desinstalación
if [[ "$INSTALL_METHOD" == "snap" ]]; then
uninstall_helix_snap
elif [[ "$INSTALL_METHOD" == "tarball" ]]; then
uninstall_helix_tarball
fi
# El método "package" ya se ha ejecutado para Arch, Fedora, FreeBSD y macOS
# Verificar que se desinstalara correctamente
if ! command -v hx &> /dev/null; then
return 0
else
return 1
fi
}
# Función para instalar Helix según el sistema operativo
install_helix() {
local OS_TYPE=$(uname -s)
local DISTRO=""
local INSTALL_METHOD="package" # Puede ser "package" o "tarball"
# Determinar el método de instalación según el sistema operativo
if [[ "$OS_TYPE" == "Linux" ]]; then
if command -v apt-get &> /dev/null; then
DISTRO="Debian/Ubuntu"
# Verificar si es Ubuntu (para usar snap) o Debian
if grep -qi "ubuntu" /etc/os-release; then
# Verificar si snap está disponible
dialog --backtitle "${title}" --infobox "${hx_014}" 3 40
if command -v snap &> /dev/null; then
INSTALL_METHOD="snap"
else
# Instalar snap si no está disponible
dialog --backtitle "${title}" --infobox "${hx_015}" 3 40
run_cmd apt-get update
run_cmd apt-get install -y snapd
# Verificar si snap se instaló correctamente
if command -v snap &> /dev/null; then
INSTALL_METHOD="snap"
else
# Fallback a tarball si no se pudo instalar snap
INSTALL_METHOD="tarball"
fi
fi
else
# Para Debian usar tarball
INSTALL_METHOD="tarball"
fi
elif command -v pacman &> /dev/null; then
DISTRO="Arch Linux"
# Arch tiene versiones recientes en sus repositorios
INSTALL_METHOD="package"
run_cmd pacman -Sy --noconfirm helix
elif command -v dnf &> /dev/null; then
DISTRO="Fedora"
INSTALL_METHOD="package"
run_cmd dnf install -y helix
elif command -v yum &> /dev/null; then
DISTRO="CentOS/RHEL"
# Mejor usar el tarball para versiones recientes
INSTALL_METHOD="tarball"
else
# Si no se reconoce el gestor de paquetes, usar el tarball
INSTALL_METHOD="tarball"
fi
elif [[ "$OS_TYPE" == "FreeBSD" ]]; then
DISTRO="FreeBSD"
INSTALL_METHOD="package"
run_cmd pkg install -y helix
elif [[ "$OS_TYPE" == "Darwin" ]]; then
DISTRO="macOS"
INSTALL_METHOD="package"
brew install helix
else
dialog_error_box "${head_error}" "${os_nofound}"
return 1
fi
# Seleccionar el método de instalación
if [[ "$INSTALL_METHOD" == "snap" ]]; then
install_helix_snap
elif [[ "$INSTALL_METHOD" == "tarball" ]]; then
install_helix_tarball
fi
# El método "package" ya se ha ejecutado para Arch, Fedora, FreeBSD y macOS
# Verificar que se instaló correctamente
if command -v hx &> /dev/null; then
return 0
else
return 1
fi
}
# Función para instalar Helix mediante Snap
install_helix_snap() {
dialog --backtitle "${title}" --infobox "${hx_013}" 3 40
# Asegurar que snap está actualizado
run_cmd snap refresh
# Instalar Helix vía snap
run_cmd snap install --classic helix
return $?
}
# Función para instalar Helix desde el tarball oficial
install_helix_tarball() {
local TMP_DIR=$(mktemp -d)
local HX_VERSION="master" # Podría ser parametrizado para instalar una versión específica
local ARCH=$(uname -m)
local OS=$(uname -s | tr '[:upper:]' '[:lower:]')
local TARBALL="helix-${OS}-${ARCH}.tar.xz"
local DOWNLOAD_URL="https://github.com/helix-editor/helix/releases/latest/download/${TARBALL}"
local INSTALL_DIR="/usr/local/bin"
local RUNTIME_DIR="/usr/local/lib/helix"
# Crear directorio temporal y entrar en él
if [ ! -d "$TMP_DIR" ]; then
mkdir -p "$TMP_DIR"
fi
cd "$TMP_DIR" || return 1
# Descargar la última versión
dialog --backtitle "${title}" --infobox "${hx_003}" 3 40
wget "$DOWNLOAD_URL" -O "$TARBALL" || curl -L "$DOWNLOAD_URL" -o "$TARBALL"
if [ $? -ne 0 ]; then
rm -rf "$TMP_DIR"
return 1
fi
# Extraer el tarball
dialog --backtitle "${title}" --infobox "${hx_004}" 3 40
tar -xJf "$TARBALL"
# Verificar si la extracción fue exitosa
if [ $? -ne 0 ]; then
rm -rf "$TMP_DIR"
return 1
fi
# Crear directorios de destino
dialog --backtitle "${title}" --infobox "${hx_012}" 3 40
run_cmd mkdir -p "$INSTALL_DIR"
run_cmd mkdir -p "$RUNTIME_DIR"
# Instalar el binario y los archivos de runtime
dialog --backtitle "${title}" --infobox "${hx_005}" 3 40
# Verificar que los directorios existan antes de copiar
if [ -f "helix/hx" ] && [ -d "helix/runtime" ]; then
run_cmd cp helix/hx "$INSTALL_DIR/"
run_cmd cp -r helix/runtime "$RUNTIME_DIR/"
else
rm -rf "$TMP_DIR"
return 1
fi
# Limpiar
rm -rf "$TMP_DIR"
return 0
}
# Si no se ejecuta como root, solicitar contraseña para escalamiento vía dialog
if [ "$EUID" -ne 0 ]; then
dialog_error_box "${head_superuser}" "${hx_011}"
dialog_input_pass "${npm_002}" "" ""
if [ $? -ne 0 ]; then
dialog_error_box "${head_error}" "${npm_003}"
finish
fi
SUDO_PASS="$value"
# Verificar que la contraseña funcione
echo "$SUDO_PASS" | sudo -S true &> /dev/null
if [ $? -ne 0 ]; then
dialog_error_box "${head_error}" "${npm_003}"
finish
fi
fi
# Mensaje de bienvenida
clear
dialog_error_box "${hx_001}" "${hx_002}"
# Preguntar al usuario si desea instalar o desinstalar Helix
if [ $UNINSTALL -eq 1 ]; then
dialog_yesno "${hx_019}"
if [ $? -ne 0 ]; then
dialog_error_box "${head_canceled}" "${npm_008}"
finish
fi
# Mostrar un progress bar para indicar el avance de la desinstalación
(
# Inicializar progreso
echo 0; sleep 1
echo 25; echo "# ${hx_022}"
sleep 1
echo 50; echo "# ${hx_016}"
sleep 1
echo 75; echo "# ${hx_021}"
# Desinstalar Helix
uninstall_helix
UNINSTALL_RESULT=$?
# Verificar el resultado de la desinstalación
if [ $UNINSTALL_RESULT -eq 0 ]; then
echo 90; echo "# ${hx_017}"
else
echo 90; echo "# ${hx_018}"
fi
sleep 1
# Finalizar
echo 100
sleep 1
) | dialog --backtitle "${title}" --title "${npm_026}" --gauge "${hx_020}" 10 70 0
# Verificar si la desinstalación fue exitosa
if ! command -v hx &> /dev/null; then
dialog_error_box "${npm_028}" "${hx_017}"
else
dialog_error_box "${head_error}" "${hx_018}"
fi
else
# Instalación
dialog_yesno "${hx_009}"
if [ $? -ne 0 ]; then
dialog_error_box "${head_canceled}" "${npm_008}"
finish
fi
# Mostrar un progress bar para indicar el avance de la instalación
(
# Inicializar progreso
echo 0; sleep 1
echo 20; echo "# ${hx_003}"
sleep 1
echo 40; echo "# ${hx_004}"
sleep 1
echo 60; echo "# ${hx_005}"
# Instalar Helix
install_helix
INSTALL_RESULT=$?
# Verificar el resultado de la instalación
if [ $INSTALL_RESULT -eq 0 ]; then
echo 90; echo "# ${hx_006}"
else
echo 90; echo "# ${hx_008}"
fi
sleep 1
# Finalizar
echo 100
sleep 1
) | dialog --backtitle "${title}" --title "${npm_018}" --gauge "${hx_010}" 10 70 0
# Verificar la versión instalada
if command -v hx &> /dev/null; then
hx_version=$(hx --version 2>&1 | head -n 1 || echo "Desconocida")
dialog_error_box "${npm_014}" "${hx_006}\n\n${hx_007} ${hx_version}"
else
dialog_error_box "${head_error}" "${hx_008}"
fi
fi
# Salir
finish